PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
← All changes | includes/sdk/google/monolog/monolog/src/Monolog/Handler/ProcessHandler.php +34 -35 1.2.2 → 1.4.2 View file →
@@ -8,11 +8,12 @@
8 8 *
9 9 * For the full copyright and license information, please view the LICENSE
10 10 * file that was distributed with this source code.
11 11 */
12 -namespace Dudlewebs\WPMCS\Monolog\Handler;
12 +namespace Dudlewebs\WPMCS\GCP\Monolog\Handler;
13 13
14 -use Dudlewebs\WPMCS\Monolog\Logger;
14 +use Dudlewebs\WPMCS\GCP\Monolog\Level;
15 +use Dudlewebs\WPMCS\GCP\Monolog\LogRecord;
15 16 /**
16 17 * Stores to STDIN of any process, specified by a command.
17 18 *
18 19 * Usage example:
@@ -30,22 +31,17 @@
30 31 *
31 32 * @var resource|bool|null
32 33 */
33 34 private $process;
35 + private string $command;
36 + private ?string $cwd;
34 37 /**
35 - * @var string
36 - */
37 - private $command;
38 - /**
39 - * @var string|null
40 - */
41 - private $cwd;
42 - /**
43 38 * @var resource[]
44 39 */
45 - private $pipes = [];
40 + private array $pipes = [];
41 + private float $timeout;
46 42 /**
47 - * @var array<int, string[]>
43 + * @var array<int, list<string>>
48 44 */
49 45 protected const DESCRIPTOR_SPEC = [
50 46 0 => ['pipe', 'r'],
51 47 // STDIN is a pipe that the child will read from
@@ -56,11 +52,12 @@
56 52 /**
57 53 * @param string $command Command for the process to start. Absolute paths are recommended,
58 54 * especially if you do not use the $cwd parameter.
59 55 * @param string|null $cwd "Current working directory" (CWD) for the process to be executed in.
56 + * @param float $timeout The maximum timeout (in seconds) for the stream_select() function.
60 57 * @throws \InvalidArgumentException
61 58 */
62 - public function __construct(string $command, $level = Logger::DEBUG, bool $bubble = \true, ?string $cwd = null)
59 + public function __construct(string $command, int|string|Level $level = Level::Debug, bool $bubble = \true, ?string $cwd = null, float $timeout = 1.0)
63 60 {
64 61 if ($command === '') {
65 62 throw new \InvalidArgumentException('The command argument must be a non-empty string.');
66 63 }
@@ -69,8 +66,9 @@
69 66 }
70 67 parent::__construct($level, $bubble);
71 68 $this->command = $command;
72 69 $this->cwd = $cwd;
70 + $this->timeout = $timeout;
73 71 }
74 72 /**
75 73 * Writes the record down to the log of the implementing handler
76 74 *
@@ -75,15 +73,15 @@
75 73 * Writes the record down to the log of the implementing handler
76 74 *
77 75 * @throws \UnexpectedValueException
78 76 */
79 - protected function write(array $record): void
77 + protected function write(LogRecord $record) : void
80 78 {
81 79 $this->ensureProcessIsStarted();
82 - $this->writeProcessInput($record['formatted']);
80 + $this->writeProcessInput($record->formatted);
83 81 $errors = $this->readProcessErrors();
84 - if (empty($errors) === \false) {
85 - throw new \UnexpectedValueException(sprintf('Errors while writing to process: %s', $errors));
82 + if ($errors !== '') {
83 + throw new \UnexpectedValueException(\sprintf('Errors while writing to process: %s', $errors));
86 84 }
87 85 }
88 86 /**
89 87 * Makes sure that the process is actually started, and if not, starts it,
@@ -88,11 +86,11 @@
88 86 /**
89 87 * Makes sure that the process is actually started, and if not, starts it,
90 88 * assigns the stream pipes, and handles startup errors, if any.
91 89 */
92 - private function ensureProcessIsStarted(): void
90 + private function ensureProcessIsStarted() : void
93 91 {
94 - if (is_resource($this->process) === \false) {
92 + if (\is_resource($this->process) === \false) {
95 93 $this->startProcess();
96 94 $this->handleStartupErrors();
97 95 }
98 96 }
@@ -98,13 +96,13 @@
98 96 }
99 97 /**
100 98 * Starts the actual process and sets all streams to non-blocking.
101 99 */
102 - private function startProcess(): void
100 + private function startProcess() : void
103 101 {
104 - $this->process = proc_open($this->command, static::DESCRIPTOR_SPEC, $this->pipes, $this->cwd);
102 + $this->process = \proc_open($this->command, static::DESCRIPTOR_SPEC, $this->pipes, $this->cwd);
105 103 foreach ($this->pipes as $pipe) {
106 - stream_set_blocking($pipe, \false);
104 + \stream_set_blocking($pipe, \false);
107 105 }
108 106 }
109 107 /**
110 108 * Selects the STDERR stream, handles upcoming startup errors, and throws an exception, if any.
@@ -110,9 +108,9 @@
110 108 * Selects the STDERR stream, handles upcoming startup errors, and throws an exception, if any.
111 109 *
112 110 * @throws \UnexpectedValueException
113 111 */
114 - private function handleStartupErrors(): void
112 + private function handleStartupErrors() : void
115 113 {
116 114 $selected = $this->selectErrorStream();
117 115 if (\false === $selected) {
118 116 throw new \UnexpectedValueException('Something went wrong while selecting a stream.');
@@ -117,10 +115,10 @@
117 115 if (\false === $selected) {
118 116 throw new \UnexpectedValueException('Something went wrong while selecting a stream.');
119 117 }
120 118 $errors = $this->readProcessErrors();
121 - if (is_resource($this->process) === \false || empty($errors) === \false) {
122 - throw new \UnexpectedValueException(sprintf('The process "%s" could not be opened: ' . $errors, $this->command));
119 + if (\is_resource($this->process) === \false || $errors !== '') {
120 + throw new \UnexpectedValueException(\sprintf('The process "%s" could not be opened: ' . $errors, $this->command));
123 121 }
124 122 }
125 123 /**
126 124 * Selects the STDERR stream.
@@ -130,9 +128,10 @@
130 128 protected function selectErrorStream()
131 129 {
132 130 $empty = [];
133 131 $errorPipes = [$this->pipes[2]];
134 - return stream_select($errorPipes, $empty, $empty, 1);
132 + $seconds = (int) $this->timeout;
133 + return \stream_select($errorPipes, $empty, $empty, $seconds, (int) (($this->timeout - $seconds) * 1000000));
135 134 }
136 135 /**
137 136 * Reads the errors of the process, if there are any.
138 137 *
@@ -138,11 +137,11 @@
138 137 *
139 138 * @codeCoverageIgnore
140 139 * @return string Empty string if there are no errors.
141 140 */
142 - protected function readProcessErrors(): string
141 + protected function readProcessErrors() : string
143 142 {
144 - return (string) stream_get_contents($this->pipes[2]);
143 + return (string) \stream_get_contents($this->pipes[2]);
145 144 }
146 145 /**
147 146 * Writes to the input stream of the opened process.
148 147 *
@@ -147,22 +146,22 @@
147 146 * Writes to the input stream of the opened process.
148 147 *
149 148 * @codeCoverageIgnore
150 149 */
151 - protected function writeProcessInput(string $string): void
150 + protected function writeProcessInput(string $string) : void
152 151 {
153 - fwrite($this->pipes[0], $string);
152 + \fwrite($this->pipes[0], $string);
154 153 }
155 154 /**
156 - * {@inheritDoc}
155 + * @inheritDoc
157 156 */
158 - public function close(): void
157 + public function close() : void
159 158 {
160 - if (is_resource($this->process)) {
159 + if (\is_resource($this->process)) {
161 160 foreach ($this->pipes as $pipe) {
162 - fclose($pipe);
161 + \fclose($pipe);
163 162 }
164 - proc_close($this->process);
163 + \proc_close($this->process);
165 164 $this->process = null;
166 165 }
167 166 }
168 167 }