PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / Stream / ProcessOutput.php

ProcessOutput.php in ManageWP Worker 4.9.25, at src/MWP/Stream/ProcessOutput.php

76 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of the ManageWP Worker plugin.
4 *
5 * (c) ManageWP LLC <contact@managewp.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 class MWP_Stream_ProcessOutput extends MWP_Stream_Callable
12 {
13
14 /**
15 * @var Symfony_Process_Process
16 */
17 private $process;
18
19 /**
20 * @var bool
21 */
22 private $ran = false;
23
24 public function __construct(Symfony_Process_Process $process)
25 {
26 parent::__construct(array($this, 'getIncrementalOutput'));
27 $this->process = $process;
28 }
29
30 /**
31 * Returns incremental process output (even if empty string) or false if the process has finished
32 * successfully and all output was already returned.
33 *
34 * @throws Symfony_Process_Exception_ProcessFailedException If the process did not exit successfully.
35 *
36 * @internal
37 *
38 * @return string|false
39 */
40 public function getIncrementalOutput()
41 {
42 if (!$this->ran) {
43 $this->ran = true;
44 try {
45 $this->process->start();
46 } catch (Symfony_Process_Exception_ExceptionInterface $e) {
47 throw new Symfony_Process_Exception_ProcessFailedException($this->process);
48 }
49 }
50
51 if ($this->process->isRunning()) {
52 $output = $this->process->getIncrementalOutput();
53 $this->process->clearOutput();
54
55 if (strlen($output) < Symfony_Process_Pipes_PipesInterface::CHUNK_SIZE) {
56 // Don't hog the processor while waiting for incremental process output.
57 usleep(100000);
58 }
59
60 // The stream will be read again because we're returning a string.
61 return (string)$output;
62 } else {
63 if (!$this->process->isSuccessful()) {
64 throw new Symfony_Process_Exception_ProcessFailedException($this->process);
65 }
66
67 $output = $this->process->getIncrementalOutput();
68 $this->process->clearOutput();
69
70 // The process has finished and is successful. This part will probably get run twice,
71 // first time we'll return final output, second time we'll return 'false' and break the loop.
72 return strlen($output) ? $output : false;
73 }
74 }
75 }
76