hiroto-k/console-wrapper

Wrapper class of symfony/console

1.2.0 2018-12-07 05:19 UTC

This package is auto-updated.

Last update: 2024-03-29 03:44:45 UTC


README

Build Status PHP from Packagist Maintainability License

Wrapper class of symfony/console

Install

composer require hiroto-k/console-wrapper:^1.0

Documents

Examples

Example Command class

<?php

namespace ExampleApp\Commands;

use HirotoK\ConsoleWrapper\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

/**
 * Command class of "greeting" command.
 */
class GreetingCommand extends Command
{
    /**
     * Definition the command name.
     *
     * @var string
     */
    protected $name = 'greeting';

    /**
     * Definition the command description.
     *
     * @var string
     */
    protected $description = 'Example greeting command with console-wrapper';

    /**
     * Setup the command class.
     */
    protected function setup()
    {
        // Setup class configure.
        // This method is called before the "configure" method.
    }

    /**
     * Execute the command.
     */
    protected function handle()
    {
        $greet = $this->option('hi') ? 'Hi, ' : 'Hello, ';

        $name = $this->argument('first-name');

        if ($this->hasArgument('family-name')) {
            $name .= ' '.$this->argument('family-name');
        }

        $this->writeln($greet.$name);
    }

    /**
     * Definition the command arguments.
     *
     * @see \Symfony\Component\Console\Command\Command::addArgument
     *
     * @return array
     */
    protected function commandArguments()
    {
        return [
            // [$name, $mode = null, $description = '', $default = null]
            ['first-name', InputArgument::REQUIRED, 'Your first name (required)'],
            ['family-name', InputArgument::OPTIONAL, 'Your family name (optional)'],
        ];
    }

    /**
     * Definition the command options.
     *
     * @see \Symfony\Component\Console\Command\Command::addOption
     *
     * @return array
     */
    protected function commandOptions()
    {
        return [
            // [$name, $shortcut = null, $mode = null, $description = '', $default = null]
            ['hi', null, InputOption::VALUE_NONE, 'Use "Hi".'],
        ];
    }
}

Example Application class

<?php

namespace ExampleApp;

use HirotoK\ConsoleWrapper\Application as WrapperApplication;
use Symfony\Component\Console\Input\InputOption;

/**
 * Customize application class.
 */
class Application extends WrapperApplication
{
    /**
     * Definition the global command options.
     *
     * @return array
     */
    protected function globalOptions()
    {
        return [
            // [$name, $shortcut = null, $mode = null, $description = '', $default = null]
            ['debug', null, InputOption::VALUE_NONE, 'Enable debug mode.'],
            ['config', 'c', InputOption::VALUE_REQUIRED, 'Set path of config file.', 'path/to/default'],
        ];
    }
}

Example execute file

<?php

use ExampleApp\Application;
use ExampleApp\Commands\GreetingCommand;

$application = new Application();

// Add command class.
$application->add(new GreetingCommand());

// Start the application.
$application->run();

Uses

Arguments

Definition the arguments, use the Command::commandArguments() method.

Returned array will pass to the Command::addArgument() method. See the documents for arguments of Command::addArgument() method.

/**
 * Definition the command arguments.
 *
 * @return array
 */
protected function commandArguments()
{
    return [
        // [$name, $mode = null, $description = '', $default = null]
        ['user-id', InputArgument::REQUIRED, 'User name (required)'],
        ['task', InputArgument::OPTIONAL, 'Task name (optional)', 'default'],
    ];
}

For the access to arguments, use the Command::argument() method.

If you need to all of the arguments, use the Command::arguments() method

To checks if the arguments exists, use the Command::hasArgument() method.

protected function handle()
{
    // Get argument value.
    $userId = $this->argument('user-id');

    // Get all arguments.
    $allArguments = $this->arguments();

    // Checks whether a argument exists.
    $hasTaskArgument = $this->hasArgument('task');
}

Options

Definition the options, use the Command::commandOptions() method.

Returned array will pass to the Command::addOption() method. See the documents for arguments of Command::addOption() method.

/**
 * Definition the command options.
 *
 * @return array
 */
protected function commandOptions()
{
    return [
        // [$name, $shortcut = null, $mode = null, $description = '', $default = null]
        ['name', null, InputOption::VALUE_REQUIRED, 'User name.', 'default name'],
        ['force', null, InputOption::VALUE_NONE, 'Force execute.'],
    ];
}

For the access to options, use the Command::option() method.

If you need to all of the options, use the Command::options() method

To checks if the options exists, use the Command::hasOption() method.

protected function handle()
{
    // Get option value.
    $name = $this->option('name');

    // Get all options.
    $allOptions = $this->options();

    // Checks whether a option exists.
    $hasForceOption = $this->hasOption('force');
}

Global options

console-wrapper can easily set global options.

<?php

namespace ExampleApp;

use HirotoK\ConsoleWrapper\Application as WrapperApplication;
use Symfony\Component\Console\Input\InputOption;

class Application extends WrapperApplication
{
    /**
     * Definition the global command options.
     *
     * @return array
     */
    protected function globalOptions()
    {
        return [
            // [$name, $shortcut = null, $mode = null, $description = '', $default = null]
            ['debug', null, InputOption::VALUE_NONE, 'Enable debug mode.'],
            ['config', 'c', InputOption::VALUE_REQUIRED, 'Set path of config file.', 'path/to/default'],
        ];
    }
}

Output

console-wrapper is provided some output methods.

protected function handle()
{
    // Display normal message.
    $this->writeln('message');
    $this->writeln([
        'multi',
        'line',
        'message',
    ]);

    // Display message with styles.
    $this->info('info style');
    $this->comment('comment style');
    $this->question('question style');
    $this->error('error style');
}

Auto add commands by PSR-4

If project using PSR-4, auto load all commands by Application::loadByPsr4() method.

/**
 * Auto add commands by PSR-4.
 * 
 * loadByPsr4(string $nameSpacePrefix, string $targetDir)
 */
$application->loadByPsr4("\ExampleApp\Commands", realpath(__DIR__.'/src/Commands'));

Logger

console-wrapper can easily use the logger.

Sets the logger instance in the execute file. Logger class must be implemented the \Psr\Log\LoggerInterface interface.

/**
 * Set logger instance to application and command class.
 */
$application->setLogger($logger);

In the command class, use the logger() method and use the logger.

protected function handle()
{
    $this->logger()->debug('Debug message');
    $this->logger()->error('Error message');
}

Default logger

If logger instance not sets in application, default using the \Psr\Log\NullLogger class.

\Psr\Log\NullLogger

protected function handle()
{
    // Output Psr\Log\NullLogger
    $this->writeln(get_class($this->logger()));
}

Override the default logger

If you want override the default logger, please override the Application::createDefaultLogger() method. Return instance must be implemented the \Psr\Log\LoggerInterface interface.

// Use monolog in default logger

use HirotoK\ConsoleWrapper\Application as WrapperApplication;
use Monolog\Logger;

class Application extends WrapperApplication
{
    /**
     * Override default logger instance.
     * Return instance must be implement the \Psr\Log\LoggerInterface
     * 
     * @return \Monolog\Logger
     */
    protected function createDefaultLogger()
    {
        return new Logger();
    }
}

Using logger in Command::setup

If using the logger in Command::setup() method, must be sets the logger instance before commands add.

// Register logger instance
$application->setLogger($logger);

// Add commands
$application->add(new GreetingCommand());
$application->loadByPsr4("\ExampleApp\Commands", realpath(__DIR__.'/src/Commands'));

Helpers and Utils

Confirm question

Simple confirmation. Default returns true.

protected function handle()
{
    if ($this->confirm('continue ? (y/n) ')) {
        // If enter y
    }
}

sets default value

protected function handle()
{
    if ($this->confirm('continue ? (y/n) ', false)) {
        // If enter y
    }
}

Call other command

Call other command in command class.

protected function handle()
{
    // Call the "other-command-name" command
    $this->callCommand('other-command-name');
}

with parameters

protected function handle()
{
    // Call the "other-command-name" command, with name parameter
    $this->callCommand('other-command-name', ['name' => 'example']);
}

Render tables

protected function handle()
{
    // Only creates Table class instance.
    $table = $this->createTable();

    // Sets headers and rows
    $headers = ['name', 'location'];
    $rows = [
        ['Hoge', 'jp'],
        ['Foo', 'us'],
    ];
    $table = $this->createTable($headers, $rows)

    // Render table
    $table->render();
}
+------+----------+
| name | location |
+------+----------+
| Hoge | jp       |
| Foo  | us       |
+------+----------+

customize tables, place see the \Symfony\Component\Console\Helper\Table class.

// Set the column width.
$this
    ->createTable($headers, $rows)
    ->setColumnWidth(0, 10)
    ->render();

Progress Bar

protected function handle()
{
    $progressBar = $this->createProgressBar(100);

    $progressBar->start();

    $i = 0;
    while ($i++ < 100) {
        $progressBar->advance();
    }

    $progressBar->finish();
}

customize progress bar, place see the \Symfony\Component\Console\Helper\ProgressBar class.

Gets a helper instance

protected function handle()
{
    // Get the question helper instance
    $this->getQuestionHelper();

    // Get the process helper instance.
    $this->getProcessHelper();
}

License

MIT License