83 lines
2.5 KiB
PHP
83 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Support\ManagedCredentialFiles;
|
|
use App\Support\ManagedPhpRedisConnector;
|
|
use App\Support\ManagedPredisConnector;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Database\DatabaseManager;
|
|
use Illuminate\Redis\RedisManager;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Illuminate\Validation\Rules\Password;
|
|
use RuntimeException;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
$config = $this->app->make('config');
|
|
if ($config->get('database.managed_credentials') !== true) {
|
|
return;
|
|
}
|
|
|
|
$redisClient = $config->get('database.redis.client');
|
|
if ($config->get('database.default') !== 'mysql'
|
|
|| ! in_array($redisClient, ['phpredis', 'predis'], true)
|
|
|| $config->get('database.connections.mysql.url')
|
|
|| $config->get('database.redis.default.url')
|
|
|| $config->get('database.redis.cache.url')) {
|
|
throw new RuntimeException('Managed credentials require the supported MySQL and standalone Redis connections.');
|
|
}
|
|
|
|
$this->callAfterResolving('db', function (DatabaseManager $manager): void {
|
|
$manager->extend('mysql', function (array $connection, string $name) {
|
|
$connection['password'] = ManagedCredentialFiles::databasePassword();
|
|
|
|
return $this->app->make('db.factory')->make($connection, $name);
|
|
});
|
|
});
|
|
|
|
$this->callAfterResolving('redis', function (RedisManager $manager) use ($redisClient): void {
|
|
$manager->extend($redisClient, fn () => $redisClient === 'predis'
|
|
? new ManagedPredisConnector
|
|
: new ManagedPhpRedisConnector);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
$this->configureDefaults();
|
|
}
|
|
|
|
/**
|
|
* Configure default behaviors for production-ready applications.
|
|
*/
|
|
protected function configureDefaults(): void
|
|
{
|
|
Date::use(CarbonImmutable::class);
|
|
|
|
DB::prohibitDestructiveCommands(
|
|
app()->isProduction(),
|
|
);
|
|
|
|
Password::defaults(fn (): ?Password => app()->isProduction()
|
|
? Password::min(12)
|
|
->mixedCase()
|
|
->letters()
|
|
->numbers()
|
|
->symbols()
|
|
->uncompromised()
|
|
: null,
|
|
);
|
|
}
|
|
}
|