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(string $name, string $controller, array $options) |
53
|
|
|
{ |
54
|
|
|
// Map non-associative members as keys, with default relationship actions |
55
|
|
|
$relationships = collect(array_get($options, 'relationships', [])) |
56
|
|
|
->mapWithKeys(function ($methods, $relationship) { |
57
|
|
|
return is_numeric($relationship) |
58
|
|
|
? [$methods => $this->relationshipDefaults] |
59
|
|
|
: [$relationship => (array) $methods]; |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
foreach (['show', 'update'] as $action) { |
63
|
|
|
$matched = (array) $relationships->filter(function ($methods) use ($action) { |
64
|
|
|
return in_array($action, $methods); |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
$this->{'addRelationship' . ucfirst($action)}($name, $base, $matched, $controller, $options); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Add a relationship show method to match named relationships on the resource. |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @param string $base |
76
|
|
|
* @param array $relationships |
77
|
|
|
* @param string $controller |
78
|
|
|
* @param array $options |
79
|
|
|
* |
80
|
|
|
* @return \Illuminate\Routing\Route |
81
|
|
|
*/ |
82
|
|
|
protected function addRelationshipShow(string $name, string $base, array $relationships, string $controller, array $options) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$uri = $this->getRelationshipUri($name, $base); |
85
|
|
|
$action = $this->getResourceAction($name, $controller, 'showRelationship', $options); |
86
|
|
|
|
87
|
|
|
return $this->router->get($uri, $action) |
88
|
|
|
->where('relationship', '(' . implode(')|(', $relationships) . ')'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add a relationship update method to match named relationships on the resource. |
93
|
|
|
* |
94
|
|
|
* @param string $name |
95
|
|
|
* @param string $base |
96
|
|
|
* @param array $relationships |
97
|
|
|
* @param string $controller |
98
|
|
|
* @param array $options |
99
|
|
|
* |
100
|
|
|
* @return \Illuminate\Routing\Route |
101
|
|
|
*/ |
102
|
|
|
protected function addRelationshipUpdate(string $name, string $base, array $relationships, string $controller, array $options) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$uri = $this->getRelationshipUri($name, $base); |
105
|
|
|
$action = $this->getResourceAction($name, $relationships, $controller, 'updateRelationship', $options); |
|
|
|
|
106
|
|
|
|
107
|
|
|
return $this->router->match(['PUT', 'PATCH'], $uri, $action) |
108
|
|
|
->where('relationship', '(' . implode(')|(', $relationships) . ')'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the URI for resource relationships. |
113
|
|
|
* |
114
|
|
|
* @param string $name |
115
|
|
|
* @param string $base |
116
|
|
|
*/ |
117
|
|
|
public function getRelationshipUri(string $name, string $base): string |
118
|
|
|
{ |
119
|
|
|
return sprintf('%s/{%s}/relationships/{relationship}', $this->getResourceUri($name), $base); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Format a resource parameter for usage. |
124
|
|
|
* |
125
|
|
|
* @param string $value |
126
|
|
|
*/ |
127
|
|
|
public function getResourceWildcard($value): string |
128
|
|
|
{ |
129
|
|
|
return camel_case(parent::getResourceWildcard($value)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.