|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD; |
|
4
|
|
|
|
|
5
|
|
|
use Route; |
|
6
|
|
|
|
|
7
|
|
|
class CrudRouter |
|
8
|
|
|
{ |
|
9
|
|
|
protected $requiredInjectables = []; |
|
10
|
|
|
|
|
11
|
|
|
protected $name = null; |
|
12
|
|
|
protected $options = null; |
|
13
|
|
|
protected $controller = null; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($name, $controller, $options) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->name = $name; |
|
18
|
|
|
$this->options = $options; |
|
19
|
|
|
$this->controller = $controller; |
|
20
|
|
|
|
|
21
|
|
|
// CRUD routes |
|
22
|
|
|
Route::post($this->name.'/search', [ |
|
23
|
|
|
'as' => 'crud.'.$this->name.'.search', |
|
24
|
|
|
'uses' => $this->controller.'@search', |
|
25
|
|
|
]); |
|
26
|
|
|
|
|
27
|
|
|
Route::get($this->name.'/reorder', [ |
|
28
|
|
|
'as' => 'crud.'.$this->name.'.reorder', |
|
29
|
|
|
'uses' => $this->controller.'@reorder', |
|
30
|
|
|
]); |
|
31
|
|
|
|
|
32
|
|
|
Route::post($this->name.'/reorder', [ |
|
33
|
|
|
'as' => 'crud.'.$this->name.'.save.reorder', |
|
34
|
|
|
'uses' => $this->controller.'@saveReorder', |
|
35
|
|
|
]); |
|
36
|
|
|
|
|
37
|
|
|
Route::get($this->name.'/{id}/details', [ |
|
38
|
|
|
'as' => 'crud.'.$this->name.'.showDetailsRow', |
|
39
|
|
|
'uses' => $this->controller.'@showDetailsRow', |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
|
|
Route::get($this->name.'/{id}/translate/{lang}', [ |
|
43
|
|
|
'as' => 'crud.'.$this->name.'.translateItem', |
|
44
|
|
|
'uses' => $this->controller.'@translateItem', |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
Route::get($this->name.'/{id}/revisions', [ |
|
48
|
|
|
'as' => 'crud.'.$this->name.'.listRevisions', |
|
49
|
|
|
'uses' => $this->controller.'@listRevisions', |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
Route::post($this->name.'/{id}/revisions/{revisionId}/restore', [ |
|
53
|
|
|
'as' => 'crud.'.$this->name.'.restoreRevision', |
|
54
|
|
|
'uses' => $this->controller.'@restoreRevision', |
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
$options_with_default_route_names = array_merge([ |
|
58
|
|
|
'names' => [ |
|
59
|
|
|
'index' => 'crud.'.$this->name.'.index', |
|
60
|
|
|
'create' => 'crud.'.$this->name.'.create', |
|
61
|
|
|
'store' => 'crud.'.$this->name.'.store', |
|
62
|
|
|
'edit' => 'crud.'.$this->name.'.edit', |
|
63
|
|
|
'update' => 'crud.'.$this->name.'.update', |
|
64
|
|
|
'show' => 'crud.'.$this->name.'.show', |
|
65
|
|
|
'destroy' => 'crud.'.$this->name.'.destroy', |
|
66
|
|
|
], |
|
67
|
|
|
], $this->options); |
|
68
|
|
|
|
|
69
|
|
|
Route::resource($this->name, $this->controller, $options_with_default_route_names); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function with($injectables) |
|
73
|
|
|
{ |
|
74
|
|
|
if (is_string($injectables)) { |
|
75
|
|
|
$this->requiredInjectables[] = 'with'.ucwords($injectables); |
|
76
|
|
|
} elseif (is_array($injectables)) { |
|
77
|
|
|
foreach ($injectables as $injectable) { |
|
78
|
|
|
$this->requiredInjectables[] = 'with'.ucwords($injectable); |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
|
|
$reflection = new \ReflectionFunction($injectables); |
|
82
|
|
|
|
|
83
|
|
|
if ($reflection->isClosure()) { |
|
84
|
|
|
$this->requiredInjectables[] = $injectables; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->inject(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function inject() |
|
92
|
|
|
{ |
|
93
|
|
|
foreach ($this->requiredInjectables as $injectable) { |
|
94
|
|
|
if (is_string($injectable)) { |
|
95
|
|
|
$this->{$injectable}(); |
|
96
|
|
|
} else { |
|
97
|
|
|
$injectable(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function __call($method, $parameters = null) |
|
103
|
|
|
{ |
|
104
|
|
|
if (method_exists($this, $method)) { |
|
105
|
|
|
$this->{$method}($parameters); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|