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