Skip to content

Latest commit

 

History

History
85 lines (61 loc) · 1.89 KB

command-callables.md

File metadata and controls

85 lines (61 loc) · 1.89 KB
currentMenu
callable

Command callables

A command can be any PHP callable:

// Closure
$app->command('foo', function () {
    // ...
});

// An object implementing __invoke()
$app->command('foo', new InvokableObject());

// An object method
$app->command('foo', [$object, 'method']);

// A static class method
$app->command('foo', ['MyClass', 'method']);

// A function
$app->command('foo', 'someFunction');

Parameters

The callable can take as parameters the arguments and options defined in the expression:

$app->command('greet name [--yell]', function ($name, $yell) {
    // ...
});

The order of parameters doesn't matter as they are always matched by name.

When running $ bin/console greet john --yell:

  • $name will be 'john'
  • $yell will be true

Input and output

You can also ask for the $input and $output parameters to get the traditional Symfony InputInterface and OutputInterface objects (the type-hint is optional):

$app->command(
    'greet name [--yell]',
    function (InputInterface $input, OutputInterface $output) {
        $name = $input->getArgument('name');
        $yell = $input->getOption('yell');

        // ...
    }
);

You can also inject the SymfonyStyle object by type-hinting it:

use \Symfony\Component\Console\Style\SymfonyStyle;

...

$app->command('greet', function (SymfonyStyle $io) {
    $io->write('hello');
});

Finally, you can mix all that (remember the order of parameters doesn't matter):

$app->command(
    'greet name [--yell]',
    function ($name, InputInterface $input, OutputInterface $output) {
        // ...
    }
);

Dependency injection

It is also possible to set up dependency injection through the callables parameters. To learn more about that, read the Dependency injection documentation.