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;
69 $this->translator->trans('commands.exec.messages.working-directory') .': ',
72 $this->io->writeln($workingDirectory);
74 $this->translator->trans('commands.exec.messages.executing-command') .': ',
77 $this->io->writeln($command);
79 $this->process = new Process($command);
80 $this->process->setWorkingDirectory($workingDirectory);
81 $this->process->enableOutput();
82 $this->process->setTimeout(null);
84 function ($type, $buffer) {
85 $this->io->write($buffer);
89 if (!$this->process->isSuccessful()) {
90 throw new ProcessFailedException($this->process);
93 return $this->process->isSuccessful();
99 public function getOutput()
101 return $this->process->getOutput();