for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace pastuhov\Command;
/**
* Command.
*/
final class Command
{
private function __construct()
}
* Execute command with params.
*
* @param string $command
* @param array $params
* @param bool $escape True to escape each param using escapeshellarg
* @return bool|string
* @throws \Exception
public static function exec($command, array $params = array(), $escape=true)
if (empty($command)) {
throw new \InvalidArgumentException('Command line is empty');
$command = self::bindParams($command, $params, $escape);
exec($command, $output, $code);
if (count($output) === 0) {
$output = $code;
} else {
$output = implode(PHP_EOL, $output);
if ($code !== 0) {
throw new CommandException($command, $output, $code);
return $output;
* Bind params to command.
* @return string
public static function bindParams($command, array $params, $escape)
if (count($params) > 0) {
$wrapper = function ($string) {
return '{' . $string . '}';
};
$converter = function ($var) use ($escape) {
if (is_array($var)) {
$var = implode(' ', $var);
if ($escape) {
$var = escapeshellarg($var);
return $var;
$command = str_replace(
array_map(
$wrapper,
array_keys($params)
),
$converter,
array_values($params)
$command
);
return $command;