Version 1
[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         $this->io->newLine();
68         $this->io->comment(
69             $this->translator->trans('commands.exec.messages.working-directory') .': ',
70             false
71         );
72         $this->io->writeln($workingDirectory);
73         $this->io->comment(
74             $this->translator->trans('commands.exec.messages.executing-command') .': ',
75             false
76         );
77         $this->io->writeln($command);
78
79         $this->process = new Process($command);
80         $this->process->setWorkingDirectory($workingDirectory);
81         $this->process->enableOutput();
82         $this->process->setTimeout(null);
83         $this->process->run(
84             function ($type, $buffer) {
85                 $this->io->write($buffer);
86             }
87         );
88
89         if (!$this->process->isSuccessful()) {
90             throw new ProcessFailedException($this->process);
91         }
92
93         return $this->process->isSuccessful();
94     }
95
96     /**
97      * @return string
98      */
99     public function getOutput()
100     {
101         return $this->process->getOutput();
102     }
103 }