yiisoft / yii-console
Yii Framework Console
Fund package maintenance!
Opencollective
yiisoft
Installs: 173 413
Dependents: 39
Suggesters: 5
Security: 0
Stars: 58
Watchers: 21
Forks: 31
Open Issues: 1
Requires
- php: ^8.0
- psr/container: ^1.0|^2.0
- psr/event-dispatcher: ^1.0
- psr/log: ^1.0|^2.0|^3.0
- symfony/console: ^5.4|^6.0
- symfony/event-dispatcher-contracts: ^2.2|^3.0
- yiisoft/friendly-exception: ^1.0
Requires (Dev)
- maglnet/composer-require-checker: ^3.8|^4.4
- phpunit/phpunit: ^9.5
- rector/rector: ^0.15.3
- roave/infection-static-analysis-plugin: ^1.16
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.30|^5.6
- yiisoft/config: ^1.3
- yiisoft/di: ^1.2
- yiisoft/test-support: ^3.0
This package is auto-updated.
Last update: 2023-06-02 06:22:59 UTC
README
Yii Framework Console
This Yii Framework package provides a console that could be added to an application.
Requirements
- PHP 8.0 or higher.
Installation
The package could be installed with composer:
composer require yiisoft/yii-console --prefer-dist
General usage
In case you use one of Yii 3 standard application templates, console could be accessed as ./yii <command>
.
If not, then in the simplest use case in your console entry script do the following:
#!/usr/bin/env php <?php declare(strict_types=1); use Yiisoft\Di\Container; use Yiisoft\Di\ContainerConfig; use Yiisoft\Yii\Console\Application; use Yiisoft\Yii\Console\CommandLoader; require_once __DIR__ . '/vendor/autoload.php'; $app = new Application(); $app->setCommandLoader(new CommandLoader( // Any container implementing `Psr\Container\ContainerInterface` for example: new Container(ContainerConfig::create()), // An array with command names as keys and service IDs as values: ['my/custom' => MyCustomCommand::class], )); $app->run();
Since \Yiisoft\Yii\Console\CommandLoader
uses lazy loading of commands, it's necessary
to specify the name and description in static properties when creating a command:
use Symfony\Component\Console\Command\Command; use Yiisoft\Yii\Console\ExitCode; final class MyCustomCommand extends Command { protected static $defaultName = 'my:custom'; protected static $defaultDescription = 'Description of my custom command.'; protected function configure(): void { // ... } protected function execute(InputInterface $input, OutputInterface $output): int { // ... return ExitCode::OK; } }
Run the console entry script with your command:
your-console-entry-script my/custom
When naming commands use
:
as a separator. For example:user:create
,user:delete
, etc.
Since the package is based on Symfony Console component, refer to its documentation for details on how to use the binary and create your own commands.
Aliases and hidden commands
To configure commands, set the names and aliases in \Yiisoft\Yii\Console\CommandLoader
configuration.
Names and aliases from the command class itself are always ignored.
The command can be marked as hidden by prefixing its name with |
.
'yiisoft/yii-console' => [ 'commands' => [ 'hello' => Hello::class, // name: 'hello', aliases: [], hidden: false 'start|run|s|r' => Run::class, // name: 'start', aliases: ['run', 's', 'r'], hidden: false '|hack|h' => Hack::class, // name: 'hack', aliases: ['h'], hidden: true ], ],
Runs PHP built-in web server
You can start local built-in web development server using the command:
./yii serve
Your application will be accessible in your web browser at http://localhost:8080 by default.
To configure default settings, set the options in \Yiisoft\Yii\Console\CommandLoader
configuration.
'yiisoft/yii-console' => [ 'serve' => [ 'appRootPath' => null, 'options' => [ 'address' => '127.0.0.1', 'port' => '8080', 'docroot' => 'public', 'router' => 'public/index.php', ], ], ],
Alternatively, you can pass the settings through the console options. To see the available options, run
./yii serve --help
Testing
Unit testing
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
Mutation testing
The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:
./vendor/bin/roave-infection-static-analysis-plugin
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
License
The Yii Framework Console is free software. It's released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Yii Software.