andrewdyer / slim3-console
Slim 框架应用的命令行界面。
v1.0
2019-04-30 13:26 UTC
Requires
- slim/slim: ^3.12
- symfony/console: ^4.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- phpunit/phpunit: ^8.1
This package is auto-updated.
Last update: 2024-09-22 14:45:50 UTC
README
Slim 框架应用的命令行界面。Slim Framework 应用。
许可证
在 MIT 许可下发布。完全免费,可用于私人或商业项目。
安装
composer require andrewdyer/slim3-console
用法
$app = new \Slim\App(); $kernel = new \Anddye\Console\Kernel\Kernel(); $kernel->addCommand(\Anddye\Console\Commands\SayHelloCommand::class); $console = new \Anddye\Console\Console($app); $console->boot($kernel); $console->run();
在无需内核的情况下创建控制台实例并添加命令。
$app = new \Slim\App(); $container = $app->getContainer(); $console = new \Anddye\Console\Console($app); $console->add(new \Anddye\Console\Commands\SayHelloCommand($container)); $console->run();
从应用容器中添加命令。
$app = new \Slim\App([ 'settings' => [ 'commands' => [ \Anddye\Console\Commands\SayGoodbyeCommand::class, \Anddye\Console\Commands\SayHelloCommand::class, ] ] ]); $container = $app->getContainer(); $commands = $container->get('settings')->get('commands'); $kernel = new \Anddye\Console\Kernel\Kernel(); $kernel->addCommands($commands); $console = new \Anddye\Console\Console($app); $console->boot($kernel); $console->run();
命令
命令应在扩展 Anddye\Console\Commands\AbstractCommand
类的类中定义。以下是一个命令的基本用法示例
<?php namespace App\Commands; use Anddye\Console\Commands\AbstractCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class SayHelloCommand extends AbstractCommand { /** * Sets an array of argument to add to the command. * * @return array */ public function arguments(): array { return [ ['name', InputArgument::REQUIRED, 'Your name.'], ]; } /** * Sets the description for the command. * * @return string */ public function description(): string { return 'Prints "Hello" and your name for a specific number of times.'; } /** * The body of the command. * * @param InputInterface $input * @param OutputInterface $output * * @return mixed */ public function handle(InputInterface $input, OutputInterface $output) { for ($i = 0; $i < $input->getOption('repeat'); ++$i) { $this->info('Hello '.$input->getArgument('name')); } } /** * Sets the help for the command. * * @return string */ public function help(): string { return ''; } /** * Sets the name of the command. * * @return string */ public function name(): string { return 'say:hello'; } /** * Sets an array of options to add to the command. * * @return array */ public function options(): array { return [ ['repeat', 'r', InputOption::VALUE_OPTIONAL, 'Times to repeat the output.', 1], ]; } }
支持
如果您在使用此库时遇到任何问题,请随时通过 Twitter 联系我。
如果您认为您发现了一个错误,请使用 问题跟踪器 报告,或者更好的是,分支存储库并提交一个 pull request。
如果您使用此包,我很乐意听听您的想法!