@@ -22,48 +22,48 @@ |
||
22 | 22 | */ |
23 | 23 | class ProcessUtils |
24 | 24 | { |
25 | - /** |
|
26 | - * This class should not be instantiated. |
|
27 | - */ |
|
28 | - private function __construct() |
|
29 | - { |
|
30 | - } |
|
25 | + /** |
|
26 | + * This class should not be instantiated. |
|
27 | + */ |
|
28 | + private function __construct() |
|
29 | + { |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Validates and normalizes a Process input. |
|
34 | - * |
|
35 | - * @param string $caller The name of method call that validates the input |
|
36 | - * @param mixed $input The input to validate |
|
37 | - * |
|
38 | - * @return mixed The validated input |
|
39 | - * |
|
40 | - * @throws InvalidArgumentException In case the input is not valid |
|
41 | - */ |
|
42 | - public static function validateInput(string $caller, $input) |
|
43 | - { |
|
44 | - if (null !== $input) { |
|
45 | - if (\is_resource($input)) { |
|
46 | - return $input; |
|
47 | - } |
|
48 | - if (\is_string($input)) { |
|
49 | - return $input; |
|
50 | - } |
|
51 | - if (is_scalar($input)) { |
|
52 | - return (string) $input; |
|
53 | - } |
|
54 | - if ($input instanceof Process) { |
|
55 | - return $input->getIterator($input::ITER_SKIP_ERR); |
|
56 | - } |
|
57 | - if ($input instanceof \Iterator) { |
|
58 | - return $input; |
|
59 | - } |
|
60 | - if ($input instanceof \Traversable) { |
|
61 | - return new \IteratorIterator($input); |
|
62 | - } |
|
32 | + /** |
|
33 | + * Validates and normalizes a Process input. |
|
34 | + * |
|
35 | + * @param string $caller The name of method call that validates the input |
|
36 | + * @param mixed $input The input to validate |
|
37 | + * |
|
38 | + * @return mixed The validated input |
|
39 | + * |
|
40 | + * @throws InvalidArgumentException In case the input is not valid |
|
41 | + */ |
|
42 | + public static function validateInput(string $caller, $input) |
|
43 | + { |
|
44 | + if (null !== $input) { |
|
45 | + if (\is_resource($input)) { |
|
46 | + return $input; |
|
47 | + } |
|
48 | + if (\is_string($input)) { |
|
49 | + return $input; |
|
50 | + } |
|
51 | + if (is_scalar($input)) { |
|
52 | + return (string) $input; |
|
53 | + } |
|
54 | + if ($input instanceof Process) { |
|
55 | + return $input->getIterator($input::ITER_SKIP_ERR); |
|
56 | + } |
|
57 | + if ($input instanceof \Iterator) { |
|
58 | + return $input; |
|
59 | + } |
|
60 | + if ($input instanceof \Traversable) { |
|
61 | + return new \IteratorIterator($input); |
|
62 | + } |
|
63 | 63 | |
64 | - throw new InvalidArgumentException(sprintf('"%s" only accepts strings, Traversable objects or stream resources.', $caller)); |
|
65 | - } |
|
64 | + throw new InvalidArgumentException(sprintf('"%s" only accepts strings, Traversable objects or stream resources.', $caller)); |
|
65 | + } |
|
66 | 66 | |
67 | - return $input; |
|
68 | - } |
|
67 | + return $input; |
|
68 | + } |
|
69 | 69 | } |
@@ -20,42 +20,42 @@ |
||
20 | 20 | */ |
21 | 21 | interface PipesInterface |
22 | 22 | { |
23 | - public const CHUNK_SIZE = 16384; |
|
24 | - |
|
25 | - /** |
|
26 | - * Returns an array of descriptors for the use of proc_open. |
|
27 | - */ |
|
28 | - public function getDescriptors(): array; |
|
29 | - |
|
30 | - /** |
|
31 | - * Returns an array of filenames indexed by their related stream in case these pipes use temporary files. |
|
32 | - * |
|
33 | - * @return string[] |
|
34 | - */ |
|
35 | - public function getFiles(): array; |
|
36 | - |
|
37 | - /** |
|
38 | - * Reads data in file handles and pipes. |
|
39 | - * |
|
40 | - * @param bool $blocking Whether to use blocking calls or not |
|
41 | - * @param bool $close Whether to close pipes if they've reached EOF |
|
42 | - * |
|
43 | - * @return string[] An array of read data indexed by their fd |
|
44 | - */ |
|
45 | - public function readAndWrite(bool $blocking, bool $close = false): array; |
|
46 | - |
|
47 | - /** |
|
48 | - * Returns if the current state has open file handles or pipes. |
|
49 | - */ |
|
50 | - public function areOpen(): bool; |
|
51 | - |
|
52 | - /** |
|
53 | - * Returns if pipes are able to read output. |
|
54 | - */ |
|
55 | - public function haveReadSupport(): bool; |
|
56 | - |
|
57 | - /** |
|
58 | - * Closes file handles and pipes. |
|
59 | - */ |
|
60 | - public function close(); |
|
23 | + public const CHUNK_SIZE = 16384; |
|
24 | + |
|
25 | + /** |
|
26 | + * Returns an array of descriptors for the use of proc_open. |
|
27 | + */ |
|
28 | + public function getDescriptors(): array; |
|
29 | + |
|
30 | + /** |
|
31 | + * Returns an array of filenames indexed by their related stream in case these pipes use temporary files. |
|
32 | + * |
|
33 | + * @return string[] |
|
34 | + */ |
|
35 | + public function getFiles(): array; |
|
36 | + |
|
37 | + /** |
|
38 | + * Reads data in file handles and pipes. |
|
39 | + * |
|
40 | + * @param bool $blocking Whether to use blocking calls or not |
|
41 | + * @param bool $close Whether to close pipes if they've reached EOF |
|
42 | + * |
|
43 | + * @return string[] An array of read data indexed by their fd |
|
44 | + */ |
|
45 | + public function readAndWrite(bool $blocking, bool $close = false): array; |
|
46 | + |
|
47 | + /** |
|
48 | + * Returns if the current state has open file handles or pipes. |
|
49 | + */ |
|
50 | + public function areOpen(): bool; |
|
51 | + |
|
52 | + /** |
|
53 | + * Returns if pipes are able to read output. |
|
54 | + */ |
|
55 | + public function haveReadSupport(): bool; |
|
56 | + |
|
57 | + /** |
|
58 | + * Closes file handles and pipes. |
|
59 | + */ |
|
60 | + public function close(); |
|
61 | 61 | } |
@@ -20,159 +20,159 @@ |
||
20 | 20 | */ |
21 | 21 | abstract class AbstractPipes implements PipesInterface |
22 | 22 | { |
23 | - public $pipes = []; |
|
24 | - |
|
25 | - private $inputBuffer = ''; |
|
26 | - private $input; |
|
27 | - private $blocked = true; |
|
28 | - private $lastError; |
|
29 | - |
|
30 | - /** |
|
31 | - * @param resource|string|int|float|bool|\Iterator|null $input |
|
32 | - */ |
|
33 | - public function __construct($input) |
|
34 | - { |
|
35 | - if (\is_resource($input) || $input instanceof \Iterator) { |
|
36 | - $this->input = $input; |
|
37 | - } elseif (\is_string($input)) { |
|
38 | - $this->inputBuffer = $input; |
|
39 | - } else { |
|
40 | - $this->inputBuffer = (string) $input; |
|
41 | - } |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * {@inheritdoc} |
|
46 | - */ |
|
47 | - public function close() |
|
48 | - { |
|
49 | - foreach ($this->pipes as $pipe) { |
|
50 | - fclose($pipe); |
|
51 | - } |
|
52 | - $this->pipes = []; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Returns true if a system call has been interrupted. |
|
57 | - */ |
|
58 | - protected function hasSystemCallBeenInterrupted(): bool |
|
59 | - { |
|
60 | - $lastError = $this->lastError; |
|
61 | - $this->lastError = null; |
|
62 | - |
|
63 | - // stream_select returns false when the `select` system call is interrupted by an incoming signal |
|
64 | - return null !== $lastError && false !== stripos($lastError, 'interrupted system call'); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Unblocks streams. |
|
69 | - */ |
|
70 | - protected function unblock() |
|
71 | - { |
|
72 | - if (!$this->blocked) { |
|
73 | - return; |
|
74 | - } |
|
75 | - |
|
76 | - foreach ($this->pipes as $pipe) { |
|
77 | - stream_set_blocking($pipe, 0); |
|
78 | - } |
|
79 | - if (\is_resource($this->input)) { |
|
80 | - stream_set_blocking($this->input, 0); |
|
81 | - } |
|
82 | - |
|
83 | - $this->blocked = false; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Writes input to stdin. |
|
88 | - * |
|
89 | - * @throws InvalidArgumentException When an input iterator yields a non supported value |
|
90 | - */ |
|
91 | - protected function write(): ?array |
|
92 | - { |
|
93 | - if (!isset($this->pipes[0])) { |
|
94 | - return null; |
|
95 | - } |
|
96 | - $input = $this->input; |
|
97 | - |
|
98 | - if ($input instanceof \Iterator) { |
|
99 | - if (!$input->valid()) { |
|
100 | - $input = null; |
|
101 | - } elseif (\is_resource($input = $input->current())) { |
|
102 | - stream_set_blocking($input, 0); |
|
103 | - } elseif (!isset($this->inputBuffer[0])) { |
|
104 | - if (!\is_string($input)) { |
|
105 | - if (!is_scalar($input)) { |
|
106 | - throw new InvalidArgumentException(sprintf('"%s" yielded a value of type "%s", but only scalars and stream resources are supported.', get_debug_type($this->input), get_debug_type($input))); |
|
107 | - } |
|
108 | - $input = (string) $input; |
|
109 | - } |
|
110 | - $this->inputBuffer = $input; |
|
111 | - $this->input->next(); |
|
112 | - $input = null; |
|
113 | - } else { |
|
114 | - $input = null; |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - $r = $e = []; |
|
119 | - $w = [$this->pipes[0]]; |
|
120 | - |
|
121 | - // let's have a look if something changed in streams |
|
122 | - if (false === @stream_select($r, $w, $e, 0, 0)) { |
|
123 | - return null; |
|
124 | - } |
|
125 | - |
|
126 | - foreach ($w as $stdin) { |
|
127 | - if (isset($this->inputBuffer[0])) { |
|
128 | - $written = fwrite($stdin, $this->inputBuffer); |
|
129 | - $this->inputBuffer = substr($this->inputBuffer, $written); |
|
130 | - if (isset($this->inputBuffer[0])) { |
|
131 | - return [$this->pipes[0]]; |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - if ($input) { |
|
136 | - for (;;) { |
|
137 | - $data = fread($input, self::CHUNK_SIZE); |
|
138 | - if (!isset($data[0])) { |
|
139 | - break; |
|
140 | - } |
|
141 | - $written = fwrite($stdin, $data); |
|
142 | - $data = substr($data, $written); |
|
143 | - if (isset($data[0])) { |
|
144 | - $this->inputBuffer = $data; |
|
145 | - |
|
146 | - return [$this->pipes[0]]; |
|
147 | - } |
|
148 | - } |
|
149 | - if (feof($input)) { |
|
150 | - if ($this->input instanceof \Iterator) { |
|
151 | - $this->input->next(); |
|
152 | - } else { |
|
153 | - $this->input = null; |
|
154 | - } |
|
155 | - } |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - // no input to read on resource, buffer is empty |
|
160 | - if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) { |
|
161 | - $this->input = null; |
|
162 | - fclose($this->pipes[0]); |
|
163 | - unset($this->pipes[0]); |
|
164 | - } elseif (!$w) { |
|
165 | - return [$this->pipes[0]]; |
|
166 | - } |
|
167 | - |
|
168 | - return null; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * @internal |
|
173 | - */ |
|
174 | - public function handleError(int $type, string $msg) |
|
175 | - { |
|
176 | - $this->lastError = $msg; |
|
177 | - } |
|
23 | + public $pipes = []; |
|
24 | + |
|
25 | + private $inputBuffer = ''; |
|
26 | + private $input; |
|
27 | + private $blocked = true; |
|
28 | + private $lastError; |
|
29 | + |
|
30 | + /** |
|
31 | + * @param resource|string|int|float|bool|\Iterator|null $input |
|
32 | + */ |
|
33 | + public function __construct($input) |
|
34 | + { |
|
35 | + if (\is_resource($input) || $input instanceof \Iterator) { |
|
36 | + $this->input = $input; |
|
37 | + } elseif (\is_string($input)) { |
|
38 | + $this->inputBuffer = $input; |
|
39 | + } else { |
|
40 | + $this->inputBuffer = (string) $input; |
|
41 | + } |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * {@inheritdoc} |
|
46 | + */ |
|
47 | + public function close() |
|
48 | + { |
|
49 | + foreach ($this->pipes as $pipe) { |
|
50 | + fclose($pipe); |
|
51 | + } |
|
52 | + $this->pipes = []; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Returns true if a system call has been interrupted. |
|
57 | + */ |
|
58 | + protected function hasSystemCallBeenInterrupted(): bool |
|
59 | + { |
|
60 | + $lastError = $this->lastError; |
|
61 | + $this->lastError = null; |
|
62 | + |
|
63 | + // stream_select returns false when the `select` system call is interrupted by an incoming signal |
|
64 | + return null !== $lastError && false !== stripos($lastError, 'interrupted system call'); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Unblocks streams. |
|
69 | + */ |
|
70 | + protected function unblock() |
|
71 | + { |
|
72 | + if (!$this->blocked) { |
|
73 | + return; |
|
74 | + } |
|
75 | + |
|
76 | + foreach ($this->pipes as $pipe) { |
|
77 | + stream_set_blocking($pipe, 0); |
|
78 | + } |
|
79 | + if (\is_resource($this->input)) { |
|
80 | + stream_set_blocking($this->input, 0); |
|
81 | + } |
|
82 | + |
|
83 | + $this->blocked = false; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Writes input to stdin. |
|
88 | + * |
|
89 | + * @throws InvalidArgumentException When an input iterator yields a non supported value |
|
90 | + */ |
|
91 | + protected function write(): ?array |
|
92 | + { |
|
93 | + if (!isset($this->pipes[0])) { |
|
94 | + return null; |
|
95 | + } |
|
96 | + $input = $this->input; |
|
97 | + |
|
98 | + if ($input instanceof \Iterator) { |
|
99 | + if (!$input->valid()) { |
|
100 | + $input = null; |
|
101 | + } elseif (\is_resource($input = $input->current())) { |
|
102 | + stream_set_blocking($input, 0); |
|
103 | + } elseif (!isset($this->inputBuffer[0])) { |
|
104 | + if (!\is_string($input)) { |
|
105 | + if (!is_scalar($input)) { |
|
106 | + throw new InvalidArgumentException(sprintf('"%s" yielded a value of type "%s", but only scalars and stream resources are supported.', get_debug_type($this->input), get_debug_type($input))); |
|
107 | + } |
|
108 | + $input = (string) $input; |
|
109 | + } |
|
110 | + $this->inputBuffer = $input; |
|
111 | + $this->input->next(); |
|
112 | + $input = null; |
|
113 | + } else { |
|
114 | + $input = null; |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + $r = $e = []; |
|
119 | + $w = [$this->pipes[0]]; |
|
120 | + |
|
121 | + // let's have a look if something changed in streams |
|
122 | + if (false === @stream_select($r, $w, $e, 0, 0)) { |
|
123 | + return null; |
|
124 | + } |
|
125 | + |
|
126 | + foreach ($w as $stdin) { |
|
127 | + if (isset($this->inputBuffer[0])) { |
|
128 | + $written = fwrite($stdin, $this->inputBuffer); |
|
129 | + $this->inputBuffer = substr($this->inputBuffer, $written); |
|
130 | + if (isset($this->inputBuffer[0])) { |
|
131 | + return [$this->pipes[0]]; |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + if ($input) { |
|
136 | + for (;;) { |
|
137 | + $data = fread($input, self::CHUNK_SIZE); |
|
138 | + if (!isset($data[0])) { |
|
139 | + break; |
|
140 | + } |
|
141 | + $written = fwrite($stdin, $data); |
|
142 | + $data = substr($data, $written); |
|
143 | + if (isset($data[0])) { |
|
144 | + $this->inputBuffer = $data; |
|
145 | + |
|
146 | + return [$this->pipes[0]]; |
|
147 | + } |
|
148 | + } |
|
149 | + if (feof($input)) { |
|
150 | + if ($this->input instanceof \Iterator) { |
|
151 | + $this->input->next(); |
|
152 | + } else { |
|
153 | + $this->input = null; |
|
154 | + } |
|
155 | + } |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + // no input to read on resource, buffer is empty |
|
160 | + if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) { |
|
161 | + $this->input = null; |
|
162 | + fclose($this->pipes[0]); |
|
163 | + unset($this->pipes[0]); |
|
164 | + } elseif (!$w) { |
|
165 | + return [$this->pipes[0]]; |
|
166 | + } |
|
167 | + |
|
168 | + return null; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * @internal |
|
173 | + */ |
|
174 | + public function handleError(int $type, string $msg) |
|
175 | + { |
|
176 | + $this->lastError = $msg; |
|
177 | + } |
|
178 | 178 | } |
@@ -22,145 +22,145 @@ |
||
22 | 22 | */ |
23 | 23 | class UnixPipes extends AbstractPipes |
24 | 24 | { |
25 | - private $ttyMode; |
|
26 | - private $ptyMode; |
|
27 | - private $haveReadSupport; |
|
28 | - |
|
29 | - public function __construct(?bool $ttyMode, bool $ptyMode, $input, bool $haveReadSupport) |
|
30 | - { |
|
31 | - $this->ttyMode = $ttyMode; |
|
32 | - $this->ptyMode = $ptyMode; |
|
33 | - $this->haveReadSupport = $haveReadSupport; |
|
34 | - |
|
35 | - parent::__construct($input); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * @return array |
|
40 | - */ |
|
41 | - public function __sleep() |
|
42 | - { |
|
43 | - throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
44 | - } |
|
45 | - |
|
46 | - public function __wakeup() |
|
47 | - { |
|
48 | - throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
49 | - } |
|
50 | - |
|
51 | - public function __destruct() |
|
52 | - { |
|
53 | - $this->close(); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * {@inheritdoc} |
|
58 | - */ |
|
59 | - public function getDescriptors(): array |
|
60 | - { |
|
61 | - if (!$this->haveReadSupport) { |
|
62 | - $nullstream = fopen('/dev/null', 'c'); |
|
63 | - |
|
64 | - return [ |
|
65 | - ['pipe', 'r'], |
|
66 | - $nullstream, |
|
67 | - $nullstream, |
|
68 | - ]; |
|
69 | - } |
|
70 | - |
|
71 | - if ($this->ttyMode) { |
|
72 | - return [ |
|
73 | - ['file', '/dev/tty', 'r'], |
|
74 | - ['file', '/dev/tty', 'w'], |
|
75 | - ['file', '/dev/tty', 'w'], |
|
76 | - ]; |
|
77 | - } |
|
78 | - |
|
79 | - if ($this->ptyMode && Process::isPtySupported()) { |
|
80 | - return [ |
|
81 | - ['pty'], |
|
82 | - ['pty'], |
|
83 | - ['pty'], |
|
84 | - ]; |
|
85 | - } |
|
86 | - |
|
87 | - return [ |
|
88 | - ['pipe', 'r'], |
|
89 | - ['pipe', 'w'], // stdout |
|
90 | - ['pipe', 'w'], // stderr |
|
91 | - ]; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * {@inheritdoc} |
|
96 | - */ |
|
97 | - public function getFiles(): array |
|
98 | - { |
|
99 | - return []; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * {@inheritdoc} |
|
104 | - */ |
|
105 | - public function readAndWrite(bool $blocking, bool $close = false): array |
|
106 | - { |
|
107 | - $this->unblock(); |
|
108 | - $w = $this->write(); |
|
109 | - |
|
110 | - $read = $e = []; |
|
111 | - $r = $this->pipes; |
|
112 | - unset($r[0]); |
|
113 | - |
|
114 | - // let's have a look if something changed in streams |
|
115 | - set_error_handler([$this, 'handleError']); |
|
116 | - if (($r || $w) && false === stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) { |
|
117 | - restore_error_handler(); |
|
118 | - // if a system call has been interrupted, forget about it, let's try again |
|
119 | - // otherwise, an error occurred, let's reset pipes |
|
120 | - if (!$this->hasSystemCallBeenInterrupted()) { |
|
121 | - $this->pipes = []; |
|
122 | - } |
|
123 | - |
|
124 | - return $read; |
|
125 | - } |
|
126 | - restore_error_handler(); |
|
127 | - |
|
128 | - foreach ($r as $pipe) { |
|
129 | - // prior PHP 5.4 the array passed to stream_select is modified and |
|
130 | - // lose key association, we have to find back the key |
|
131 | - $read[$type = array_search($pipe, $this->pipes, true)] = ''; |
|
132 | - |
|
133 | - do { |
|
134 | - $data = @fread($pipe, self::CHUNK_SIZE); |
|
135 | - $read[$type] .= $data; |
|
136 | - } while (isset($data[0]) && ($close || isset($data[self::CHUNK_SIZE - 1]))); |
|
137 | - |
|
138 | - if (!isset($read[$type][0])) { |
|
139 | - unset($read[$type]); |
|
140 | - } |
|
141 | - |
|
142 | - if ($close && feof($pipe)) { |
|
143 | - fclose($pipe); |
|
144 | - unset($this->pipes[$type]); |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - return $read; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * {@inheritdoc} |
|
153 | - */ |
|
154 | - public function haveReadSupport(): bool |
|
155 | - { |
|
156 | - return $this->haveReadSupport; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * {@inheritdoc} |
|
161 | - */ |
|
162 | - public function areOpen(): bool |
|
163 | - { |
|
164 | - return (bool) $this->pipes; |
|
165 | - } |
|
25 | + private $ttyMode; |
|
26 | + private $ptyMode; |
|
27 | + private $haveReadSupport; |
|
28 | + |
|
29 | + public function __construct(?bool $ttyMode, bool $ptyMode, $input, bool $haveReadSupport) |
|
30 | + { |
|
31 | + $this->ttyMode = $ttyMode; |
|
32 | + $this->ptyMode = $ptyMode; |
|
33 | + $this->haveReadSupport = $haveReadSupport; |
|
34 | + |
|
35 | + parent::__construct($input); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * @return array |
|
40 | + */ |
|
41 | + public function __sleep() |
|
42 | + { |
|
43 | + throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
44 | + } |
|
45 | + |
|
46 | + public function __wakeup() |
|
47 | + { |
|
48 | + throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
49 | + } |
|
50 | + |
|
51 | + public function __destruct() |
|
52 | + { |
|
53 | + $this->close(); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * {@inheritdoc} |
|
58 | + */ |
|
59 | + public function getDescriptors(): array |
|
60 | + { |
|
61 | + if (!$this->haveReadSupport) { |
|
62 | + $nullstream = fopen('/dev/null', 'c'); |
|
63 | + |
|
64 | + return [ |
|
65 | + ['pipe', 'r'], |
|
66 | + $nullstream, |
|
67 | + $nullstream, |
|
68 | + ]; |
|
69 | + } |
|
70 | + |
|
71 | + if ($this->ttyMode) { |
|
72 | + return [ |
|
73 | + ['file', '/dev/tty', 'r'], |
|
74 | + ['file', '/dev/tty', 'w'], |
|
75 | + ['file', '/dev/tty', 'w'], |
|
76 | + ]; |
|
77 | + } |
|
78 | + |
|
79 | + if ($this->ptyMode && Process::isPtySupported()) { |
|
80 | + return [ |
|
81 | + ['pty'], |
|
82 | + ['pty'], |
|
83 | + ['pty'], |
|
84 | + ]; |
|
85 | + } |
|
86 | + |
|
87 | + return [ |
|
88 | + ['pipe', 'r'], |
|
89 | + ['pipe', 'w'], // stdout |
|
90 | + ['pipe', 'w'], // stderr |
|
91 | + ]; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * {@inheritdoc} |
|
96 | + */ |
|
97 | + public function getFiles(): array |
|
98 | + { |
|
99 | + return []; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * {@inheritdoc} |
|
104 | + */ |
|
105 | + public function readAndWrite(bool $blocking, bool $close = false): array |
|
106 | + { |
|
107 | + $this->unblock(); |
|
108 | + $w = $this->write(); |
|
109 | + |
|
110 | + $read = $e = []; |
|
111 | + $r = $this->pipes; |
|
112 | + unset($r[0]); |
|
113 | + |
|
114 | + // let's have a look if something changed in streams |
|
115 | + set_error_handler([$this, 'handleError']); |
|
116 | + if (($r || $w) && false === stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) { |
|
117 | + restore_error_handler(); |
|
118 | + // if a system call has been interrupted, forget about it, let's try again |
|
119 | + // otherwise, an error occurred, let's reset pipes |
|
120 | + if (!$this->hasSystemCallBeenInterrupted()) { |
|
121 | + $this->pipes = []; |
|
122 | + } |
|
123 | + |
|
124 | + return $read; |
|
125 | + } |
|
126 | + restore_error_handler(); |
|
127 | + |
|
128 | + foreach ($r as $pipe) { |
|
129 | + // prior PHP 5.4 the array passed to stream_select is modified and |
|
130 | + // lose key association, we have to find back the key |
|
131 | + $read[$type = array_search($pipe, $this->pipes, true)] = ''; |
|
132 | + |
|
133 | + do { |
|
134 | + $data = @fread($pipe, self::CHUNK_SIZE); |
|
135 | + $read[$type] .= $data; |
|
136 | + } while (isset($data[0]) && ($close || isset($data[self::CHUNK_SIZE - 1]))); |
|
137 | + |
|
138 | + if (!isset($read[$type][0])) { |
|
139 | + unset($read[$type]); |
|
140 | + } |
|
141 | + |
|
142 | + if ($close && feof($pipe)) { |
|
143 | + fclose($pipe); |
|
144 | + unset($this->pipes[$type]); |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + return $read; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * {@inheritdoc} |
|
153 | + */ |
|
154 | + public function haveReadSupport(): bool |
|
155 | + { |
|
156 | + return $this->haveReadSupport; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * {@inheritdoc} |
|
161 | + */ |
|
162 | + public function areOpen(): bool |
|
163 | + { |
|
164 | + return (bool) $this->pipes; |
|
165 | + } |
|
166 | 166 | } |
@@ -26,182 +26,182 @@ |
||
26 | 26 | */ |
27 | 27 | class WindowsPipes extends AbstractPipes |
28 | 28 | { |
29 | - private $files = []; |
|
30 | - private $fileHandles = []; |
|
31 | - private $lockHandles = []; |
|
32 | - private $readBytes = [ |
|
33 | - Process::STDOUT => 0, |
|
34 | - Process::STDERR => 0, |
|
35 | - ]; |
|
36 | - private $haveReadSupport; |
|
37 | - |
|
38 | - public function __construct($input, bool $haveReadSupport) |
|
39 | - { |
|
40 | - $this->haveReadSupport = $haveReadSupport; |
|
41 | - |
|
42 | - if ($this->haveReadSupport) { |
|
43 | - // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big. |
|
44 | - // Workaround for this problem is to use temporary files instead of pipes on Windows platform. |
|
45 | - // |
|
46 | - // @see https://bugs.php.net/51800 |
|
47 | - $pipes = [ |
|
48 | - Process::STDOUT => Process::OUT, |
|
49 | - Process::STDERR => Process::ERR, |
|
50 | - ]; |
|
51 | - $tmpDir = sys_get_temp_dir(); |
|
52 | - $lastError = 'unknown reason'; |
|
53 | - set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; }); |
|
54 | - for ($i = 0;; ++$i) { |
|
55 | - foreach ($pipes as $pipe => $name) { |
|
56 | - $file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name); |
|
57 | - |
|
58 | - if (!$h = fopen($file.'.lock', 'w')) { |
|
59 | - if (file_exists($file.'.lock')) { |
|
60 | - continue 2; |
|
61 | - } |
|
62 | - restore_error_handler(); |
|
63 | - throw new RuntimeException('A temporary file could not be opened to write the process output: '.$lastError); |
|
64 | - } |
|
65 | - if (!flock($h, \LOCK_EX | \LOCK_NB)) { |
|
66 | - continue 2; |
|
67 | - } |
|
68 | - if (isset($this->lockHandles[$pipe])) { |
|
69 | - flock($this->lockHandles[$pipe], \LOCK_UN); |
|
70 | - fclose($this->lockHandles[$pipe]); |
|
71 | - } |
|
72 | - $this->lockHandles[$pipe] = $h; |
|
73 | - |
|
74 | - if (!($h = fopen($file, 'w')) || !fclose($h) || !$h = fopen($file, 'r')) { |
|
75 | - flock($this->lockHandles[$pipe], \LOCK_UN); |
|
76 | - fclose($this->lockHandles[$pipe]); |
|
77 | - unset($this->lockHandles[$pipe]); |
|
78 | - continue 2; |
|
79 | - } |
|
80 | - $this->fileHandles[$pipe] = $h; |
|
81 | - $this->files[$pipe] = $file; |
|
82 | - } |
|
83 | - break; |
|
84 | - } |
|
85 | - restore_error_handler(); |
|
86 | - } |
|
87 | - |
|
88 | - parent::__construct($input); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - public function __sleep() |
|
95 | - { |
|
96 | - throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
97 | - } |
|
98 | - |
|
99 | - public function __wakeup() |
|
100 | - { |
|
101 | - throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
102 | - } |
|
103 | - |
|
104 | - public function __destruct() |
|
105 | - { |
|
106 | - $this->close(); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * {@inheritdoc} |
|
111 | - */ |
|
112 | - public function getDescriptors(): array |
|
113 | - { |
|
114 | - if (!$this->haveReadSupport) { |
|
115 | - $nullstream = fopen('NUL', 'c'); |
|
116 | - |
|
117 | - return [ |
|
118 | - ['pipe', 'r'], |
|
119 | - $nullstream, |
|
120 | - $nullstream, |
|
121 | - ]; |
|
122 | - } |
|
123 | - |
|
124 | - // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/51800) |
|
125 | - // We're not using file handles as it can produce corrupted output https://bugs.php.net/65650 |
|
126 | - // So we redirect output within the commandline and pass the nul device to the process |
|
127 | - return [ |
|
128 | - ['pipe', 'r'], |
|
129 | - ['file', 'NUL', 'w'], |
|
130 | - ['file', 'NUL', 'w'], |
|
131 | - ]; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * {@inheritdoc} |
|
136 | - */ |
|
137 | - public function getFiles(): array |
|
138 | - { |
|
139 | - return $this->files; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * {@inheritdoc} |
|
144 | - */ |
|
145 | - public function readAndWrite(bool $blocking, bool $close = false): array |
|
146 | - { |
|
147 | - $this->unblock(); |
|
148 | - $w = $this->write(); |
|
149 | - $read = $r = $e = []; |
|
150 | - |
|
151 | - if ($blocking) { |
|
152 | - if ($w) { |
|
153 | - @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6); |
|
154 | - } elseif ($this->fileHandles) { |
|
155 | - usleep(Process::TIMEOUT_PRECISION * 1E6); |
|
156 | - } |
|
157 | - } |
|
158 | - foreach ($this->fileHandles as $type => $fileHandle) { |
|
159 | - $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]); |
|
160 | - |
|
161 | - if (isset($data[0])) { |
|
162 | - $this->readBytes[$type] += \strlen($data); |
|
163 | - $read[$type] = $data; |
|
164 | - } |
|
165 | - if ($close) { |
|
166 | - ftruncate($fileHandle, 0); |
|
167 | - fclose($fileHandle); |
|
168 | - flock($this->lockHandles[$type], \LOCK_UN); |
|
169 | - fclose($this->lockHandles[$type]); |
|
170 | - unset($this->fileHandles[$type], $this->lockHandles[$type]); |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - return $read; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * {@inheritdoc} |
|
179 | - */ |
|
180 | - public function haveReadSupport(): bool |
|
181 | - { |
|
182 | - return $this->haveReadSupport; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * {@inheritdoc} |
|
187 | - */ |
|
188 | - public function areOpen(): bool |
|
189 | - { |
|
190 | - return $this->pipes && $this->fileHandles; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * {@inheritdoc} |
|
195 | - */ |
|
196 | - public function close() |
|
197 | - { |
|
198 | - parent::close(); |
|
199 | - foreach ($this->fileHandles as $type => $handle) { |
|
200 | - ftruncate($handle, 0); |
|
201 | - fclose($handle); |
|
202 | - flock($this->lockHandles[$type], \LOCK_UN); |
|
203 | - fclose($this->lockHandles[$type]); |
|
204 | - } |
|
205 | - $this->fileHandles = $this->lockHandles = []; |
|
206 | - } |
|
29 | + private $files = []; |
|
30 | + private $fileHandles = []; |
|
31 | + private $lockHandles = []; |
|
32 | + private $readBytes = [ |
|
33 | + Process::STDOUT => 0, |
|
34 | + Process::STDERR => 0, |
|
35 | + ]; |
|
36 | + private $haveReadSupport; |
|
37 | + |
|
38 | + public function __construct($input, bool $haveReadSupport) |
|
39 | + { |
|
40 | + $this->haveReadSupport = $haveReadSupport; |
|
41 | + |
|
42 | + if ($this->haveReadSupport) { |
|
43 | + // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big. |
|
44 | + // Workaround for this problem is to use temporary files instead of pipes on Windows platform. |
|
45 | + // |
|
46 | + // @see https://bugs.php.net/51800 |
|
47 | + $pipes = [ |
|
48 | + Process::STDOUT => Process::OUT, |
|
49 | + Process::STDERR => Process::ERR, |
|
50 | + ]; |
|
51 | + $tmpDir = sys_get_temp_dir(); |
|
52 | + $lastError = 'unknown reason'; |
|
53 | + set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; }); |
|
54 | + for ($i = 0;; ++$i) { |
|
55 | + foreach ($pipes as $pipe => $name) { |
|
56 | + $file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name); |
|
57 | + |
|
58 | + if (!$h = fopen($file.'.lock', 'w')) { |
|
59 | + if (file_exists($file.'.lock')) { |
|
60 | + continue 2; |
|
61 | + } |
|
62 | + restore_error_handler(); |
|
63 | + throw new RuntimeException('A temporary file could not be opened to write the process output: '.$lastError); |
|
64 | + } |
|
65 | + if (!flock($h, \LOCK_EX | \LOCK_NB)) { |
|
66 | + continue 2; |
|
67 | + } |
|
68 | + if (isset($this->lockHandles[$pipe])) { |
|
69 | + flock($this->lockHandles[$pipe], \LOCK_UN); |
|
70 | + fclose($this->lockHandles[$pipe]); |
|
71 | + } |
|
72 | + $this->lockHandles[$pipe] = $h; |
|
73 | + |
|
74 | + if (!($h = fopen($file, 'w')) || !fclose($h) || !$h = fopen($file, 'r')) { |
|
75 | + flock($this->lockHandles[$pipe], \LOCK_UN); |
|
76 | + fclose($this->lockHandles[$pipe]); |
|
77 | + unset($this->lockHandles[$pipe]); |
|
78 | + continue 2; |
|
79 | + } |
|
80 | + $this->fileHandles[$pipe] = $h; |
|
81 | + $this->files[$pipe] = $file; |
|
82 | + } |
|
83 | + break; |
|
84 | + } |
|
85 | + restore_error_handler(); |
|
86 | + } |
|
87 | + |
|
88 | + parent::__construct($input); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + public function __sleep() |
|
95 | + { |
|
96 | + throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
97 | + } |
|
98 | + |
|
99 | + public function __wakeup() |
|
100 | + { |
|
101 | + throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
102 | + } |
|
103 | + |
|
104 | + public function __destruct() |
|
105 | + { |
|
106 | + $this->close(); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * {@inheritdoc} |
|
111 | + */ |
|
112 | + public function getDescriptors(): array |
|
113 | + { |
|
114 | + if (!$this->haveReadSupport) { |
|
115 | + $nullstream = fopen('NUL', 'c'); |
|
116 | + |
|
117 | + return [ |
|
118 | + ['pipe', 'r'], |
|
119 | + $nullstream, |
|
120 | + $nullstream, |
|
121 | + ]; |
|
122 | + } |
|
123 | + |
|
124 | + // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/51800) |
|
125 | + // We're not using file handles as it can produce corrupted output https://bugs.php.net/65650 |
|
126 | + // So we redirect output within the commandline and pass the nul device to the process |
|
127 | + return [ |
|
128 | + ['pipe', 'r'], |
|
129 | + ['file', 'NUL', 'w'], |
|
130 | + ['file', 'NUL', 'w'], |
|
131 | + ]; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * {@inheritdoc} |
|
136 | + */ |
|
137 | + public function getFiles(): array |
|
138 | + { |
|
139 | + return $this->files; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * {@inheritdoc} |
|
144 | + */ |
|
145 | + public function readAndWrite(bool $blocking, bool $close = false): array |
|
146 | + { |
|
147 | + $this->unblock(); |
|
148 | + $w = $this->write(); |
|
149 | + $read = $r = $e = []; |
|
150 | + |
|
151 | + if ($blocking) { |
|
152 | + if ($w) { |
|
153 | + @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6); |
|
154 | + } elseif ($this->fileHandles) { |
|
155 | + usleep(Process::TIMEOUT_PRECISION * 1E6); |
|
156 | + } |
|
157 | + } |
|
158 | + foreach ($this->fileHandles as $type => $fileHandle) { |
|
159 | + $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]); |
|
160 | + |
|
161 | + if (isset($data[0])) { |
|
162 | + $this->readBytes[$type] += \strlen($data); |
|
163 | + $read[$type] = $data; |
|
164 | + } |
|
165 | + if ($close) { |
|
166 | + ftruncate($fileHandle, 0); |
|
167 | + fclose($fileHandle); |
|
168 | + flock($this->lockHandles[$type], \LOCK_UN); |
|
169 | + fclose($this->lockHandles[$type]); |
|
170 | + unset($this->fileHandles[$type], $this->lockHandles[$type]); |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + return $read; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * {@inheritdoc} |
|
179 | + */ |
|
180 | + public function haveReadSupport(): bool |
|
181 | + { |
|
182 | + return $this->haveReadSupport; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * {@inheritdoc} |
|
187 | + */ |
|
188 | + public function areOpen(): bool |
|
189 | + { |
|
190 | + return $this->pipes && $this->fileHandles; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * {@inheritdoc} |
|
195 | + */ |
|
196 | + public function close() |
|
197 | + { |
|
198 | + parent::close(); |
|
199 | + foreach ($this->fileHandles as $type => $handle) { |
|
200 | + ftruncate($handle, 0); |
|
201 | + fclose($handle); |
|
202 | + flock($this->lockHandles[$type], \LOCK_UN); |
|
203 | + fclose($this->lockHandles[$type]); |
|
204 | + } |
|
205 | + $this->fileHandles = $this->lockHandles = []; |
|
206 | + } |
|
207 | 207 | } |
@@ -25,48 +25,48 @@ |
||
25 | 25 | */ |
26 | 26 | class PhpProcess extends Process |
27 | 27 | { |
28 | - /** |
|
29 | - * @param string $script The PHP script to run (as a string) |
|
30 | - * @param string|null $cwd The working directory or null to use the working dir of the current PHP process |
|
31 | - * @param array|null $env The environment variables or null to use the same environment as the current PHP process |
|
32 | - * @param int $timeout The timeout in seconds |
|
33 | - * @param array|null $php Path to the PHP binary to use with any additional arguments |
|
34 | - */ |
|
35 | - public function __construct(string $script, string $cwd = null, array $env = null, int $timeout = 60, array $php = null) |
|
36 | - { |
|
37 | - if (null === $php) { |
|
38 | - $executableFinder = new PhpExecutableFinder(); |
|
39 | - $php = $executableFinder->find(false); |
|
40 | - $php = false === $php ? null : array_merge([$php], $executableFinder->findArguments()); |
|
41 | - } |
|
42 | - if ('phpdbg' === \PHP_SAPI) { |
|
43 | - $file = tempnam(sys_get_temp_dir(), 'dbg'); |
|
44 | - file_put_contents($file, $script); |
|
45 | - register_shutdown_function('unlink', $file); |
|
46 | - $php[] = $file; |
|
47 | - $script = null; |
|
48 | - } |
|
28 | + /** |
|
29 | + * @param string $script The PHP script to run (as a string) |
|
30 | + * @param string|null $cwd The working directory or null to use the working dir of the current PHP process |
|
31 | + * @param array|null $env The environment variables or null to use the same environment as the current PHP process |
|
32 | + * @param int $timeout The timeout in seconds |
|
33 | + * @param array|null $php Path to the PHP binary to use with any additional arguments |
|
34 | + */ |
|
35 | + public function __construct(string $script, string $cwd = null, array $env = null, int $timeout = 60, array $php = null) |
|
36 | + { |
|
37 | + if (null === $php) { |
|
38 | + $executableFinder = new PhpExecutableFinder(); |
|
39 | + $php = $executableFinder->find(false); |
|
40 | + $php = false === $php ? null : array_merge([$php], $executableFinder->findArguments()); |
|
41 | + } |
|
42 | + if ('phpdbg' === \PHP_SAPI) { |
|
43 | + $file = tempnam(sys_get_temp_dir(), 'dbg'); |
|
44 | + file_put_contents($file, $script); |
|
45 | + register_shutdown_function('unlink', $file); |
|
46 | + $php[] = $file; |
|
47 | + $script = null; |
|
48 | + } |
|
49 | 49 | |
50 | - parent::__construct($php, $cwd, $env, $script, $timeout); |
|
51 | - } |
|
50 | + parent::__construct($php, $cwd, $env, $script, $timeout); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * {@inheritdoc} |
|
55 | - */ |
|
56 | - public static function fromShellCommandline(string $command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60) |
|
57 | - { |
|
58 | - throw new LogicException(sprintf('The "%s()" method cannot be called when using "%s".', __METHOD__, self::class)); |
|
59 | - } |
|
53 | + /** |
|
54 | + * {@inheritdoc} |
|
55 | + */ |
|
56 | + public static function fromShellCommandline(string $command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60) |
|
57 | + { |
|
58 | + throw new LogicException(sprintf('The "%s()" method cannot be called when using "%s".', __METHOD__, self::class)); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * {@inheritdoc} |
|
63 | - */ |
|
64 | - public function start(callable $callback = null, array $env = []) |
|
65 | - { |
|
66 | - if (null === $this->getCommandLine()) { |
|
67 | - throw new RuntimeException('Unable to find the PHP executable.'); |
|
68 | - } |
|
61 | + /** |
|
62 | + * {@inheritdoc} |
|
63 | + */ |
|
64 | + public function start(callable $callback = null, array $env = []) |
|
65 | + { |
|
66 | + if (null === $this->getCommandLine()) { |
|
67 | + throw new RuntimeException('Unable to find the PHP executable.'); |
|
68 | + } |
|
69 | 69 | |
70 | - parent::start($callback, $env); |
|
71 | - } |
|
70 | + parent::start($callback, $env); |
|
71 | + } |
|
72 | 72 | } |
@@ -20,75 +20,75 @@ |
||
20 | 20 | */ |
21 | 21 | class InputStream implements \IteratorAggregate |
22 | 22 | { |
23 | - /** @var callable|null */ |
|
24 | - private $onEmpty = null; |
|
25 | - private $input = []; |
|
26 | - private $open = true; |
|
23 | + /** @var callable|null */ |
|
24 | + private $onEmpty = null; |
|
25 | + private $input = []; |
|
26 | + private $open = true; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Sets a callback that is called when the write buffer becomes empty. |
|
30 | - */ |
|
31 | - public function onEmpty(callable $onEmpty = null) |
|
32 | - { |
|
33 | - $this->onEmpty = $onEmpty; |
|
34 | - } |
|
28 | + /** |
|
29 | + * Sets a callback that is called when the write buffer becomes empty. |
|
30 | + */ |
|
31 | + public function onEmpty(callable $onEmpty = null) |
|
32 | + { |
|
33 | + $this->onEmpty = $onEmpty; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Appends an input to the write buffer. |
|
38 | - * |
|
39 | - * @param resource|string|int|float|bool|\Traversable|null $input The input to append as scalar, |
|
40 | - * stream resource or \Traversable |
|
41 | - */ |
|
42 | - public function write($input) |
|
43 | - { |
|
44 | - if (null === $input) { |
|
45 | - return; |
|
46 | - } |
|
47 | - if ($this->isClosed()) { |
|
48 | - throw new RuntimeException(sprintf('"%s" is closed.', static::class)); |
|
49 | - } |
|
50 | - $this->input[] = ProcessUtils::validateInput(__METHOD__, $input); |
|
51 | - } |
|
36 | + /** |
|
37 | + * Appends an input to the write buffer. |
|
38 | + * |
|
39 | + * @param resource|string|int|float|bool|\Traversable|null $input The input to append as scalar, |
|
40 | + * stream resource or \Traversable |
|
41 | + */ |
|
42 | + public function write($input) |
|
43 | + { |
|
44 | + if (null === $input) { |
|
45 | + return; |
|
46 | + } |
|
47 | + if ($this->isClosed()) { |
|
48 | + throw new RuntimeException(sprintf('"%s" is closed.', static::class)); |
|
49 | + } |
|
50 | + $this->input[] = ProcessUtils::validateInput(__METHOD__, $input); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Closes the write buffer. |
|
55 | - */ |
|
56 | - public function close() |
|
57 | - { |
|
58 | - $this->open = false; |
|
59 | - } |
|
53 | + /** |
|
54 | + * Closes the write buffer. |
|
55 | + */ |
|
56 | + public function close() |
|
57 | + { |
|
58 | + $this->open = false; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Tells whether the write buffer is closed or not. |
|
63 | - */ |
|
64 | - public function isClosed() |
|
65 | - { |
|
66 | - return !$this->open; |
|
67 | - } |
|
61 | + /** |
|
62 | + * Tells whether the write buffer is closed or not. |
|
63 | + */ |
|
64 | + public function isClosed() |
|
65 | + { |
|
66 | + return !$this->open; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @return \Traversable |
|
71 | - */ |
|
72 | - #[\ReturnTypeWillChange] |
|
73 | - public function getIterator() |
|
74 | - { |
|
75 | - $this->open = true; |
|
69 | + /** |
|
70 | + * @return \Traversable |
|
71 | + */ |
|
72 | + #[\ReturnTypeWillChange] |
|
73 | + public function getIterator() |
|
74 | + { |
|
75 | + $this->open = true; |
|
76 | 76 | |
77 | - while ($this->open || $this->input) { |
|
78 | - if (!$this->input) { |
|
79 | - yield ''; |
|
80 | - continue; |
|
81 | - } |
|
82 | - $current = array_shift($this->input); |
|
77 | + while ($this->open || $this->input) { |
|
78 | + if (!$this->input) { |
|
79 | + yield ''; |
|
80 | + continue; |
|
81 | + } |
|
82 | + $current = array_shift($this->input); |
|
83 | 83 | |
84 | - if ($current instanceof \Iterator) { |
|
85 | - yield from $current; |
|
86 | - } else { |
|
87 | - yield $current; |
|
88 | - } |
|
89 | - if (!$this->input && $this->open && null !== $onEmpty = $this->onEmpty) { |
|
90 | - $this->write($onEmpty($this)); |
|
91 | - } |
|
92 | - } |
|
93 | - } |
|
84 | + if ($current instanceof \Iterator) { |
|
85 | + yield from $current; |
|
86 | + } else { |
|
87 | + yield $current; |
|
88 | + } |
|
89 | + if (!$this->input && $this->open && null !== $onEmpty = $this->onEmpty) { |
|
90 | + $this->write($onEmpty($this)); |
|
91 | + } |
|
92 | + } |
|
93 | + } |
|
94 | 94 | } |
@@ -19,81 +19,81 @@ |
||
19 | 19 | */ |
20 | 20 | class PhpExecutableFinder |
21 | 21 | { |
22 | - private $executableFinder; |
|
23 | - |
|
24 | - public function __construct() |
|
25 | - { |
|
26 | - $this->executableFinder = new ExecutableFinder(); |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Finds The PHP executable. |
|
31 | - * |
|
32 | - * @return string|false The PHP executable path or false if it cannot be found |
|
33 | - */ |
|
34 | - public function find(bool $includeArgs = true) |
|
35 | - { |
|
36 | - if ($php = getenv('PHP_BINARY')) { |
|
37 | - if (!is_executable($php)) { |
|
38 | - $command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v'; |
|
39 | - if ($php = strtok(exec($command.' '.escapeshellarg($php)), \PHP_EOL)) { |
|
40 | - if (!is_executable($php)) { |
|
41 | - return false; |
|
42 | - } |
|
43 | - } else { |
|
44 | - return false; |
|
45 | - } |
|
46 | - } |
|
47 | - |
|
48 | - return $php; |
|
49 | - } |
|
50 | - |
|
51 | - $args = $this->findArguments(); |
|
52 | - $args = $includeArgs && $args ? ' '.implode(' ', $args) : ''; |
|
53 | - |
|
54 | - // PHP_BINARY return the current sapi executable |
|
55 | - if (\PHP_BINARY && \in_array(\PHP_SAPI, ['cgi-fcgi', 'cli', 'cli-server', 'phpdbg'], true)) { |
|
56 | - return \PHP_BINARY.$args; |
|
57 | - } |
|
58 | - |
|
59 | - if ($php = getenv('PHP_PATH')) { |
|
60 | - if (!@is_executable($php)) { |
|
61 | - return false; |
|
62 | - } |
|
63 | - |
|
64 | - return $php; |
|
65 | - } |
|
66 | - |
|
67 | - if ($php = getenv('PHP_PEAR_PHP_BIN')) { |
|
68 | - if (@is_executable($php)) { |
|
69 | - return $php; |
|
70 | - } |
|
71 | - } |
|
72 | - |
|
73 | - if (@is_executable($php = \PHP_BINDIR.('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php'))) { |
|
74 | - return $php; |
|
75 | - } |
|
76 | - |
|
77 | - $dirs = [\PHP_BINDIR]; |
|
78 | - if ('\\' === \DIRECTORY_SEPARATOR) { |
|
79 | - $dirs[] = 'C:\xampp\php\\'; |
|
80 | - } |
|
81 | - |
|
82 | - return $this->executableFinder->find('php', false, $dirs); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Finds the PHP executable arguments. |
|
87 | - * |
|
88 | - * @return array The PHP executable arguments |
|
89 | - */ |
|
90 | - public function findArguments() |
|
91 | - { |
|
92 | - $arguments = []; |
|
93 | - if ('phpdbg' === \PHP_SAPI) { |
|
94 | - $arguments[] = '-qrr'; |
|
95 | - } |
|
96 | - |
|
97 | - return $arguments; |
|
98 | - } |
|
22 | + private $executableFinder; |
|
23 | + |
|
24 | + public function __construct() |
|
25 | + { |
|
26 | + $this->executableFinder = new ExecutableFinder(); |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Finds The PHP executable. |
|
31 | + * |
|
32 | + * @return string|false The PHP executable path or false if it cannot be found |
|
33 | + */ |
|
34 | + public function find(bool $includeArgs = true) |
|
35 | + { |
|
36 | + if ($php = getenv('PHP_BINARY')) { |
|
37 | + if (!is_executable($php)) { |
|
38 | + $command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v'; |
|
39 | + if ($php = strtok(exec($command.' '.escapeshellarg($php)), \PHP_EOL)) { |
|
40 | + if (!is_executable($php)) { |
|
41 | + return false; |
|
42 | + } |
|
43 | + } else { |
|
44 | + return false; |
|
45 | + } |
|
46 | + } |
|
47 | + |
|
48 | + return $php; |
|
49 | + } |
|
50 | + |
|
51 | + $args = $this->findArguments(); |
|
52 | + $args = $includeArgs && $args ? ' '.implode(' ', $args) : ''; |
|
53 | + |
|
54 | + // PHP_BINARY return the current sapi executable |
|
55 | + if (\PHP_BINARY && \in_array(\PHP_SAPI, ['cgi-fcgi', 'cli', 'cli-server', 'phpdbg'], true)) { |
|
56 | + return \PHP_BINARY.$args; |
|
57 | + } |
|
58 | + |
|
59 | + if ($php = getenv('PHP_PATH')) { |
|
60 | + if (!@is_executable($php)) { |
|
61 | + return false; |
|
62 | + } |
|
63 | + |
|
64 | + return $php; |
|
65 | + } |
|
66 | + |
|
67 | + if ($php = getenv('PHP_PEAR_PHP_BIN')) { |
|
68 | + if (@is_executable($php)) { |
|
69 | + return $php; |
|
70 | + } |
|
71 | + } |
|
72 | + |
|
73 | + if (@is_executable($php = \PHP_BINDIR.('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php'))) { |
|
74 | + return $php; |
|
75 | + } |
|
76 | + |
|
77 | + $dirs = [\PHP_BINDIR]; |
|
78 | + if ('\\' === \DIRECTORY_SEPARATOR) { |
|
79 | + $dirs[] = 'C:\xampp\php\\'; |
|
80 | + } |
|
81 | + |
|
82 | + return $this->executableFinder->find('php', false, $dirs); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Finds the PHP executable arguments. |
|
87 | + * |
|
88 | + * @return array The PHP executable arguments |
|
89 | + */ |
|
90 | + public function findArguments() |
|
91 | + { |
|
92 | + $arguments = []; |
|
93 | + if ('phpdbg' === \PHP_SAPI) { |
|
94 | + $arguments[] = '-qrr'; |
|
95 | + } |
|
96 | + |
|
97 | + return $arguments; |
|
98 | + } |
|
99 | 99 | } |
@@ -19,68 +19,68 @@ |
||
19 | 19 | */ |
20 | 20 | class ExecutableFinder |
21 | 21 | { |
22 | - private $suffixes = ['.exe', '.bat', '.cmd', '.com']; |
|
22 | + private $suffixes = ['.exe', '.bat', '.cmd', '.com']; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Replaces default suffixes of executable. |
|
26 | - */ |
|
27 | - public function setSuffixes(array $suffixes) |
|
28 | - { |
|
29 | - $this->suffixes = $suffixes; |
|
30 | - } |
|
24 | + /** |
|
25 | + * Replaces default suffixes of executable. |
|
26 | + */ |
|
27 | + public function setSuffixes(array $suffixes) |
|
28 | + { |
|
29 | + $this->suffixes = $suffixes; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Adds new possible suffix to check for executable. |
|
34 | - */ |
|
35 | - public function addSuffix(string $suffix) |
|
36 | - { |
|
37 | - $this->suffixes[] = $suffix; |
|
38 | - } |
|
32 | + /** |
|
33 | + * Adds new possible suffix to check for executable. |
|
34 | + */ |
|
35 | + public function addSuffix(string $suffix) |
|
36 | + { |
|
37 | + $this->suffixes[] = $suffix; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Finds an executable by name. |
|
42 | - * |
|
43 | - * @param string $name The executable name (without the extension) |
|
44 | - * @param string|null $default The default to return if no executable is found |
|
45 | - * @param array $extraDirs Additional dirs to check into |
|
46 | - * |
|
47 | - * @return string|null The executable path or default value |
|
48 | - */ |
|
49 | - public function find(string $name, string $default = null, array $extraDirs = []) |
|
50 | - { |
|
51 | - if (ini_get('open_basedir')) { |
|
52 | - $searchPath = array_merge(explode(\PATH_SEPARATOR, ini_get('open_basedir')), $extraDirs); |
|
53 | - $dirs = []; |
|
54 | - foreach ($searchPath as $path) { |
|
55 | - // Silencing against https://bugs.php.net/69240 |
|
56 | - if (@is_dir($path)) { |
|
57 | - $dirs[] = $path; |
|
58 | - } else { |
|
59 | - if (basename($path) == $name && @is_executable($path)) { |
|
60 | - return $path; |
|
61 | - } |
|
62 | - } |
|
63 | - } |
|
64 | - } else { |
|
65 | - $dirs = array_merge( |
|
66 | - explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')), |
|
67 | - $extraDirs |
|
68 | - ); |
|
69 | - } |
|
40 | + /** |
|
41 | + * Finds an executable by name. |
|
42 | + * |
|
43 | + * @param string $name The executable name (without the extension) |
|
44 | + * @param string|null $default The default to return if no executable is found |
|
45 | + * @param array $extraDirs Additional dirs to check into |
|
46 | + * |
|
47 | + * @return string|null The executable path or default value |
|
48 | + */ |
|
49 | + public function find(string $name, string $default = null, array $extraDirs = []) |
|
50 | + { |
|
51 | + if (ini_get('open_basedir')) { |
|
52 | + $searchPath = array_merge(explode(\PATH_SEPARATOR, ini_get('open_basedir')), $extraDirs); |
|
53 | + $dirs = []; |
|
54 | + foreach ($searchPath as $path) { |
|
55 | + // Silencing against https://bugs.php.net/69240 |
|
56 | + if (@is_dir($path)) { |
|
57 | + $dirs[] = $path; |
|
58 | + } else { |
|
59 | + if (basename($path) == $name && @is_executable($path)) { |
|
60 | + return $path; |
|
61 | + } |
|
62 | + } |
|
63 | + } |
|
64 | + } else { |
|
65 | + $dirs = array_merge( |
|
66 | + explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')), |
|
67 | + $extraDirs |
|
68 | + ); |
|
69 | + } |
|
70 | 70 | |
71 | - $suffixes = ['']; |
|
72 | - if ('\\' === \DIRECTORY_SEPARATOR) { |
|
73 | - $pathExt = getenv('PATHEXT'); |
|
74 | - $suffixes = array_merge($pathExt ? explode(\PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes); |
|
75 | - } |
|
76 | - foreach ($suffixes as $suffix) { |
|
77 | - foreach ($dirs as $dir) { |
|
78 | - if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) { |
|
79 | - return $file; |
|
80 | - } |
|
81 | - } |
|
82 | - } |
|
71 | + $suffixes = ['']; |
|
72 | + if ('\\' === \DIRECTORY_SEPARATOR) { |
|
73 | + $pathExt = getenv('PATHEXT'); |
|
74 | + $suffixes = array_merge($pathExt ? explode(\PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes); |
|
75 | + } |
|
76 | + foreach ($suffixes as $suffix) { |
|
77 | + foreach ($dirs as $dir) { |
|
78 | + if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) { |
|
79 | + return $file; |
|
80 | + } |
|
81 | + } |
|
82 | + } |
|
83 | 83 | |
84 | - return $default; |
|
85 | - } |
|
84 | + return $default; |
|
85 | + } |
|
86 | 86 | } |