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