Skip to content

Commit

Permalink
Merge pull request #87 from clementtalleu/chore/allow_to_provide_redi…
Browse files Browse the repository at this point in the history
…s_host_port

Allow to provide redis host port
  • Loading branch information
clementtalleu authored Sep 13, 2024
2 parents 1baad20 + 5b72222 commit 4fd4375
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ test.php
/.php-cs-fixer.cache
/.phpunit.cache/
Makefile
coverage.xml
coverage.xml
composer.lock
1 change: 1 addition & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ services:
tty: true
environment:
REDIS_HOST: redis
REDIS_PORT: 6379
XDEBUG_MODE: 'coverage'

volumes:
Expand Down
9 changes: 9 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
22 changes: 19 additions & 3 deletions src/Client/PredisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/Client/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Command/GenerateSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down

0 comments on commit 4fd4375

Please sign in to comment.