Support managed runtime credentials with Predis #2
@@ -4,6 +4,7 @@ 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;
|
||||
@@ -25,12 +26,13 @@ class AppServiceProvider extends ServiceProvider
|
||||
return;
|
||||
}
|
||||
|
||||
$redisClient = $config->get('database.redis.client');
|
||||
if ($config->get('database.default') !== 'mysql'
|
||||
|| $config->get('database.redis.client') !== 'phpredis'
|
||||
|| ! 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 PhpRedis connections.');
|
||||
throw new RuntimeException('Managed credentials require the supported MySQL and standalone Redis connections.');
|
||||
}
|
||||
|
||||
$this->callAfterResolving('db', function (DatabaseManager $manager): void {
|
||||
@@ -41,8 +43,10 @@ class AppServiceProvider extends ServiceProvider
|
||||
});
|
||||
});
|
||||
|
||||
$this->callAfterResolving('redis', function (RedisManager $manager): void {
|
||||
$manager->extend('phpredis', fn () => new ManagedPhpRedisConnector);
|
||||
$this->callAfterResolving('redis', function (RedisManager $manager) use ($redisClient): void {
|
||||
$manager->extend($redisClient, fn () => $redisClient === 'predis'
|
||||
? new ManagedPredisConnector
|
||||
: new ManagedPhpRedisConnector);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Redis\Connectors\PredisConnector;
|
||||
use RuntimeException;
|
||||
|
||||
final class ManagedPredisConnector extends PredisConnector
|
||||
{
|
||||
/**
|
||||
* @param array<array-key, mixed> $config
|
||||
* @param array<array-key, mixed> $options
|
||||
*/
|
||||
public function connect(array $config, array $options)
|
||||
{
|
||||
$config['password'] = ManagedCredentialFiles::redisPassword();
|
||||
|
||||
return parent::connect($config, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<array-key, mixed> $config
|
||||
* @param array<array-key, mixed> $clusterOptions
|
||||
* @param array<array-key, mixed> $options
|
||||
*/
|
||||
public function connectToCluster(array $config, array $clusterOptions, array $options): never
|
||||
{
|
||||
throw new RuntimeException('Managed credential delivery requires standalone Redis.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user