1 | <?php |
||
14 | class AsyncTable |
||
15 | { |
||
16 | /** |
||
17 | * @var Pool |
||
18 | */ |
||
19 | protected $pool; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $tableName; |
||
25 | |||
26 | /** |
||
27 | * @var AnnotationReader |
||
28 | */ |
||
29 | protected $annotationReader; |
||
30 | |||
31 | /** |
||
32 | * @var \ReflectionClass |
||
33 | */ |
||
34 | protected $reflectionClass; |
||
35 | |||
36 | /** |
||
37 | * @param Pool $pool |
||
38 | * @param string $tableName |
||
39 | */ |
||
40 | 15 | public function __construct(Pool $pool, $tableName, $tableClass) |
|
47 | |||
48 | /** |
||
49 | * @param $function |
||
50 | * @param array $arguments |
||
51 | * @return PromiseInterface |
||
52 | */ |
||
53 | 14 | public function __call($function, array $arguments = []) |
|
54 | { |
||
55 | 1 | if ( |
|
56 | 14 | $this->returnsQuery($function) || |
|
57 | 12 | $this->hasMethodAnnotation($function, Async::class) || |
|
58 | ( |
||
59 | 9 | $this->hasClassAnnotation(Async::class) && |
|
60 | 2 | $this->hasNoMethodAnnotation($function) |
|
61 | 9 | ) || |
|
62 | 8 | strpos(strtolower($function), 'save') === 0 || |
|
63 | 8 | strpos(strtolower($function), 'find') === 0 || |
|
64 | 7 | strpos(strtolower($function), 'fetch') === 0 || |
|
65 | 8 | strpos(strtolower($function), 'retrieve') === 0 |
|
66 | 14 | ) { |
|
67 | 8 | return $this->callAsync($function, $arguments); |
|
68 | } |
||
69 | |||
70 | 6 | return $this->callSync($function, $arguments); |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param $function |
||
75 | * @param array $arguments |
||
76 | * @return PromiseInterface |
||
77 | */ |
||
78 | 1 | protected function callSync($function, array $arguments = []) |
|
79 | { |
||
80 | $table = TableRegistry::get($this->tableName); |
||
81 | if (isset(class_uses($table)[TableRegistryTrait::class])) { |
||
82 | $table->setRegistry(AsyncTableRegistry::class); |
||
83 | } |
||
84 | 1 | return \React\Promise\resolve( |
|
85 | call_user_func_array( |
||
86 | [ |
||
87 | 1 | $table, |
|
88 | $function |
||
89 | ], |
||
90 | $arguments |
||
91 | ) |
||
92 | ); |
||
93 | 1 | } |
|
94 | |||
95 | /** |
||
96 | * @param $function |
||
97 | * @param array $arguments |
||
98 | * @return PromiseInterface |
||
99 | */ |
||
100 | protected function callAsync($function, array $arguments = []) |
||
101 | { |
||
102 | $unSerialize = function ($input) { |
||
103 | if (is_string($input)) { |
||
104 | return unserialize($input); |
||
105 | } |
||
106 | |||
107 | return $input; |
||
108 | }; |
||
109 | return $this-> |
||
110 | pool-> |
||
111 | call($this->tableName, $function, $arguments)-> |
||
112 | then($unSerialize, $unSerialize, $unSerialize); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param $class |
||
117 | * @return bool |
||
118 | */ |
||
119 | 9 | protected function hasClassAnnotation($class) |
|
123 | |||
124 | /** |
||
125 | * @param $method |
||
126 | * @param $class |
||
127 | * @return bool |
||
128 | */ |
||
129 | 12 | protected function hasMethodAnnotation($method, $class) |
|
134 | |||
135 | /** |
||
136 | * @param $method |
||
137 | * @return bool |
||
138 | */ |
||
139 | 2 | protected function hasNoMethodAnnotation($method) |
|
147 | |||
148 | /** |
||
149 | * @param $function |
||
150 | * @return bool |
||
151 | */ |
||
152 | 14 | protected function returnsQuery($function) |
|
168 | |||
169 | /** |
||
170 | * @param $docBlockContents |
||
171 | * @return DocBlock |
||
172 | */ |
||
173 | 8 | protected function getDocBlock($docBlockContents) |
|
181 | } |
||
182 |
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: