1 | <?php |
||
38 | class ProcessIterator implements \Iterator |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * Key of the processes, that are waiting to be executed. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $waitingQueue = array(); |
||
47 | |||
48 | /** |
||
49 | * Keys of the processes, that are currently running. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $runningQueue = array(); |
||
54 | |||
55 | /** |
||
56 | * Processes to iterate over. |
||
57 | * |
||
58 | * @var Process[] |
||
59 | */ |
||
60 | protected $processes = array(); |
||
61 | |||
62 | /** |
||
63 | * Last exception, thrown by each process. |
||
64 | * |
||
65 | * @var \Exception[] |
||
66 | */ |
||
67 | protected $exceptions = array(); |
||
68 | |||
69 | /** |
||
70 | * Current process key. |
||
71 | * |
||
72 | * @var mixed |
||
73 | */ |
||
74 | protected $key; |
||
75 | |||
76 | /** |
||
77 | * Maximal number of simultaneously executing processes. |
||
78 | * |
||
79 | * @var integer |
||
80 | */ |
||
81 | protected $limit; |
||
82 | |||
83 | /** |
||
84 | * Maximal amount of time to wait before iterator will yield the result. |
||
85 | * |
||
86 | * @var integer |
||
87 | */ |
||
88 | protected $timeout; |
||
89 | |||
90 | /** |
||
91 | * Result waiting timeout was reached. |
||
92 | * |
||
93 | * @var boolean |
||
94 | */ |
||
95 | protected $isTimeout = false; |
||
96 | |||
97 | /** |
||
98 | * Create a new iterator over a list of processes. |
||
99 | * |
||
100 | * @param array $processes List of processes to execute. |
||
101 | * |
||
102 | * @throws \InvalidArgumentException When unknown elements are present in $processes array. |
||
103 | */ |
||
104 | public function __construct(array $processes) |
||
119 | |||
120 | /** |
||
121 | * Block until all processes have executed. |
||
122 | * |
||
123 | * @return self |
||
124 | */ |
||
125 | public function runAll() |
||
133 | |||
134 | /** |
||
135 | * Add another process to the set of processes. This is useful if you have a |
||
136 | * set of processes to run mostly in parallel, but some processes depend on |
||
137 | * others. |
||
138 | * |
||
139 | * @param Process $process Process to add to iterator. |
||
140 | * @param mixed $key Key. |
||
141 | * |
||
142 | * @return self |
||
143 | * @throws \InvalidArgumentException When given key is already in use. |
||
144 | */ |
||
145 | public function addProcess(Process $process, $key = null) |
||
167 | |||
168 | /** |
||
169 | * Set a maximum amount of time you want to wait before the iterator will |
||
170 | * yield a result. If no process has executed yet, the iterator will yield |
||
171 | * empty string for key and null for value. Among other potential uses, you |
||
172 | * can use this to show some busy indicator: |
||
173 | * $processes = (new ProcessIterator($processes)) |
||
174 | * ->setUpdateInterval(1); |
||
175 | * foreach ($processes as $process) { |
||
176 | * if ($process === null) { |
||
177 | * echo "Still working...\n"; |
||
178 | * } else { |
||
179 | * // ... |
||
180 | * } |
||
181 | * } |
||
182 | * This will echo "Still working..." once per second as long as processes are |
||
183 | * resolving. By default, ProcessIterator never yields null. |
||
184 | * |
||
185 | * @param float $interval Maximum number of seconds to block waiting on processes before yielding null. |
||
186 | * |
||
187 | * @return self |
||
188 | */ |
||
189 | public function setUpdateInterval($interval) |
||
195 | |||
196 | /** |
||
197 | * Limit the number of simultaneously executing processes. |
||
198 | * $processes = (new ProcessIterator($processes)) |
||
199 | * ->limit(4); |
||
200 | * foreach ($processes as $process) { |
||
201 | * // Run no more than 4 processes simultaneously. |
||
202 | * } |
||
203 | * |
||
204 | * @param integer $max Maximum number of simultaneously running processes allowed. |
||
205 | * |
||
206 | * @return self |
||
207 | */ |
||
208 | public function limit($max) |
||
214 | |||
215 | /** |
||
216 | * Rewind the Iterator to the first element. |
||
217 | * |
||
218 | * @return void |
||
219 | */ |
||
220 | public function rewind() |
||
227 | |||
228 | /** |
||
229 | * Move forward to next element. |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | public function next() |
||
289 | |||
290 | /** |
||
291 | * Remembers exception, associated with a process. |
||
292 | * |
||
293 | * @param mixed $key Process key. |
||
294 | * @param \Exception $exception Exception. |
||
295 | * |
||
296 | * @return void |
||
297 | */ |
||
298 | protected function setProcessException($key, \Exception $exception) |
||
302 | |||
303 | /** |
||
304 | * Gets exception, associated with a process. |
||
305 | * |
||
306 | * @return \Exception|null |
||
307 | */ |
||
308 | public function getProcessException() |
||
314 | |||
315 | /** |
||
316 | * Return the current element. |
||
317 | * |
||
318 | * @return Process|null |
||
319 | */ |
||
320 | public function current() |
||
328 | |||
329 | /** |
||
330 | * Return the key of the current element. |
||
331 | * |
||
332 | * @return mixed|null |
||
333 | */ |
||
334 | public function key() |
||
343 | |||
344 | /** |
||
345 | * Checks if current position is valid. |
||
346 | * |
||
347 | * @return boolean |
||
348 | */ |
||
349 | public function valid() |
||
357 | |||
358 | /** |
||
359 | * Ensure, that needed number of processes are running in parallel. |
||
360 | * |
||
361 | * @return void |
||
362 | */ |
||
363 | protected function updateWorkingSet() |
||
381 | |||
382 | } |
||
383 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.