1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Gerard van Helden <[email protected]> |
4
|
|
|
* @copyright Zicht online <http://zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Zicht\Tool\Container; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Runs the commands in the shell. |
13
|
|
|
*/ |
14
|
|
|
class Executor |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Constructor |
18
|
|
|
* |
19
|
|
|
* @param Container $container |
20
|
|
|
*/ |
21
|
8 |
|
public function __construct(Container $container) |
22
|
|
|
{ |
23
|
8 |
|
$this->container = $container; |
|
|
|
|
24
|
8 |
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Executes the passed command line in the shell. |
29
|
|
|
* |
30
|
|
|
* @param string $cmd |
31
|
|
|
* @param null &$captureOutput |
32
|
|
|
* @param string $captureOutput |
33
|
|
|
* @return int |
34
|
|
|
* |
35
|
|
|
* @throws ExecutionAbortedException |
36
|
|
|
* @throws \UnexpectedValueException |
37
|
|
|
*/ |
38
|
|
|
public function execute($cmd, &$captureOutput = null) |
39
|
|
|
{ |
40
|
|
|
$isInteractive = $this->container->get('INTERACTIVE'); |
41
|
|
|
$process = $this->createProcess($isInteractive); |
42
|
|
|
|
43
|
|
|
if ($isInteractive) { |
44
|
|
|
$process->setCommandLine(sprintf('/bin/bash -c \'%s\'', $cmd)); |
45
|
|
|
} else { |
46
|
|
|
$process->setInput($cmd); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (null !== $captureOutput && false === $isInteractive) { |
50
|
|
|
$process->run(function ($type, $data) use(&$captureOutput) { |
51
|
|
|
$captureOutput .= $data; |
52
|
|
|
}); |
53
|
|
|
} else { |
54
|
|
|
$process->run(array($this, 'processCallback')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$ret = $process->getExitCode(); |
58
|
|
|
|
59
|
|
|
if ($ret != 0) { |
|
|
|
|
60
|
|
|
if ((int)$ret == Container::ABORT_EXIT_CODE) { |
61
|
|
|
throw new ExecutionAbortedException("Command '$cmd' was aborted"); |
62
|
|
|
} else { |
63
|
|
|
throw new \UnexpectedValueException("Command '$cmd' failed with exit code {$ret}"); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
return $ret; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create the process instance to use for non-interactive handling |
72
|
|
|
* |
73
|
|
|
* @var bool $interactive |
74
|
|
|
* @return \Symfony\Component\Process\Process |
75
|
|
|
*/ |
76
|
|
|
protected function createProcess($interactive = false) |
77
|
|
|
{ |
78
|
|
|
$process = new Process($this->container->resolve('SHELL'), null, null, null, null, []); |
79
|
|
|
|
80
|
|
|
if ($interactive) { |
81
|
|
|
$process->setTty(true); |
82
|
|
|
} else { |
83
|
|
|
if ($this->container->has('TIMEOUT') && $timeout = $this->container->get('TIMEOUT')) { |
84
|
|
|
$process->setTimeout($this->container->get('TIMEOUT')); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $process; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* The callback used for the process executed by Process |
94
|
|
|
* |
95
|
|
|
* @param mixed $mode |
96
|
|
|
* @param string $data |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function processCallback($mode, $data) |
100
|
|
|
{ |
101
|
|
|
if (isset($this->container->output)) { |
102
|
|
|
$this->container->output->write($data); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: