All of the provided application skeletons include the Console component by default. To enable component in alternative
builds make sure to require composer package spiral/console
and modify the application bootloader:
[
//...
Spiral\Bootloader\CommandBootloader::class,
]
Make sure to include this bootloader at last, as it will also activate the set of default commands for previously added components.
To invoke application command run:
php app.php command:name
To get a list of available commands:
php app.php
To get help about the particular command:
php app.php help command:name
You can invoke console commands inside your application or application tests. This approach can be useful to create mock data for tests or automatically pre-configure the database.
Use Spiral\Console\Console
to do that:
use Spiral\Console\Console;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
// ...
public function test(Console $console): string
{
$input = new ArrayInput([
'--mount' => '.env',
'-p' => '{encrypt-key}'
]);
$output = new BufferedOutput();
return $console->run('encrypt:key', $input, $output)->fetch();
}
Spiral Console dispatcher built at the top of powerful Symfony Console component.
Note
You can register native Symfony Commands in your CLI application.
To apply the custom configuration to the Console component use Spiral\Config\ConfiguratorInterface
or create a config
file in app/config/console.php
:
return [
// application name
'name' => null,
// application version
'version' => null,
// list of application commands (if auto-discover disabled)
'commands' => [],
// list of commands and sequences to run in `app configure`
'configure' => [],
// list of commands and sequences to run in `app update`
'update' => []
];
You can modify some of these values during application bootload via Spiral\Bootloader\ConsoleBootloader
. To register
new user command:
public function boot(ConsoleBootloader $console)
{
$console->addCommand(MyCommand::class);
}
Note
By default Console component use auto-discovery mode to find all user commands inapp/
automatically.
There two type of sequences in the Spiral Framework:
The set of commands that will be run after invoke command php app.php configure
To register command in configure sequence:
use Symfony\Component\Console\Output\OutputInterface;
use Psr\Container\ContainerInterface;
public function boot(ConsoleBootloader $console)
{
// Add console command in a sequence
$console->addConfigureSequence('my:command', '<info>Running my:command...</info>');
// Add closure in a sequence
// It supports auto-wiring of arguments
$console->addConfigureSequence(function(OutputInterface $output, ContainerInterface $container) {
// do something
$output->writeln('...');
}, '<info>Running my:command...</info>');
}
The set of commands that will be run after invoke command php app.php update
To register command in update sequence:
public function boot(Console
$console->addUpdateSequence('my:command', '<info>Running my:command...</info>');
// Add closure in a sequence
// It supports auto-wiring of arguments
$console->addUpdateSequence(function(OutputInterface $output, ContainerInterface $container) {
// do something
$output->writeln('...');
}, '<info>Running my:command...</info>');
}
Please note, console commands invoke outside of the RoadRunner server. Make sure to run an instance of application server if any of your commands must communicate with it.