1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Providers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
6
|
|
|
use SleepingOwl\Admin\AliasBinder; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
use Illuminate\Foundation\AliasLoader; |
9
|
|
|
use Illuminate\Support\ServiceProvider; |
10
|
|
|
use SleepingOwl\Admin\Routing\ModelRouter; |
11
|
|
|
use SleepingOwl\Admin\Widgets\WidgetsRegistry; |
12
|
|
|
use SleepingOwl\Admin\Exceptions\TemplateException; |
13
|
|
|
use Illuminate\Contracts\View\Factory as ViewFactory; |
14
|
|
|
use SleepingOwl\Admin\Model\ModelConfigurationManager; |
15
|
|
|
use SleepingOwl\Admin\Contracts\Form\FormButtonsInterface; |
16
|
|
|
use SleepingOwl\Admin\Contracts\Repositories\RepositoryInterface; |
17
|
|
|
use SleepingOwl\Admin\Contracts\Widgets\WidgetsRegistryInterface; |
18
|
|
|
use SleepingOwl\Admin\Contracts\Display\TableHeaderColumnInterface; |
19
|
|
|
|
20
|
|
|
class AdminServiceProvider extends ServiceProvider |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $directory; |
26
|
|
|
|
27
|
285 |
|
public function register() |
28
|
|
|
{ |
29
|
285 |
|
$this->registerWysiwyg(); |
30
|
285 |
|
$this->registerTemplate(); |
31
|
285 |
|
$this->initializeNavigation(); |
32
|
285 |
|
$this->registerAliases(); |
33
|
|
|
|
34
|
|
|
$this->app->singleton('sleeping_owl.widgets', function () { |
35
|
285 |
|
return new WidgetsRegistry($this->app); |
36
|
285 |
|
}); |
37
|
|
|
|
38
|
|
|
$this->app->booted(function () { |
39
|
285 |
|
$this->app['sleeping_owl.widgets']->placeWidgets( |
40
|
285 |
|
$this->app[ViewFactory::class] |
41
|
285 |
|
); |
42
|
285 |
|
}); |
43
|
|
|
|
44
|
|
|
$this->app->booted(function () { |
45
|
285 |
|
$this->registerCustomRoutes(); |
46
|
285 |
|
$this->registerDefaultRoutes(); |
47
|
285 |
|
$this->registerNavigationFile(); |
48
|
|
|
|
49
|
285 |
|
$this->app['sleeping_owl']->initialize(); |
50
|
285 |
|
}); |
51
|
|
|
|
52
|
285 |
|
ModelConfigurationManager::setEventDispatcher($this->app['events']); |
53
|
285 |
|
} |
54
|
|
|
|
55
|
285 |
|
protected function registerTemplate() |
56
|
|
|
{ |
57
|
|
|
$this->app->singleton('assets.packages', function ($app) { |
|
|
|
|
58
|
285 |
|
return new \KodiCMS\Assets\PackageManager(); |
59
|
285 |
|
}); |
60
|
|
|
|
61
|
|
|
$this->app->singleton('sleeping_owl.meta', function ($app) { |
62
|
285 |
|
return new \SleepingOwl\Admin\Templates\Meta( |
63
|
285 |
|
new \SleepingOwl\Admin\Templates\Assets( |
64
|
285 |
|
$app['assets.packages'] |
65
|
285 |
|
) |
66
|
285 |
|
); |
67
|
285 |
|
}); |
68
|
|
|
|
69
|
|
|
$this->app->singleton('sleeping_owl.template', function ($app) { |
70
|
285 |
|
if (! class_exists($class = $this->getConfig('template'))) { |
71
|
|
|
throw new TemplateException("Template class [{$class}] not found"); |
72
|
|
|
} |
73
|
|
|
|
74
|
285 |
|
return $app->make($class); |
75
|
285 |
|
}); |
76
|
|
|
|
77
|
285 |
|
if (file_exists($assetsFile = __DIR__.'/../../resources/assets.php')) { |
78
|
285 |
|
include $assetsFile; |
79
|
285 |
|
} |
80
|
285 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $key |
84
|
|
|
* |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
285 |
|
protected function getConfig($key) |
88
|
|
|
{ |
89
|
285 |
|
return $this->app['config']->get('sleeping_owl.'.$key); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $path |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
285 |
|
protected function getBootstrapPath($path = null) |
98
|
|
|
{ |
99
|
285 |
|
if (! is_null($path)) { |
100
|
285 |
|
$path = DIRECTORY_SEPARATOR.$path; |
101
|
285 |
|
} |
102
|
|
|
|
103
|
285 |
|
return $this->getConfig('bootstrapDirectory').$path; |
104
|
|
|
} |
105
|
|
|
|
106
|
285 |
|
public function boot() |
107
|
|
|
{ |
108
|
285 |
|
$this->registerMessages(); |
109
|
285 |
|
$this->registerBootstrap(); |
110
|
285 |
|
} |
111
|
|
|
|
112
|
285 |
|
protected function registerMessages() |
113
|
|
|
{ |
114
|
|
|
$messageTypes = [ |
115
|
285 |
|
'error' => \SleepingOwl\Admin\Widgets\Messages\ErrorMessages::class, |
116
|
285 |
|
'info' => \SleepingOwl\Admin\Widgets\Messages\InfoMessages::class, |
117
|
285 |
|
'success' => \SleepingOwl\Admin\Widgets\Messages\SuccessMessages::class, |
118
|
285 |
|
'warning' => \SleepingOwl\Admin\Widgets\Messages\WarningMessages::class, |
119
|
285 |
|
]; |
120
|
285 |
|
foreach ($messageTypes as $messageType) { |
121
|
285 |
|
$this->app[WidgetsRegistryInterface::class]->registerWidget($messageType); |
122
|
285 |
|
} |
123
|
|
|
|
124
|
|
|
$this->app->singleton('sleeping_owl.message', function () use ($messageTypes) { |
125
|
|
|
return new \SleepingOwl\Admin\Widgets\Messages\MessageStack($messageTypes); |
126
|
285 |
|
}); |
127
|
285 |
|
} |
128
|
|
|
|
129
|
285 |
|
protected function initializeNavigation() |
130
|
|
|
{ |
131
|
285 |
|
$this->app->bind(TableHeaderColumnInterface::class, \SleepingOwl\Admin\Display\TableHeaderColumn::class); |
132
|
285 |
|
$this->app->bind(RepositoryInterface::class, \SleepingOwl\Admin\Repositories\BaseRepository::class); |
133
|
285 |
|
$this->app->bind(FormButtonsInterface::class, \SleepingOwl\Admin\Form\FormButtons::class); |
134
|
|
|
|
135
|
285 |
|
$this->app->bind(\KodiComponents\Navigation\Contracts\PageInterface::class, \SleepingOwl\Admin\Navigation\Page::class); |
136
|
285 |
|
$this->app->bind(\KodiComponents\Navigation\Contracts\BadgeInterface::class, \SleepingOwl\Admin\Navigation\Badge::class); |
137
|
|
|
|
138
|
|
|
$this->app->singleton('sleeping_owl.navigation', function () { |
139
|
285 |
|
return new \SleepingOwl\Admin\Navigation(); |
140
|
285 |
|
}); |
141
|
285 |
|
} |
142
|
|
|
|
143
|
285 |
|
protected function registerWysiwyg() |
144
|
|
|
{ |
145
|
|
|
$this->app->singleton('sleeping_owl.wysiwyg', function () { |
146
|
285 |
|
return new \SleepingOwl\Admin\Wysiwyg\Manager($this->app); |
147
|
285 |
|
}); |
148
|
285 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
285 |
|
protected function registerBootstrap() |
154
|
|
|
{ |
155
|
285 |
|
$directory = $this->getBootstrapPath(); |
156
|
|
|
|
157
|
285 |
|
if (! is_dir($directory)) { |
158
|
285 |
|
return; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$files = Finder::create() |
162
|
|
|
->files() |
163
|
|
|
->name('/^.+\.php$/') |
164
|
|
|
->notName('routes.php') |
165
|
|
|
->notName('navigation.php') |
166
|
|
|
->in($directory) |
167
|
|
|
->sort(function ($a) { |
168
|
|
|
return $a->getFilename() != 'bootstrap.php'; |
169
|
|
|
}); |
170
|
|
|
|
171
|
|
|
foreach ($files as $file) { |
172
|
|
|
require $file; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
285 |
|
protected function registerAliases() |
177
|
|
|
{ |
178
|
285 |
|
AliasLoader::getInstance(config('sleeping_owl.aliases', [])); |
179
|
285 |
|
} |
180
|
|
|
|
181
|
285 |
|
protected function registerCustomRoutes() |
182
|
|
|
{ |
183
|
285 |
|
if (file_exists($file = $this->getBootstrapPath('routes.php'))) { |
184
|
|
|
$this->registerRoutes(function (Router $route) use ($file) { |
185
|
|
|
require $file; |
186
|
|
|
}); |
187
|
|
|
} |
188
|
285 |
|
} |
189
|
|
|
|
190
|
285 |
|
protected function registerDefaultRoutes() |
191
|
|
|
{ |
192
|
|
|
$this->registerRoutes(function (Router $router) { |
193
|
285 |
|
(new ModelRouter($this->app, $router))->register($this->app['sleeping_owl']->getModels()); |
194
|
|
|
|
195
|
285 |
|
if (file_exists($routesFile = __DIR__.'/../Http/routes.php')) { |
196
|
285 |
|
require $routesFile; |
197
|
285 |
|
} |
198
|
|
|
|
199
|
285 |
|
AliasBinder::registerRoutes($router); |
200
|
285 |
|
}); |
201
|
285 |
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param \Closure $callback |
205
|
|
|
*/ |
206
|
285 |
|
protected function registerRoutes(\Closure $callback) |
207
|
|
|
{ |
208
|
285 |
|
$this->app['router']->group([ |
209
|
285 |
|
'prefix' => $this->getConfig('url_prefix'), |
210
|
285 |
|
'middleware' => $this->getConfig('middleware'), |
211
|
285 |
|
], function ($route) use ($callback) { |
212
|
285 |
|
call_user_func($callback, $route); |
213
|
285 |
|
}); |
214
|
285 |
|
} |
215
|
|
|
|
216
|
285 |
|
protected function registerNavigationFile() |
217
|
|
|
{ |
218
|
285 |
|
if (file_exists($navigation = $this->getBootstrapPath('navigation.php'))) { |
219
|
|
|
$items = include $navigation; |
220
|
|
|
|
221
|
|
|
if (is_array($items)) { |
222
|
|
|
$this->app['sleeping_owl.navigation']->setFromArray($items); |
223
|
|
|
} |
224
|
|
|
} |
225
|
285 |
|
} |
226
|
|
|
} |
227
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.