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