# 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://example.com
	lazy: false
1
2
3
4
5
6
7

In SAPI (CLI) mode, there is no HTTP request and thus no URL address. You have to set base URL on your own so that link generator works. Use console.url option:

console:
	url: https://example.com
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

# Example command

In case of having console.php as entrypoint (see below), this would add a user with username john.doe:

php console.php user:add john.doe

namespace App\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class AddUserCommand extends Command
{

	private UsersModel $usersModel;

	/**
	 * Pass dependencies with constructor injection
	 */
	public function __construct(UsersModel $usersModel)
	{
		parent::__construct(); // don't forget parent call as we extends from Command
		$this->usersModel = $usersModel;
	}

	protected function configure(): void
	{
		// choose command name
		$this->setName('user:add')
			// description (optional)
			->setDescription('Adds user with given username to database')
			// arguments (maybe required or not)
			->addArgument('username', InputArgument::REQUIRED, 'User\'s username');
			// you can list options as well (refer to symfony/console docs for more info)
	}

	/**
	 * Don't forget to return 0 for success or non-zero for error
	 */
	protected function execute(InputInterface $input, OutputInterface $output): int
	{
		// retrieve passed arguments/options
		$username = $input->getArgument('username');

		// you can use symfony/console output
		$output->writeln(\sprintf('Adding user %sā€¦', $username));

		try {
			// do your logic
			$this->usersModel->add($username);
			// styled output is supported as well
			$output->writeln('<success>āœ” Successfully added</success>');
			return 0;

		} catch (\Exception $e) {
			// handle error
			$output->writeln(\sprintf(
				'<error>āŒ Error occurred: </error>',
				$e->getMessage(),
			));
			return 1;
		}
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

# Register command

services:
	- App\Console\AddUserCommand
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