1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Huntie\JsonApi\Routing; |
4
|
|
|
|
5
|
|
|
class ResourceRegistrar extends \Illuminate\Routing\ResourceRegistrar |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The default actions for a resourceful controller. |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $resourceDefaults = ['index', 'store', 'show', 'update', 'destroy']; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The default actions for each named resource relationship. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $relationshipDefaults = ['show']; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create a new resource registrar instance. |
23
|
|
|
* |
24
|
|
|
* @param \Huntie\JsonApi\Routing\Router $router |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Router $router) |
27
|
|
|
{ |
28
|
|
|
$this->router = $router; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Route a resource to a controller. |
33
|
|
|
* |
34
|
|
|
* @param string $name |
35
|
|
|
* @param string $controller |
36
|
|
|
* @param array $options |
37
|
|
|
*/ |
38
|
|
|
public function register($name, $controller, array $options = []) |
39
|
|
|
{ |
40
|
|
|
parent::register($name, $controller, $options); |
41
|
|
|
|
42
|
|
|
$this->registerRelationships($name, $controller, $options); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Route any resource relationships to a controller. |
47
|
|
|
* |
48
|
|
|
* @param string $name |
49
|
|
|
* @param string $controller |
50
|
|
|
* @param array $options |
51
|
|
|
*/ |
52
|
|
|
protected function registerRelationships($name, $controller, array $options) |
53
|
|
|
{ |
54
|
|
|
$base = $this->getResourceWildcard(last(explode('.', $name))); |
55
|
|
|
|
56
|
|
|
// Map non-associative members as keys, with default relationship actions |
57
|
|
|
$relationships = collect(array_get($options, 'relationships', [])) |
58
|
|
|
->mapWithKeys(function ($methods, $relationship) { |
59
|
|
|
return is_numeric($relationship) |
60
|
|
|
? [$methods => $this->relationshipDefaults] |
61
|
|
|
: [$relationship => (array) $methods]; |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
foreach (['show', 'update'] as $action) { |
65
|
|
|
$matched = $relationships->filter(function ($methods) use ($action) { |
66
|
|
|
return in_array($action, $methods); |
67
|
|
|
})->keys()->toArray(); |
68
|
|
|
|
69
|
|
|
$this->{'addRelationship' . ucfirst($action)}($name, $base, $matched, $controller, $options); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add a relationship show method to match named relationships on the resource. |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
* @param string $base |
78
|
|
|
* @param array $relationships |
79
|
|
|
* @param string $controller |
80
|
|
|
* @param array $options |
81
|
|
|
* |
82
|
|
|
* @return \Illuminate\Routing\Route |
83
|
|
|
*/ |
84
|
|
|
protected function addRelationshipShow($name, $base, array $relationships, $controller, array $options) |
85
|
|
|
{ |
86
|
|
|
$uri = $this->getRelationshipUri($name, $base); |
87
|
|
|
$action = $this->getResourceAction($name, $controller, 'showRelationship', $options); |
88
|
|
|
|
89
|
|
|
return $this->router->get($uri, $action) |
90
|
|
|
->where('relationship', '(' . implode(')|(', $relationships) . ')'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Add a relationship update method to match named relationships on the resource. |
95
|
|
|
* |
96
|
|
|
* @param string $name |
97
|
|
|
* @param string $base |
98
|
|
|
* @param array $relationships |
99
|
|
|
* @param string $controller |
100
|
|
|
* @param array $options |
101
|
|
|
* |
102
|
|
|
* @return \Illuminate\Routing\Route |
103
|
|
|
*/ |
104
|
|
|
protected function addRelationshipUpdate($name, $base, array $relationships, $controller, array $options) |
105
|
|
|
{ |
106
|
|
|
$uri = $this->getRelationshipUri($name, $base); |
107
|
|
|
$action = $this->getResourceAction($name, $relationships, $controller, 'updateRelationship', $options); |
|
|
|
|
108
|
|
|
|
109
|
|
|
return $this->router->match(['PUT', 'PATCH'], $uri, $action) |
110
|
|
|
->where('relationship', '(' . implode(')|(', $relationships) . ')'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the URI for resource relationships. |
115
|
|
|
* |
116
|
|
|
* @param string $name |
117
|
|
|
* @param string $base |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getRelationshipUri($name, $base) |
122
|
|
|
{ |
123
|
|
|
return sprintf('%s/{%s}/relationships/{relationship}', $this->getResourceUri($name), $base); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Format a resource parameter for usage. |
128
|
|
|
* |
129
|
|
|
* @param string $value |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getResourceWildcard($value) |
134
|
|
|
{ |
135
|
|
|
if ($this->parameters === 'singular' || static::$singularParameters) { |
136
|
|
|
$value = str_singular($value); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (isset($this->parameters[$value])) { |
140
|
|
|
return $this->parameters[$value]; |
141
|
|
|
} else if (isset(static::$parameterMap[$value])) { |
142
|
|
|
return static::$parameterMap[$value]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return camel_case($value); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: