1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Cli; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Exception; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Cli Process Runner |
8
|
|
|
* |
9
|
|
|
* @package phpbu |
10
|
|
|
* @subpackage Backup |
11
|
|
|
* @author Sebastian Feldmann <[email protected]> |
12
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
13
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
14
|
|
|
* @link http://phpbu.de/ |
15
|
|
|
* @since Class available since Release 2.1.0 |
16
|
|
|
*/ |
17
|
|
|
class Process |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* List of system commands to execute. |
21
|
|
|
* |
22
|
|
|
* @var array<\phpbu\Backup\Cli\Cmd> |
23
|
|
|
*/ |
24
|
|
|
private $commands = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Redirect the output |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $redirectOutput; |
32
|
|
|
|
33
|
|
|
private $sCmdCompressor; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Redirect the stdOut. |
37
|
|
|
* |
38
|
12 |
|
* @param string $path |
39
|
|
|
*/ |
40
|
12 |
|
public function redirectOutputTo($path) |
41
|
12 |
|
{ |
42
|
|
|
$this->redirectOutput = $path; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set the compressor cmd |
47
|
|
|
* |
48
|
1 |
|
* @param string $cmd |
|
|
|
|
49
|
|
|
*/ |
50
|
1 |
|
public function setCompression($sCmdCompressor) |
51
|
|
|
{ |
52
|
|
|
$this->sCmdCompressor = $sCmdCompressor; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Should the output be redirected. |
57
|
|
|
* |
58
|
1 |
|
* @return boolean |
59
|
|
|
*/ |
60
|
1 |
|
public function isOutputRedirected() |
61
|
|
|
{ |
62
|
|
|
return !empty($this->redirectOutput); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Redirect getter. |
67
|
|
|
* |
68
|
99 |
|
* @return string |
69
|
|
|
*/ |
70
|
99 |
|
public function getRedirectPath() |
71
|
99 |
|
{ |
72
|
|
|
return $this->redirectOutput; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Adds a cli command to the command list. |
77
|
|
|
* |
78
|
|
|
* @param \phpbu\App\Cli\Cmd $cmd |
79
|
97 |
|
*/ |
80
|
|
|
public function addCommand(Cmd $cmd) |
81
|
97 |
|
{ |
82
|
97 |
|
$this->commands[] = $cmd; |
83
|
1 |
|
} |
84
|
|
|
|
85
|
96 |
|
/** |
86
|
96 |
|
* Generates the system command. |
87
|
|
|
* |
88
|
96 |
|
* @return string |
89
|
|
|
* @throws \phpbu\App\Exception |
90
|
|
|
*/ |
91
|
|
|
public function getCommandLine() |
92
|
|
|
{ |
93
|
|
|
$amount = count($this->commands); |
94
|
|
|
if ($amount < 1) { |
95
|
|
|
throw new Exception('no command to execute'); |
96
|
|
|
} |
97
|
1 |
|
$cmd = ($amount > 1 ? '(' . implode(' && ', $this->commands) . ')' : $this->commands[0]) |
98
|
|
|
. (!empty($this->sCmdCompressor) ? ' | ' . $this->sCmdCompressor : '') |
99
|
1 |
|
. (!empty($this->redirectOutput) ? ' > ' . $this->redirectOutput : ''); |
100
|
1 |
|
|
101
|
1 |
|
return $cmd; |
102
|
1 |
|
} |
103
|
1 |
|
|
104
|
1 |
|
/** |
105
|
|
|
* Executes the commands. |
106
|
1 |
|
* |
107
|
|
|
* @return \phpbu\App\Cli\Result |
108
|
|
|
* @throws \phpbu\App\Exception |
109
|
|
|
*/ |
110
|
|
|
public function run() |
111
|
|
|
{ |
112
|
|
|
$cmd = $this->getCommandLine(); |
113
|
|
|
$old = error_reporting(0); |
114
|
|
|
$descriptorSpec = [ |
115
|
|
|
['pipe', 'r'], |
116
|
|
|
['pipe', 'w'], |
117
|
|
|
['pipe', 'w'], |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
$process = proc_open($cmd, $descriptorSpec, $pipes); |
121
|
|
|
if (!is_resource($process)) { |
122
|
|
|
throw new Exception('can\'t execute \'proc_open\''); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$stdOut = stream_get_contents($pipes[1]); |
126
|
|
|
fclose($pipes[1]); |
127
|
|
|
|
128
|
|
|
$stdErr = stream_get_contents($pipes[2]); |
129
|
|
|
fclose($pipes[2]); |
130
|
|
|
|
131
|
|
|
$code = proc_close($process); |
132
|
|
|
error_reporting($old); |
133
|
|
|
|
134
|
|
|
return new Result($cmd, $code, $stdOut, $stdErr); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.