# Content

# Setup

composer require contributte/console
1
extensions:
    console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
1
2

The extension will look for all commands extending from Symfony\Component\Console\Command\Command (opens new window) and automatically add them to the console application. That's all. You don't have to worry about anything else.

# Configuration

console:
    name: Acme Project
    version: '1.0'
    catchExceptions: true / false
    autoExit: true / false
    url: https://contributte.com
    lazy: false
1
2
3
4
5
6
7

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
1
2

# Helpers

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
1
2
3
4
5

Directly defined helperSet:

console:
    helperSet: App\Model\MyCustomHelperSet
1
2

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
1
2
3

# Lazy-loading

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
1
2

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';
}
1
2
3
4
5
6

Or via a service tag.

services:
    commands.foo:
        class: App\FooCommand
        tags: [console.command: app:foo]
1
2
3
4

# Command

# Create command


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..
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# Register command

services:
    - App\Console\FooCommand
1
2

Maybe you will have to flush the temp/cache directory.

# Entrypoint

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.

# Nette 3.0+
#!/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());
1
2
3
4
5
6
7
8
9
# Nette <= 2.4
#!/usr/bin/env php
<?php declare(strict_types = 1);

$container = require __DIR__ . '/../app/bootstrap.php';

exit($container->getByType(Contributte\Console\Application::class)->run());
1
2
3
4
5
6