|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Foundation\Providers; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Routing\Router; |
|
8
|
|
|
use Cortex\Foundation\Models\Menu; |
|
9
|
|
|
use Illuminate\Contracts\Http\Kernel; |
|
10
|
|
|
use Illuminate\Support\ServiceProvider; |
|
11
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
|
12
|
|
|
use Cortex\Foundation\Console\Commands\SeedCommand; |
|
13
|
|
|
use Cortex\Foundation\Console\Commands\InstallCommand; |
|
14
|
|
|
use Cortex\Foundation\Console\Commands\MigrateCommand; |
|
15
|
|
|
use Cortex\Foundation\Console\Commands\PublishCommand; |
|
16
|
|
|
use Cortex\Foundation\Console\Commands\CoreSeedCommand; |
|
17
|
|
|
use Cortex\Foundation\Console\Commands\CoreInstallCommand; |
|
18
|
|
|
use Cortex\Foundation\Console\Commands\CoreMigrateCommand; |
|
19
|
|
|
use Cortex\Foundation\Console\Commands\CorePublishCommand; |
|
20
|
|
|
use Cortex\Foundation\Http\Middleware\TrailingSlashEnforce; |
|
21
|
|
|
use Cortex\Foundation\Http\Middleware\NotificationMiddleware; |
|
22
|
|
|
use Cortex\Foundation\Overrides\Illuminate\Routing\Redirector; |
|
23
|
|
|
use Cortex\Foundation\Overrides\Yajra\DataTables\Html\Builder; |
|
24
|
|
|
use Cortex\Foundation\Overrides\Illuminate\Routing\UrlGenerator; |
|
25
|
|
|
use Cortex\Foundation\Overrides\Mcamara\LaravelLocalization\LaravelLocalization; |
|
26
|
|
|
|
|
27
|
|
|
class FoundationServiceProvider extends ServiceProvider |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* The commands to be registered. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $commands = [ |
|
35
|
|
|
CoreMigrateCommand::class => 'command.cortex.foundation.coremigrate', |
|
36
|
|
|
CoreInstallCommand::class => 'command.cortex.foundation.coreinstall', |
|
37
|
|
|
CorePublishCommand::class => 'command.cortex.foundation.corempublish', |
|
38
|
|
|
CoreSeedCommand::class => 'command.cortex.foundation.coreseed', |
|
39
|
|
|
MigrateCommand::class => 'command.cortex.foundation.migrate', |
|
40
|
|
|
PublishCommand::class => 'command.cortex.foundation.publish', |
|
41
|
|
|
InstallCommand::class => 'command.cortex.foundation.install', |
|
42
|
|
|
SeedCommand::class => 'command.cortex.foundation.seed', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Register any application services. |
|
47
|
|
|
* |
|
48
|
|
|
* This service provider is a great spot to register your various container |
|
49
|
|
|
* bindings with the application. As you can see, we are registering our |
|
50
|
|
|
* "Registrar" implementation here. You can add your own bindings too! |
|
51
|
|
|
* |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function register() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->overrideNotificationMiddleware(); |
|
57
|
|
|
$this->overrideLaravelLocalization(); |
|
58
|
|
|
$this->overrideUrlGenerator(); |
|
59
|
|
|
$this->overrideRedirector(); |
|
60
|
|
|
$this->bindBladeCompiler(); |
|
61
|
|
|
|
|
62
|
|
|
// Merge config |
|
63
|
|
|
$this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'cortex.foundation'); |
|
64
|
|
|
|
|
65
|
|
|
// Register custom datatables html builder |
|
66
|
|
|
$this->app->singleton('cortex.foundation.datatables.html', function () { |
|
67
|
|
|
return $this->app->make(Builder::class); |
|
68
|
|
|
}); |
|
69
|
|
|
|
|
70
|
|
|
// Register console commands |
|
71
|
|
|
! $this->app->runningInConsole() || $this->registerCommands(); |
|
72
|
|
|
|
|
73
|
|
|
$this->registerMenus(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Bootstrap any application services. |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function boot(Router $router) |
|
82
|
|
|
{ |
|
83
|
|
|
// Early set application locale globaly |
|
84
|
|
|
$router->pattern('locale', '[a-z]{2}'); |
|
85
|
|
|
$this->app['laravellocalization']->setLocale(); |
|
86
|
|
|
|
|
87
|
|
|
// Load resources |
|
88
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web.php'); |
|
89
|
|
|
$this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/foundation'); |
|
90
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/foundation'); |
|
91
|
|
|
! $this->app->runningInConsole() || $this->loadMigrationsFrom(__DIR__.'/../../database/migrations'); |
|
92
|
|
|
|
|
93
|
|
|
// Publish Resources |
|
94
|
|
|
! $this->app->runningInConsole() || $this->publishResources(); |
|
95
|
|
|
|
|
96
|
|
|
$this->app->booted(function () { |
|
97
|
|
|
if ($this->app->routesAreCached()) { |
|
98
|
|
|
require $this->app->getCachedRoutesPath(); |
|
99
|
|
|
} else { |
|
100
|
|
|
$this->app['router']->getRoutes()->refreshNameLookups(); |
|
101
|
|
|
$this->app['router']->getRoutes()->refreshActionLookups(); |
|
102
|
|
|
} |
|
103
|
|
|
}); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Override notification middleware. |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function overrideNotificationMiddleware() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->app->singleton('Cortex\Foundation\Http\Middleware\NotificationMiddleware', function ($app) { |
|
114
|
|
|
return new NotificationMiddleware( |
|
115
|
|
|
$app['session.store'], |
|
116
|
|
|
$app['notification'], |
|
117
|
|
|
$app['config']->get('notification.session_key') |
|
118
|
|
|
); |
|
119
|
|
|
}); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Bind blade compiler. |
|
124
|
|
|
* |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function bindBladeCompiler() |
|
128
|
|
|
{ |
|
129
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
|
130
|
|
|
|
|
131
|
|
|
// @alerts('container') |
|
132
|
|
|
$bladeCompiler->directive('alerts', function ($container = null) { |
|
133
|
|
|
if (strcasecmp('()', $container) === 0) { |
|
134
|
|
|
$container = null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return "<?php echo app('notification')->container({$container})->show(); ?>"; |
|
138
|
|
|
}); |
|
139
|
|
|
}); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Override the Redirector instance. |
|
144
|
|
|
* |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function overrideRedirector() |
|
148
|
|
|
{ |
|
149
|
|
|
$this->app->singleton('redirect', function ($app) { |
|
150
|
|
|
$redirector = new Redirector($app['url']); |
|
151
|
|
|
|
|
152
|
|
|
// If the session is set on the application instance, we'll inject it into |
|
153
|
|
|
// the redirector instance. This allows the redirect responses to allow |
|
154
|
|
|
// for the quite convenient "with" methods that flash to the session. |
|
155
|
|
|
if (isset($app['session.store'])) { |
|
156
|
|
|
$redirector->setSession($app['session.store']); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $redirector; |
|
160
|
|
|
}); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Override the UrlGenerator instance. |
|
165
|
|
|
* |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function overrideUrlGenerator() |
|
169
|
|
|
{ |
|
170
|
|
|
$this->app->singleton('url', function ($app) { |
|
171
|
|
|
$routes = $app['router']->getRoutes(); |
|
172
|
|
|
|
|
173
|
|
|
// The URL generator needs the route collection that exists on the router. |
|
174
|
|
|
// Keep in mind this is an object, so we're passing by references here |
|
175
|
|
|
// and all the registered routes will be available to the generator. |
|
176
|
|
|
$app->instance('routes', $routes); |
|
177
|
|
|
|
|
178
|
|
|
$url = new UrlGenerator( |
|
179
|
|
|
$routes, $app->rebinding( |
|
180
|
|
|
'request', $this->requestRebinder() |
|
181
|
|
|
) |
|
182
|
|
|
); |
|
183
|
|
|
|
|
184
|
|
|
$url->setSessionResolver(function () { |
|
185
|
|
|
return $this->app['session']; |
|
186
|
|
|
}); |
|
187
|
|
|
|
|
188
|
|
|
// If the route collection is "rebound", for example, when the routes stay |
|
189
|
|
|
// cached for the application, we will need to rebind the routes on the |
|
190
|
|
|
// URL generator instance so it has the latest version of the routes. |
|
191
|
|
|
$app->rebinding('routes', function ($app, $routes) { |
|
192
|
|
|
$app['url']->setRoutes($routes); |
|
193
|
|
|
}); |
|
194
|
|
|
|
|
195
|
|
|
return $url; |
|
196
|
|
|
}); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Get the URL generator request rebinder. |
|
201
|
|
|
* |
|
202
|
|
|
* @return \Closure |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function requestRebinder() |
|
205
|
|
|
{ |
|
206
|
|
|
return function ($app, $request) { |
|
207
|
|
|
$app['url']->setRequest($request); |
|
208
|
|
|
}; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Override the LaravelLocalization instance. |
|
213
|
|
|
* |
|
214
|
|
|
* @return void |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function overrideLaravelLocalization() |
|
217
|
|
|
{ |
|
218
|
|
|
$this->app->singleton('laravellocalization', function () { |
|
219
|
|
|
return new LaravelLocalization(); |
|
220
|
|
|
}); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Publish resources. |
|
225
|
|
|
* |
|
226
|
|
|
* @return void |
|
227
|
|
|
*/ |
|
228
|
|
|
protected function publishResources() |
|
229
|
|
|
{ |
|
230
|
|
|
$this->publishes([realpath(__DIR__.'/../../config/config.php') => config_path('cortex.foundation.php')], 'cortex-foundation-config'); |
|
|
|
|
|
|
231
|
|
|
$this->publishes([realpath(__DIR__.'/../../resources/lang') => resource_path('lang/vendor/cortex/foundation')], 'cortex-foundation-lang'); |
|
|
|
|
|
|
232
|
|
|
$this->publishes([realpath(__DIR__.'/../../resources/views') => resource_path('views/vendor/cortex/foundation')], 'cortex-foundation-views'); |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Register menus. |
|
237
|
|
|
* |
|
238
|
|
|
* @return void |
|
239
|
|
|
*/ |
|
240
|
|
|
protected function registerMenus() |
|
241
|
|
|
{ |
|
242
|
|
|
$this->app->singleton(Menu::class, function () { |
|
243
|
|
|
return Menu::new(); |
|
244
|
|
|
}); |
|
245
|
|
|
|
|
246
|
|
|
$this->app->alias(Menu::class, 'menu'); |
|
247
|
|
|
|
|
248
|
|
|
$this->registerAdminareaMenus(); |
|
249
|
|
|
$this->registerUserareaMenus(); |
|
250
|
|
|
$this->registerGuestareaMenus(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Register adminarea menus. |
|
255
|
|
|
* |
|
256
|
|
|
* @return void |
|
257
|
|
|
*/ |
|
258
|
|
|
protected function registerAdminareaMenus() |
|
259
|
|
|
{ |
|
260
|
|
|
$app = $this->app; |
|
261
|
|
|
|
|
262
|
|
|
Menu::macro('adminareaSidebar', function ($section = null) use ($app) { |
|
263
|
|
|
$app->bound('menu.adminarea.sidebar') || $app->singleton('menu.adminarea.sidebar', function () { |
|
264
|
|
|
return Menu::new(); |
|
265
|
|
|
}); |
|
266
|
|
|
|
|
267
|
|
|
return $app['menu.adminarea.sidebar']->setSection($section); |
|
268
|
|
|
}); |
|
269
|
|
|
|
|
270
|
|
|
Menu::macro('adminareaTopbar', function ($section = null) use ($app) { |
|
271
|
|
|
$app->bound('menu.adminarea.topbar') || $app->singleton('menu.adminarea.topbar', function () { |
|
272
|
|
|
return Menu::new(); |
|
273
|
|
|
}); |
|
274
|
|
|
|
|
275
|
|
|
return $app['menu.adminarea.topbar']->setSection($section); |
|
276
|
|
|
}); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Register memberarea menus. |
|
281
|
|
|
* |
|
282
|
|
|
* @return void |
|
283
|
|
|
*/ |
|
284
|
|
|
protected function registerUserareaMenus() |
|
285
|
|
|
{ |
|
286
|
|
|
$app = $this->app; |
|
287
|
|
|
|
|
288
|
|
|
Menu::macro('memberareaTopbar', function ($section = null) use ($app) { |
|
289
|
|
|
$app->bound('menu.memberarea.topbar') || $app->singleton('menu.memberarea.topbar', function () { |
|
290
|
|
|
return Menu::new(); |
|
291
|
|
|
}); |
|
292
|
|
|
|
|
293
|
|
|
return $app['menu.memberarea.topbar']->setSection($section); |
|
294
|
|
|
}); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Register guestarea menus. |
|
299
|
|
|
* |
|
300
|
|
|
* @return void |
|
301
|
|
|
*/ |
|
302
|
|
|
protected function registerGuestareaMenus() |
|
303
|
|
|
{ |
|
304
|
|
|
$app = $this->app; |
|
305
|
|
|
|
|
306
|
|
|
Menu::macro('guestareaTopbar', function ($section = null) use ($app) { |
|
307
|
|
|
$app->bound('menu.guestarea.topbar') || $app->singleton('menu.guestarea.topbar', function () { |
|
308
|
|
|
return Menu::new(); |
|
309
|
|
|
}); |
|
310
|
|
|
|
|
311
|
|
|
return $app['menu.guestarea.topbar']->setSection($section); |
|
312
|
|
|
}); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Register console commands. |
|
317
|
|
|
* |
|
318
|
|
|
* @return void |
|
319
|
|
|
*/ |
|
320
|
|
|
protected function registerCommands() |
|
321
|
|
|
{ |
|
322
|
|
|
// Register artisan commands |
|
323
|
|
|
foreach ($this->commands as $key => $value) { |
|
324
|
|
|
$this->app->singleton($value, function ($app) use ($key) { |
|
|
|
|
|
|
325
|
|
|
return new $key(); |
|
326
|
|
|
}); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
$this->commands(array_values($this->commands)); |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.