baikal-storage-plugin/lib/Console.php

40 lines
898 B
PHP
Raw Permalink Normal View History

<?php
2020-05-03 16:41:59 -05:00
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-03 16:41:59 -05:00
}
return $input;
2020-05-03 16:41:59 -05:00
}
public function execute($cmd, $args, $input = null, $envs = []) {
2020-05-03 16:41:59 -05:00
$stdin[] = $cmd;
$stdin = array_merge($stdin, $this->defaultArgs, $args);
2020-05-03 16:41:59 -05:00
if (isset($input)) {
$input = $this->convertToString($input);
2020-05-03 16:41:59 -05:00
}
$process = new Process($stdin, null, $envs, $input);
2020-05-03 16:41:59 -05:00
try {
$process->mustRun();
return $process->getOutput();
} catch (ProcessFailedException $error) {
throw $error;
}
}
}