1 | <?php |
||
2 | |||
3 | use Illuminate\Support\Facades\Route; |
||
4 | use VGirol\JsonApi\Controllers\JsonApiController; |
||
5 | |||
6 | // Route macro |
||
7 | Route::macro( |
||
0 ignored issues
–
show
|
|||
8 | 'jsonApiGet', |
||
9 | function (string $uri, $action = null, $options = []) { |
||
10 | $route = static::get($uri, $action); |
||
11 | |||
12 | if (empty($options)) { |
||
13 | $route->middleware('jsonapi'); |
||
14 | } else { |
||
15 | $router = app('router'); |
||
16 | $middlewares = $router->getMiddlewareGroups()['jsonapi']; |
||
17 | |||
18 | if (array_key_exists('except', $options)) { |
||
19 | $except = is_array($options['except']) ? $options['except'] : [$options['except']]; |
||
20 | $middlewares = array_diff($middlewares, $except); |
||
21 | } |
||
22 | if (array_key_exists('only', $options)) { |
||
23 | $only = is_array($options['only']) ? $options['only'] : [$options['only']]; |
||
24 | $middlewares = array_intersect($middlewares, $only); |
||
25 | } |
||
26 | if (array_key_exists('add', $options)) { |
||
27 | $add = is_array($options['add']) ? $options['add'] : [$options['add']]; |
||
28 | $middlewares = array_merge($middlewares, $add); |
||
29 | } |
||
30 | |||
31 | $route->middleware($middlewares); |
||
32 | } |
||
33 | } |
||
34 | ); |
||
35 | |||
36 | Route::macro( |
||
37 | 'jsonApiResource', |
||
38 | function (string $name, ?string $controller = null, array $options = []) { |
||
39 | $controller = static::jsonApiGetController($controller); |
||
40 | |||
41 | if (!isset($options['parameters'])) { |
||
42 | $options['parameters'] = [$name => 'id']; |
||
43 | } |
||
44 | |||
45 | static::apiResource($name, $controller, $options)->middleware('jsonapi'); |
||
46 | |||
47 | foreach (array_keys($options) as $opt) { |
||
48 | $fn = 'jsonApi' . ucfirst(strtolower($opt)); |
||
49 | if (static::hasMacro($fn)) { |
||
50 | call_user_func([Route::class, $fn], $name, $controller, $options); |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 | ); |
||
55 | |||
56 | Route::macro( |
||
57 | 'jsonApiRelationships', |
||
58 | function (string $name, ?string $controller = null, array $options = []) { |
||
59 | if (!isset($options['relationships']) || ($options['relationships'] === true)) { |
||
60 | $options['relationships'] = []; |
||
61 | } |
||
62 | if ($options['relationships'] === false) { |
||
63 | return; |
||
64 | } |
||
65 | |||
66 | $controller = static::jsonApiGetController($controller); |
||
67 | |||
68 | static::group(['middleware' => 'jsonapi'], function () use ($name, $controller) { |
||
69 | // TODO : |
||
70 | // if $options['relationships'] is true or empty array -> create generic routes |
||
71 | // else -> create routes only for relationships provided in $options['relationships'] |
||
72 | |||
73 | // Relationships |
||
74 | static::name("{$name}.relationship.index")->get( |
||
75 | "{$name}/{parentId}/relationships/{relationship}", |
||
76 | "{$controller}@relationshipIndex" |
||
77 | ); |
||
78 | static::name("{$name}.relationship.store")->post( |
||
79 | "{$name}/{parentId}/relationships/{relationship}", |
||
80 | "{$controller}@relationshipStore" |
||
81 | ); |
||
82 | static::name("{$name}.relationship.update")->match( |
||
83 | ['put', 'patch'], |
||
84 | "{$name}/{parentId}/relationships/{relationship}", |
||
85 | "{$controller}@relationshipUpdate" |
||
86 | ); |
||
87 | static::name("{$name}.relationship.destroy")->delete( |
||
88 | "{$name}/{parentId}/relationships/{relationship}", |
||
89 | "{$controller}@relationshipDestroy" |
||
90 | ); |
||
91 | |||
92 | // Related resources |
||
93 | static::name("{$name}.related.index")->get( |
||
94 | "{$name}/{parentId}/{relationship}", |
||
95 | "{$controller}@relatedIndex" |
||
96 | ); |
||
97 | static::name("{$name}.related.store")->post( |
||
98 | "{$name}/{parentId}/{relationship}", |
||
99 | "{$controller}@relatedStore" |
||
100 | ); |
||
101 | static::name("{$name}.related.show")->get( |
||
102 | "{$name}/{parentId}/{relationship}/{id}", |
||
103 | "{$controller}@relatedShow" |
||
104 | ); |
||
105 | static::name("{$name}.related.update")->match( |
||
106 | ['put', 'patch'], |
||
107 | "{$name}/{parentId}/{relationship}/{id}", |
||
108 | "{$controller}@relatedUpdate" |
||
109 | ); |
||
110 | static::name("{$name}.related.destroy")->delete( |
||
111 | "{$name}/{parentId}/{relationship}/{id?}", |
||
112 | "{$controller}@relatedDestroy" |
||
113 | ); |
||
114 | }); |
||
115 | } |
||
116 | ); |
||
117 | |||
118 | Route::macro( |
||
119 | 'jsonApiGetController', |
||
120 | function (string $controller = null): string { |
||
121 | return $controller ?? '\\' . JsonApiController::class; |
||
122 | } |
||
123 | ); |
||
124 |
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.