interitty/console

Extension of the standard Symfony/Console by some other features that are specific for use in the Interitty projects.

v1.0.7 2024-08-31 15:52 UTC

This package is auto-updated.

Last update: 2024-10-31 00:23:33 UTC


README

Extension of the standard Symfony/Console by some other features that are specific for use in the Interitty projects.

Requirements

Installation

The best way to install interitty/console is using Composer:

composer require interitty/console

Then register the extension in the Nette config file:

# app/config/config.neon
extensions:
    console: Interitty\Console\Nette\DI\ConsoleExtension(%consoleMode%)

Features

The Interitty/Console provides some additional features to the standard Symfony/Console library.

Console output

The BaseCommand class provides write and writeError methods that allow writing to standard output respectively to standard error output without the need to transmit the OutputInterface object to every method.

protected function foo(): void
{
    // outputs multiple lines to the console (adding PHP_EOL at the end of each line)
    $this->write([
        'User Creator',
        '============',
        '',
    ]);

    // the value returned by someMethod() can be an iterator (https://php.net/iterator)
    // that generates and returns the messages with the 'yield' PHP keyword
    $this->write($this->someMethod());

    // outputs a message followed by a PHP_EOL
    $this->write('Whoa!');

    // outputs a message without adding a PHP_EOL at the end of the line
    $this->write('You are about to ', false);
    $this->write('create a user.', false);

    // outputs a message to standard error output
    $this->writeError('<error>big bada bum</error>');
}

This is possible thanks to stored InputInterface and OutputInterface objects that are accessible via getInput, getOutput, and getErrorOutput methods.

Exception processor

When the Exception or any Throwable object was thrown in the execute process, there is a standard processException method that converts the exception message to a standard error output message. It also dumps a trace log when the Debug verbosity mode is set.

Lazy-loading

The ConsoleExtension extension automatically binds all registered services of type Symfony\Component\Console\Command\Command using a simple implementation LazyCommandLoader.

The value defined by the AsCommand attribute is used as the command name.

use Interitty\Console\BaseCommand;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'app:dummy')]
class DummyCommand extends BaseCommand
{
}

However, many existing classes still need to start using this attribute. It may also be helpful to set the name to a different value. In these cases, using so-called tags when defining a service is possible.

services:
    commands.dummy:
        class: App\DummyCommand
        tags: [console.command: app:dummy]
        # or
        tags: [console.command: {name: app:dummy}]

ProgressBar factory

The factory method createProgressBar is available for easier access to the progress bar.

protected function processExecute(): int
{
    $data = $this->getData();
    $progressBar = $this->createProgressBar($data->count());
    $progressBar->start();
    foreach($data as $item) {
        // Do some logic here
        $progressBar->advance();
    }
    $progressBar->finish();
    return self::SUCCESS;
}