2020-05-03 16:41:59 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Aerex\BaikalStorage;
|
|
|
|
|
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException as ProcessFailedException;
|
|
|
|
|
|
|
|
class Console extends AbstractConsole {
|
|
|
|
private $defaultArgs = [];
|
|
|
|
|
|
|
|
function __construct($defaultArgs) {
|
|
|
|
$this->defaultArgs = $defaultArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function convertToString($input) {
|
|
|
|
if (is_array($input)) {
|
|
|
|
return json_encode($input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 23:56:22 -05:00
|
|
|
public function execute($cmd, $args, $input = null, $envs = []) {
|
2020-05-03 16:41:59 -05:00
|
|
|
$stdin[] = $cmd;
|
2020-05-12 23:56:22 -05:00
|
|
|
$stdin = array_merge($stdin, $this->defaultArgs, $args);
|
2020-05-03 16:41:59 -05:00
|
|
|
|
|
|
|
if (isset($input)) {
|
2020-06-02 21:16:18 -05:00
|
|
|
$input = $this->convertToString($input);
|
2020-05-03 16:41:59 -05:00
|
|
|
}
|
2020-06-03 00:04:43 -05:00
|
|
|
$process = new Process(implode(' ', $stdin), null, $envs, $input);
|
2020-05-12 23:56:22 -05:00
|
|
|
$process->inheritEnvironmentVariables();
|
2020-05-03 16:41:59 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
$process->mustRun();
|
|
|
|
return $process->getOutput();
|
|
|
|
} catch (ProcessFailedException $error) {
|
|
|
|
throw $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|