|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Foundation\Providers; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Routing\Router; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
use Illuminate\Support\Facades\Schema; |
|
10
|
|
|
use Illuminate\Support\ServiceProvider; |
|
11
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
12
|
|
|
use Cortex\Foundation\Models\ImportRecord; |
|
13
|
|
|
use Cortex\Foundation\Models\AbstractModel; |
|
14
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
|
15
|
|
|
use Cortex\Foundation\Generators\LangJsGenerator; |
|
16
|
|
|
use Cortex\Foundation\Console\Commands\SeedCommand; |
|
17
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
18
|
|
|
use Cortex\Foundation\Console\Commands\InstallCommand; |
|
19
|
|
|
use Cortex\Foundation\Console\Commands\MigrateCommand; |
|
20
|
|
|
use Cortex\Foundation\Console\Commands\PublishCommand; |
|
21
|
|
|
use Cortex\Foundation\Console\Commands\CoreSeedCommand; |
|
22
|
|
|
use Cortex\Foundation\Console\Commands\RollbackCommand; |
|
23
|
|
|
use Illuminate\Support\Facades\Session as SessionFacade; |
|
24
|
|
|
use Cortex\Foundation\Verifiers\EloquentPresenceVerifier; |
|
25
|
|
|
use Cortex\Foundation\Console\Commands\CoreInstallCommand; |
|
26
|
|
|
use Cortex\Foundation\Console\Commands\CoreMigrateCommand; |
|
27
|
|
|
use Cortex\Foundation\Console\Commands\CorePublishCommand; |
|
28
|
|
|
use Mariuzzo\LaravelJsLocalization\Commands\LangJsCommand; |
|
29
|
|
|
use Cortex\Foundation\Console\Commands\CoreRollbackCommand; |
|
30
|
|
|
use Cortex\Foundation\Http\Middleware\NotificationMiddleware; |
|
31
|
|
|
use Cortex\Foundation\Overrides\Illuminate\Routing\Redirector; |
|
32
|
|
|
use Cortex\Foundation\Overrides\Illuminate\Routing\UrlGenerator; |
|
33
|
|
|
use Cortex\Foundation\Overrides\Mcamara\LaravelLocalization\LaravelLocalization; |
|
34
|
|
|
|
|
35
|
|
|
class FoundationServiceProvider extends ServiceProvider |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* The commands to be registered. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $commands = [ |
|
43
|
|
|
SeedCommand::class => 'command.cortex.foundation.seed', |
|
44
|
|
|
InstallCommand::class => 'command.cortex.foundation.install', |
|
45
|
|
|
MigrateCommand::class => 'command.cortex.foundation.migrate', |
|
46
|
|
|
PublishCommand::class => 'command.cortex.foundation.publish', |
|
47
|
|
|
RollbackCommand::class => 'command.cortex.foundation.rollback', |
|
48
|
|
|
CoreSeedCommand::class => 'command.cortex.foundation.coreseed', |
|
49
|
|
|
CoreInstallCommand::class => 'command.cortex.foundation.coreinstall', |
|
50
|
|
|
CoreMigrateCommand::class => 'command.cortex.foundation.coremigrate', |
|
51
|
|
|
CorePublishCommand::class => 'command.cortex.foundation.corempublish', |
|
52
|
|
|
CoreRollbackCommand::class => 'command.cortex.foundation.corerollback', |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Register any application services. |
|
57
|
|
|
* |
|
58
|
|
|
* This service provider is a great spot to register your various container |
|
59
|
|
|
* bindings with the application. As you can see, we are registering our |
|
60
|
|
|
* "Registrar" implementation here. You can add your own bindings too! |
|
61
|
|
|
* |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
public function register(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->overrideNotificationMiddleware(); |
|
67
|
|
|
$this->overrideLaravelLocalization(); |
|
68
|
|
|
$this->overrideUrlGenerator(); |
|
69
|
|
|
$this->bindPresenceVerifier(); |
|
70
|
|
|
$this->bindBlueprintMacro(); |
|
71
|
|
|
$this->overrideRedirector(); |
|
72
|
|
|
$this->bindBladeCompiler(); |
|
73
|
|
|
$this->overrideLangJS(); |
|
74
|
|
|
|
|
75
|
|
|
// Bind eloquent models to IoC container |
|
76
|
|
|
$this->app->singleton('cortex.foundation.import_record', $importerModel = $this->app['config']['cortex.foundation.models.import_record']); |
|
|
|
|
|
|
77
|
|
|
$importerModel === ImportRecord::class || $this->app->alias('cortex.foundation.import_record', ImportRecord::class); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
// Merge config |
|
80
|
|
|
$this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'cortex.foundation'); |
|
81
|
|
|
|
|
82
|
|
|
// Override datatables html builder |
|
83
|
|
|
$this->app->bind(\Yajra\DataTables\Html\Builder::class, \Cortex\Foundation\Overrides\Yajra\DataTables\Html\Builder::class); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
// Register console commands |
|
86
|
|
|
! $this->app->runningInConsole() || $this->registerCommands(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Bootstrap any application services. |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
public function boot(Router $router): void |
|
95
|
|
|
{ |
|
96
|
|
|
// Fix the specified key was too long error |
|
97
|
|
|
Schema::defaultStringLength(191); |
|
98
|
|
|
|
|
99
|
|
|
// Override presence verifier |
|
100
|
|
|
$this->app['validator']->setPresenceVerifier($this->app['cortex.foundation.presence.verifier']); |
|
101
|
|
|
|
|
102
|
|
|
// Early set application locale globaly |
|
103
|
|
|
$router->pattern('locale', '[a-z]{2}'); |
|
104
|
|
|
$this->app['laravellocalization']->setLocale(); |
|
105
|
|
|
|
|
106
|
|
|
$router->model('media', config('medialibrary.media_model')); |
|
107
|
|
|
|
|
108
|
|
|
// Map relations |
|
109
|
|
|
Relation::morphMap([ |
|
110
|
|
|
'media' => config('medialibrary.media_model'), |
|
111
|
|
|
]); |
|
112
|
|
|
|
|
113
|
|
|
// Load resources |
|
114
|
|
|
require __DIR__.'/../../routes/breadcrumbs/frontarea.php'; |
|
115
|
|
|
require __DIR__.'/../../routes/breadcrumbs/tenantarea.php'; |
|
116
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web/adminarea.php'); |
|
117
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web/frontarea.php'); |
|
118
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web/tenantarea.php'); |
|
119
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web/managerarea.php'); |
|
120
|
|
|
$this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/foundation'); |
|
121
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/foundation'); |
|
122
|
|
|
! $this->app->runningInConsole() || $this->loadMigrationsFrom(__DIR__.'/../../database/migrations'); |
|
123
|
|
|
$this->app->runningInConsole() || $this->app->afterResolving('blade.compiler', function () { |
|
124
|
|
|
require __DIR__.'/../../routes/menus/managerarea.php'; |
|
125
|
|
|
require __DIR__.'/../../routes/menus/tenantarea.php'; |
|
126
|
|
|
require __DIR__.'/../../routes/menus/adminarea.php'; |
|
127
|
|
|
require __DIR__.'/../../routes/menus/frontarea.php'; |
|
128
|
|
|
}); |
|
129
|
|
|
|
|
130
|
|
|
// Publish Resources |
|
131
|
|
|
! $this->app->runningInConsole() || $this->publishResources(); |
|
132
|
|
|
|
|
133
|
|
|
SessionFacade::extend('database', function ($app) { |
|
134
|
|
|
$table = $app['config']['session.table']; |
|
135
|
|
|
|
|
136
|
|
|
$lifetime = $app['config']['session.lifetime']; |
|
137
|
|
|
$connection = $app['config']['session.connection']; |
|
138
|
|
|
|
|
139
|
|
|
return new \Cortex\Foundation\Overrides\Illuminate\Session\DatabaseSessionHandler( |
|
140
|
|
|
$app['db']->connection($connection), $table, $lifetime, $app |
|
141
|
|
|
); |
|
142
|
|
|
}); |
|
143
|
|
|
|
|
144
|
|
|
$this->app->booted(function () { |
|
145
|
|
|
if ($this->app->routesAreCached()) { |
|
146
|
|
|
require $this->app->getCachedRoutesPath(); |
|
147
|
|
|
} else { |
|
148
|
|
|
$this->app['router']->getRoutes()->refreshNameLookups(); |
|
149
|
|
|
$this->app['router']->getRoutes()->refreshActionLookups(); |
|
150
|
|
|
} |
|
151
|
|
|
}); |
|
152
|
|
|
|
|
153
|
|
|
Collection::macro('similar', function (Collection $newCollection) { |
|
154
|
|
|
return $newCollection->diff($this)->isEmpty() && $this->diff($newCollection)->isEmpty(); |
|
|
|
|
|
|
155
|
|
|
}); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Override notification middleware. |
|
160
|
|
|
* |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function overrideNotificationMiddleware(): void |
|
164
|
|
|
{ |
|
165
|
|
|
$this->app->singleton('Cortex\Foundation\Http\Middleware\NotificationMiddleware', function ($app) { |
|
166
|
|
|
return new NotificationMiddleware( |
|
167
|
|
|
$app['session.store'], |
|
168
|
|
|
$app['notification'], |
|
169
|
|
|
$app['config']->get('notification.session_key') |
|
170
|
|
|
); |
|
171
|
|
|
}); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Bind blade compiler. |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function bindBladeCompiler(): void |
|
180
|
|
|
{ |
|
181
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
|
182
|
|
|
|
|
183
|
|
|
// @alerts('container') |
|
184
|
|
|
$bladeCompiler->directive('alerts', function ($container = null) { |
|
185
|
|
|
if (strcasecmp('()', $container) === 0) { |
|
186
|
|
|
$container = null; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return "<?php echo app('notification')->container({$container})->show(); ?>"; |
|
190
|
|
|
}); |
|
191
|
|
|
}); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Override the Redirector instance. |
|
196
|
|
|
* |
|
197
|
|
|
* @return void |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function overrideRedirector(): void |
|
200
|
|
|
{ |
|
201
|
|
|
$this->app->singleton('redirect', function ($app) { |
|
202
|
|
|
$redirector = new Redirector($app['url']); |
|
203
|
|
|
|
|
204
|
|
|
// If the session is set on the application instance, we'll inject it into |
|
205
|
|
|
// the redirector instance. This allows the redirect responses to allow |
|
206
|
|
|
// for the quite convenient "with" methods that flash to the session. |
|
207
|
|
|
if (isset($app['session.store'])) { |
|
208
|
|
|
$redirector->setSession($app['session.store']); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $redirector; |
|
212
|
|
|
}); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Override the UrlGenerator instance. |
|
217
|
|
|
* |
|
218
|
|
|
* @return void |
|
219
|
|
|
*/ |
|
220
|
|
|
protected function overrideUrlGenerator(): void |
|
221
|
|
|
{ |
|
222
|
|
|
$this->app->singleton('url', function ($app) { |
|
223
|
|
|
$routes = $app['router']->getRoutes(); |
|
224
|
|
|
|
|
225
|
|
|
// The URL generator needs the route collection that exists on the router. |
|
226
|
|
|
// Keep in mind this is an object, so we're passing by references here |
|
227
|
|
|
// and all the registered routes will be available to the generator. |
|
228
|
|
|
$app->instance('routes', $routes); |
|
229
|
|
|
|
|
230
|
|
|
$url = new UrlGenerator( |
|
231
|
|
|
$routes, $app->rebinding( |
|
232
|
|
|
'request', $this->requestRebinder() |
|
233
|
|
|
) |
|
234
|
|
|
); |
|
235
|
|
|
|
|
236
|
|
|
$url->setSessionResolver(function () { |
|
237
|
|
|
return $this->app['session']; |
|
238
|
|
|
}); |
|
239
|
|
|
|
|
240
|
|
|
// If the route collection is "rebound", for example, when the routes stay |
|
241
|
|
|
// cached for the application, we will need to rebind the routes on the |
|
242
|
|
|
// URL generator instance so it has the latest version of the routes. |
|
243
|
|
|
$app->rebinding('routes', function ($app, $routes) { |
|
244
|
|
|
$app['url']->setRoutes($routes); |
|
245
|
|
|
}); |
|
246
|
|
|
|
|
247
|
|
|
return $url; |
|
248
|
|
|
}); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Get the URL generator request rebinder. |
|
253
|
|
|
* |
|
254
|
|
|
* @return \Closure |
|
255
|
|
|
*/ |
|
256
|
|
|
protected function requestRebinder() |
|
257
|
|
|
{ |
|
258
|
|
|
return function ($app, $request) { |
|
259
|
|
|
$app['url']->setRequest($request); |
|
260
|
|
|
}; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Override the LaravelLocalization instance. |
|
265
|
|
|
* |
|
266
|
|
|
* @return void |
|
267
|
|
|
*/ |
|
268
|
|
|
protected function overrideLaravelLocalization(): void |
|
269
|
|
|
{ |
|
270
|
|
|
$this->app->singleton('laravellocalization', function () { |
|
271
|
|
|
return new LaravelLocalization(); |
|
272
|
|
|
}); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Publish resources. |
|
277
|
|
|
* |
|
278
|
|
|
* @return void |
|
279
|
|
|
*/ |
|
280
|
|
|
protected function publishResources(): void |
|
281
|
|
|
{ |
|
282
|
|
|
$this->publishes([realpath(__DIR__.'/../../database/migrations') => database_path('migrations')], 'cortex-foundation-migrations'); |
|
|
|
|
|
|
283
|
|
|
$this->publishes([realpath(__DIR__.'/../../config/config.php') => config_path('cortex.foundation.php')], 'cortex-foundation-config'); |
|
|
|
|
|
|
284
|
|
|
$this->publishes([realpath(__DIR__.'/../../resources/lang') => resource_path('lang/vendor/cortex/foundation')], 'cortex-foundation-lang'); |
|
|
|
|
|
|
285
|
|
|
$this->publishes([realpath(__DIR__.'/../../resources/views') => resource_path('views/vendor/cortex/foundation')], 'cortex-foundation-views'); |
|
|
|
|
|
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Register console commands. |
|
290
|
|
|
* |
|
291
|
|
|
* @return void |
|
292
|
|
|
*/ |
|
293
|
|
|
protected function registerCommands(): void |
|
294
|
|
|
{ |
|
295
|
|
|
// Register artisan commands |
|
296
|
|
|
foreach ($this->commands as $key => $value) { |
|
297
|
|
|
$this->app->singleton($value, $key); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
$this->commands(array_values($this->commands)); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Register console commands. |
|
305
|
|
|
* |
|
306
|
|
|
* @return void |
|
307
|
|
|
*/ |
|
308
|
|
|
protected function overrideLangJS(): void |
|
309
|
|
|
{ |
|
310
|
|
|
// Bind the Laravel JS Localization command into the app IOC. |
|
311
|
|
|
$this->app->singleton('localization.js', function ($app) { |
|
|
|
|
|
|
312
|
|
|
$app = $this->app; |
|
313
|
|
|
$laravelMajorVersion = (int) $app::VERSION; |
|
314
|
|
|
|
|
315
|
|
|
$files = $app['files']; |
|
316
|
|
|
|
|
317
|
|
|
if ($laravelMajorVersion === 4) { |
|
318
|
|
|
$langs = $app['path.base'].'/app/lang'; |
|
319
|
|
|
} elseif ($laravelMajorVersion === 5) { |
|
320
|
|
|
$langs = $app['path.base'].'/resources/lang'; |
|
321
|
|
|
} |
|
322
|
|
|
$messages = $app['config']->get('localization-js.messages'); |
|
323
|
|
|
$generator = new LangJsGenerator($files, $langs, $messages); |
|
|
|
|
|
|
324
|
|
|
|
|
325
|
|
|
return new LangJsCommand($generator); |
|
326
|
|
|
}); |
|
327
|
|
|
|
|
328
|
|
|
// Bind the Laravel JS Localization command into Laravel Artisan. |
|
329
|
|
|
$this->commands('localization.js'); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Bind presence verifier. |
|
334
|
|
|
* |
|
335
|
|
|
* @return void |
|
336
|
|
|
*/ |
|
337
|
|
|
protected function bindPresenceVerifier(): void |
|
338
|
|
|
{ |
|
339
|
|
|
$this->app->bind('cortex.foundation.presence.verifier', function ($app) { |
|
340
|
|
|
return new EloquentPresenceVerifier($app['db'], new $app[AbstractModel::class]()); |
|
341
|
|
|
}); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* Bind blueprint macro. |
|
346
|
|
|
* |
|
347
|
|
|
* @return void |
|
348
|
|
|
*/ |
|
349
|
|
|
protected function bindBlueprintMacro(): void |
|
350
|
|
|
{ |
|
351
|
|
|
Blueprint::macro('auditable', function () { |
|
352
|
|
|
$this->integer('created_by_id')->unsigned()->after('created_at')->nullable(); |
|
|
|
|
|
|
353
|
|
|
$this->string('created_by_type')->after('created_at')->nullable(); |
|
|
|
|
|
|
354
|
|
|
$this->integer('updated_by_id')->unsigned()->after('updated_at')->nullable(); |
|
|
|
|
|
|
355
|
|
|
$this->string('updated_by_type')->after('updated_at')->nullable(); |
|
|
|
|
|
|
356
|
|
|
}); |
|
357
|
|
|
|
|
358
|
|
|
Blueprint::macro('dropAuditable', function () { |
|
359
|
|
|
$this->dropForeign($this->createIndexName('foreign', ['updated_by_type'])); |
|
|
|
|
|
|
360
|
|
|
$this->dropForeign($this->createIndexName('foreign', ['updated_by_id'])); |
|
|
|
|
|
|
361
|
|
|
$this->dropForeign($this->createIndexName('foreign', ['created_by_type'])); |
|
|
|
|
|
|
362
|
|
|
$this->dropForeign($this->createIndexName('foreign', ['created_by_id'])); |
|
|
|
|
|
|
363
|
|
|
$this->dropColumn(['updated_by_type', 'updated_by_id', 'created_by_type', 'created_by_id']); |
|
|
|
|
|
|
364
|
|
|
}); |
|
365
|
|
|
|
|
366
|
|
|
Blueprint::macro('auditableAndTimestamps', function ($precision = 0) { |
|
367
|
|
|
$this->timestamp('created_at', $precision)->nullable(); |
|
|
|
|
|
|
368
|
|
|
$this->integer('created_by_id')->unsigned()->nullable(); |
|
|
|
|
|
|
369
|
|
|
$this->string('created_by_type')->nullable(); |
|
|
|
|
|
|
370
|
|
|
$this->timestamp('updated_at', $precision)->nullable(); |
|
|
|
|
|
|
371
|
|
|
$this->integer('updated_by_id')->unsigned()->nullable(); |
|
|
|
|
|
|
372
|
|
|
$this->string('updated_by_type')->nullable(); |
|
|
|
|
|
|
373
|
|
|
}); |
|
374
|
|
|
|
|
375
|
|
|
Blueprint::macro('dropauditableAndTimestamps', function () { |
|
376
|
|
|
$this->dropForeign($this->createIndexName('foreign', ['updated_by_type'])); |
|
|
|
|
|
|
377
|
|
|
$this->dropForeign($this->createIndexName('foreign', ['updated_by_id'])); |
|
|
|
|
|
|
378
|
|
|
$this->dropForeign($this->createIndexName('foreign', ['updated_at'])); |
|
|
|
|
|
|
379
|
|
|
$this->dropForeign($this->createIndexName('foreign', ['created_by_type'])); |
|
|
|
|
|
|
380
|
|
|
$this->dropForeign($this->createIndexName('foreign', ['created_by_id'])); |
|
|
|
|
|
|
381
|
|
|
$this->dropForeign($this->createIndexName('foreign', ['created_at'])); |
|
|
|
|
|
|
382
|
|
|
$this->dropColumn(['updated_by_type', 'updated_by_id', 'updated_at', 'created_by_type', 'created_by_id', 'created_at']); |
|
|
|
|
|
|
383
|
|
|
}); |
|
384
|
|
|
} |
|
385
|
|
|
} |
|
386
|
|
|
|
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.