1 | <?php |
||
15 | class LaravelGenerator extends AbstractGenerator |
||
16 | { |
||
17 | /** |
||
18 | * @param Route $route |
||
19 | * |
||
20 | * @return mixed |
||
21 | */ |
||
22 | public function getUri($route) |
||
30 | |||
31 | /** |
||
32 | * @param Route $route |
||
33 | * |
||
34 | * @return mixed |
||
35 | */ |
||
36 | public function getMethods($route) |
||
44 | |||
45 | /** |
||
46 | * @param \Illuminate\Routing\Route $route |
||
47 | * @param array $bindings |
||
48 | * @param array $headers |
||
49 | * @param bool $withResponse |
||
50 | * |
||
51 | * @return array |
||
52 | */ |
||
53 | public function processRoute($route, $bindings = [], $headers = [], $withResponse = true) |
||
100 | |||
101 | /** |
||
102 | * Prepares / Disables route middlewares. |
||
103 | * |
||
104 | * @param bool $disable |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | public function prepareMiddleware($disable = true) |
||
112 | |||
113 | /** |
||
114 | * Call the given URI and return the Response. |
||
115 | * |
||
116 | * @param string $method |
||
117 | * @param string $uri |
||
118 | * @param array $parameters |
||
119 | * @param array $cookies |
||
120 | * @param array $files |
||
121 | * @param array $server |
||
122 | * @param string $content |
||
123 | * |
||
124 | * @return \Illuminate\Http\Response |
||
125 | */ |
||
126 | public function callRoute($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null) |
||
150 | |||
151 | /** |
||
152 | * Get a response from the transformer tags. |
||
153 | * |
||
154 | * @param array $tags |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | protected function getTransformerResponse($tags) |
||
159 | { |
||
160 | try { |
||
161 | $transFormerTags = array_filter($tags, function ($tag) { |
||
162 | if (! ($tag instanceof Tag)) { |
||
163 | return false; |
||
164 | } |
||
165 | |||
166 | return \in_array(\strtolower($tag->getName()), ['transformer', 'transformercollection']); |
||
167 | }); |
||
168 | if (empty($transFormerTags)) { |
||
169 | // we didn't have any of the tags so goodbye |
||
170 | return false; |
||
171 | } |
||
172 | |||
173 | $modelTag = array_first(array_filter($tags, function ($tag) { |
||
174 | if (! ($tag instanceof Tag)) { |
||
175 | return false; |
||
176 | } |
||
177 | |||
178 | return \in_array(\strtolower($tag->getName()), ['transformermodel']); |
||
179 | })); |
||
180 | $tag = \array_first($transFormerTags); |
||
181 | $transformer = $tag->getContent(); |
||
182 | if (! \class_exists($transformer)) { |
||
183 | // if we can't find the transformer we can't generate a response |
||
184 | return; |
||
185 | } |
||
186 | $demoData = []; |
||
187 | |||
188 | $reflection = new ReflectionClass($transformer); |
||
189 | $method = $reflection->getMethod('transform'); |
||
190 | $parameter = \array_first($method->getParameters()); |
||
191 | $type = null; |
||
192 | if ($modelTag) { |
||
193 | $type = $modelTag->getContent(); |
||
194 | } |
||
195 | if (version_compare(PHP_VERSION, '7.0.0') >= 0 && \is_null($type)) { |
||
196 | // we can only get the type with reflection for PHP 7 |
||
197 | if ($parameter->hasType() && |
||
198 | ! $parameter->getType()->isBuiltin() && |
||
199 | \class_exists((string) $parameter->getType())) { |
||
200 | //we have a type |
||
201 | $type = (string) $parameter->getType(); |
||
202 | } |
||
203 | } |
||
204 | if ($type) { |
||
205 | // we have a class so we try to create an instance |
||
206 | $demoData = new $type; |
||
207 | try { |
||
208 | // try a factory |
||
209 | $demoData = \factory($type)->make(); |
||
210 | } catch (\Exception $e) { |
||
211 | if ($demoData instanceof \Illuminate\Database\Eloquent\Model) { |
||
212 | // we can't use a factory but can try to get one from the database |
||
213 | try { |
||
214 | // check if we can find one |
||
215 | $newDemoData = $type::first(); |
||
216 | if ($newDemoData) { |
||
217 | $demoData = $newDemoData; |
||
218 | } |
||
219 | } catch (\Exception $e) { |
||
220 | // do nothing |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | $fractal = new Manager(); |
||
227 | $resource = []; |
||
228 | if ($tag->getName() == 'transformer') { |
||
229 | // just one |
||
230 | $resource = new Item($demoData, new $transformer); |
||
231 | } |
||
232 | if ($tag->getName() == 'transformercollection') { |
||
233 | // a collection |
||
234 | $resource = new Collection([$demoData, $demoData], new $transformer); |
||
235 | } |
||
236 | |||
237 | return \response($fractal->createData($resource)->toJson()); |
||
238 | } catch (\Exception $e) { |
||
239 | // it isn't possible to parse the transformer |
||
240 | return; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param string $route |
||
246 | * @param array $bindings |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | protected function getRouteRules($route, $bindings) |
||
280 | } |
||
281 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.