1 | <?php |
||
2 | |||
3 | namespace SleepingOwl\Admin\Providers; |
||
4 | |||
5 | use Illuminate\Contracts\View\Factory as ViewFactory; |
||
6 | use Illuminate\Foundation\AliasLoader; |
||
7 | use Illuminate\Foundation\Application; |
||
8 | use Illuminate\Routing\Router; |
||
9 | use Illuminate\Support\ServiceProvider; |
||
10 | use SleepingOwl\Admin\AliasBinder; |
||
11 | use SleepingOwl\Admin\Contracts\Display\TableHeaderColumnInterface; |
||
12 | use SleepingOwl\Admin\Contracts\Form\FormButtonsInterface; |
||
13 | use SleepingOwl\Admin\Contracts\Repositories\RepositoryInterface; |
||
14 | use SleepingOwl\Admin\Contracts\Widgets\WidgetsRegistryInterface; |
||
15 | use SleepingOwl\Admin\Exceptions\TemplateException; |
||
16 | use SleepingOwl\Admin\Model\ModelConfigurationManager; |
||
17 | use SleepingOwl\Admin\Navigation; |
||
18 | use SleepingOwl\Admin\Routing\ModelRouter; |
||
19 | use SleepingOwl\Admin\Templates\Assets; |
||
20 | use SleepingOwl\Admin\Templates\Meta; |
||
21 | use SleepingOwl\Admin\Widgets\EnvEditor; |
||
22 | use SleepingOwl\Admin\Widgets\Messages\ErrorMessages; |
||
23 | use SleepingOwl\Admin\Widgets\Messages\InfoMessages; |
||
24 | use SleepingOwl\Admin\Widgets\Messages\MessageStack; |
||
25 | use SleepingOwl\Admin\Widgets\Messages\SuccessMessages; |
||
26 | use SleepingOwl\Admin\Widgets\Messages\WarningMessages; |
||
27 | use SleepingOwl\Admin\Widgets\WidgetsRegistry; |
||
28 | use SleepingOwl\Admin\Wysiwyg\Manager; |
||
29 | use Symfony\Component\Finder\Finder; |
||
30 | use Symfony\Component\Finder\SplFileInfo; |
||
31 | |||
32 | class AdminServiceProvider extends ServiceProvider |
||
33 | { |
||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $directory; |
||
38 | 285 | ||
39 | /** |
||
40 | 285 | * All global widgets. |
|
41 | 285 | * @var array |
|
42 | 285 | */ |
|
43 | 285 | protected $widgets = [ |
|
44 | EnvEditor::class, |
||
45 | ]; |
||
46 | 285 | ||
47 | 285 | public function register() |
|
48 | { |
||
49 | $this->registerWysiwyg(); |
||
50 | 285 | $this->registerTemplate(); |
|
51 | 285 | $this->initializeNavigation(); |
|
52 | 285 | $this->registerAliases(); |
|
53 | 285 | ||
54 | $this->app->singleton('sleeping_owl.widgets', function () { |
||
55 | return new WidgetsRegistry($this->app); |
||
56 | 285 | }); |
|
57 | 285 | ||
58 | 285 | $this->app->booted(function () { |
|
59 | 285 | $this->app['sleeping_owl.widgets']->placeWidgets( |
|
60 | $this->app[ViewFactory::class] |
||
61 | 285 | ); |
|
62 | 285 | }); |
|
63 | |||
64 | 285 | $this->app->booted(function () { |
|
65 | 285 | $this->registerCustomRoutes(); |
|
66 | $this->registerDefaultRoutes(); |
||
67 | 285 | $this->registerSupportRoutes(); |
|
68 | $this->registerNavigationFile(); |
||
69 | |||
70 | 285 | $this->app['sleeping_owl']->initialize(); |
|
71 | 285 | }); |
|
72 | |||
73 | ModelConfigurationManager::setEventDispatcher($this->app['events']); |
||
74 | 285 | } |
|
75 | 285 | ||
76 | 285 | protected function registerTemplate() |
|
77 | 285 | { |
|
78 | 285 | $this->app->singleton('assets.packages', function ($app) { |
|
79 | 285 | return new \KodiCMS\Assets\PackageManager(); |
|
80 | }); |
||
81 | |||
82 | 285 | $this->app->singleton('sleeping_owl.meta', function ($app) { |
|
83 | return new Meta( |
||
84 | new Assets( |
||
85 | $app['assets.packages'] |
||
86 | 285 | ) |
|
87 | 285 | ); |
|
88 | }); |
||
89 | 285 | ||
90 | 285 | $this->app->singleton('sleeping_owl.template', function (Application $app) { |
|
91 | 285 | if (! class_exists($class = $this->getConfig('template'))) { |
|
92 | 285 | throw new TemplateException("Template class [{$class}] not found in config file"); |
|
93 | } |
||
94 | |||
95 | return $app->make($class); |
||
96 | }); |
||
97 | |||
98 | if (file_exists($assetsFile = __DIR__.'/../../resources/assets.php')) { |
||
99 | 285 | include $assetsFile; |
|
100 | } |
||
101 | 285 | } |
|
102 | |||
103 | /** |
||
104 | * @param string $key |
||
105 | * |
||
106 | * @return mixed |
||
107 | */ |
||
108 | protected function getConfig($key) |
||
109 | 285 | { |
|
110 | return $this->app['config']->get('sleeping_owl.'.$key); |
||
111 | 285 | } |
|
112 | 285 | ||
113 | 285 | /** |
|
114 | * @param string $path |
||
115 | 285 | * |
|
116 | * @return string |
||
117 | */ |
||
118 | 285 | protected function getBootstrapPath($path = null) |
|
119 | { |
||
120 | 285 | if (! is_null($path)) { |
|
121 | 285 | $path = DIRECTORY_SEPARATOR.$path; |
|
122 | 285 | } |
|
123 | |||
124 | return $this->getConfig('bootstrapDirectory').$path; |
||
125 | } |
||
126 | |||
127 | 285 | public function boot() |
|
128 | { |
||
129 | $this->registerMessages(); |
||
130 | 285 | $this->registerBootstrap(); |
|
131 | 285 | $this->registerWidgets(); |
|
132 | 285 | } |
|
133 | 285 | ||
134 | 285 | /** |
|
135 | 285 | * Global register widgets. |
|
136 | 285 | */ |
|
137 | 285 | protected function registerWidgets() |
|
138 | { |
||
139 | $widgetsRegistry = $this->app[WidgetsRegistryInterface::class]; |
||
140 | |||
141 | 285 | foreach ($this->widgets as $widget) { |
|
142 | 285 | $widgetsRegistry->registerWidget($widget); |
|
143 | } |
||
144 | 285 | } |
|
145 | |||
146 | 285 | /** |
|
147 | 285 | * Global register messages of adminpanel. |
|
148 | */ |
||
149 | 285 | protected function registerMessages() |
|
150 | { |
||
151 | 285 | $messageTypes = [ |
|
152 | 285 | 'error' => ErrorMessages::class, |
|
153 | 'info' => InfoMessages::class, |
||
154 | 285 | 'success' => SuccessMessages::class, |
|
155 | 'warning' => WarningMessages::class, |
||
156 | 285 | ]; |
|
157 | 285 | foreach ($messageTypes as $messageType) { |
|
158 | $this->app[WidgetsRegistryInterface::class]->registerWidget($messageType); |
||
159 | 285 | } |
|
160 | |||
161 | 285 | $this->app->singleton('sleeping_owl.message', function () use ($messageTypes) { |
|
162 | 285 | return new MessageStack($messageTypes); |
|
163 | }); |
||
164 | 285 | } |
|
165 | |||
166 | 285 | protected function initializeNavigation() |
|
167 | 285 | { |
|
168 | $this->app->bind( |
||
169 | 285 | TableHeaderColumnInterface::class, |
|
170 | \SleepingOwl\Admin\Display\TableHeaderColumn::class |
||
171 | ); |
||
172 | 285 | ||
173 | 285 | $this->app->bind( |
|
174 | 285 | RepositoryInterface::class, |
|
175 | \SleepingOwl\Admin\Repositories\BaseRepository::class |
||
176 | 285 | ); |
|
177 | |||
178 | $this->app->bind( |
||
179 | 285 | FormButtonsInterface::class, |
|
180 | 285 | \SleepingOwl\Admin\Form\FormButtons::class |
|
181 | 285 | ); |
|
182 | |||
183 | $this->app->bind( |
||
184 | \KodiComponents\Navigation\Contracts\PageInterface::class, |
||
185 | \SleepingOwl\Admin\Navigation\Page::class |
||
186 | 285 | ); |
|
187 | |||
188 | 285 | $this->app->bind( |
|
189 | \KodiComponents\Navigation\Contracts\BadgeInterface::class, |
||
190 | 285 | \SleepingOwl\Admin\Navigation\Badge::class |
|
191 | 285 | ); |
|
192 | |||
193 | $this->app->singleton('sleeping_owl.navigation', function () { |
||
194 | return new Navigation(); |
||
195 | }); |
||
196 | } |
||
197 | |||
198 | protected function registerWysiwyg() |
||
199 | { |
||
200 | $this->app->singleton('sleeping_owl.wysiwyg', function () { |
||
201 | return new Manager($this->app); |
||
202 | }); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Register bootstrap file. |
||
207 | */ |
||
208 | protected function registerBootstrap() |
||
209 | { |
||
210 | $directory = $this->getBootstrapPath(); |
||
211 | |||
212 | 285 | if (! is_dir($directory)) { |
|
213 | return; |
||
214 | 285 | } |
|
215 | 285 | ||
216 | $files = Finder::create() |
||
217 | ->files() |
||
218 | ->name('/^.+\.php$/') |
||
219 | ->notName('routes.php') |
||
220 | 285 | ->notName('*.blade.php') |
|
221 | ->notName('navigation.php') |
||
222 | 285 | ->in($directory) |
|
223 | ->sort(function (SplFileInfo $a) { |
||
224 | return $a->getFilename() != 'bootstrap.php'; |
||
225 | }); |
||
226 | |||
227 | 285 | foreach ($files as $file) { |
|
228 | require_once $file; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | 285 | /** |
|
233 | * Register Alias from App. |
||
234 | */ |
||
235 | 285 | protected function registerAliases() |
|
236 | { |
||
237 | 285 | AliasLoader::getInstance(config('sleeping_owl.aliases', [])); |
|
238 | 285 | } |
|
239 | 285 | ||
240 | /** |
||
241 | 285 | * Register Custom Routes From Users. |
|
242 | 285 | */ |
|
243 | 285 | protected function registerCustomRoutes() |
|
244 | { |
||
245 | if (file_exists($file = $this->getBootstrapPath('routes.php'))) { |
||
246 | $this->registerRoutes(function (Router $route) use ($file) { |
||
247 | require $file; |
||
248 | 285 | }); |
|
249 | } |
||
250 | 285 | } |
|
251 | 285 | ||
252 | /** |
||
253 | * Register Default Admin Routes. |
||
254 | 285 | */ |
|
255 | 285 | protected function registerDefaultRoutes() |
|
256 | 285 | { |
|
257 | 285 | $this->registerRoutes(function (Router $router) { |
|
258 | (new ModelRouter($this->app, $router))->register($this->app['sleeping_owl']->getModels()); |
||
259 | 285 | ||
260 | 285 | if (file_exists($routesFile = __DIR__.'/../Http/routes.php')) { |
|
261 | 285 | require $routesFile; |
|
262 | 285 | } |
|
263 | 285 | ||
264 | 285 | AliasBinder::registerRoutes($router); |
|
265 | }); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | 285 | * Register CKEditor Upload and D&D plugins. |
|
270 | */ |
||
271 | 285 | protected function registerSupportRoutes() |
|
272 | 285 | { |
|
273 | 285 | $domain = config('sleeping_owl.domain', false); |
|
274 | 285 | ||
275 | 285 | $middlewares = collect($this->getConfig('middleware')); |
|
276 | 285 | $configGroup = collect([ |
|
277 | 285 | 'prefix' => $this->getConfig('url_prefix'), |
|
278 | 'middleware' => $middlewares, |
||
279 | ]); |
||
280 | |||
281 | if ($domain) { |
||
282 | 285 | $configGroup->put('domain', $domain); |
|
283 | } |
||
284 | 285 | ||
285 | $this->app['router']->group($configGroup->toArray(), function (Router $route) { |
||
286 | $route->get('ckeditor/upload/image', [ |
||
287 | 'as' => 'admin.ckeditor.upload', |
||
288 | 'uses' => 'SleepingOwl\Admin\Http\Controllers\UploadController@ckEditorStore', |
||
0 ignored issues
–
show
|
|||
289 | ]); |
||
290 | |||
291 | 285 | $route->post('ckeditor/upload/image', [ |
|
292 | 'as' => 'admin.ckeditor.upload', |
||
293 | 'uses' => 'SleepingOwl\Admin\Http\Controllers\UploadController@ckEditorStore', |
||
0 ignored issues
–
show
The controller
App\Http\Controllers\Sle...ollers\UploadController does not seem to exist.
If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed. If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed: $app->group(['as' => 'foo', 'prefix' => 'foo', 'namespace' => 'Foo'], function($app) {
$app->group(['as' => 'foo', 'prefix' => 'foo'], function($app) {
$app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'FooController@getFoo']);
$app->get('/all/{from}/{to}', ['as' => 'all', 'uses' => 'BarController@getBar']);
});
});
![]() |
|||
294 | ]); |
||
295 | }); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @param \Closure $callback |
||
300 | */ |
||
301 | protected function registerRoutes(\Closure $callback) |
||
302 | { |
||
303 | $domain = config('sleeping_owl.domain', false); |
||
304 | $configGroup = collect([ |
||
305 | 'prefix' => $this->getConfig('url_prefix'), |
||
306 | 'middleware' => $this->getConfig('middleware'), |
||
307 | ]); |
||
308 | |||
309 | if ($domain) { |
||
310 | $configGroup->put('domain', $domain); |
||
311 | } |
||
312 | |||
313 | $this->app['router']->group($configGroup->toArray(), function (Router $route) use ($callback) { |
||
314 | call_user_func($callback, $route); |
||
315 | }); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Register navigation file. |
||
320 | */ |
||
321 | protected function registerNavigationFile() |
||
322 | { |
||
323 | if (file_exists($navigation = $this->getBootstrapPath('navigation.php'))) { |
||
324 | $items = include $navigation; |
||
325 | |||
326 | if (is_array($items)) { |
||
327 | $this->app['sleeping_owl.navigation']->setFromArray($items); |
||
328 | } |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 |
If there is a route defined but the controller class cannot be found there are two options: 1. the controller class needs to be implemented or 2. the route is outdated and can be removed.
If ?FooController? was found and ?BarController? is missing for the following example, either the controller should be implemented or the route should be removed: