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