1 | <?php |
||
16 | class Co implements CoInterface |
||
17 | { |
||
18 | /** |
||
19 | * Instance of myself. |
||
20 | * @var Co |
||
21 | */ |
||
22 | private static $self; |
||
23 | |||
24 | /** |
||
25 | * Options. |
||
26 | * @var CoOption |
||
27 | */ |
||
28 | private $options; |
||
29 | |||
30 | /** |
||
31 | * cURL request pool object. |
||
32 | * @var Pool |
||
33 | */ |
||
34 | private $pool; |
||
35 | |||
36 | /** |
||
37 | * Running cURL or Generator identifiers. |
||
38 | * @var array |
||
39 | */ |
||
40 | private $runners = []; |
||
41 | |||
42 | /** |
||
43 | * Overwrite CoOption default. |
||
44 | * @param array $options |
||
45 | */ |
||
46 | 1 | public static function setDefaultOptions(array $options) |
|
50 | |||
51 | /** |
||
52 | * Get CoOption default as array. |
||
53 | * @return array |
||
54 | */ |
||
55 | 1 | public static function getDefaultOptions() |
|
59 | |||
60 | /** |
||
61 | * Wait until value is recursively resolved to return it. |
||
62 | * This function call must be atomic. |
||
63 | * @param mixed $value |
||
64 | * @param array $options |
||
65 | * @return mixed |
||
66 | */ |
||
67 | 31 | public static function wait($value, array $options = []) |
|
82 | // @codeCoverageIgnoreEnd |
||
83 | |||
84 | /** |
||
85 | * Value is recursively resolved, but we never wait it. |
||
86 | * This function must be called along with Co::wait(). |
||
87 | * @param mixed $value |
||
88 | * @param mixed $throw |
||
89 | */ |
||
90 | 6 | public static function async($value, $throw = null) |
|
105 | |||
106 | /** |
||
107 | * Return if Co::wait() is running. |
||
108 | * @return bool |
||
109 | */ |
||
110 | 2 | public static function isRunning() |
|
114 | |||
115 | /** |
||
116 | * External instantiation is forbidden. |
||
117 | */ |
||
118 | private function __construct() {} |
||
119 | |||
120 | /** |
||
121 | * Start resovling. |
||
122 | * @param mixed $value |
||
123 | * @param bool $wait |
||
124 | * @param mixed $throw Used for Co::async() overrides. |
||
125 | * @param mixed If $wait, return resolved value. |
||
126 | */ |
||
127 | 31 | private function start($value, $wait = true, $throw = null) |
|
143 | |||
144 | /** |
||
145 | * Handle resolving generators. |
||
146 | * @param GeneratorContainer $gc |
||
147 | * @return PromiseInterface |
||
148 | */ |
||
149 | 27 | private function processGeneratorContainer(GeneratorContainer $gc) |
|
155 | |||
156 | /** |
||
157 | * Handle resolving generators already done. |
||
158 | * @param GeneratorContainer $gc |
||
159 | * @return PromiseInterface |
||
160 | */ |
||
161 | 27 | private function processGeneratorContainerDone(GeneratorContainer $gc) |
|
189 | |||
190 | /** |
||
191 | * Handle resolving generators still running. |
||
192 | * @param GeneratorContainer $gc |
||
193 | * @return PromiseInterface |
||
194 | */ |
||
195 | 31 | private function processGeneratorContainerRunning(GeneratorContainer $gc) |
|
227 | |||
228 | /** |
||
229 | * Return root wrapper generator. |
||
230 | * @param mixed $throw |
||
231 | * @param mixed $value |
||
232 | * @param mixed &$return |
||
233 | */ |
||
234 | 31 | private function getRootGenerator($throw, $value, &$return) |
|
247 | |||
248 | /** |
||
249 | * Promise all changes in yieldables are prepared. |
||
250 | * @param array $yieldables |
||
251 | * @param bool $throw_acceptable |
||
252 | * @return PromiseInterface |
||
253 | */ |
||
254 | 27 | private function promiseAll(array $yieldables, $throw_acceptable) |
|
279 | |||
280 | /** |
||
281 | * Wrap value with the Generator that returns the first successful result. |
||
282 | * If all yieldables failed, AllFailedException is thrown. |
||
283 | * If no yieldables found, AllFailedException is thrown. |
||
284 | * |
||
285 | * @param mixed $value |
||
286 | * @return \Generator Resolved value. |
||
287 | * @throws AllFailedException |
||
288 | */ |
||
289 | 4 | public static function any($value) |
|
298 | // @codeCoverageIgnoreEnd |
||
299 | |||
300 | /** |
||
301 | * Wrap value with the Generator that returns the first result. |
||
302 | * If no yieldables found, AllFailedException is thrown. |
||
303 | * |
||
304 | * @param mixed $value |
||
305 | * @return \Generator Resolved value. |
||
306 | * @throws \RuntimeException|AllFailedException |
||
307 | */ |
||
308 | 3 | public static function race($value) |
|
317 | // @codeCoverageIgnoreEnd |
||
318 | |||
319 | /** |
||
320 | * Wrap value with the Generator that returns the all results. |
||
321 | * Normally you don't have to use this method, just yield an array that contains yieldables. |
||
322 | * You should use only with Co::race() or Co::any(). |
||
323 | * |
||
324 | * @param mixed $value |
||
325 | * @return \Generator Resolved value. |
||
326 | * @throws \RuntimeException |
||
327 | */ |
||
328 | 1 | public static function all($value) |
|
333 | // @codeCoverageIgnoreEnd |
||
334 | } |
||
335 |