Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to provide redis host port #87

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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