2017 © Pedro Peláez
 

library console

Refactored Symfony Console Component

image

danack/console

Refactored Symfony Console Component

  • Friday, January 23, 2015
  • by Danack
  • Repository
  • 3 Watchers
  • 9 Stars
  • 1,997 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 142 Forks
  • 3 Open issues
  • 3 Versions
  • 33 % Grown

The README.md

Console Router

Build Status, (*1)

This is not the Symfony console - it is a forking refactor to:, (*2)

  • To split the 'routing' and dispatching of commands.
  • Remove the events, because they don't belong in what should be a reasonable, simple piece of code.
  • Stop the console application catching and dumping exceptions when it has no idea how to handle them.

Basically although most of the Symfony/console library does a great job, the fact that you have to let it run the application is stupid. The console library should stick to console stuff, you should then be able to run the application yourself., (*3)

The example below shows how to create commands with a callable, have the console application 'route' the input, and then run the callable with Auryn., (*4)

$console = new Application();
$console->add(new AboutCommand());

// Create a command that will call the function 'uploadFile'
$uploadCommand = new Command('uploadFile', 'upload');
$uploadCommand->addArgument('name', InputArgument::REQUIRED, 'The name of the thing to foo');
$console->add($uploadCommand);

$helloWorldCallable = function ($name) {
    echo "Hello world, and particularly $name".PHP_EOL;
};

// Create a command that will call the closure
$callableCommand = new Command($helloWorldCallable, 'greet');
$callableCommand->addArgument('name', InputArgument::REQUIRED, 'The name of the person to say hello to.');
$console->add($callableCommand);

try {
    $parsedCommand = $console->parseCommandLine();
}
catch (\Exception $e) {
    $output = new BufferedOutput();
    $console->renderException($e, $output);
    echo $output->fetch();
    exit(-1);
}

$provider = new Auryn\Provider();
$provider->execute(
    $parsedCommand->getCallable(),
    lowrey($parsedCommand->getParams())
);


function uploadFile($filename) {
    echo "Need to upload the file $filename".PHP_EOL;
}

// Auryn needs scalars prefixed with a colon
function lowrey($params) {
    $newParams = [];
    foreach ($params as $key => $value) {
        $newParams[':'.$key] = $value;
    }
    return $newParams;
}

If the example above was in the file example.php running the command php example.php greet Danack would output:, (*5)

Hello world, and particularly Danack, (*6)

\o/, (*7)

If you want to see an example running please run the file Tests/example.php with some appropriate arguments e.g.:, (*8)

  • php Tests/example.php upload backup.zip --dir=/var/log
  • php Tests/example.php greet Danack
  • php Tests/example.php greet

Will show the 'upload' and 'greet' commands being routed correctly, (*9)

Migrating from Symfony/console

The only major work needed to migrate from Symfony/console to Danack/console is to change any command objects to return a callable instead of having an execute method., (*10)

This includes commands that just display information rather than having a 'proper' executable e.g. the ListCommand., (*11)

Then just change from:, (*12)

  • Application::run which runs the command and returns a status code

to, (*13)

  • Application::parseCommandLine which just parsed the command line args and returns a ParsedCommand object.

Previous readme

Console eases the creation of beautiful and testable command line interfaces., (*14)

Tests

You can run the unit tests with the following command:, (*15)

$ cd path/to/Symfony/Component/Console/
$ composer.phar install
$ phpunit

Third Party

Resources/bin/hiddeninput.exe is a third party binary provided within this component. Find sources and license at https://github.com/Seldaek/hidden-input., (*16)

Resources

The Console Component, (*17)

How to create a Console Command, (*18)

The Versions

23/01 2015

dev-master

9999999-dev http://console.basereality.com/

Refactored Symfony Console Component

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Avatar Danack

console cli

23/01 2015

v2.6.1

2.6.1.0 http://console.basereality.com/

Refactored Symfony Console Component

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Avatar Danack

console cli

22/09 2014

v2.6.0

2.6.0.0 http://console.basereality.com/

Refactored Symfony Console Component

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Avatar Danack

console cli