Skip to content

Commit

Permalink
RedisExtension: allow configuring redis clients directly from config
Browse files Browse the repository at this point in the history
```yml
redis:
        database: 0
        session: {native: off, database: 1}

        clients:
                emailsCount < sessionHandler:
                        database: 2

                sentMessages < sessionHandler:
                        database: 3

                notifications < sessionHandler:
                        database: 4
```
  • Loading branch information
fprochazka committed Nov 25, 2014
1 parent 011460b commit 28c26e0
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/Kdyby/Redis/DI/RedisExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RedisExtension extends Nette\DI\CompilerExtension
'journal' => FALSE,
'storage' => FALSE,
'session' => FALSE,
'clients' => array(),
);

/**
Expand All @@ -55,18 +56,18 @@ class RedisExtension extends Nette\DI\CompilerExtension
/**
* @var array
*/
private $checkClients = array();
private $configuredClients = array();



public function loadConfiguration()
{
$this->checkClients = array();
$this->configuredClients = array();

$builder = $this->getContainerBuilder();
$config = self::fixClientConfig($this->getConfig($this->defaults + $this->clientDefaults));

$this->buildClient('client', $config);
$this->buildClient(NULL, $config);

$builder->addDefinition($this->prefix('driver'))
->setClass(class_exists('Redis') ? 'Kdyby\Redis\Driver\PhpRedisDriver' : 'Kdyby\Redis\IRedisDriver')
Expand All @@ -75,6 +76,10 @@ public function loadConfiguration()
$this->loadJournal($config);
$this->loadStorage($config);
$this->loadSession($config, $builder);

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


Expand All @@ -88,10 +93,16 @@ protected function buildClient($name, $config)
{
$builder = $this->getContainerBuilder();

$config = Config\Helpers::merge($config, $builder->expand($this->clientDefaults));
$config = self::fixClientConfig($config);
$defaultConfig = $builder->expand($this->clientDefaults);
if ($parentName = Config\Helpers::takeParent($config)) {
Nette\Utils\Validators::assertField($this->configuredClients, $parentName, 'array', "parent configuration '%', are you sure it's defined?");
$defaultConfig = Config\Helpers::merge($this->configuredClients[$parentName], $defaultConfig);
}

$config = Config\Helpers::merge($config, $defaultConfig);
$config = array_intersect_key(self::fixClientConfig($config), $this->clientDefaults);

$client = $builder->addDefinition($clientName = $this->prefix($name))
$client = $builder->addDefinition($clientName = $this->prefix(($name ? $name . '_' : '') . 'client'))
->setClass('Kdyby\Redis\RedisClient', array(
'host' => $config['host'],
'port' => $config['port'],
Expand All @@ -104,24 +115,28 @@ protected function buildClient($name, $config)
if (empty($builder->parameters[$this->name]['defaultClient'])) {
$builder->parameters[$this->name]['defaultClient'] = $clientName;

$this->configuredClients['default'] = $config;
$builder->addDefinition($this->prefix('default_client'))
->setClass('Kdyby\Redis\RedisClient')
->setFactory('@' . $clientName)
->setAutowired(FALSE);

} else {
$client->setAutowired(FALSE);
}

if ($config['versionCheck']) {
$this->checkClients[] = $config;
}
$this->configuredClients[$name] = $config;

$client->addSetup('setupLockDuration', array($config['lockDuration']));
$client->addSetup('setConnectionAttempts', array($config['connectionAttempts']));

if (array_key_exists('debugger', $config) && $config['debugger']) {
$builder->addDefinition($this->prefix($panelName = $name . '.panel'))
$builder->addDefinition($panelName = $clientName . '.panel')
->setClass('Kdyby\Redis\Diagnostics\Panel')
->setFactory('Kdyby\Redis\Diagnostics\Panel::register')
->addSetup('$renderPanel', array($config['debugger'] !== self::PANEL_COUNT_MODE));

$client->addSetup('setPanel', array($this->prefix('@' . $panelName)));
$client->addSetup('setPanel', array('@' . $panelName));
}

return $client;
Expand Down Expand Up @@ -199,7 +214,7 @@ protected function loadSession(array $config)
$this->loadNativeSessionHandler($sessionConfig);

} else {
$this->buildClient('sessionHandler_client', array('debugger' => FALSE) + $sessionConfig);
$this->buildClient('sessionHandler', array('debugger' => FALSE) + $sessionConfig);

$builder->addDefinition($this->prefix('sessionHandler'))
->setClass('Kdyby\Redis\RedisSessionHandler', array($this->prefix('@sessionHandler_client')));
Expand Down Expand Up @@ -257,7 +272,11 @@ protected function loadNativeSessionHandler(array $session)
*/
public function beforeCompile()
{
foreach ($this->checkClients as $config) {
foreach ($this->configuredClients as $config) {
if (!$config['versionCheck']) {
continue;
}

$client = new RedisClient($config['host'], $config['port'], $config['database'], $config['timeout'], $config['auth']);
$client->assertVersion();
$client->close();
Expand Down

0 comments on commit 28c26e0

Please sign in to comment.