Revision: Fri, 17 May 2024 13:03:36 GMT
v2.14 – outdated
This version of the documentation is outdated. Consider upgrading your project to Spiral Framework 3.12
Edit this page

Console - Installation and Configuration

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:

php
[
    //...
    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

Invoke in Application

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:

php
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();
}

Symfony/Console

Spiral Console dispatcher built at the top of powerful Symfony Console component.

Note
You can register native Symfony Commands in your CLI application.

Configuration

To apply the custom configuration to the Console component use Spiral\Config\ConfiguratorInterface or create a config file in app/config/console.php:

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:

php
public function boot(ConsoleBootloader $console)
{
    $console->addCommand(MyCommand::class);
}

Note
By default Console component use auto-discovery mode to find all user commands in app/ automatically.

Sequences

There two type of sequences in the Spiral Framework:

Configure

The set of commands that will be run after invoke command php app.php configure

To register command in configure sequence:

php
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>');
}

Update

The set of commands that will be run after invoke command php app.php update

To register command in update sequence:

php
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>');
}

Connection with RoadRunner

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.