cristiang / pimple-console
Pimple Console ServiceProvider
1.0.2
2024-06-25 13:31 UTC
Requires
- php: ^8.1
- pimple/pimple: ^3.5
- symfony/console: ^6.0|^7.0
- symfony/event-dispatcher: ^6.0|^7.0
README
安装
使用命令行将控制台服务提供者添加到您的 composer.json
。
composer require cristiang/pimple-console
配置
use Pimple\Container; use CristianG\PimpleConsole\ServiceProvider; $container = new Container(); $container->register(new ServiceProvider(), [ /** * Set the console application name. Defaults to 'Console' * @param string */ 'console.name' => 'Your App Console Commands', /** * Set the console application version. Defaults to '2.0' * @param string */ 'console.version' => "2.0.0", /** * Set console application list * @param array */ 'console.classes' => [ "\App\Console\Version", "\App\Console\Sync", ], /** * Set namespace command --namespace="\Namespace\Run" to be provided on command * !Note : console.classes will be ignore if namespace is been pass to command * @param bool */ 'console.allow_namespace' => true, /** * Set your DI new Pimple\Container() for your app to be load before execute */ 'console.di' => $container ]); $console = $container['console']; $console->run();
使用方法
在您的项目中创建一个脚本并手动设置 Pimple 容器。
#!/usr/bin/env php <?php require '[path to composer vendor folder]/autoload.php'; $container = new \Pimple\Container(); $container->register(new \CristianG\PimpleConsole\ServiceProvider(), array( 'console.name' => 'Console Application', 'console.version' => "2.0.0", 'console.classes' => [ "\App\Console\Version", "\App\Console\Sync", ] ));
创建一个在控制台.classes 提供的命名空间上的类,并使用依赖注入。
namespace App\Console; use Pimple\Container; use CristianG\PimpleConsole\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class Version extends Command { protected ?Container $di = null; /** * @param Container $di * @return void */ public function setDi(Container $di): void { $this->di = $di; } /** * @return Container|null */ public function getDi(): ?Container { return $this->di; } /** * @return void */ protected function configure(): void { $this->setName('app:version'); $this->setDescription('retrun a version of your app'); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output): int { // Logic to load your app version //.... //Return to console the version $this->info("App version is : {$APP_VERSION}"); return Command::SUCCESS; } }
创建一个在控制台.classes 提供的命名空间上的类,但不使用依赖注入。
namespace App\Console; use Pimple\Container; use CristianG\PimpleConsole\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class Version extends Command { /** * @return void */ protected function configure(): void { $this->setName('app:version'); $this->setDescription('retrun a version of your app'); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output): int { // Logic to load your app version //.... //Return to console the version $this->info("App version is : {$APP_VERSION}"); return Command::SUCCESS; } }
扩展到可用的命令功能
// Output normal text $this->line($string) //Output info text $this->info($string) //Output info comment $this->comment($string) //Output info question $this->question($string) //Output info error $this->error($string) //Confirm a question $this->confirm($question, $default = false, $trueAnswerRegex = '/^y/i') //Asks a question to the user. $this->ask($question, $default) // Give the user a single choice from an array of answers. $this->choice($question, array $choices, $default = null, $attempts = null, $multiple = null) // Build a table with a style // Headers : ["Name", "App", "version"] // Rows An array can be single-dimensional, multidimensional $this->table(array $headers, array $rows, $style = 'default')