Giter Club home page Giter Club logo

command-handler's Introduction

Command Handler

Build Status

Install

Install from composer:

$ composer require skillberto/command-handler "~1.0"

Authors and contributors

Usage

The following example show you how you can us it:

use Symfony\Component\Console\Output\ConsoleOutput;
use Skillberto\CommandHandler\CommandHandler;
use Skillberto\CommandHandler\Command;

$output = new ConsoleOutput();
$handler = new CommandHandler($output);
$handler->add( 'some commands' );

... or

$handler->addCommand(new Command('some commands'));
$handler->execute();

You can define collections too:

$handler->addCollection(array('some commands'));

... or

$handler->addCommands(array(new Command('some commands')));

If you want to skip a command if it's not required:

$handler->addSkippable('some commands');

... or

$handler->addCommand(new Command('some commands', false));

... or add more commands

$handler->addSkippableCollection( array( 'some commands' ) );

... or

$handler->addCommands(array(new Command('some commands', false)));

$handler->execute();

...after that you can get these commands:

$handler->getSkippedMessages();

But, if you don't skip a command, and it's not successful:

$handler->getErrorMessage();

Advanced usage

Prefix

The following example show you have can you use the prefix:

...

$handler = new CommandHandler($output, "php ");

... or

$handler = new CommandHandler($output);
$handler->addPrefix("php ");

... then

$handler->add("--version");
$handler->execute();

In this case, you will execute php --version.

Timeout

It can be defined for each command, or only for some commands, or both of them.

$handler = new CommandHandler($output, "", 0.2);

... or

$handler = new CommandHandler($output);
$handler->setTimeout(0.2);

... then

$handler->addCommand(new Command("php --version", true, 0.3);
$handler->add('something');
$handler->execute();

In the previous example every command will have "0.2 seconds" for execution, except php --version, it has got "0.3 seconds".

Let's see how can you define group timeout:

$handler = new CommandHandler($output);
$handler->setTimeout(0.2);
$handler->addCollection(array('some command'));
$handler->setTimeout(0.3);
$handler->addCollection(array('some command'));
$handler->execute();

In this example the first collection have "0.2 seconds", the second "0.3 seconds".

Handler injection, merge

In some case, we need to define more then one handler, eg.: for different prefixes. But don't worry about it, we have got a useful method:

$handler_1->addHandler($handler_2);

It's good, but what will be with prefixes and timeout? For these problems, CommandHandler has got three different merge types: MERGE_ALL, MERGE_NON, MERGE_NOT_DEFINED The default is MERGE_NON, but you can change it:

$handler_1->addHandler($handler_2, CommandHandler::MERGE_ALL, CommandHandler::MERGE_NOT_DEFINED);

In the previous example prefix merge has got MERGE_ALL type, timeout has got MERGE_NOT_DEFINED type. MERGE_ALL means that $handler_1 prefix / timeout will use for all of them. MERGE_NOT_DEFINED means that $handler_1 prefix / timeout will use, if it's not defined for $handler_2 command(s). MERGE_NON means that prefixes and timeouts will be separated.

Callback

You can define a callback for each execution:

...
use Symfony\Component\Process\Process;
use Skillberto\CommandHandler\Command;
...

$handler->execute(function(Progress $progress, Command $command) {
   //something more
});

command-handler's People

Contributors

skillberto avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.