Skip to content

Commit

Permalink
Merge pull request Kdyby#80 from pmgdev/compatibility-with-phpredis-4
Browse files Browse the repository at this point in the history
Can be used with phpredis v3 and also with v4
  • Loading branch information
enumag authored May 3, 2018
2 parents 7ac96ed + 287b066 commit 15f5274
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 17 deletions.
25 changes: 14 additions & 11 deletions src/Kdyby/Redis/DI/RedisExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ public function loadConfiguration()

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

$phpRedisDriverClass = phpversion('redis') >= '4.0.0' ? Kdyby\Redis\Driver\PhpRedisDriver::class : Kdyby\Redis\Driver\PhpRedisDriverOld::class;
$phpRedisDriverInterface = phpversion('redis') >= '4.0.0' ? Kdyby\Redis\IRedisDriver::class : Kdyby\Redis\IRedisDriverOld::class;

$builder->addDefinition($this->prefix('driver'))
->setClass(class_exists('Redis') ? 'Kdyby\Redis\Driver\PhpRedisDriver' : 'Kdyby\Redis\IRedisDriver')
->setClass(class_exists('Redis') ? $phpRedisDriverClass : $phpRedisDriverInterface)
->setFactory($this->prefix('@client') . '::getDriver');

$this->loadJournal($config);
Expand Down Expand Up @@ -104,7 +107,7 @@ protected function buildClient($name, $config)
$config = array_intersect_key(self::fixClientConfig($config), $this->clientDefaults);

$client = $builder->addDefinition($clientName = $this->prefix(($name ? $name . '_' : '') . 'client'))
->setClass('Kdyby\Redis\RedisClient', [
->setClass(Kdyby\Redis\RedisClient::class, [
'host' => $config['host'],
'port' => $config['port'],
'database' => $config['database'],
Expand All @@ -118,7 +121,7 @@ protected function buildClient($name, $config)

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

Expand All @@ -134,8 +137,8 @@ protected function buildClient($name, $config)

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

Expand All @@ -156,12 +159,12 @@ protected function loadJournal(array $config)
$builder = $this->getContainerBuilder();

// overwrite
$journalService = $builder->getByType('Nette\Caching\Storages\IJournal') ?: 'nette.cacheJournal';
$journalService = $builder->getByType(Nette\Caching\Storages\IJournal::class) ?: 'nette.cacheJournal';
$builder->removeDefinition($journalService);
$builder->addDefinition($journalService)->setFactory($this->prefix('@cacheJournal'));

$builder->addDefinition($this->prefix('cacheJournal'))
->setClass('Kdyby\Redis\RedisLuaJournal');
->setClass(Kdyby\Redis\RedisLuaJournal::class);
}


Expand All @@ -178,12 +181,12 @@ protected function loadStorage(array $config)
'locks' => TRUE,
]);

$storageService = $builder->getByType('Nette\Caching\IStorage') ?: 'cacheStorage';
$storageService = $builder->getByType(Nette\Caching\IStorage::class) ?: 'cacheStorage';
$builder->removeDefinition($storageService);
$builder->addDefinition($storageService)->setFactory($this->prefix('@cacheStorage'));

$cacheStorage = $builder->addDefinition($this->prefix('cacheStorage'))
->setClass('Kdyby\Redis\RedisStorage');
->setClass(Kdyby\Redis\RedisStorage::class);

if (!$storageConfig['locks']) {
$cacheStorage->addSetup('disableLocking');
Expand Down Expand Up @@ -223,9 +226,9 @@ protected function loadSession(array $config)

} else {
$builder->addDefinition($this->prefix('sessionHandler'))
->setClass('Kdyby\Redis\RedisSessionHandler', [$this->prefix('@sessionHandler_client')]);
->setClass(Kdyby\Redis\RedisSessionHandler::class, [$this->prefix('@sessionHandler_client')]);

$sessionService = $builder->getByType('Nette\Http\Session') ?: 'session';
$sessionService = $builder->getByType(Nette\Http\Session::class) ?: 'session';
$builder->getDefinition($sessionService)
->addSetup('?->bind(?)', [$this->prefix('@sessionHandler'), '@self']);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Kdyby/Redis/Driver/PhpRedisDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PhpRedisDriver extends \Redis implements Kdyby\Redis\IRedisDriver
/**
* {@inheritdoc}
*/
public function connect($host, $port = NULL, $timeout = 0)
public function connect($host, $port = NULL, $timeout = NULL, $retry_interval = NULL)
{
$args = func_get_args();
return call_user_func_array('parent::connect', $args);
Expand All @@ -46,7 +46,7 @@ public function select($database)
/**
* {@inheritdoc}
*/
public function script($command, $script = NULL)
public function script($cmd, ...$args)
{
$args = func_get_args();
return call_user_func_array('parent::script', $args);
Expand Down
66 changes: 66 additions & 0 deletions src/Kdyby/Redis/Driver/PhpRedisDriverOld.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka (filip@prochazka.su)
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Redis\Driver;

use Kdyby;
use Nette;



/**
* @author Filip Procházka <filip@prochazka.su>
*/
class PhpRedisDriverOld extends \Redis implements Kdyby\Redis\IRedisDriverOld
{

/**
* {@inheritdoc}
*/
public function connect($host, $port = NULL, $timeout = 0)
{
$args = func_get_args();
return @call_user_func_array('parent::connect', $args); // intentionally @ - from v3.1.0 Redis::connect() throws warning on failed
}



/**
* {@inheritdoc}
*/
public function select($database)
{
$args = func_get_args();
return call_user_func_array('parent::select', $args);
}



/**
* {@inheritdoc}
*/
public function script($command, $script = NULL)
{
$args = func_get_args();
return call_user_func_array('parent::script', $args);
}



/**
* {@inheritdoc}
*/
public function evalsha($scriptSha, $argsArray = [], $numKeys = 0)
{
$args = func_get_args();
return call_user_func_array('parent::evalsha', $args);
}

}
4 changes: 2 additions & 2 deletions src/Kdyby/Redis/IRedisDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function isConnected();
* @param int $timeout value in seconds (optional, default is 0 meaning unlimited)
* @return bool
*/
function connect($host, $port = NULL, $timeout = 0);
function connect($host, $port = NULL, $timeout = NULL, $retry_interval = NULL);

/**
* Change the selected database for the current connection.
Expand Down Expand Up @@ -214,7 +214,7 @@ function clearLastError();
* @param $script
* @return mixed
*/
function script($command, $script = NULL);
function script($cmd, ...$args);

/**
* @param string $scriptSha The sha1 encoded hash of the script you want to run.
Expand Down
Loading

0 comments on commit 15f5274

Please # to comment.