From 28c26e022fb24943afe1871d10174a2f10cc3ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Proch=C3=A1zka?= Date: Tue, 25 Nov 2014 03:05:49 +0100 Subject: [PATCH] RedisExtension: allow configuring redis clients directly from config ```yml redis: database: 0 session: {native: off, database: 1} clients: emailsCount < sessionHandler: database: 2 sentMessages < sessionHandler: database: 3 notifications < sessionHandler: database: 4 ``` --- src/Kdyby/Redis/DI/RedisExtension.php | 45 +++++++++++++++++++-------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/Kdyby/Redis/DI/RedisExtension.php b/src/Kdyby/Redis/DI/RedisExtension.php index 3bfaa50..5bf3553 100644 --- a/src/Kdyby/Redis/DI/RedisExtension.php +++ b/src/Kdyby/Redis/DI/RedisExtension.php @@ -34,6 +34,7 @@ class RedisExtension extends Nette\DI\CompilerExtension 'journal' => FALSE, 'storage' => FALSE, 'session' => FALSE, + 'clients' => array(), ); /** @@ -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') @@ -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); + } } @@ -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'], @@ -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; @@ -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'))); @@ -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();