diff --git a/.gitignore b/.gitignore index 9fde038..3ca449e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ test.php /.php-cs-fixer.cache /.phpunit.cache/ Makefile -coverage.xml \ No newline at end of file +coverage.xml +composer.lock \ No newline at end of file diff --git a/compose.yaml b/compose.yaml index 5aa180b..def6ca9 100644 --- a/compose.yaml +++ b/compose.yaml @@ -17,6 +17,7 @@ services: tty: true environment: REDIS_HOST: redis + REDIS_PORT: 6379 XDEBUG_MODE: 'coverage' volumes: diff --git a/docs/installation.md b/docs/installation.md index 5fe0383..e7e6db4 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -35,3 +35,12 @@ Or add the library to your `composer.json` file: Then run `composer update` to install the library. + +### Redis configuration + +if you want to inject a particular configuration for Redis, you can add the following environment variables to your .env file (all optional) + + REDIS_HOST: localhost + REDIS_PORT: 6379 + REDIS_USER: + REDIS_PASSWORD: diff --git a/src/Client/PredisClient.php b/src/Client/PredisClient.php index 63eeb16..81dc2a1 100644 --- a/src/Client/PredisClient.php +++ b/src/Client/PredisClient.php @@ -17,9 +17,25 @@ final class PredisClient implements RedisClientInterface { public function __construct(protected ?Predis $redis = null) { - $this->redis = $redis ?? new Predis( - array_key_exists('REDIS_HOST', $_SERVER) ? ['host' => $_SERVER['REDIS_HOST']] : null, - ); + $redisConfig = []; + + if (array_key_exists('REDIS_HOST', $_SERVER)) { + $redisConfig['host'] = $_SERVER['REDIS_HOST']; + } + + if (array_key_exists('REDIS_PORT', $_SERVER)) { + $redisConfig['port'] = $_SERVER['REDIS_PORT']; + } + + if (array_key_exists('REDIS_USER', $_SERVER)) { + $redisConfig['parameters']['username'] = $_SERVER['REDIS_USER']; + } + + if (array_key_exists('REDIS_PASSWORD', $_SERVER)) { + $redisConfig['parameters']['password'] = $_SERVER['REDIS_PASSWORD']; + } + + $this->redis = $redis ?? new Predis($redisConfig !== [] ? $redisConfig : null); } public function createPersistentConnection(?string $host = null, ?int $port = null, ?int $timeout = 0): void diff --git a/src/Client/RedisClient.php b/src/Client/RedisClient.php index 739aa0b..a675a26 100644 --- a/src/Client/RedisClient.php +++ b/src/Client/RedisClient.php @@ -15,7 +15,21 @@ final class RedisClient implements RedisClientInterface { public function __construct(protected ?\Redis $redis = null) { - $this->redis = $redis ?? new \Redis(array_key_exists('REDIS_HOST', $_SERVER) ? ['host' => $_SERVER['REDIS_HOST']] : null); + $redisConfig = []; + + if (array_key_exists('REDIS_HOST', $_SERVER)) { + $redisConfig['host'] = $_SERVER['REDIS_HOST']; + } + + if (array_key_exists('REDIS_PORT', $_SERVER)) { + $redisConfig['port'] = (int) $_SERVER['REDIS_PORT']; + } + + if (array_key_exists('REDIS_USER', $_SERVER) && array_key_exists('REDIS_PASSWORD', $_SERVER)) { + $redisConfig['auth'] = [$_SERVER['REDIS_USER'], $_SERVER['REDIS_PASSWORD']]; + } + + $this->redis = $redis ?? new \Redis($redisConfig !== [] ? $redisConfig : null); } /** diff --git a/src/Command/GenerateSchema.php b/src/Command/GenerateSchema.php index fef0fb3..bf22fae 100644 --- a/src/Command/GenerateSchema.php +++ b/src/Command/GenerateSchema.php @@ -21,6 +21,7 @@ public static function generateSchema(string $dir): void $redisOm = new RedisObjectManager( getenv('REDIS_CLIENT') === 'predis' ? new PredisClient() : new RedisClient(), ); + $rii = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)); $phpFiles = [];