mirror of
https://github.com/predis/predis.git
synced 2026-08-24 17:59:43 +00:00
dd20e8557f
This change is useless right now, but when Predis will support Redis cluster we will need to have access the connection factory object since it will be required by our future RedisCluster class.
82 lines
2.1 KiB
PHP
82 lines
2.1 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\Options;
|
|
|
|
use Predis\Network\IConnectionCluster;
|
|
use Predis\Network\PredisCluster;
|
|
|
|
/**
|
|
* Option class that returns a connection cluster to be used by a client.
|
|
*
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
*/
|
|
class ClientCluster extends Option
|
|
{
|
|
/**
|
|
* Checks if the specified value is a valid instance of IConnectionCluster.
|
|
*
|
|
* @param IConnectionCluster $cluster Instance of a connection cluster.
|
|
* @return IConnectionCluster
|
|
*/
|
|
protected function checkInstance($cluster)
|
|
{
|
|
if (!$cluster instanceof IConnectionCluster) {
|
|
throw new \InvalidArgumentException(
|
|
'Instance of Predis\Network\IConnectionCluster expected'
|
|
);
|
|
}
|
|
|
|
return $cluster;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function validate(IClientOptions $options, $value)
|
|
{
|
|
if (is_callable($value)) {
|
|
return $this->checkInstance(call_user_func($value));
|
|
}
|
|
$initializer = $this->getInitializer($options, $value);
|
|
|
|
return $this->checkInstance($initializer());
|
|
}
|
|
|
|
/**
|
|
* Returns an initializer for the specified FQN or type.
|
|
*
|
|
* @param string $fqnOrType Type of cluster of FQN of a class implementing IConnectionCluster.
|
|
* @param IClientOptions $options Instance of the client options.
|
|
* @return \Closure
|
|
*/
|
|
protected function getInitializer(IClientOptions $options, $fqnOrType)
|
|
{
|
|
switch ($fqnOrType) {
|
|
case 'predis':
|
|
return function() { return new PredisCluster(); };
|
|
|
|
default:
|
|
return function() use($fqnOrType) {
|
|
return new $fqnOrType();
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getDefault(IClientOptions $options)
|
|
{
|
|
return new PredisCluster();
|
|
}
|
|
}
|