forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.php
More file actions
158 lines (129 loc) · 3.47 KB
/
Process.php
File metadata and controls
158 lines (129 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace WP_CLI;
use RuntimeException;
/**
* Run a system process, and learn what happened.
*/
class Process {
/**
* @var string The full command to execute by the system.
*/
private $command;
/**
* @var string|null The path of the working directory for the process or NULL if not specified (defaults to current working directory).
*/
private $cwd;
/**
* @var array Environment variables to set when running the command.
*/
private $env;
/**
* @var array Descriptor spec for `proc_open()`.
*/
private static $descriptors = [
0 => STDIN,
1 => [ 'pipe', 'w' ],
2 => [ 'pipe', 'w' ],
];
/**
* @var bool Whether to log run time info or not.
*/
public static $log_run_times = false;
/**
* @var array Array of process run time info, keyed by process command, each a 2-element array containing run time and run count.
*/
public static $run_times = [];
/**
* @param string $command Command to execute.
* @param string $cwd Directory to execute the command in.
* @param array $env Environment variables to set when running the command.
*
* @return Process
*/
public static function create( $command, $cwd = null, $env = [] ) {
$proc = new self();
$proc->command = $command;
$proc->cwd = $cwd;
$proc->env = $env;
return $proc;
}
private function __construct() {}
/**
* Run the command.
*
* @return ProcessRun
*/
public function run() {
Utils\check_proc_available( 'Process::run' );
$start_time = microtime( true );
$pipes = [];
$proc = Utils\proc_open_compat( $this->command, self::$descriptors, $pipes, $this->cwd, $this->env );
$stdout = stream_get_contents( $pipes[1] );
fclose( $pipes[1] );
$stderr = stream_get_contents( $pipes[2] );
fclose( $pipes[2] );
$return_code = proc_close( $proc );
$run_time = microtime( true ) - $start_time;
if ( self::$log_run_times ) {
if ( ! isset( self::$run_times[ $this->command ] ) ) {
self::$run_times[ $this->command ] = [ 0, 0 ];
}
self::$run_times[ $this->command ][0] += $run_time;
++self::$run_times[ $this->command ][1];
}
return new ProcessRun(
[
'stdout' => $stdout,
'stderr' => $stderr,
'return_code' => $return_code,
'command' => $this->command,
'cwd' => $this->cwd,
'env' => $this->env,
'run_time' => $run_time,
]
);
}
/**
* Run the command, but throw an Exception on error.
*
* @return ProcessRun
*/
public function run_check() {
$r = $this->run();
if ( $r->return_code ) {
throw new RuntimeException( $r );
}
return $r;
}
/**
* Run the command, but throw an Exception on error.
* Same as `run_check()` above, but checks the correct stderr.
*
* @return ProcessRun
*/
public function run_check_stderr() {
$r = $this->run();
if ( $r->return_code ) {
throw new RuntimeException( $r );
}
if ( ! empty( $r->stderr ) ) {
// If the only thing that STDERR caught was the Requests deprecated message, ignore it.
// This is a temporary fix until we have a better solution for dealing with Requests
// as a dependency shared between WP Core and WP-CLI.
$stderr_lines = array_filter( explode( "\n", $r->stderr ) );
if ( 1 === count( $stderr_lines ) ) {
$stderr_line = $stderr_lines[0];
if (
false !== strpos(
$stderr_line,
'The PSR-0 `Requests_...` class names in the Request library are deprecated.'
)
) {
return $r;
}
}
throw new RuntimeException( $r );
}
return $r;
}
}