yiisoft / app-console
控制台应用程序模板
Requires
- php: ^8.1
- vlucas/phpdotenv: ^5.3
- yiisoft/aliases: ^3.0
- yiisoft/log: ^2.0
- yiisoft/log-target-file: ^3.0
- yiisoft/yii-console: ^2.0
- yiisoft/yii-runner-console: ^2.0
Requires (Dev)
- codeception/codeception: ^5.0
- codeception/module-asserts: ^3.0
- codeception/module-cli: ^2.0
- codeception/module-phpbrowser: ^3.0
- vimeo/psalm: ^5.24
This package is auto-updated.
Last update: 2024-08-23 11:57:19 UTC
README
Yii 控制台应用程序
该包是一个仅限 控制台 的应用程序模板,可用于在 Yii 应用程序中执行常见任务。如果您需要经典网页或 API,请从相应的模板开始
它基于 Yii 控制台运行器,该运行器用于入口命令脚本 ./yii
。您可以自由调整此模板的任何部分,包括入口命令脚本,以满足您的需求。
要求
- PHP 8.1 或更高版本。
创建项目
使用 Composer 从此模板创建新项目
composer create-project yiisoft/app-console <your project>
一般用法
控制台在应用程序根目录下作为 ./yii
可用
$ ./yii Yii Console 1.0 Usage: command [options] [arguments] Options: -h, --help Display help for the given command. When no command is given display help for the list command -q, --quiet Do not output any message -V, --version Display this application version --ansi|--no-ansi Force (or disable --no-ansi) ANSI output -n, --no-interaction Do not ask any interactive question --config=CONFIG Set alternative configuration name -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Available commands: completion Dump the shell completion script echo An example command that echoes exactly what it is told to. help Display help for a command list List commands serve Runs PHP built-in web server
通过在命令本身中添加 --help
可以显示特定命令的帮助
$ ./yii echo --help Description: An example command that echoes exactly what it is told to. Usage: echo [<sentence>] Arguments: sentence Sentence to say. [default: "Hello!"] Options: -h, --help Display help for the given command. When no command is given display help for the list command -q, --quiet Do not output any message -V, --version Display this application version --ansi|--no-ansi Force (or disable --no-ansi) ANSI output -n, --no-interaction Do not ask any interactive question --config=CONFIG Set alternative configuration name -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
使用命令的方式如下
$ ./yii echo You said: Hello! $ ./yii echo 'Code something' You said: Code something
环境
默认情况下,有三个可用环境
- dev — 用于开发。
- prod — 用于生产。
- test — 用于运行测试。
这些配置文件位于 config/environments
。
可以通过设置 YII_ENV
来选择环境
YII_ENV=prod ./yii
额外的调试
要启用容器和事件的验证,请设置 YII_DEBUG
环境变量
YII_DEBUG=1 ./yii
创建您自己的命令
命令放置在 src/Command
中。让我们看看 hello
命令如何在 src/Command/HelloCommand.php
中实现
namespace App\Command; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Yiisoft\Yii\Console\ExitCode; #[AsCommand( name: 'echo', description: 'An example command that echoes exactly what it is told to.' )] final class EchoCommand extends Command { private string $sentence = 'sentence'; protected function configure(): void { $this->setDefinition( new InputDefinition([ new InputArgument($this->sentence, InputArgument::OPTIONAL, 'Sentence to say.', 'Hello!'), ]) ); } protected function execute(InputInterface $input, OutputInterface $output): int { $output->writeln("You said: {$input->getArgument('sentence')}"); return ExitCode::OK; } }
要注册命令,将其添加到 config/commands.php
use App\Command\EchoCommand; return [ 'echo' => EchoCommand::class, ];
信息:Yii 控制台基于 Symfony 控制台,因此对于额外的使用文档,请参阅 Yii 控制台 和 Symfony 控制台指南。
事件
在运行命令之前,应用程序引发 ApplicationStartup
,在运行命令之后引发 ApplicationShutdown
。
测试
模板附带即用的 Codeception 配置。要执行测试,请运行
composer run serve > ./runtime/yii.log 2>&1 & vendor/bin/codecept run
静态分析
代码使用 Psalm 进行静态分析。要运行静态分析
./vendor/bin/psalm
许可
Yii 控制台应用程序是免费软件。它根据 BSD 许可证发布。有关更多信息,请参阅 LICENSE
。
由 Yii 软件公司 维护。