1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use SleepingOwl\Admin\Navigation\Page; |
8
|
|
|
use Illuminate\Contracts\Support\Renderable; |
9
|
|
|
use SleepingOwl\Admin\Model\ModelCollection; |
10
|
|
|
use Illuminate\Foundation\ProviderRepository; |
11
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
12
|
|
|
use SleepingOwl\Admin\Contracts\AdminInterface; |
13
|
|
|
use SleepingOwl\Admin\Model\ModelConfiguration; |
14
|
|
|
use Illuminate\Contracts\Foundation\Application; |
15
|
|
|
use SleepingOwl\Admin\Contracts\Template\MetaInterface; |
16
|
|
|
use SleepingOwl\Admin\Http\Controllers\AdminController; |
17
|
|
|
use SleepingOwl\Admin\Contracts\Template\TemplateInterface; |
18
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
19
|
|
|
use SleepingOwl\Admin\Contracts\Navigation\NavigationInterface; |
20
|
|
|
|
21
|
|
|
class Admin implements AdminInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ModelConfigurationInterface[]|ModelCollection |
25
|
|
|
*/ |
26
|
|
|
protected $models; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TemplateInterface |
30
|
|
|
*/ |
31
|
|
|
protected $template; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Application |
35
|
|
|
*/ |
36
|
|
|
protected $app; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $missedSections = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Admin constructor. |
45
|
|
|
* |
46
|
|
|
* @param Application $application |
47
|
|
|
*/ |
48
|
283 |
|
public function __construct(Application $application) |
49
|
|
|
{ |
50
|
283 |
|
$this->app = $application; |
51
|
283 |
|
$this->models = new ModelCollection(); |
52
|
|
|
|
53
|
283 |
|
$this->registerBaseServiceProviders(); |
54
|
283 |
|
$this->registerCoreContainerAliases(); |
55
|
283 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param TemplateInterface $template |
59
|
|
|
*/ |
60
|
283 |
|
public function setTemplate(TemplateInterface $template) |
61
|
|
|
{ |
62
|
283 |
|
$this->template = $template; |
63
|
283 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Initialize class. |
67
|
|
|
*/ |
68
|
283 |
|
public function initialize() |
69
|
|
|
{ |
70
|
283 |
|
$this->template->initialize(); |
71
|
283 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $class |
75
|
|
|
* @param Closure|null $callback |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
2 |
|
public function registerModel($class, Closure $callback = null) |
80
|
|
|
{ |
81
|
2 |
|
$this->register($model = new ModelConfiguration($this->app, $class)); |
82
|
|
|
|
83
|
2 |
|
if (is_callable($callback)) { |
84
|
1 |
|
call_user_func($callback, $model); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
2 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ModelConfigurationInterface $model |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
2 |
|
public function register(ModelConfigurationInterface $model) |
96
|
|
|
{ |
97
|
2 |
|
$this->setModel($model->getClass(), $model); |
98
|
|
|
|
99
|
2 |
|
if ($model instanceof Initializable) { |
100
|
1 |
|
$model->initialize(); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
2 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $sections |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
283 |
|
public function registerSections(array $sections) |
112
|
|
|
{ |
113
|
283 |
|
foreach ($sections as $model => $section) { |
114
|
|
|
if (class_exists($section)) { |
115
|
|
|
$this->register(new $section($model)); |
116
|
|
|
} else { |
117
|
|
|
$this->missedSections[$model] = $section; |
118
|
|
|
} |
119
|
283 |
|
} |
120
|
|
|
|
121
|
283 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getMissedSections() |
128
|
|
|
{ |
129
|
|
|
return $this->missedSections; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $class |
134
|
|
|
* @param ModelConfigurationInterface $model |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
2 |
|
public function setModel($class, ModelConfigurationInterface $model) |
139
|
|
|
{ |
140
|
2 |
|
$this->models->put($class, $model); |
141
|
|
|
|
142
|
2 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string|Model $class |
147
|
|
|
* @return ModelConfigurationInterface |
148
|
|
|
*/ |
149
|
2 |
|
public function getModel($class) |
150
|
|
|
{ |
151
|
2 |
|
if (is_object($class)) { |
152
|
1 |
|
$class = get_class($class); |
153
|
1 |
|
} |
154
|
|
|
|
155
|
2 |
|
if (! $this->hasModel($class)) { |
156
|
2 |
|
$this->registerModel($class); |
157
|
2 |
|
} |
158
|
|
|
|
159
|
2 |
|
return $this->models->get($class); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return ModelConfigurationInterface[]|ModelCollection |
164
|
|
|
*/ |
165
|
285 |
|
public function getModels() |
166
|
|
|
{ |
167
|
285 |
|
return $this->models; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $class |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
2 |
|
public function hasModel($class) |
176
|
|
|
{ |
177
|
2 |
|
return $this->models->has($class); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return NavigationInterface |
182
|
|
|
*/ |
183
|
|
|
public function navigation() |
184
|
|
|
{ |
185
|
|
|
return $this->template()->navigation(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return MetaInterface |
190
|
|
|
*/ |
191
|
|
|
public function meta() |
192
|
|
|
{ |
193
|
|
|
return $this->template()->meta(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return TemplateInterface |
198
|
|
|
*/ |
199
|
1 |
|
public function template() |
200
|
|
|
{ |
201
|
1 |
|
return $this->template; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $class |
206
|
|
|
* @param int $priority |
207
|
|
|
* |
208
|
|
|
* @return Page |
209
|
|
|
*/ |
210
|
1 |
|
public function addMenuPage($class = null, $priority = 100) |
211
|
|
|
{ |
212
|
1 |
|
return $this->getModel($class)->addToNavigation($priority); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return Navigation |
217
|
|
|
* |
218
|
|
|
* @deprecated |
219
|
|
|
*/ |
220
|
|
|
public function getNavigation() |
221
|
|
|
{ |
222
|
|
|
return $this->navigation(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string|Renderable $content |
227
|
|
|
* @param string|null $title |
228
|
|
|
* |
229
|
|
|
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
230
|
|
|
*/ |
231
|
1 |
|
public function view($content, $title = null) |
232
|
|
|
{ |
233
|
1 |
|
return $this->app[AdminController::class]->renderContent($content, $title); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Register all of the base service providers. |
238
|
|
|
* |
239
|
|
|
* @return void |
240
|
|
|
*/ |
241
|
283 |
|
protected function registerBaseServiceProviders() |
242
|
|
|
{ |
243
|
|
|
$providers = [ |
244
|
283 |
|
\SleepingOwl\Admin\Providers\AliasesServiceProvider::class, |
245
|
283 |
|
\Collective\Html\HtmlServiceProvider::class, |
246
|
283 |
|
\SleepingOwl\Admin\Providers\BreadcrumbsServiceProvider::class, |
247
|
283 |
|
\SleepingOwl\Admin\Providers\AdminServiceProvider::class, |
248
|
283 |
|
]; |
249
|
|
|
|
250
|
|
|
/* Workaround to allow use ServiceProvider-based configurations in old fashion */ |
251
|
283 |
|
if (is_file(app_path('Providers/AdminSectionsServiceProvider.php'))) { |
252
|
|
|
$providers[] = $this->app->getNamespace().'Providers\\AdminSectionsServiceProvider'; |
253
|
|
|
} |
254
|
|
|
|
255
|
283 |
|
$manifestPath = $this->app->bootstrapPath().'/cache/sleepingowladmin-services.php'; |
256
|
|
|
|
257
|
283 |
|
(new ProviderRepository($this->app, new Filesystem(), $manifestPath))->load($providers); |
258
|
283 |
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Register the core class aliases in the container. |
262
|
|
|
* |
263
|
|
|
* @return void |
264
|
|
|
*/ |
265
|
283 |
|
protected function registerCoreContainerAliases() |
266
|
|
|
{ |
267
|
|
|
$aliases = [ |
268
|
283 |
|
'sleeping_owl' => ['SleepingOwl\Admin\Admin', 'SleepingOwl\Admin\Contracts\AdminInterface'], |
269
|
283 |
|
'sleeping_owl.template' => ['SleepingOwl\Admin\Contracts\Template\TemplateInterface'], |
270
|
283 |
|
'sleeping_owl.breadcrumbs' => ['SleepingOwl\Admin\Contracts\Template\Breadcrumbs'], |
271
|
283 |
|
'sleeping_owl.widgets' => ['SleepingOwl\Admin\Contracts\Widgets\WidgetsRegistryInterface', 'SleepingOwl\Admin\Widgets\WidgetsRegistry'], |
272
|
283 |
|
'sleeping_owl.message' => ['SleepingOwl\Admin\Widgets\Messages\MessageStack'], |
273
|
283 |
|
'sleeping_owl.navigation' => ['SleepingOwl\Admin\Navigation', 'SleepingOwl\Admin\Contracts\Navigation\NavigationInterface'], |
274
|
283 |
|
'sleeping_owl.wysiwyg' => ['SleepingOwl\Admin\Wysiwyg\Manager', 'SleepingOwl\Admin\Contracts\Wysiwyg\WysiwygMangerInterface'], |
275
|
283 |
|
'sleeping_owl.meta' => ['assets.meta', 'SleepingOwl\Admin\Contracts\Template\MetaInterface', 'SleepingOwl\Admin\Templates\Meta'], |
276
|
283 |
|
]; |
277
|
|
|
|
278
|
283 |
|
foreach ($aliases as $key => $aliases) { |
279
|
283 |
|
foreach ($aliases as $alias) { |
280
|
283 |
|
$this->app->alias($key, $alias); |
281
|
283 |
|
} |
282
|
283 |
|
} |
283
|
283 |
|
} |
284
|
|
|
} |
285
|
|
|
|