1 | <?php |
||
12 | class Co implements CoInterface |
||
13 | { |
||
14 | /** |
||
15 | * Instance of myself. |
||
16 | * @var Co |
||
17 | */ |
||
18 | private static $self; |
||
19 | |||
20 | /** |
||
21 | * Options. |
||
22 | * @var CoOption |
||
23 | */ |
||
24 | private $options; |
||
25 | |||
26 | /** |
||
27 | * cURL request pool object. |
||
28 | * @var Pool |
||
29 | */ |
||
30 | private $pool; |
||
31 | |||
32 | /** |
||
33 | * Running cURL or Generator identifiers. |
||
34 | * @var array |
||
35 | */ |
||
36 | private $runners = []; |
||
37 | |||
38 | /** |
||
39 | * Overwrite CoOption default. |
||
40 | * @param array $options |
||
41 | */ |
||
42 | 1 | public static function setDefaultOptions(array $options) |
|
46 | |||
47 | /** |
||
48 | * Get CoOption default as array. |
||
49 | * @return array |
||
50 | */ |
||
51 | 1 | public static function getDefaultOptions() |
|
55 | |||
56 | /** |
||
57 | * Wait until value is recursively resolved to return it. |
||
58 | * This function call must be atomic. |
||
59 | * @param mixed $value |
||
60 | * @param array $options |
||
61 | * @return mixed |
||
62 | */ |
||
63 | 22 | public static function wait($value, array $options = []) |
|
64 | 22 | { |
|
65 | try { |
||
66 | 22 | if (self::$self) { |
|
67 | 1 | throw new \BadMethodCallException('Co::wait() is already running. Use Co::async() instead.'); |
|
68 | } |
||
69 | 22 | self::$self = new self; |
|
70 | 22 | self::$self->options = new CoOption($options); |
|
71 | 22 | self::$self->pool = new Pool(self::$self->options); |
|
72 | 22 | return self::$self->start($value); |
|
73 | } finally { |
||
74 | 22 | self::$self = null; |
|
75 | } |
||
76 | // @codeCoverageIgnoreStart |
||
77 | } |
||
78 | // @codeCoverageIgnoreEnd |
||
79 | |||
80 | /** |
||
81 | * Value is recursively resolved, but we never wait it. |
||
82 | * This function must be called along with Co::wait(). |
||
83 | * @param mixed $value |
||
84 | * @param mixed $throw |
||
85 | */ |
||
86 | 6 | public static function async($value, $throw = null) |
|
87 | 6 | { |
|
88 | 6 | if (!self::$self) { |
|
89 | 1 | throw new \BadMethodCallException('Co::async() must be called along with Co::wait(). '); |
|
90 | } |
||
91 | 5 | if ($throw !== null) { |
|
92 | 3 | $throw = filter_var($throw, FILTER_VALIDATE_BOOLEAN, [ |
|
93 | 3 | 'flags' => FILTER_NULL_ON_FAILURE, |
|
94 | ]); |
||
95 | 3 | if ($throw === null) { |
|
96 | 1 | throw new \InvalidArgumentException("\$throw must be null or boolean."); |
|
97 | } |
||
98 | } |
||
99 | 4 | self::$self->start($value, false, $throw); |
|
100 | 4 | } |
|
101 | |||
102 | /** |
||
103 | * External instantiation is forbidden. |
||
104 | */ |
||
105 | private function __construct() {} |
||
106 | |||
107 | /** |
||
108 | * Start resovling. |
||
109 | * @param mixed $value |
||
110 | * @param bool $wait |
||
111 | * @param mixed $throw Used for Co::async() overrides. |
||
112 | * @param mixed If $wait, return resolved value. |
||
113 | */ |
||
114 | 22 | private function start($value, $wait = true, $throw = null) |
|
128 | |||
129 | /** |
||
130 | * Handle resolving generators. |
||
131 | * @param GeneratorContainer $gc |
||
132 | * @param Deferred $deferred |
||
133 | */ |
||
134 | 18 | private function processGeneratorContainer(GeneratorContainer $gc, Deferred $deferred) |
|
140 | |||
141 | /** |
||
142 | * Handle resolving generators already done. |
||
143 | * @param GeneratorContainer $gc |
||
144 | * @param Deferred $deferred |
||
145 | */ |
||
146 | 15 | private function processGeneratorContainerDone(GeneratorContainer $gc, Deferred $deferred) |
|
175 | |||
176 | /** |
||
177 | * Handle resolving generators still running. |
||
178 | * @param GeneratorContainer $gc |
||
179 | * @param Deferred $deferred |
||
180 | */ |
||
181 | 22 | private function processGeneratorContainerRunning(GeneratorContainer $gc, Deferred $deferred) |
|
221 | |||
222 | /** |
||
223 | * Return root wrapper generator. |
||
224 | * @param mixed $throw |
||
225 | * @param mixed $value |
||
226 | * @param mixed &$return |
||
227 | */ |
||
228 | 22 | private function getRootGenerator($throw, $value, &$return) |
|
241 | |||
242 | /** |
||
243 | * Return function that apply changes in yieldables. |
||
244 | * @param mixed $yielded |
||
245 | * @param array $yieldables |
||
246 | * @param callable $next |
||
247 | */ |
||
248 | 15 | private static function getApplier($yielded, array $yieldables, callable $next) |
|
262 | |||
263 | /** |
||
264 | * Promise all changes in yieldables are prepared. |
||
265 | * @param array $yieldables |
||
266 | * @param bool $throw_acceptable |
||
267 | * @return PromiseInterface |
||
268 | */ |
||
269 | 18 | private function promiseAll(array $yieldables, $throw_acceptable) |
|
293 | |||
294 | /** |
||
295 | * Return Deferred that absorbs rejects. |
||
296 | * @param Deferred $original_dfd |
||
297 | * @return Deferred |
||
298 | */ |
||
299 | 10 | private static function safeDeferred(Deferred $original_dfd) |
|
308 | } |
||
309 |