1 | <?php |
||
14 | trait AsyncTable |
||
15 | { |
||
16 | /** |
||
17 | * @var Pool |
||
18 | */ |
||
19 | private $pool; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $tableName; |
||
25 | |||
26 | /** |
||
27 | * @var AnnotationReader |
||
28 | */ |
||
29 | private $annotationReader; |
||
30 | |||
31 | /** |
||
32 | * @var \ReflectionClass |
||
33 | */ |
||
34 | private $reflectionClass; |
||
35 | |||
36 | /** |
||
37 | * @param Pool $pool |
||
38 | * @param string $tableName |
||
39 | */ |
||
40 | public function setUpAsyncTable(Pool $pool, $tableName, $tableClass) |
||
47 | |||
48 | /** |
||
49 | * @param $function |
||
50 | * @param array $arguments |
||
51 | * @return PromiseInterface |
||
52 | */ |
||
53 | protected function callAsyncOrSync($function, $arguments) |
||
54 | { |
||
55 | if ($this->pool === null) { |
||
56 | return (new $this->tableName)->$function(...$arguments); |
||
57 | } |
||
58 | |||
59 | if ( |
||
60 | $this->returnsQuery($function) || |
||
61 | $this->hasMethodAnnotation($function, Async::class) || |
||
62 | ( |
||
63 | $this->hasClassAnnotation(Async::class) && |
||
64 | $this->hasNoMethodAnnotation($function) |
||
65 | ) || |
||
66 | strpos(strtolower($function), 'save') === 0 || |
||
67 | strpos(strtolower($function), 'find') === 0 || |
||
68 | strpos(strtolower($function), 'fetch') === 0 || |
||
69 | strpos(strtolower($function), 'retrieve') === 0 |
||
70 | ) { |
||
71 | return $this->callAsync($function, $arguments); |
||
72 | } |
||
73 | |||
74 | return $this->callSync($function, $arguments); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param $function |
||
79 | * @param array $arguments |
||
80 | * @return PromiseInterface |
||
81 | */ |
||
82 | private function callSync($function, array $arguments = []) |
||
83 | { |
||
84 | $table = TableRegistry::get(md5($this->tableName), [ |
||
85 | 'className' => $this->tableName, |
||
86 | 'table' => 'screenshots', |
||
87 | ]); |
||
88 | if (isset(class_uses($table)[TableRegistryTrait::class])) { |
||
89 | $table->setRegistry(AsyncTableRegistry::class); |
||
90 | } |
||
91 | return \React\Promise\resolve( |
||
92 | call_user_func_array( |
||
93 | [ |
||
94 | $table, |
||
95 | $function |
||
96 | ], |
||
97 | $arguments |
||
98 | ) |
||
99 | ); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param $function |
||
104 | * @param array $arguments |
||
105 | * @return PromiseInterface |
||
106 | */ |
||
107 | private function callAsync($function, array $arguments = []) |
||
108 | { |
||
109 | $unSerialize = function ($input) { |
||
110 | if (is_string($input)) { |
||
111 | return unserialize($input); |
||
112 | } |
||
113 | |||
114 | return $input; |
||
115 | }; |
||
116 | return $this-> |
||
117 | pool-> |
||
118 | call($this->tableName, $function, $arguments)-> |
||
119 | then($unSerialize, $unSerialize, $unSerialize); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param $class |
||
124 | * @return bool |
||
125 | */ |
||
126 | private function hasClassAnnotation($class) |
||
130 | |||
131 | /** |
||
132 | * @param $method |
||
133 | * @param $class |
||
134 | * @return bool |
||
135 | */ |
||
136 | private function hasMethodAnnotation($method, $class) |
||
141 | |||
142 | /** |
||
143 | * @param $method |
||
144 | * @return bool |
||
145 | */ |
||
146 | private function hasNoMethodAnnotation($method) |
||
147 | { |
||
148 | $methodReflection = $this->reflectionClass->getMethod($method); |
||
149 | return ( |
||
150 | $this->annotationReader->getMethodAnnotation($methodReflection, Async::class) === null && |
||
151 | $this->annotationReader->getMethodAnnotation($methodReflection, Sync::class) === null |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param $function |
||
157 | * @return bool |
||
158 | */ |
||
159 | private function returnsQuery($function) |
||
160 | { |
||
161 | $docBlockContents = $this->reflectionClass->getMethod($function)->getDocComment(); |
||
162 | if (!is_string($docBlockContents)) { |
||
163 | return false; |
||
164 | } |
||
165 | |||
166 | $docBlock = $this->getDocBlock($docBlockContents); |
||
167 | foreach ($docBlock->getTags() as $tag) { |
||
168 | if ($tag->getName() === 'return' && ltrim($tag->getType(), '\\') == Query::class) { |
||
|
|||
169 | return true; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | return false; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param $docBlockContents |
||
178 | * @return DocBlock |
||
179 | */ |
||
180 | private function getDocBlock($docBlockContents) |
||
188 | } |
||
189 |
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: