Skip to content

Commit

Permalink
Merge pull request #108 from PavelJurasek/php8-4
Browse files Browse the repository at this point in the history
Fix: session and storage config sections can be client definitions
  • Loading branch information
Spamercz authored Sep 8, 2021
2 parents 6402655 + a60886a commit 6f26d9a
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: redis
extensions: redis, igbinary
env:
REDIS_CONFIGURE_OPTS: --enable-redis --enable-redis-igbinary

- name: Start Redis
uses: supercharge/[email protected]
Expand Down
10 changes: 8 additions & 2 deletions src/Kdyby/Redis/DI/Config/ClientSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public function __construct(\Nette\DI\ContainerBuilder $builder)

public function normalize($value, \Nette\Schema\Context $context)
{
if (\is_bool($value)) {
return $value;
}

$value = $this->getSchema()->normalize($value, $context);

if (\array_key_exists('host', $value) && $value['host'][0] === '/') {
Expand All @@ -36,7 +40,9 @@ public function merge($value, $base)

public function complete($value, \Nette\Schema\Context $context)
{
$value = $this->expandParameters($value);
if ( ! \is_bool($value)) {
$value = $this->expandParameters($value);
}

$value = $this->getSchema()->complete($value, $context);

Expand Down Expand Up @@ -75,7 +81,7 @@ private function getSchema(): \Nette\Schema\Schema
'lockAcquireTimeout' => \Nette\Schema\Expect::bool(FALSE),
'debugger' => \Nette\Schema\Expect::bool($this->builder->parameters['debugMode']),
'versionCheck' => \Nette\Schema\Expect::bool(TRUE),
]);
])->castTo('array');
}

}
11 changes: 8 additions & 3 deletions src/Kdyby/Redis/DI/Config/RedisSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ private function expandParameters(array $config): array
private function getSchema(): \Nette\Schema\Schema
{
if ($this->schema === NULL) {
$storageSchema = \Nette\Schema\Expect::structure([
'locks' => \Nette\Schema\Expect::bool(TRUE),
])->castTo('array');
$sessionClientSchema = new \Kdyby\Redis\DI\Config\SessionClientSchema($this->builder);

$this->schema = \Nette\Schema\Expect::structure([
'journal' => \Nette\Schema\Expect::bool(FALSE),
'storage' => \Nette\Schema\Expect::bool(FALSE),
'session' => \Nette\Schema\Expect::bool(FALSE),
'storage' => \Nette\Schema\Expect::anyOf($storageSchema, TRUE, FALSE)->default(FALSE),
'session' => \Nette\Schema\Expect::anyOf($sessionClientSchema, TRUE, FALSE)->default(FALSE),
'clients' => \Nette\Schema\Expect::arrayOf(
new \Kdyby\Redis\DI\Config\ClientSchema($this->builder)
)->default([
Expand All @@ -103,7 +108,7 @@ private function getSchema(): \Nette\Schema\Schema
'versionCheck' => TRUE,
],
]),
]);
])->castTo('array');
}

return $this->schema;
Expand Down
90 changes: 90 additions & 0 deletions src/Kdyby/Redis/DI/Config/SessionClientSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php declare(strict_types = 1);

namespace Kdyby\Redis\DI\Config;

class SessionClientSchema implements \Nette\Schema\Schema
{

private \Nette\DI\ContainerBuilder $builder;


public function __construct(\Nette\DI\ContainerBuilder $builder)
{
$this->builder = $builder;
}

public function normalize($value, \Nette\Schema\Context $context)
{
if (\is_bool($value)) {
return $value;
}

$value = $this->getSchema()->normalize($value, $context);

if (\array_key_exists('host', $value) && $value['host'][0] === '/') {
$value['port'] = NULL; // sockets have no ports

} elseif ( ! \array_key_exists('port', $value)) {
$value['port'] = \Kdyby\Redis\RedisClient::DEFAULT_PORT;
}

return $value;
}


public function merge($value, $base)
{
return \Nette\Schema\Helpers::merge($value, $base);
}


public function complete($value, \Nette\Schema\Context $context)
{
if ( ! \is_bool($value)) {
$value = $this->expandParameters($value);
}

$value = $this->getSchema()->complete($value, $context);

return $value;
}


public function completeDefault(\Nette\Schema\Context $context)
{

}

private function expandParameters(array $config): array
{
$params = $this->builder->parameters;
if (isset($config['parameters'])) {
foreach ((array) $config['parameters'] as $k => $v) {
$v = \explode(' ', \is_int($k) ? $v : $k);
$params[\end($v)] = $this->builder::literal('$' . \end($v));
}
}
return \Nette\DI\Helpers::expand($config, $params);
}

private function getSchema(): \Nette\Schema\Schema
{
return \Nette\Schema\Expect::structure([
'host' => \Nette\Schema\Expect::string('127.0.0.1'),
'port' => \Nette\Schema\Expect::int()->nullable(),
'timeout' => \Nette\Schema\Expect::int(10),
'database' => \Nette\Schema\Expect::int(0),
'auth' => \Nette\Schema\Expect::string()->nullable(),
'persistent' => \Nette\Schema\Expect::bool(FALSE),
'connectionAttempts' => \Nette\Schema\Expect::int(1),
'lockDuration' => \Nette\Schema\Expect::int(15),
'lockAcquireTimeout' => \Nette\Schema\Expect::bool(FALSE),
'debugger' => \Nette\Schema\Expect::bool(FALSE),
'versionCheck' => \Nette\Schema\Expect::bool(TRUE),
'native' => \Nette\Schema\Expect::bool(TRUE),
'prefix' => \Nette\Schema\Expect::string(\Kdyby\Redis\DI\RedisExtension::DEFAULT_SESSION_PREFIX),
'weight' => \Nette\Schema\Expect::int(1),
])->castTo('array');
}

}
34 changes: 18 additions & 16 deletions src/Kdyby/Redis/DI/RedisExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class RedisExtension extends \Nette\DI\CompilerExtension
private const PANEL_COUNT_MODE = 'count';

/**
* @var array
* @var array<string, array<string, mixed>>
*/
private $configuredClients = [];
private array $configuredClients = [];


public function getConfigSchema(): \Nette\Schema\Schema
Expand All @@ -32,10 +32,11 @@ public function getConfigSchema(): \Nette\Schema\Schema

public function loadConfiguration(): void
{
$this->configuredClients = [];

$builder = $this->getContainerBuilder();
$config = (array) $this->getConfig();
$config = $this->getConfig();

// we need to register default client before session is processed
$this->buildClient(NULL, $config['clients']['']);

$phpRedisDriverClass = \Kdyby\Redis\Driver\PhpRedisDriver::class;

Expand All @@ -47,8 +48,9 @@ public function loadConfiguration(): void
$this->loadStorage($config);
$this->loadSession($config);

unset($config['clients']['']);
foreach ($config['clients'] as $name => $clientConfig) {
$this->buildClient($name, (array) $clientConfig);
$this->buildClient($name, $clientConfig);
}
}

Expand Down Expand Up @@ -164,19 +166,19 @@ protected function loadSession(array $config): void
$clientConfig = $config['clients'][NULL];

$sessionConfig = \Nette\DI\Config\Helpers::merge(\is_array($config['session']) ? $config['session'] : [], [
'host' => $clientConfig->host,
'port' => $clientConfig->port,
'host' => $clientConfig['host'],
'port' => $clientConfig['port'],
'weight' => 1,
'timeout' => $clientConfig->timeout,
'database' => $clientConfig->database,
'timeout' => $clientConfig['timeout'],
'database' => $clientConfig['database'],
'prefix' => self::DEFAULT_SESSION_PREFIX,
'auth' => $clientConfig->auth,
'auth' => $clientConfig['auth'],
'native' => TRUE,
'lockDuration' => $clientConfig->lockDuration,
'lockAcquireTimeout' => $clientConfig->lockAcquireTimeout,
'connectionAttempts' => $clientConfig->connectionAttempts,
'persistent' => $clientConfig->persistent,
'versionCheck' => $clientConfig->versionCheck,
'lockDuration' => $clientConfig['lockDuration'],
'lockAcquireTimeout' => $clientConfig['lockAcquireTimeout'],
'connectionAttempts' => $clientConfig['connectionAttempts'],
'persistent' => $clientConfig['persistent'],
'versionCheck' => $clientConfig['versionCheck'],
]);

$sessionConfig['debugger'] = FALSE;
Expand Down
12 changes: 12 additions & 0 deletions tests/KdybyTests/Redis/ExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ class ExtensionTest extends \Tester\TestCase
Assert::false($dic->hasService('redis.cacheStorage'));
}

public function testSessionConfiguration(): void
{
$config = $this->createConfig();
$config->addConfig(__DIR__ . '/files/session.neon');
$dic = $config->createContainer();
Assert::true($dic->getService('redis.client') instanceof Kdyby\Redis\RedisClient);
Assert::true($dic->hasService('redis.sessionHandler'));
Assert::type(\Kdyby\Redis\RedisSessionHandler::class, $dic->getService('redis.sessionHandler'));
Assert::false($dic->hasService('redis.cacheJournal'));
Assert::false($dic->hasService('redis.cacheStorage'));
}

}

(new ExtensionTest())->run();
7 changes: 7 additions & 0 deletions tests/KdybyTests/Redis/files/session.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
redis:
journal: false
storage: false
session:
database: 1
native: false
versionCheck: false

0 comments on commit 6f26d9a

Please sign in to comment.