cakephp / console
使用CakePHP构建漂亮的控制台应用程序
5.1.0
2024-09-06 12:23 UTC
Requires
- php: >=8.1
- cakephp/core: ^5.1
- cakephp/event: ^5.1
- cakephp/log: ^5.1
- cakephp/utility: ^5.1
Suggests
- cakephp/datasource: To use the Command base classes
- cakephp/orm: To use the Command base classes
- dev-master
- 5.x-dev
- 5.1.0
- 5.1.0-RC2
- 5.1.0-RC1
- 5.0.11
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.0
- 5.0.0-RC2
- 5.0.0-RC1
- 5.0.0-beta2
- 5.0.0-beta1
- 4.x-dev
- 4.5.7
- 4.5.6
- 4.5.5
- 4.5.4
- 4.5.3
- 4.5.2
- 4.5.1
- 4.5.0
- 4.5.0-RC1
- 4.4.17
- 4.4.16
- 4.4.15
- 4.4.14
- 4.4.13
- 4.4.12
- 4.4.11
- 4.4.10
- 4.4.9
- 4.4.8
- 4.4.7
- 4.4.6
- 4.4.5
- 4.4.4
- 4.4.3
- 4.4.2
- 4.4.1
- 4.4.0
- 4.4.0-RC2
- 4.4.0-RC1
- 4.3.x-dev
- 4.3.11
- 4.3.10
- 4.3.9
- 4.3.8
- 4.3.7
- 4.3.6
- 4.3.5
- 4.3.4
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.3.0-RC4
- 4.3.0-RC3
- 4.3.0-RC2
- 4.3.0-RC1
- 4.2.x-dev
- 4.2.12
- 4.2.11
- 4.2.10
- 4.2.9
- 4.2.8
- 4.2.7
- 4.2.6
- 4.2.5
- 4.2.4
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.2.0-RC1
- 4.2.0-beta1
- 4.1.7
- 4.1.6
- 4.1.5
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.1.0-RC2
- 4.1.0-RC1
- 4.1.0-beta1
- 4.0.x-dev
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 4.0.0-RC2
- 4.0.0-RC1
- dev-5.next
- dev-4.next
This package is auto-updated.
Last update: 2024-09-21 03:02:43 UTC
README
CakePHP控制台库
此库提供了一套框架,用于从一组命令构建命令行应用程序。它提供了定义选项和参数解析器以及分发命令的抽象。
安装
您可以通过Composer安装它。在您的项目中
composer require cakephp/console
入门
首先,定义一个入口点脚本和Application类,该类定义启动逻辑并绑定您的命令。让我们将我们的入口点脚本放在bin/tool.php
#!/usr/bin/php -q <?php // Check platform requirements require dirname(__DIR__) . '/vendor/autoload.php'; use App\Application; use Cake\Console\CommandRunner; // Build the runner with an application and root executable name. $runner = new CommandRunner(new Application(), 'tool'); exit($runner->run($argv));
对于我们的Application
类,我们可以从以下内容开始
<?php namespace App; use App\Command\HelloCommand; use Cake\Core\ConsoleApplicationInterface; use Cake\Console\CommandCollection; class Application implements ConsoleApplicationInterface { /** * Load all the application configuration and bootstrap logic. * * @return void */ public function bootstrap(): void { // Load configuration here. This is the first // method Cake\Console\CommandRunner will call on your application. } /** * Define the console commands for an application. * * @param \Cake\Console\CommandCollection $commands The CommandCollection to add commands into. * @return \Cake\Console\CommandCollection The updated collection. */ public function console(CommandCollection $commands): CommandCollection { $commands->add('hello', HelloCommand::class); return $commands; } }
接下来,我们将构建一个非常简单的HelloCommand
<?php namespace App\Command; use Cake\Console\Arguments; use Cake\Console\BaseCommand; use Cake\Console\ConsoleIo; use Cake\Console\ConsoleOptionParser; class HelloCommand extends BaseCommand { protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser { $parser ->addArgument('name', [ 'required' => true, 'help' => 'The name to say hello to', ]) ->addOption('color', [ 'choices' => ['none', 'green'], 'default' => 'none', 'help' => 'The color to use.' ]); return $parser; } public function execute(Arguments $args, ConsoleIo $io): ?int { $color = $args->getOption('color'); if ($color === 'none') { $io->out("Hello {$args->getArgument('name')}"); } elseif ($color == 'green') { $io->out("<success>Hello {$args->getArgument('name')}</success>"); } return static::CODE_SUCCESS; } }
接下来,我们可以使用php bin/tool.php hello Syd
来运行我们的命令。要了解更多关于我们在这个示例中使用的各种功能的信息,请阅读文档