Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / drupal / console-core / src / Utils / ShellProcess.php
1 <?php
2 namespace Drupal\Console\Core\Utils;
3
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;
9
10 /**
11  * Class ShellProcess
12  *
13  * @package Drupal\Console\Core\Utils
14  */
15 class ShellProcess
16 {
17     /**
18      * @var string
19      */
20     protected $appRoot;
21
22     /**
23      * @var TranslatorManagerInterface
24      */
25     protected $translator;
26
27     /**
28      * @var ShellProcess
29      */
30     private $process;
31
32     /**
33      * @var DrupalStyle
34      */
35     private $io;
36
37     /**
38      * Process constructor.
39      *
40      * @param string                     $appRoot
41      * @param TranslatorManagerInterface $translator
42      */
43     public function __construct($appRoot, $translator)
44     {
45         $this->appRoot = $appRoot;
46         $this->translator = $translator;
47
48         $output = new ConsoleOutput();
49         $input = new ArrayInput([]);
50         $this->io = new DrupalStyle($input, $output);
51     }
52
53     /**
54      * @param string $command
55      * @param string $workingDirectory
56      *
57      * @throws ProcessFailedException
58      *
59      * @return Process
60      */
61     public function exec($command, $workingDirectory=null)
62     {
63         if (!$workingDirectory || $workingDirectory==='') {
64             $workingDirectory = $this->appRoot;
65         }
66
67         if (realpath($workingDirectory)) {
68             $this->io->comment(
69                 $this->translator->trans('commands.exec.messages.working-directory') .': ',
70                 false
71             );
72             $this->io->writeln(realpath($workingDirectory));
73         }
74
75         $this->io->comment(
76             $this->translator->trans('commands.exec.messages.executing-command') .': ',
77             false
78         );
79         $this->io->writeln($command);
80
81         $this->process = new Process($command);
82         $this->process->setWorkingDirectory($workingDirectory);
83         $this->process->enableOutput();
84         $this->process->setTimeout(null);
85         $this->process->run(
86             function ($type, $buffer) {
87                 $this->io->write($buffer);
88             }
89         );
90
91         if (!$this->process->isSuccessful()) {
92             throw new ProcessFailedException($this->process);
93         }
94
95         return $this->process->isSuccessful();
96     }
97
98     /**
99      * @return string
100      */
101     public function getOutput()
102     {
103         return $this->process->getOutput();
104     }
105 }