mirror of
https://github.com/predis/predis.git
synced 2026-08-20 23:11:50 +00:00
ca468b785c
While "replication" do accept values evaluating to TRUE, the same cannot be said for values evaluating to FALSE. TRUE is used to tell the client that we want replication handled using the default backend for unmanaged replication setups. For using redis-sentinel the "sentinel" string value must be passed. Setting "replication" to FALSE led to a failure (and a PHP warning) on client initialization because this condition was not handled properly. Being able to do so would not make sense anyway: when the client does not need to be set up to rely on replication, users simply have to omit the option. Furthermore, users must always specify either "replication" or "cluster" and not both with one of them set to FALSE. Unfortunately options for aggregate connections in Predis v1.1 are a bit of a mess, they did not scale well with the addition of new features and are also quite inconsistent (e.g. "cluster" does not accept TRUE). This has been largely fixed in Predis v2.0-dev but required implementing a few breaking changes. It also means that this change does not need to be ported to the main branch. Addresses #381 using a different approach.
78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Predis\Configuration;
|
|
|
|
use Predis\Connection\Aggregate\MasterSlaveReplication;
|
|
use Predis\Connection\Aggregate\ReplicationInterface;
|
|
use Predis\Connection\Aggregate\SentinelReplication;
|
|
|
|
/**
|
|
* Configures an aggregate connection used for master/slave replication among
|
|
* multiple Redis nodes.
|
|
*
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
*/
|
|
class ReplicationOption implements OptionInterface
|
|
{
|
|
/**
|
|
* {@inheritdoc}
|
|
*
|
|
* @todo There's more code than needed due to a bug in filter_var() as
|
|
* discussed here https://bugs.php.net/bug.php?id=49510 and different
|
|
* behaviours when encountering NULL values on PHP 5.3.
|
|
*/
|
|
public function filter(OptionsInterface $options, $value)
|
|
{
|
|
if ($value instanceof ReplicationInterface) {
|
|
return $value;
|
|
}
|
|
|
|
if ($value === 'sentinel') {
|
|
return function ($sentinels, $options) {
|
|
return new SentinelReplication($options->service, $sentinels, $options->connections);
|
|
};
|
|
}
|
|
|
|
if (
|
|
!is_object($value) &&
|
|
null !== $asbool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
|
|
) {
|
|
if (true === $asbool) {
|
|
return $this->getDefault($options);
|
|
} else {
|
|
throw new \InvalidArgumentException(
|
|
"Values evaluating to FALSE are not accepted for `replication`"
|
|
);
|
|
}
|
|
}
|
|
|
|
throw new \InvalidArgumentException(
|
|
"An instance of type 'Predis\Connection\Aggregate\ReplicationInterface' was expected."
|
|
);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getDefault(OptionsInterface $options)
|
|
{
|
|
$replication = new MasterSlaveReplication();
|
|
|
|
if ($options->autodiscovery) {
|
|
$replication->setConnectionFactory($options->connections);
|
|
$replication->setAutoDiscovery(true);
|
|
}
|
|
|
|
return $replication;
|
|
}
|
|
}
|