1 | <?php |
||
15 | class Co implements CoInterface |
||
16 | { |
||
17 | /** |
||
18 | * Instance of myself. |
||
19 | * @var Co |
||
20 | */ |
||
21 | private static $self; |
||
22 | |||
23 | /** |
||
24 | * Options. |
||
25 | * @var CoOption |
||
26 | */ |
||
27 | private $options; |
||
28 | |||
29 | /** |
||
30 | * cURL request pool object. |
||
31 | * @var Pool |
||
32 | */ |
||
33 | private $pool; |
||
34 | |||
35 | /** |
||
36 | * Running cURL or Generator identifiers. |
||
37 | * @var array |
||
38 | */ |
||
39 | private $runners = []; |
||
40 | |||
41 | /** |
||
42 | * Overwrite CoOption default. |
||
43 | * @param array $options |
||
44 | */ |
||
45 | 1 | public static function setDefaultOptions(array $options) |
|
49 | |||
50 | /** |
||
51 | * Get CoOption default as array. |
||
52 | * @return array |
||
53 | */ |
||
54 | 1 | public static function getDefaultOptions() |
|
58 | |||
59 | /** |
||
60 | * Wait until value is recursively resolved to return it. |
||
61 | * This function call must be atomic. |
||
62 | * @param mixed $value |
||
63 | * @param array $options |
||
64 | * @return mixed |
||
65 | */ |
||
66 | 30 | public static function wait($value, array $options = []) |
|
81 | // @codeCoverageIgnoreEnd |
||
82 | |||
83 | /** |
||
84 | * Value is recursively resolved, but we never wait it. |
||
85 | * This function must be called along with Co::wait(). |
||
86 | * @param mixed $value |
||
87 | * @param mixed $throw |
||
88 | */ |
||
89 | 6 | public static function async($value, $throw = null) |
|
104 | |||
105 | /** |
||
106 | * External instantiation is forbidden. |
||
107 | */ |
||
108 | private function __construct() {} |
||
109 | |||
110 | /** |
||
111 | * Start resovling. |
||
112 | * @param mixed $value |
||
113 | * @param bool $wait |
||
114 | * @param mixed $throw Used for Co::async() overrides. |
||
115 | * @param mixed If $wait, return resolved value. |
||
116 | */ |
||
117 | 30 | private function start($value, $wait = true, $throw = null) |
|
131 | |||
132 | /** |
||
133 | * Handle resolving generators. |
||
134 | * @param GeneratorContainer $gc |
||
135 | * @param Deferred $deferred |
||
136 | */ |
||
137 | 26 | private function processGeneratorContainer(GeneratorContainer $gc, Deferred $deferred) |
|
143 | |||
144 | /** |
||
145 | * Handle resolving generators already done. |
||
146 | * @param GeneratorContainer $gc |
||
147 | * @param Deferred $deferred |
||
148 | */ |
||
149 | 23 | private function processGeneratorContainerDone(GeneratorContainer $gc, Deferred $deferred) |
|
178 | |||
179 | /** |
||
180 | * Handle resolving generators still running. |
||
181 | * @param GeneratorContainer $gc |
||
182 | * @param Deferred $deferred |
||
183 | */ |
||
184 | 30 | private function processGeneratorContainerRunning(GeneratorContainer $gc, Deferred $deferred) |
|
224 | |||
225 | /** |
||
226 | * Return root wrapper generator. |
||
227 | * @param mixed $throw |
||
228 | * @param mixed $value |
||
229 | * @param mixed &$return |
||
230 | */ |
||
231 | 30 | private function getRootGenerator($throw, $value, &$return) |
|
244 | |||
245 | /** |
||
246 | * Promise all changes in yieldables are prepared. |
||
247 | * @param array $yieldables |
||
248 | * @param bool $throw_acceptable |
||
249 | * @return PromiseInterface |
||
250 | */ |
||
251 | 26 | private function promiseAll(array $yieldables, $throw_acceptable) |
|
275 | |||
276 | /** |
||
277 | * Wrap value with the Generator that returns the first successful result. |
||
278 | * If all yieldables failed, AllFailedException is thrown. |
||
279 | * If no yieldables found, AllFailedException is thrown. |
||
280 | * |
||
281 | * @param mixed $value |
||
282 | * @return mixed Resolved value. |
||
283 | * @throws AllFailedException |
||
284 | */ |
||
285 | 4 | public static function any($value) |
|
294 | // @codeCoverageIgnoreEnd |
||
295 | |||
296 | /** |
||
297 | * Wrap value with the Generator that returns the first result. |
||
298 | * If no yieldables found, AllFailedException is thrown. |
||
299 | * |
||
300 | * @param mixed $value |
||
301 | * @return mixed Resolved value. |
||
302 | * @throws \RuntimeException|AllFailedException |
||
303 | */ |
||
304 | 3 | public static function race($value) |
|
313 | // @codeCoverageIgnoreEnd |
||
314 | |||
315 | /** |
||
316 | * Wrap value with the Generator that returns the all results. |
||
317 | * Normally you don't have to use this method, just yield an array that contains yieldables. |
||
318 | * You should use only with Co::race() or Co::any(). |
||
319 | * |
||
320 | * @param mixed $value |
||
321 | * @return mixed Resolved value. |
||
322 | * @throws \RuntimeException |
||
323 | */ |
||
324 | 1 | public static function all($value) |
|
329 | // @codeCoverageIgnoreEnd |
||
330 | } |
||
331 |