2017 © Pedro Peláez
 

library command-runner

Execute console command and process result.

image

command-runner/command-runner

Execute console command and process result.

  • Tuesday, August 23, 2016
  • by avpretty
  • Repository
  • 1 Watchers
  • 0 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Build Status Test Coverage, (*1)

Command runner

Execute console command and process result., (*2)

This library allows you to launch an external application via the console and process the results. Also can be use to safely run daemons., (*3)

Installation

Add to your composer.json, (*4)

"command-runner/command-runner": "dev-master"

Example of single command:, (*5)

$commandRunner = new CommandRunner;
$result = $commandRunner->setCommand('pwd')->execute();

//  [
//      'output'     => 'path_to_your_working_dir',
//      'resultCode' => 0
//  ]

With additionals arguments:, (*6)

...
$result = $commandRunner->setCommand('ls -la')
                        ->setArgument(['./assets'])
                        ->execute();

//  [
//      'output'     => [
//          'drwxrwxr-x 1 vagrant vagrant  4096 Aug 22 13:32 e45d6ac2',
//          'drwxrwxr-x 1 vagrant vagrant  4096 May 12 17:51 efd003fa',
//          ...
//      ],
//      'resultCode' => 0
//  ]

Arguments are optional. You can path command with all arguments to setCommand method., (*7)

If you want to get raw command output:, (*8)

...
$commandRunner->setRawOutput(true); // false by default

//  [
//      'output'     => 
//          'drwxrwxr-x 1 vagrant vagrant  4096 Aug 22 13:32 e45d6ac2
//          drwxrwxr-x 1 vagrant vagrant  4096 May 12 17:51 efd003fa
//          ...
//      ,
//      'resultCode' => 0
//  ]

Accordingly to redirect standard command output use setWaitForOutput(false)., (*9)

Command queue

Class CommandQueue can be used to perform multiple commands with the possibility of setting the individual parameters:, (*10)

$commandQueue = new CommandQueue();
$commandQueue->setCommandQueue(
    [
        // first command
        [
            'command'   => 'ps aux | grep',
            'escape'    => false,           // false by default
            'arguments' => ['rabbitmq'],    // optional
            'options'   => [
                'saveOutputName' => 'ps_output_file_name'
            ],
        ],
        // second command
        [
            'command' => 'ls -la',
            'escape'  => false,
            'options' => [
                'saveOutputName' => 'ls_output_file_name'
            ],
        ],
         // call daemon
        [
            'command' => './start_daemon',
            'escape'  => false,
            'options' => [
                'waitForOutput' => false
            ],
        ]
    ]
);

$result = $commandQueue->execute();

// Source array will be modified with command results:
// [
//     'command'     => 'ls -la',
//     'escape'      => false,
//     'options'     => [
//         'saveOutputName' => 'ls_output_file_name'
//     ],
//     'output'      => [
//           ...
//           'drwxrwxr-x 1 vagrant vagrant  4096 Aug 22 13:30 fonts',
//           'drwxrwxr-x 1 vagrant vagrant  4096 Aug 22 13:30 img'
//           ...
//     'resultCode' => 0
// ]

Save output

Every command result can be saved to file. By default all files saves to /tmp/commandRunnerOutput. Create new instance of CommandOutput class to change default behavior:, (*11)

...
$commandOutput = new CommandOutput;
$commandOutput->setOutputPath(__DIR__);

$commandRunner->setCommandOutput($commandOutput)
              ->setSaveOutputName('resultFileName.txt');
...
// File with command result will be saved to working dir.

You can implement OutputInterface for you custom output handler., (*12)

In case of command queue output handler can be specified with:, (*13)

...
$commandOutput = new CommandOutput;
$commandOutput->setOutputPath(__DIR__);

$commandQueue->setCommandQueue(
    [
        [
            'command'   => 'ps aux | grep',
            'escape'    => false,           // false by default
            'arguments' => ['rabbitmq'],    // optional
            'options'   => [
                'saveOutputName'   => 'ps_output_file_name',
                'setCommandOutput' => $commandOutput
            ],
        ],
    ]
);
...

The Versions

23/08 2016

dev-master

9999999-dev

Execute console command and process result.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by AlexyAV

php command console terminal