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
|
|
|
* CrudManager constructor. |
95
|
|
|
* |
96
|
|
|
* @param mixed $model |
97
|
|
|
*/ |
98
|
|
|
public function __construct($model = null) |
99
|
|
|
{ |
100
|
|
|
if (!is_null($model)) |
101
|
|
|
{ |
102
|
|
|
$name = $this->normalizeClassName($model); |
103
|
|
|
$pluralized = str_plural($name); |
104
|
|
|
$lowered = strtolower($pluralized); |
105
|
|
|
|
106
|
|
|
$this->setController($pluralized . 'Controller') |
107
|
|
|
->setName($name) |
108
|
|
|
->setNamePrefix($lowered) |
109
|
|
|
->setUriPrefix($lowered); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Make staticly a new instance. |
115
|
|
|
* |
116
|
|
|
* @param void |
117
|
|
|
* @return CrudManager |
118
|
|
|
*/ |
119
|
|
|
public static function make($model = null) |
120
|
|
|
{ |
121
|
|
|
return new static($model); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param void |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getName() |
129
|
|
|
{ |
130
|
|
|
return $this->name; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $name |
135
|
|
|
* @param null|string $pluralized_name |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
|
|
public function setName($name, $pluralized_name = null) |
139
|
|
|
{ |
140
|
|
|
$this->name = $name; |
141
|
|
|
$this->pluralized_name = is_null($pluralized_name) ? str_plural($name) : $pluralized_name; |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param void |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getPluralizedName() |
151
|
|
|
{ |
152
|
|
|
return $this->pluralized_name; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $prefix |
157
|
|
|
* @return self |
158
|
|
|
*/ |
159
|
|
|
public function setUriPrefix($prefix) |
160
|
|
|
{ |
161
|
|
|
$this->route_uri_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 getUriPrefix() |
171
|
|
|
{ |
172
|
|
|
return $this->route_uri_prefix; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $prefix |
177
|
|
|
* @return self |
178
|
|
|
*/ |
179
|
|
|
public function setNamePrefix($prefix) |
180
|
|
|
{ |
181
|
|
|
$this->route_name_prefix = rtrim($prefix, "\r\n\r\0\x0B\\/.") . '.'; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param void |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function getNamePrefix() |
191
|
|
|
{ |
192
|
|
|
return $this->route_name_prefix; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param void |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function getController() |
200
|
|
|
{ |
201
|
|
|
return $this->controller; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $prefix |
206
|
|
|
* @return self |
207
|
|
|
*/ |
208
|
|
|
public function setController($prefix) |
209
|
|
|
{ |
210
|
|
|
$this->controller = $prefix; |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string $identifier |
217
|
|
|
* @return string |
218
|
|
|
* @throws InvalidRouteIdentifierException |
219
|
|
|
*/ |
220
|
|
|
public function getActionRoute($identifier) |
221
|
|
|
{ |
222
|
|
|
if (array_key_exists($identifier, $this->routes)) |
223
|
|
|
{ |
224
|
|
|
$route = $this->routes[$identifier]; |
225
|
|
|
$name = $this->route_name_prefix . $route['as']; |
226
|
|
|
$params = []; |
227
|
|
|
|
228
|
|
|
if ($identifier == 'destroy') |
229
|
|
|
{ |
230
|
|
|
$params = [ |
231
|
|
|
$this->entry->getId(), |
232
|
|
|
csrf_token() |
233
|
|
|
]; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if ($identifier == 'update' || $identifier == 'edit') |
237
|
|
|
{ |
238
|
|
|
$params = [$this->entry->getId()]; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
if (Route::has($name) === false) |
242
|
|
|
{ |
243
|
|
|
return '#'; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return empty($params) ? route($name) : route($name, $params); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
throw new InvalidRouteIdentifierException("$identifier route identifier not found."); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $identifier |
254
|
|
|
* @return string |
255
|
|
|
* @throws InvalidRouteIdentifierException |
256
|
|
|
*/ |
257
|
|
|
public function getActionMethod($identifier) |
258
|
|
|
{ |
259
|
|
|
if (array_key_exists($identifier, $this->routes)) |
260
|
|
|
{ |
261
|
|
|
$route = $this->routes[$identifier]; |
262
|
|
|
return $route['method']; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
throw new InvalidRouteIdentifierException("$identifier route identifier not found."); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param void |
270
|
|
|
* @return self |
271
|
|
|
*/ |
272
|
|
|
public function registerRoutes() |
273
|
|
|
{ |
274
|
|
|
$routes = $this->routes; |
275
|
|
|
$controller = $this->controller; |
276
|
|
|
|
277
|
|
|
Route::group([ |
278
|
|
|
'prefix' => $this->route_uri_prefix, |
279
|
|
|
'as' => $this->route_name_prefix, |
280
|
|
|
'namespace' => 'App\Http\Controllers', |
281
|
|
|
'middleware' => 'web' |
282
|
|
|
], function (Router $router) use ($routes, $controller) |
283
|
|
|
{ |
284
|
|
|
foreach ($routes as $route) |
285
|
|
|
{ |
286
|
|
|
$action = $controller . '@' . $route['action']; |
287
|
|
|
|
288
|
|
|
$router->{$route['method']}($route['uri'], [ |
289
|
|
|
'as' => $route['as'], |
290
|
|
|
'uses' => $action |
291
|
|
|
]); |
292
|
|
|
} |
293
|
|
|
}); |
294
|
|
|
|
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param void |
300
|
|
|
* @return CrudEntry|null |
301
|
|
|
*/ |
302
|
|
|
public function getEntry() |
303
|
|
|
{ |
304
|
|
|
return $this->entry; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @param CrudEntry $entry |
309
|
|
|
* @return self |
310
|
|
|
*/ |
311
|
|
|
public function setEntry(CrudEntry $entry) |
312
|
|
|
{ |
313
|
|
|
$this->entry = $entry; |
314
|
|
|
|
315
|
|
|
return $this; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param int $per_page |
320
|
|
|
* @return self |
321
|
|
|
*/ |
322
|
|
|
public function setPerPage($per_page = 25) |
323
|
|
|
{ |
324
|
|
|
$this->per_page = (int)$per_page; |
325
|
|
|
|
326
|
|
|
return $this; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param void |
331
|
|
|
* @return int |
332
|
|
|
*/ |
333
|
|
|
public function getPerPage() |
334
|
|
|
{ |
335
|
|
|
return $this->per_page; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param mixed $model |
340
|
|
|
* @return string |
341
|
|
|
*/ |
342
|
|
|
protected function normalizeClassName($model) |
343
|
|
|
{ |
344
|
|
|
$class = is_string($model) ? $model : get_class($model); |
345
|
|
|
$name = stripos($class, '\\') !== false ? substr(strrchr($class, '\\'), 1) : $class; |
346
|
|
|
|
347
|
|
|
return $name; |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|