1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Services; |
4
|
|
|
|
5
|
|
|
use Akibatech\Crud\Exceptions\InvalidRouteIdentifierException; |
6
|
|
|
use Illuminate\Routing\Router; |
7
|
|
|
use Illuminate\Support\Facades\Route; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CrudManager |
11
|
|
|
* |
12
|
|
|
* @package Akibatech\Crud\Services |
13
|
|
|
*/ |
14
|
|
|
class CrudManager |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name = 'Entry'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $pluralized_name = 'Entries'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var null|CrudEntry |
28
|
|
|
*/ |
29
|
|
|
protected $entry; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $route_uri_prefix = 'crud/'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $route_name_prefix = 'crud.'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $controller; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $per_page = 25; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $routes = [ |
55
|
|
|
'index' => [ |
56
|
|
|
'method' => 'GET', |
57
|
|
|
'uri' => 'index', |
58
|
|
|
'as' => 'index', |
59
|
|
|
'action' => 'index' |
60
|
|
|
], |
61
|
|
|
'create' => [ |
62
|
|
|
'method' => 'GET', |
63
|
|
|
'uri' => 'create', |
64
|
|
|
'as' => 'create', |
65
|
|
|
'action' => 'create' |
66
|
|
|
], |
67
|
|
|
'store' => [ |
68
|
|
|
'method' => 'POST', |
69
|
|
|
'uri' => 'store', |
70
|
|
|
'as' => 'store', |
71
|
|
|
'action' => 'store' |
72
|
|
|
], |
73
|
|
|
'edit' => [ |
74
|
|
|
'method' => 'GET', |
75
|
|
|
'uri' => 'edit/{id}', |
76
|
|
|
'as' => 'edit', |
77
|
|
|
'action' => 'edit' |
78
|
|
|
], |
79
|
|
|
'update' => [ |
80
|
|
|
'method' => 'PUT', |
81
|
|
|
'uri' => 'update/{id}', |
82
|
|
|
'as' => 'update', |
83
|
|
|
'action' => 'update' |
84
|
|
|
], |
85
|
|
|
'destroy' => [ |
86
|
|
|
'method' => 'GET', |
87
|
|
|
'uri' => 'destroy/{id}/{csrf}', |
88
|
|
|
'as' => 'destroy', |
89
|
|
|
'action' => 'destroy' |
90
|
|
|
], |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Make staticly a new instance. |
95
|
|
|
* |
96
|
|
|
* @param void |
97
|
|
|
* @return CrudManager |
98
|
|
|
*/ |
99
|
|
|
public static function make() |
100
|
|
|
{ |
101
|
|
|
return (new static); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param void |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getName() |
109
|
|
|
{ |
110
|
|
|
return $this->name; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $name |
115
|
|
|
* @param null|string $pluralized_name |
116
|
|
|
* @return self |
117
|
|
|
*/ |
118
|
|
|
public function setName($name, $pluralized_name = null) |
119
|
|
|
{ |
120
|
|
|
$this->name = $name; |
121
|
|
|
$this->pluralized_name = is_null($pluralized_name) ? str_plural($name) : $pluralized_name; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param void |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getPluralizedName() |
131
|
|
|
{ |
132
|
|
|
return $this->pluralized_name; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $prefix |
137
|
|
|
* @return self |
138
|
|
|
*/ |
139
|
|
|
public function setUriPrefix($prefix) |
140
|
|
|
{ |
141
|
|
|
$this->route_uri_prefix = rtrim($prefix, "\r\n\r\0\x0B\\/") . '/'; |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param void |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getUriPrefix() |
151
|
|
|
{ |
152
|
|
|
return $this->route_uri_prefix; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $prefix |
157
|
|
|
* @return self |
158
|
|
|
*/ |
159
|
|
|
public function setNamePrefix($prefix) |
160
|
|
|
{ |
161
|
|
|
$this->route_name_prefix = rtrim($prefix, "\r\n\r\0\x0B\\/.") . '.'; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param void |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public function getNamePrefix() |
171
|
|
|
{ |
172
|
|
|
return $this->route_name_prefix; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param void |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getController() |
180
|
|
|
{ |
181
|
|
|
return $this->controller; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $prefix |
186
|
|
|
* @return self |
187
|
|
|
*/ |
188
|
|
|
public function setController($prefix) |
189
|
|
|
{ |
190
|
|
|
$this->controller = $prefix; |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $identifier |
197
|
|
|
* @return string |
198
|
|
|
* @throws InvalidRouteIdentifierException |
199
|
|
|
*/ |
200
|
|
|
public function getActionRoute($identifier) |
201
|
|
|
{ |
202
|
|
|
if (array_key_exists($identifier, $this->routes)) |
203
|
|
|
{ |
204
|
|
|
$route = $this->routes[$identifier]; |
205
|
|
|
$name = $this->route_name_prefix . $route['as']; |
206
|
|
|
$params = []; |
207
|
|
|
|
208
|
|
|
if ($identifier == 'destroy') |
209
|
|
|
{ |
210
|
|
|
$params = [ |
211
|
|
|
$this->entry->getId(), |
212
|
|
|
csrf_token() |
213
|
|
|
]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if ($identifier == 'update' || $identifier == 'edit') |
217
|
|
|
{ |
218
|
|
|
$params = [$this->entry->getId()]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if (Route::has($name) === false) |
222
|
|
|
{ |
223
|
|
|
return '#'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return empty($params) ? route($name) : route($name, $params); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
throw new InvalidRouteIdentifierException("$identifier route identifier not found."); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $identifier |
234
|
|
|
* @return string |
235
|
|
|
* @throws InvalidRouteIdentifierException |
236
|
|
|
*/ |
237
|
|
|
public function getActionMethod($identifier) |
238
|
|
|
{ |
239
|
|
|
if (array_key_exists($identifier, $this->routes)) |
240
|
|
|
{ |
241
|
|
|
$route = $this->routes[$identifier]; |
242
|
|
|
return $route['method']; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
throw new InvalidRouteIdentifierException("$identifier route identifier not found."); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param void |
250
|
|
|
* @return self |
251
|
|
|
*/ |
252
|
|
|
public function registerRoutes() |
253
|
|
|
{ |
254
|
|
|
$routes = $this->routes; |
255
|
|
|
$controller = $this->controller; |
256
|
|
|
|
257
|
|
|
Route::group([ |
258
|
|
|
'prefix' => $this->route_uri_prefix, |
259
|
|
|
'as' => $this->route_name_prefix, |
260
|
|
|
'middleware' => 'web' |
261
|
|
|
], function (Router $router) use ($routes, $controller) |
262
|
|
|
{ |
263
|
|
|
foreach ($routes as $route) |
264
|
|
|
{ |
265
|
|
|
$action = $controller . '@' . $route['action']; |
266
|
|
|
|
267
|
|
|
$router->{$route['method']}($route['uri'], [ |
268
|
|
|
'as' => $route['as'], |
269
|
|
|
'uses' => $action |
270
|
|
|
]); |
271
|
|
|
} |
272
|
|
|
}); |
273
|
|
|
|
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param void |
279
|
|
|
* @return CrudEntry|null |
280
|
|
|
*/ |
281
|
|
|
public function getEntry() |
282
|
|
|
{ |
283
|
|
|
return $this->entry; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param CrudEntry $entry |
288
|
|
|
* @return self |
289
|
|
|
*/ |
290
|
|
|
public function setEntry(CrudEntry $entry) |
291
|
|
|
{ |
292
|
|
|
$this->entry = $entry; |
293
|
|
|
|
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param int $per_page |
299
|
|
|
* @return self |
300
|
|
|
*/ |
301
|
|
|
public function setPerPage($per_page = 25) |
302
|
|
|
{ |
303
|
|
|
$this->per_page = (int)$per_page; |
304
|
|
|
|
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param void |
310
|
|
|
* @return int |
311
|
|
|
*/ |
312
|
|
|
public function getPerPage() |
313
|
|
|
{ |
314
|
|
|
return $this->per_page; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|