|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Illuminate\Routing\Router; |
|
8
|
|
|
use LaravelFlare\Flare\Support\ControllerInspector; |
|
9
|
|
|
|
|
10
|
|
|
abstract class Admin |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Admin Section Icon. |
|
14
|
|
|
* |
|
15
|
|
|
* Font Awesome Defined Icon, eg 'user' = 'fa-user' |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $icon; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Title of Admin Section. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $title; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Plural Title of Admin Section. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $pluralTitle; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* URL Prefix of Admin Section. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $urlPrefix; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The Controller to be used by the Admin. |
|
44
|
|
|
* |
|
45
|
|
|
* This defaults to parent::getController() |
|
46
|
|
|
* if it has been left undefined. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $controller = \LaravelFlare\Flare\Http\Controllers\AdminController::class; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The Policy used for the Admin Authorization logic. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $policy = '\LaravelFlare\Flare\Permissions\AdminPolicy'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* An array of subclasses of Admin |
|
61
|
|
|
* which allows hierachy in a Module. |
|
62
|
|
|
* |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $subAdmin = []; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The Admin Default View. |
|
69
|
|
|
* |
|
70
|
|
|
* By Default this is the 404 page |
|
71
|
|
|
* |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $view = 'admin.404'; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Array of View Data to Render. |
|
78
|
|
|
* |
|
79
|
|
|
* @var array |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $viewData = []; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Class Suffix used for matching and removing term |
|
85
|
|
|
* from user provided Admin sections. |
|
86
|
|
|
* |
|
87
|
|
|
* @var string |
|
88
|
|
|
*/ |
|
89
|
|
|
const CLASS_SUFFIX = 'Admin'; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* __construct. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __construct() |
|
95
|
|
|
{ |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Register the routes for this Admin Section. |
|
100
|
|
|
* |
|
101
|
|
|
* Default routes include, create:, read:, update:, delete: |
|
102
|
|
|
* |
|
103
|
|
|
* Also attempts to load in ModelAdminController |
|
104
|
|
|
* based on the shortName of the class, for |
|
105
|
|
|
* overloading and adding additional routes |
|
106
|
|
|
* |
|
107
|
|
|
* @param \Illuminate\Routing\Router $router |
|
108
|
|
|
*/ |
|
109
|
|
|
public function registerRoutes(Router $router) |
|
110
|
|
|
{ |
|
111
|
|
|
// We will need to throw an exception if a ModelAdmin manages a Model which conflicts with an internal flare endpoint |
|
112
|
|
|
// such as (create, edit, view, delete etc) |
|
113
|
|
|
$router->group(['prefix' => $this->urlPrefix(), 'namespace' => get_called_class(), 'as' => $this->urlPrefix()], function ($router) { |
|
|
|
|
|
|
114
|
|
|
$this->registerSubRoutes(); |
|
115
|
|
|
$this->registerController($this->getController()); |
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Register subRoutes for Defined Admin instances. |
|
121
|
|
|
* |
|
122
|
|
|
* @return |
|
123
|
|
|
*/ |
|
124
|
|
|
public function registerSubRoutes() |
|
125
|
|
|
{ |
|
126
|
|
|
if (!is_array($this->subAdmin)) { |
|
127
|
|
|
return; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
foreach ($this->subAdmin as $adminItem) { |
|
131
|
|
|
$this->registerRoute($adminItem->getController(), $adminItem->routeParameters()); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Register an individual route. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $controller |
|
139
|
|
|
* @param array $parameters |
|
140
|
|
|
* |
|
141
|
|
|
* @return |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function registerRoute($controller, $parameters = []) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
\Route::group($parameters, function ($controller) { |
|
146
|
|
|
\Route::registerController($controller); |
|
147
|
|
|
}); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Stolen Method from 5.2 Illumiante/Routing/Router. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $controller |
|
154
|
|
|
* @return void |
|
155
|
|
|
*/ |
|
156
|
|
|
public static function registerController($controller) |
|
157
|
|
|
{ |
|
158
|
|
|
$uri = '/'; |
|
159
|
|
|
|
|
160
|
|
|
$routable = (new ControllerInspector) |
|
161
|
|
|
->getRoutable($controller, $uri); |
|
162
|
|
|
|
|
163
|
|
|
// When a controller is routed using this method, we use Reflection to parse |
|
164
|
|
|
// out all of the routable methods for the controller, then register each |
|
165
|
|
|
// route explicitly for the developers, so reverse routing is possible. |
|
166
|
|
|
foreach ($routable as $method => $routes) { |
|
167
|
|
|
foreach ($routes as $route) { |
|
168
|
|
|
$action = ['uses' => $controller.'@'.$method]; |
|
169
|
|
|
|
|
170
|
|
|
\Route::{$route['verb']}($route['uri'], $action); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
\Route::any($uri.'/{_missing}', $controller.'@missingMethod'); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Returns the Route Paramets. |
|
179
|
|
|
* |
|
180
|
|
|
* @return array |
|
181
|
|
|
*/ |
|
182
|
|
|
public function routeParameters() |
|
183
|
|
|
{ |
|
184
|
|
|
return [ |
|
185
|
|
|
'prefix' => $this->urlPrefix(), |
|
186
|
|
|
'as' => $this->urlPrefix(), |
|
187
|
|
|
]; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Returns the Requested Route Action as a |
|
192
|
|
|
* string, namespace is returned by default. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $key |
|
195
|
|
|
* |
|
196
|
|
|
* @return string|void |
|
197
|
|
|
*/ |
|
198
|
|
|
public static function getRequested($key = 'namespace') |
|
199
|
|
|
{ |
|
200
|
|
|
if (!\Route::current()) { |
|
201
|
|
|
return; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$currentAction = \Route::current()->getAction(); |
|
205
|
|
|
|
|
206
|
|
|
if (isset($currentAction[$key])) { |
|
207
|
|
|
return $currentAction[$key]; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Returns the Controller Class for the current Admin section. |
|
215
|
|
|
* |
|
216
|
|
|
* @return string |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getController() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->controller; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Set the Controller Class for the current Admin section. |
|
225
|
|
|
* |
|
226
|
|
|
* @return string |
|
227
|
|
|
*/ |
|
228
|
|
|
public function setController($controller = null) |
|
229
|
|
|
{ |
|
230
|
|
|
$this->controller = $controller; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Returns the Module Admin View. |
|
235
|
|
|
* |
|
236
|
|
|
* @return string |
|
237
|
|
|
*/ |
|
238
|
|
|
public function getView() |
|
239
|
|
|
{ |
|
240
|
|
|
if (view()->exists($this->view)) { |
|
|
|
|
|
|
241
|
|
|
return $this->view; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return 'flare::'.$this->view; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Set the Module Admin View. |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $view |
|
251
|
|
|
*/ |
|
252
|
|
|
public function setView($view = null) |
|
253
|
|
|
{ |
|
254
|
|
|
$this->view = $view; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Returns the View Data. |
|
259
|
|
|
* |
|
260
|
|
|
* @return array |
|
261
|
|
|
*/ |
|
262
|
|
|
public function getViewData() |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->viewData; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Set the View Data. |
|
269
|
|
|
* |
|
270
|
|
|
* @param array $viewData |
|
271
|
|
|
*/ |
|
272
|
|
|
public function setViewData($viewData = []) |
|
273
|
|
|
{ |
|
274
|
|
|
$this->viewData = $viewData; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Menu Items. |
|
279
|
|
|
* |
|
280
|
|
|
* @return array |
|
281
|
|
|
*/ |
|
282
|
|
|
public function menuItems() |
|
283
|
|
|
{ |
|
284
|
|
|
return []; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Icon of a Admin Section Class. |
|
289
|
|
|
* |
|
290
|
|
|
* @return string |
|
291
|
|
|
*/ |
|
292
|
|
|
public function getIcon() |
|
293
|
|
|
{ |
|
294
|
|
|
return $this->icon; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Set Icon of a Admin Section Class. |
|
299
|
|
|
* |
|
300
|
|
|
* @param string $icon |
|
301
|
|
|
*/ |
|
302
|
|
|
public function setIcon($icon = null) |
|
303
|
|
|
{ |
|
304
|
|
|
$this->icon = $icon; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Shortname of a Admin Section Class. |
|
309
|
|
|
* |
|
310
|
|
|
* @return string |
|
311
|
|
|
*/ |
|
312
|
|
|
public static function shortName() |
|
313
|
|
|
{ |
|
314
|
|
|
return (new \ReflectionClass(new static()))->getShortName(); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Title of a Admin Section Class. |
|
319
|
|
|
* |
|
320
|
|
|
* @return string |
|
321
|
|
|
*/ |
|
322
|
|
|
public function getTitle() |
|
323
|
|
|
{ |
|
324
|
|
|
if (!isset($this->title) || !$this->title) { |
|
325
|
|
|
return Str::title(str_replace('_', ' ', snake_case(preg_replace('/'.static::CLASS_SUFFIX.'$/', '', static::shortName())))); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
return $this->title; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Set Title of a Admin Section Class. |
|
333
|
|
|
* |
|
334
|
|
|
* @param string $title |
|
335
|
|
|
*/ |
|
336
|
|
|
public function setTitle($title = null) |
|
337
|
|
|
{ |
|
338
|
|
|
$this->title = $title; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Plural of the Admin Section Class Title. |
|
343
|
|
|
* |
|
344
|
|
|
* @return string |
|
345
|
|
|
*/ |
|
346
|
|
|
public function getPluralTitle() |
|
347
|
|
|
{ |
|
348
|
|
|
if (!isset($this->pluralTitle) || !$this->pluralTitle) { |
|
349
|
|
|
return Str::plural($this->getTitle()); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
return $this->pluralTitle; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* Set Plural Title. |
|
357
|
|
|
* |
|
358
|
|
|
* @param string $pluralTitle |
|
359
|
|
|
*/ |
|
360
|
|
|
public function setPluralTitle($pluralTitle = null) |
|
361
|
|
|
{ |
|
362
|
|
|
$this->pluralTitle = $pluralTitle; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* URL Prefix to a Admin Section Top Level Page. |
|
367
|
|
|
* |
|
368
|
|
|
* @return string |
|
369
|
|
|
*/ |
|
370
|
|
|
public function urlPrefix() |
|
371
|
|
|
{ |
|
372
|
|
|
if (!isset($this->urlPrefix) || !$this->urlPrefix) { |
|
373
|
|
|
return str_slug($this->getPluralTitle()); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
return $this->urlPrefix; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* URL to a Admin Top Level Page. |
|
381
|
|
|
* |
|
382
|
|
|
* @param string $path |
|
383
|
|
|
* |
|
384
|
|
|
* @return string |
|
385
|
|
|
*/ |
|
386
|
|
|
public function url($path = '') |
|
387
|
|
|
{ |
|
388
|
|
|
return url($this->relativeUrl($path)); |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* Relative URL to an Admin Top Level Page. |
|
393
|
|
|
* |
|
394
|
|
|
* @param string $path |
|
395
|
|
|
* |
|
396
|
|
|
* @return string |
|
397
|
|
|
*/ |
|
398
|
|
|
public function relativeUrl($path = '') |
|
399
|
|
|
{ |
|
400
|
|
|
return \Flare::relativeAdminUrl($this->urlPrefix().($path ? '/'.$path : '')); |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* Retrieves the Current Admin Route URL. |
|
405
|
|
|
* |
|
406
|
|
|
* @param string $path |
|
407
|
|
|
* |
|
408
|
|
|
* @return string |
|
409
|
|
|
*/ |
|
410
|
|
|
public function currentUrl($path = '') |
|
411
|
|
|
{ |
|
412
|
|
|
return url($this->relativeCurrentUrl($path)); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* Retrieves the Current Admin Route URL. |
|
417
|
|
|
* |
|
418
|
|
|
* @param string $path |
|
419
|
|
|
* |
|
420
|
|
|
* @return string |
|
421
|
|
|
*/ |
|
422
|
|
|
public function relativeCurrentUrl($path) |
|
423
|
|
|
{ |
|
424
|
|
|
return \Route::current() ? \Route::current()->getPrefix().'/'.$path : null; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
/* |
|
428
|
|
|
* Handle dynamic static method calls into the Admin. |
|
429
|
|
|
* |
|
430
|
|
|
* @param string $method |
|
431
|
|
|
* @param array $parameters |
|
432
|
|
|
* |
|
433
|
|
|
* @return mixed |
|
434
|
|
|
*/ |
|
435
|
|
|
// public static function __callStatic($method, $parameters) |
|
436
|
|
|
// { |
|
437
|
|
|
// $instance = new static(); |
|
438
|
|
|
|
|
439
|
|
|
// return call_user_func_array([$instance, $method], $parameters); |
|
440
|
|
|
// } |
|
441
|
|
|
} |
|
442
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.