79 lines
1.9 KiB
PHP
79 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Dedoc\Scramble\Scramble;
|
|
use Dedoc\Scramble\Support\Generator\OpenApi;
|
|
use Dedoc\Scramble\Support\Generator\SecurityScheme;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
Model::shouldBeStrict(! app()->isProduction());
|
|
|
|
Event::listen(
|
|
events: ['eloquent.created: *', 'eloquent.updated: *', 'eloquent.deleted: *'],
|
|
listener: fn (string $eventName, array $data) => storeResourceEvent($eventName, $data, request())
|
|
);
|
|
|
|
$this->loadMigrationsFrom($this->findModuleMigrations());
|
|
|
|
Scramble::afterOpenApiGenerated(function (OpenApi $openApi) {
|
|
$openApi->secure(SecurityScheme::http('bearer'));
|
|
});
|
|
|
|
$this->listenDB();
|
|
}
|
|
|
|
/**
|
|
* Find Module migrations
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public function findModuleMigrations(): array
|
|
{
|
|
/** @var array<int, string> */
|
|
$modulesDir = scandir(modules_path());
|
|
|
|
$migrationDirectories = [];
|
|
foreach ($modulesDir as $module) {
|
|
if (is_dir(modules_path($module.'/Database/Migrations'))) {
|
|
$migrationDirectories[] = modules_path($module.'/Database/Migrations');
|
|
}
|
|
}
|
|
|
|
return $migrationDirectories;
|
|
}
|
|
|
|
/**
|
|
* Write db queries
|
|
*/
|
|
public function listenDB(): void
|
|
{
|
|
if (! app()->isLocal()) {
|
|
return;
|
|
}
|
|
|
|
DB::listen(function ($query) {
|
|
Log::info($query->sql, $query->bindings, $query->time); // @phpstan-ignore-line
|
|
});
|
|
}
|
|
}
|