composer require contributte/console
extensions:
console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
The extension will look for all commands extending from Symfony\Component\Console\Command\Command
and automatically add them to the console application.
That's all. You don't have to worry about anything else.
console:
name: Acme Project
version: '1.0'
catchExceptions: true / false
autoExit: true / false
url: https://contributte.com
lazy: false
In SAPI (CLI) mode there is no http request and thus no URL address. This is an inconvenience you have to solve by yourself - via the console.url
option.
console:
url: https://contributte.org
You could also define you custom helperSet
just in case. There are 2 possible approaches. You can register your
App\Model\MyCustomHelperSet
as a service under the services
section or provide it directly to the extension config helperSet
.
Already defined service:
services:
customHelperSet: App\Model\MyCustomHelperSet
console:
helperSet: @customHelperSet
Directly defined helperSet:
console:
helperSet: App\Model\MyCustomHelperSet
By default, helperSet contains 4 helpers defined in Symfony\Component\Console\Application
. You can add more helpers, if need them.
console:
helpers:
- App\Model\MyReallyGreatHelper
From version 3.4 Symfony\Console
uses command lazy-loading. This extension fully supports this feature and
you can enable it in the NEON file.
console:
lazy: true
From this point forward, all commands are instantiated only if needed. Don't forget that listing all commands will instantiate them all.
How to define command names? Define $defaultName
in the command or via the console.command
tag on the service.
use Symfony\Component\Console\Command\Command;
class FooCommand extends Command
{
protected static $defaultName = 'app:foo';
}
Or via a service tag.
services:
commands.foo:
class: App\FooCommand
tags: [console.command: app:foo]
namespace App\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
final class FooCommand extends Command
{
protected function configure(): void
{
$this->setName('foo');
}
protected function execute(InputInterface $input, OutputInterface $output): void
{
// Some magic..
}
}
services:
- App\Console\FooCommand
Maybe you will have to flush the temp/cache
directory.
The very last piece of the puzzle is the console entrypoint. It is a simple script that loads the DI container and fires Contributte\Console\Application::run
.
You can copy & paste it to your project, for example to <root>/bin/console
.
Make sure to set it as executable. chmod +x <root>/bin/console
.
#!/usr/bin/env php
<?php declare(strict_types = 1);
require __DIR__ . '/../vendor/autoload.php';
exit(App\Bootstrap::boot()
->createContainer()
->getByType(Contributte\Console\Application::class)
->run());
#!/usr/bin/env php
<?php declare(strict_types = 1);
$container = require __DIR__ . '/../app/bootstrap.php';
exit($container->getByType(Contributte\Console\Application::class)->run());