simple, dependency-free, uncomplicated shell scripting command class
[TOC]
composer require treehousetim/command
<?php
$command = new \treehousetim\command\command();
$command
->command( 'ls' )
->arg( '-lah' );
echo $command->getExecutableString();
// output:
// ls '-lah'
?>
Notice the use of the factory method here to make code pretty.
<?php
echo \treehousetim\command\command::factory()
->command( 'ls' )
->arg( '-lah' )
->pipeCommand(
command::factory()
->command( 'more' )
)
->getExecutableString();
// output:
// ls '-lah' | more
treehousetim/command supports sub commands, combining stderr and stdout and output redirection to a file.
<?php
use \treehousetim\command\command;
$connection = '[email protected]';
$loginPath = 'mylogin';
$db = 'my_database';
$tables = ['table1', 'table2'];
$where = 'id > 1200';
echo command::factory()
->command( 'ssh' )
->arg( $connection )
->subCommand(
command::factory()
->command( '/usr/bin/mysqldump' )
->arg( '--login-path=' . $loginPath )
->arg( '--skip-add-drop-table' )
->arg( '--no-create-info' )
->arg( $db )
->arg( implode( ',', $tables ) )
->arg( '--where=' . $where )
->stdoutStderrCombine()
)
->stdoutStderrCombine()
->outputFile( '/path/to/file.sql' )
->getExecutableString();
// output
// ssh '[email protected]' "/usr/bin/mysqldump '--login-path=mylogin' '--skip-add-drop-table' '--no-create-info' 'my_database' 'table1,table2' '--where=id > 1200' 2>&1" 2>&1 > /path/to/file.sql
// minor difference: outputAppendFile - uses >> instead of >
echo command::factory()
->command( 'ssh' )
->arg( $connection )
->subCommand(
command::factory()
->command( '/usr/bin/mysqldump' )
->arg( '--login-path=' . $loginPath )
->arg( '--skip-add-drop-table' )
->arg( '--no-create-info' )
->arg( $db )
->arg( implode( ',', $tables ) )
->arg( '--where=' . $where )
->stdoutStderrCombine()
)
->stdoutStderrCombine()
->outputAppendFile( '/path/to/file.sql' )
->getExecutableString();
// output
// ssh '[email protected]' "/usr/bin/mysqldump '--login-path=mylogin' '--skip-add-drop-table' '--no-create-info' 'my_database' 'table1,table2' '--where=id > 1200' 2>&1" 2>&1 >> /path/to/file.sql
Simply call $command->exec();
to execute your command.
Each time ->exec();
is called, an entry in the $command->execResults
array is created.
The format of $command->execResults[]
is as follows:
[
'content' => $content,
'status' => $returnStatus,
'command' => $command
];
If all you want is a list of commands that have been executed, without the other array entries you may use $command->commandLog[]
.
echo $command->commandLog[0];
The output from your command is returned from the exec()
method.
Command executions may be logged. To set up logging, pass any \Psr\Log\LoggerInterface
object to the constructor or the factory method on \treehousetim\command\command
to log executions.
<?php
use \treehousetim\command\command;
$log = new \Monolog\Logger( 'My Example Program' );
$log->pushHandler( new StreamHandler( './example.log', \Monolog\Logger::DEBUG ) );
$command = command::factory( $log )
->command( 'ls' )
->arg( '-lah' )
->exec();
$output = $command->exec();
echo $command->commandLog[0];
echo PHP_EOL;
echo $output
?>
If you have cloned this repo, you can run the tests.
Run composer install to install PHPUnit and Monolog.
composer install
./vendor/bin/phpunit test