1 | <?php |
||
13 | class Co implements CoInterface |
||
14 | { |
||
15 | /** |
||
16 | * Instance of myself. |
||
17 | * @var Co |
||
18 | */ |
||
19 | private static $self; |
||
20 | |||
21 | /** |
||
22 | * Options. |
||
23 | * @var CoOption |
||
24 | */ |
||
25 | private $options; |
||
26 | |||
27 | /** |
||
28 | * cURL request pool object. |
||
29 | * @var CURLPool |
||
30 | */ |
||
31 | private $pool; |
||
32 | |||
33 | /** |
||
34 | * Overwrite CoOption default. |
||
35 | * @param array $options |
||
36 | */ |
||
37 | public static function setDefaultOptions(array $options) |
||
41 | |||
42 | /** |
||
43 | * Get CoOption default as array. |
||
44 | * @return array |
||
45 | */ |
||
46 | public static function getDefaultOptions() |
||
50 | |||
51 | /** |
||
52 | * Wait until value is recursively resolved to return it. |
||
53 | * This function call must be atomic. |
||
54 | * @param mixed $value |
||
55 | * @param array $options |
||
56 | * @return mixed |
||
57 | */ |
||
58 | 3 | public static function wait($value, array $options = []) |
|
70 | |||
71 | /** |
||
72 | * Value is recursively resolved, but we never wait it. |
||
73 | * This function must be called along with Co::wait(). |
||
74 | * @param mixed $value |
||
75 | * @param array $options |
||
76 | */ |
||
77 | 1 | public static function async($value, array $options = []) |
|
78 | 1 | { |
|
79 | 1 | if (!self::$self) { |
|
80 | throw new \BadMethodCallException( |
||
81 | 'Co::async() must be called along with Co::wait(). ' . |
||
82 | 'This method is mainly expected to be used in CURLOPT_WRITEFUNCTION callback.' |
||
83 | ); |
||
84 | } |
||
85 | 1 | self::$self->start($value, self::$self->options->reconfigure($options), false); |
|
86 | 1 | } |
|
87 | |||
88 | /** |
||
89 | * External instantiation is forbidden. |
||
90 | */ |
||
91 | private function __construct() {} |
||
92 | |||
93 | /** |
||
94 | * Start resovling. |
||
95 | * @param mixed $value |
||
96 | * @param CoOption $options |
||
97 | * @param bool $wait |
||
98 | * @param mixed If $wait, return resolved value. |
||
99 | */ |
||
100 | 3 | private function start($value, CoOption $options, $wait = true) |
|
101 | 3 | { |
|
102 | 3 | $this->options = $options; |
|
103 | 3 | $this->pool = new CURLPool($options); |
|
104 | // If $wait, final result is stored into referenced $return |
||
105 | 3 | if ($wait) { |
|
106 | 3 | $deferred = new Deferred; |
|
107 | $deferred->promise()->done(function ($r) use (&$return) { |
||
1 ignored issue
–
show
|
|||
108 | 3 | $return = $r; |
|
109 | 3 | }); |
|
110 | } |
||
111 | // For convenience, all values are wrapped into generator |
||
112 | $genfunc = function () use ($value) { |
||
113 | 3 | yield CoInterface::RETURN_WITH => (yield $value); |
|
114 | 3 | }; |
|
115 | 3 | $con = Utils::normalize($genfunc, $options); |
|
116 | // We have to provide deferred object only if $wait |
||
117 | 3 | $this->processGeneratorContainer($con, $wait ? $deferred : null); |
|
1 ignored issue
–
show
|
|||
118 | // We have to wait $return only if $wait |
||
119 | 3 | if ($wait) { |
|
120 | 3 | $this->pool->wait(); |
|
121 | 3 | return $return; |
|
1 ignored issue
–
show
|
|||
122 | } |
||
123 | 1 | } |
|
124 | |||
125 | /** |
||
126 | * Handle resolving generators. |
||
127 | * @param GeneratorContainer $gc |
||
128 | * @param Deferred $deferred |
||
129 | */ |
||
130 | 3 | private function processGeneratorContainer(GeneratorContainer $gc, Deferred $deferred = null) |
|
192 | |||
193 | /** |
||
194 | * Return function that apply changes in yieldables. |
||
195 | * @param mixed $yielded |
||
196 | * @param array $yieldables |
||
197 | * @param callable $next |
||
198 | */ |
||
199 | 2 | private static function getApplier($yielded, $yieldables, callable $next) |
|
212 | |||
213 | /** |
||
214 | * Promise all changes in yieldables are prepared. |
||
215 | * @param arrya $yieldables |
||
216 | * @param bool $throw_acceptable |
||
217 | * @return PromiseInterface |
||
218 | */ |
||
219 | 2 | private function promiseAll($yieldables, $throw_acceptable) |
|
248 | } |
||
249 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: