-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathParameters.php
197 lines (178 loc) · 6.71 KB
/
Parameters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Efficiently\AuthorityController;
use Event;
// TODO: Move this class in its own Laravel package
class Parameters
{
protected $params = [];
/**
* Fill the $params property of the given Controller
*
* @param \Illuminate\Routing\Controller $controller
*/
public function fillController($controller)
{
$router = app('router');
$controllerClass = get_classname($controller);
$paramsFilterPrefix = "router.filter: ";
$paramsFilterName = "controller.parameters.".$controllerClass;
if (! Event::hasListeners($paramsFilterPrefix.$paramsFilterName)) {
Event::listen($paramsFilterPrefix.$paramsFilterName, function () use ($controller, $router) {
$currentRoute = $router->current();
$resourceParams = [];
list($resourceParams['controller'], $resourceParams['action']) = explode('@', $router->currentRouteAction());
$resourceParams['controller'] = $this->normalizeControllerName($resourceParams['controller']);
$resourceId = str_singular($resourceParams['controller']);
if (request()->has($resourceId)) {
$params = request()->all();
} else {
$specialInputKeys = $this->specialInputKeys();
$params = [$resourceId => request()->except($specialInputKeys)] + request()->only($specialInputKeys);
}
$routeParams = $currentRoute->parametersWithoutNulls();
// In Laravel, unlike Rails, by default 'id' parameter of a 'Product' resource is 'products'
// And 'shop_id' parameter of a 'Shop' parent resource is 'shops'
// So we need to reaffect correct parameter name before any controller's actions or filters.
$routeParamsParsed = [];
$keysToRemove = [];
$lastRouteParamKey = last(array_keys($routeParams));
if ($lastRouteParamKey === 'id' || $resourceId === str_singular($lastRouteParamKey)) {
$id = last($routeParams);
if (is_a($id, 'Illuminate\Database\Eloquent\Model')) {
$object = $id;
$id = $id->getKey();
}
if (is_string($id) || is_numeric($id)) {
array_pop($routeParams);
$routeParamsParsed[$object->getKeyName()] = $id;
}
}
foreach ($routeParams as $parentIdKey => $parentIdValue) {
if (is_a($parentIdValue, 'Illuminate\Database\Eloquent\Model')) {
$parentIdValue = $parentIdValue->getKey();
}
if (is_string($parentIdValue) || is_numeric($parentIdValue)) {
if (! ends_with($parentIdKey, '_id')) {
$parentIdKey = str_singular($parentIdKey).'_id';
}
$routeParamsParsed[$parentIdKey] = $parentIdValue;
$keysToRemove[] = $parentIdKey;
}
}
$routeParams = array_except($routeParams, $keysToRemove);
/**
* You can escape or purify these parameters. For example:
*
* class ProductsController extends Controller
* {
* public function __construct()
* {
* $self = $this;
* $this->beforeFilter(function () use($self) {
* if (array_get($self->params, 'product')) {
* $productParams = $this->yourPurifyOrEscapeMethod('product');
* $self->params['product'] = $productParams;
* }
* });
* }
* }
*
*/
$this->params = array_filter(array_merge($params, $routeParams, $routeParamsParsed, $resourceParams));
if (property_exists($controller, 'params')) {
set_property($controller, 'params', $this->params);
} else {
$controller->params = $this->params;
}
});
$controller->paramsBeforeFilter($paramsFilterName);
}
}
/**
* Get an item from the parameters.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
return array_get($this->params, $key, $default);
}
/**
* Determine if the request contains a given parameter item.
*
* @param string|array $key
* @return bool
*/
public function has($key)
{
return !!array_get($this->params, $key);
}
/**
* Get all of the parameters for the request.
*
* @return array
*/
public function all()
{
return $this->params;
}
/**
* Get a subset of the items from the parameters.
*
* @param array $keys
* @return array
*/
public function only($keys = null)
{
$keys = is_array($keys) ? $keys : func_get_args();
return array_only($this->params, $keys);
}
/**
* Get all of the input except for a specified array of items.
*
* @param array $keys
* @return array
*/
public function except($keys = null)
{
$keys = is_array($keys) ? $keys : func_get_args();
return array_except($this->params, $keys);
}
/**
* Adds an item to the parameters.
*
* @param string $key Key to add value to.
* @param mixed $value New data.
*
* @return mixed
*/
public function add($key, $value)
{
return array_set($this->params, $key, $value);
}
/**
* Returns all inputs keys who starts with an underscore character (<code>_</code>).
* For exmaple '_method' and '_token' inputs
*
* @param array $inputKeys
* @return array
*/
protected function specialInputKeys($inputKeys = [])
{
$inputKeys = $inputKeys ?: array_keys(request()->all());
return array_filter($inputKeys, function ($value) {
return is_string($value) ? starts_with($value, '_') : false;
});
}
/**
* @param string $controller
* @return string
*/
protected function normalizeControllerName($controller)
{
$name = preg_replace("/^(.+)Controller$/", "$1", $controller);
return str_plural(snake_case(class_basename($name)));
}
}