2 namespace Drupal\Console\Core\Utils;
4 use Symfony\Component\Console\Input\ArrayInput;
5 use Symfony\Component\Console\Output\ConsoleOutput;
6 use Symfony\Component\Process\Process;
7 use Symfony\Component\Process\Exception\ProcessFailedException;
8 use Drupal\Console\Core\Style\DrupalStyle;
13 * @package Drupal\Console\Core\Utils
23 * @var TranslatorManagerInterface
25 protected $translator;
38 * Process constructor.
40 * @param string $appRoot
41 * @param TranslatorManagerInterface $translator
43 public function __construct($appRoot, $translator)
45 $this->appRoot = $appRoot;
46 $this->translator = $translator;
48 $output = new ConsoleOutput();
49 $input = new ArrayInput([]);
50 $this->io = new DrupalStyle($input, $output);
54 * @param string $command
55 * @param string $workingDirectory
57 * @throws ProcessFailedException
61 public function exec($command, $workingDirectory=null)
63 if (!$workingDirectory || $workingDirectory==='') {
64 $workingDirectory = $this->appRoot;
67 if (realpath($workingDirectory)) {
69 $this->translator->trans('commands.exec.messages.working-directory') .': ',
72 $this->io->writeln(realpath($workingDirectory));
76 $this->translator->trans('commands.exec.messages.executing-command') .': ',
79 $this->io->writeln($command);
81 $this->process = new Process($command);
82 $this->process->setWorkingDirectory($workingDirectory);
83 $this->process->enableOutput();
84 $this->process->setTimeout(null);
86 function ($type, $buffer) {
87 $this->io->write($buffer);
91 if (!$this->process->isSuccessful()) {
92 throw new ProcessFailedException($this->process);
95 return $this->process->isSuccessful();
101 public function getOutput()
103 return $this->process->getOutput();