99[ ![ Latest Stable Version] ( https://poser.pugx.org/yiisoft/app-console/v/stable.png )] ( https://packagist.org/packages/yiisoft/app-console )
1010[ ![ Total Downloads] ( https://poser.pugx.org/yiisoft/app-console/downloads.png )] ( https://packagist.org/packages/yiisoft/app-console )
1111[ ![ Build status] ( https://github.com/yiisoft/app-console/workflows/build/badge.svg )] ( https://github.com/yiisoft/app-console/actions?query=workflow%3Abuild )
12- [ ![ Scrutinizer Code Quality] ( https://scrutinizer-ci.com/g/yiisoft/app-console/badges/quality-score.png?b=master )] ( https://scrutinizer-ci.com/g/yiisoft/app-console/?branch=master )
13- [ ![ Code Coverage] ( https://scrutinizer-ci.com/g/yiisoft/app-console/badges/coverage.png?b=master )] ( https://scrutinizer-ci.com/g/yiisoft/app-console/?branch=master )
1412[ ![ static analysis] ( https://github.com/yiisoft/app-console/workflows/static%20analysis/badge.svg )] ( https://github.com/yiisoft/app-console/actions?query=workflow%3A%22static+analysis%22 )
1513[ ![ type-coverage] ( https://shepherd.dev/github/yiisoft/app-console/coverage.svg )] ( https://shepherd.dev/github/yiisoft/app-console )
1614
17- The package is a console application that can be used to perform common tasks in a Yii application.
15+ The package is a ** console only** application template that can be used to perform common tasks in a Yii application.
16+ If you need classic web or API please start with corresponding templates:
17+
18+ - [ Classic web application template] ( https://github.com/yiisoft/app )
19+ - [ API application template] ( https://github.com/yiisoft/app-api )
20+
21+ It is based on [ Yii console runner] ( https://github.com/yiisoft/yii-runner-console ) that is used in the entry
22+ command script, ` ./yii ` . You are free to adjust any part of this template including the entry command script
23+ to suit your needs.
1824
1925## Requirements
2026
@@ -25,33 +31,119 @@ The package is a console application that can be used to perform common tasks in
2531The package could be installed with [ Composer] ( https://getcomposer.org ) :
2632
2733``` shell
28- composer create-project --stability=dev yiisoft/app-console < your project>
34+ composer create-project yiisoft/app-console < your project>
2935```
3036
3137## General usage
3238
33- ### Create command console
39+ Console is available as ` ./yii ` from the root directory of the application:
40+
41+ ``` shell
42+ $ ./yii
43+
44+ Yii Console 1.0
45+
46+ Usage:
47+ command [options] [arguments]
48+
49+ Options:
50+ -h, --help Display help for the given command. When no command is given display help for the list command
51+ -q, --quiet Do not output any message
52+ -V, --version Display this application version
53+ --ansi| --no-ansi Force (or disable --no-ansi) ANSI output
54+ -n, --no-interaction Do not ask any interactive question
55+ --config=CONFIG Set alternative configuration name
56+ -v| vv| vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
57+
58+ Available commands:
59+ completion Dump the shell completion script
60+ echo An example command that echoes exactly what it is told to.
61+ help Display help for a command
62+ list List commands
63+ serve Runs PHP built-in web server
64+ ```
65+
66+ Help for specific command could be displayed by adding ` --help ` to the command itself:
67+
68+ ``` shell
69+ $ ./yii echo --help
70+
71+ Description:
72+ An example command that echoes exactly what it is told to.
73+
74+ Usage:
75+ echo [< sentence> ]
76+
77+ Arguments:
78+ sentence Sentence to say. [default: " Hello!" ]
79+
80+ Options:
81+ -h, --help Display help for the given command. When no command is given display help for the list command
82+ -q, --quiet Do not output any message
83+ -V, --version Display this application version
84+ --ansi| --no-ansi Force (or disable --no-ansi) ANSI output
85+ -n, --no-interaction Do not ask any interactive question
86+ --config=CONFIG Set alternative configuration name
87+ -v| vv| vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
88+ ```
89+
90+ Using the command is like the following:
91+
92+ ``` shell
93+ $ ./yii echo
94+ You said: Hello!
95+
96+ $ ./yii echo ' Code something'
97+ You said: Code something
98+ ```
99+
100+ ## Environments
101+
102+ Out of the box, three environments are available:
103+
104+ - dev — for development.
105+ - prod — for production.
106+ - test — for running tests.
107+
108+ Config files for these are in ` config/environments ` .
109+
110+ Environment could be chosen by setting ` YII_ENV ` :
111+
112+ ``` shell
113+ YII_ENV=prod ./yii
114+ ```
115+
116+ ## Extra debugging
117+
118+ To enable validation of container and events, set ` YII_DEBUG ` environment variable:
119+
120+ ``` shell
121+ YII_DEBUG=1 ./yii
122+ ```
123+
124+ ## Creating your own command
125+
126+ Commands are placed into ` src/Command ` . Let's see how ` hello ` command is implemented in ` src/Command/HelloCommand.php ` :
34127
35128``` php
129+ namespace App\Command;
130+
131+ use Symfony\Component\Console\Attribute\AsCommand;
36132use Symfony\Component\Console\Command\Command;
37133use Symfony\Component\Console\Input\InputArgument;
38134use Symfony\Component\Console\Input\InputDefinition;
39135use Symfony\Component\Console\Input\InputInterface;
40136use Symfony\Component\Console\Output\OutputInterface;
41137use Yiisoft\Yii\Console\ExitCode;
42138
43- final class Hello extends Command
139+ #[AsCommand(
140+ name: 'echo',
141+ description: 'An example command that echoes exactly what it is told to.'
142+ )]
143+ final class EchoCommand extends Command
44144{
45- protected static $defaultName = 'hello';
46- protected static $defaultDescription = 'An example command';
47-
48145 private string $sentence = 'sentence';
49146
50- public function __construct()
51- {
52- parent::__construct();
53- }
54-
55147 protected function configure(): void
56148 {
57149 $this->setDefinition(
@@ -70,50 +162,41 @@ final class Hello extends Command
70162}
71163```
72164
73- ### Using command console
165+ To register the command, add it to ` config/commands.php ` :
74166
75- ``` shell
76- $ ./yii
77- Yii Console 1.0
167+ ``` php
168+ use App\Command\EchoCommand;
78169
79- Usage:
80- command [options] [arguments]
170+ return [
171+ 'echo' => EchoCommand::class,
172+ ];
173+ ```
81174
82- Options:
83- -h, --help Display help for the given command. When no command is given display help for the list command
84- -q, --quiet Do not output any message
85- -V, --version Display this application version
86- --ansi| --no-ansi Force (or disable --no-ansi) ANSI output
87- -n, --no-interaction Do not ask any interactive question
88- --config=CONFIG Set alternative configuration name
89- -v| vv| vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
175+ > Info: Yii console is based on Symfony console so for additional usage documentation, please follow
176+ > [ Yii console] ( https://github.com/yiisoft/yii-console ) and
177+ > [ Symfony console guide] ( https://symfony.com/doc/current/console.html ) .
90178
91- Available commands:
92- completion Dump the shell completion script
93- hello An example command
94- help Display help for a command
95- list List commands
96- serve Runs PHP built-in web server
97- debug
98- debug:container Show information about container
99- debug:events Show information about events and listeners
100- debug:reset Clear debug data
101- ```
179+ ## Events
102180
103- ``` shell
104- $ ./yii hello
105- You said: Hello!
181+ The application raises ` ApplicationStartup ` before and ` ApplicationShutdown ` after running a command.
106182
107- $ ./yii hello ' Code something'
108- You said: Code something
183+ ## Tests
184+
185+ The template comes with ready to use [ Codeception] ( https://codeception.com/ ) configuration.
186+ In order to execute tests run:
187+
188+ ``` shell
189+ composer run serve > ./runtime/yii.log 2>&1 &
190+ vendor/bin/codecept run
109191```
110192
111- ## Documentation
193+ ## Static analysis
112194
113- - [ Internals ] ( docs/internals.md )
195+ The code is statically analyzed with [ Psalm ] ( https://psalm.dev/ ) . To run static analysis:
114196
115- If you need help or have a question, the [ Yii Forum] ( https://forum.yiiframework.com/c/yii-3-0/63 ) is a good place for that.
116- You may also check out other [ Yii Community Resources] ( https://www.yiiframework.com/community ) .
197+ ``` shell
198+ ./vendor/bin/psalm
199+ ```
117200
118201## License
119202
0 commit comments