1 | <?php |
||
24 | class BackgroundProcess |
||
25 | { |
||
26 | const OS_WINDOWS = 1; |
||
27 | const OS_NIX = 2; |
||
28 | const OS_OTHER = 3; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $command; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $pid; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $serverOS; |
||
44 | |||
45 | /** |
||
46 | * @param string $command The command to execute |
||
|
|||
47 | * |
||
48 | * @codeCoverageIgnore |
||
49 | */ |
||
50 | public function __construct($command = null) |
||
55 | |||
56 | /** |
||
57 | * Runs the command in a background process. |
||
58 | * |
||
59 | * @param string $outputFile File to write the output of the process to; defaults to /dev/null |
||
60 | * currently $outputFile has no effect when used in conjunction with a Windows server |
||
61 | * @param bool $append - set to true if output should be appended to $outputfile |
||
62 | */ |
||
63 | 2 | public function run($outputFile = '/dev/null', $append = false) |
|
85 | |||
86 | /** |
||
87 | * Returns if the process is currently running. |
||
88 | * |
||
89 | * @return bool TRUE if the process is running, FALSE if not. |
||
90 | */ |
||
91 | 1 | public function isRunning() |
|
92 | { |
||
93 | 1 | $this->checkSupportingOS('Cocur\BackgroundProcess can only check if a process is running on *nix-based '. |
|
94 | 1 | 'systems, such as Unix, Linux or Mac OS X. You are running "%s".'); |
|
95 | |||
96 | try { |
||
97 | 1 | $result = shell_exec("ps -ef | awk '{print $1}'"); |
|
98 | 1 | $pidArray = explode(PHP_EOL, $result); |
|
99 | 1 | if (in_array($this->pid,$pidArray) && !preg_match('/ERROR: Process ID out of range/', $result)) { |
|
100 | 1 | return true; |
|
101 | } |
||
102 | } catch (Exception $e) { |
||
103 | } |
||
104 | |||
105 | return false; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Stops the process. |
||
110 | * |
||
111 | * @return bool `true` if the processes was stopped, `false` otherwise. |
||
112 | */ |
||
113 | 1 | public function stop() |
|
114 | { |
||
115 | 1 | $this->checkSupportingOS('Cocur\BackgroundProcess can only stop a process on *nix-based systems, such as '. |
|
116 | 1 | 'Unix, Linux or Mac OS X. You are running "%s".'); |
|
117 | |||
118 | try { |
||
119 | 1 | $result = shell_exec(sprintf('kill %d 2>&1', $this->pid)); |
|
120 | 1 | if (!preg_match('/No such process/', $result)) { |
|
121 | 1 | return true; |
|
122 | } |
||
123 | } catch (Exception $e) { |
||
124 | } |
||
125 | |||
126 | return false; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Returns the ID of the process. |
||
131 | * |
||
132 | * @return int The ID of the process |
||
133 | */ |
||
134 | 1 | public function getPid() |
|
135 | { |
||
136 | 1 | $this->checkSupportingOS('Cocur\BackgroundProcess can only return the PID of a process on *nix-based systems, '. |
|
137 | 1 | 'such as Unix, Linux or Mac OS X. You are running "%s".'); |
|
138 | |||
139 | 1 | return $this->pid; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Set the process id. |
||
144 | * |
||
145 | * @param $pid |
||
146 | */ |
||
147 | protected function setPid($pid) |
||
148 | { |
||
149 | $this->pid = $pid; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @return int |
||
154 | */ |
||
155 | 5 | protected function getOS() |
|
167 | |||
168 | /** |
||
169 | * @param string $message Exception message if the OS is not supported |
||
170 | * |
||
171 | * @throws RuntimeException if the operating system is not supported by Cocur\BackgroundProcess |
||
172 | * |
||
173 | * @codeCoverageIgnore |
||
174 | */ |
||
175 | protected function checkSupportingOS($message) |
||
181 | |||
182 | /** |
||
183 | * @param int $pid PID of process to resume |
||
184 | * |
||
185 | * @return Cocur\BackgroundProcess\BackgroundProcess |
||
186 | */ |
||
187 | 1 | static public function createFromPID($pid) { |
|
193 | } |
||
194 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.