Created
November 3, 2012 18:21
-
-
Save LeonardoCA/4008177 to your computer and use it in GitHub Desktop.
Nette RedisExtension with fallback if redis not runing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file is part of the Kdyby (http://www.kdyby.org) | |
* | |
* Copyright (c) 2008, 2012 Filip Procházka ([email protected]) | |
* | |
* For the full copyright and license information, please view the file license.md that was distributed with this source code. | |
*/ | |
namespace Kdyby\Extension\Redis\DI; | |
use Kdyby; | |
use Kdyby\Extension\Redis\RedisClient; | |
use Kdyby\Extension\Redis\RedisClientException; | |
use Nette; | |
use Nette\Config\Configurator; | |
use Nette\Config\Compiler; | |
use Nette\Config\CompilerExtension; | |
use Nette\DI\ContainerBuilder; | |
use Nette\DI\Container; | |
use Nette\DI\Statement; | |
use Nette\Utils\Validators; | |
/** | |
* @author Filip Procházka <[email protected]> | |
* @author Leonard Odložilík | |
*/ | |
class RedisExtension extends CompilerExtension | |
{ | |
/** | |
* @var array | |
*/ | |
public $defaults = array( | |
'journal' => FALSE, | |
'storage' => FALSE, | |
'session' => FALSE, | |
'host' => 'localhost', | |
'port' => 6379, | |
'timeout' => 10, | |
'database' => 0, | |
'debug' => true | |
); | |
public function loadConfiguration() | |
{ | |
$builder = $this->getContainerBuilder(); | |
$config = $this->getConfig($this->defaults); | |
$client = $builder->addDefinition($this->prefix('client')) | |
->setClass('Kdyby\Extension\Redis\RedisClient', array( | |
'host' => $config['host'], | |
'port' => $config['port'], | |
'database' => $config['database'], | |
'timeout' => $config['timeout'] | |
)); | |
if ($builder->parameters['debugMode'] && $config['debug']) { | |
$client->addSetup('setPanel'); | |
} | |
$builder->addDefinition($this->prefix('cacheJournal')) | |
->setClass('Kdyby\Extension\Redis\RedisJournal') | |
->setAutowired(false); | |
$builder->addDefinition($this->prefix('cacheStorage')) | |
->setClass('Kdyby\Extension\Redis\RedisStorage') | |
->setAutowired(false); | |
$builder->addDefinition($this->prefix('session')) | |
->setClass('Kdyby\Extension\Redis\RedisSessionHandler') | |
->setAutowired(false); | |
$builder->addDefinition($this->prefix('panel')) | |
->setFactory('Kdyby\Extension\Redis\Diagnostics\Panel::register'); | |
} | |
public function afterCompile(Nette\Utils\PhpGenerator\ClassType $class) | |
{ | |
$container = $this->getContainerBuilder(); | |
$config = $this->getConfig($this->defaults); | |
$initialize = $class->methods['initialize']; | |
$bodyBackup = $initialize->body; | |
$initialize->body = ''; | |
$initialize->addBody('Kdyby\Extension\Redis\DI\RedisExtension::run($this->getService(?), $this, ?, ? ,?, ?);', | |
array($this->prefix('client'), $this->prefix(''), $config['journal'], $config['storage'], $config['session'])); | |
$initialize->body .= $bodyBackup; | |
} | |
/** | |
* Run - try to connect to redis, if succesfull finish configuration | |
* @param RedisClient $client | |
* @param Container $container | |
* @param string $prefix | |
* @param mixed $journal | |
* @param mixed $storage | |
* @param mixed $session | |
*/ | |
public static function run(RedisClient $client, Container $container, $prefix, $journalOn, $storageOn, $sessionOn) | |
{ | |
try { | |
$client->connect(); | |
if ($journalOn) { | |
$container->removeService('nette.cacheJournal'); | |
$container->addService('nette.cacheJournal', $container->getService($prefix.'cacheJournal')); | |
} | |
if ($storageOn) { | |
$container->removeService('cacheStorage'); | |
$container->addService('cacheStorage', $container->getService($prefix.'cacheStorage')); | |
} | |
if ($sessionOn) { | |
$container->getService('session')->setStorage($container->getService($prefix . 'session')); | |
} | |
} catch (Kdyby\Extension\Redis\RedisClientException $e) { | |
//@todo add message to panel if redis is not running | |
} | |
} | |
/** | |
* @param \Nette\Config\Configurator $config | |
*/ | |
public static function register(Configurator $config) | |
{ | |
$config->onCompile[] = function (Configurator $config, Compiler $compiler) { | |
$compiler->addExtension('redis', new RedisExtension()); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment