mirror of
https://github.com/predis/predis.git
synced 2026-08-22 02:50:59 +00:00
59813cd74e
This is just a convention implemented to client options supporting
callable initializers such as "profile", "cluster" and "replication".
This is useful to get a fully-initialized default value and perform
additional operations before returning it. An example with "profile":
$options = array(
'commands' => array(
'test1' => 'Predis\Command\ConnectionEcho',
'test2' => 'Predis\Command\ConnectionEcho',
),
'profile' => function ($options, $option) {
$profile = $option->getDefault($options);
if (is_array($options->commands)) {
foreach ($options->commands as $id => $cmd) {
$profile->defineCommand($id, $cmd);
}
}
return $profile;
},
);
96 lines
2.8 KiB
PHP
96 lines
2.8 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\Option;
|
|
|
|
use Predis\Connection\ClusterConnectionInterface;
|
|
use Predis\Connection\PredisCluster;
|
|
use Predis\Connection\RedisCluster;
|
|
|
|
/**
|
|
* Option class that returns a connection cluster to be used by a client.
|
|
*
|
|
* @author Daniele Alessandri <suppakilla@gmail.com>
|
|
*/
|
|
class ClientCluster extends AbstractOption
|
|
{
|
|
/**
|
|
* Checks if the specified value is a valid instance of ClusterConnectionInterface.
|
|
*
|
|
* @param ClusterConnectionInterface $cluster Instance of a connection cluster.
|
|
* @return ClusterConnectionInterface
|
|
*/
|
|
protected function checkInstance($cluster)
|
|
{
|
|
if (!$cluster instanceof ClusterConnectionInterface) {
|
|
throw new \InvalidArgumentException('Instance of Predis\Connection\ClusterConnectionInterface expected');
|
|
}
|
|
|
|
return $cluster;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function filter(ClientOptionsInterface $options, $value)
|
|
{
|
|
if (is_callable($value)) {
|
|
return $this->checkInstance(call_user_func($value, $options, $this));
|
|
}
|
|
|
|
$initializer = $this->getInitializer($options, $value);
|
|
|
|
return $this->checkInstance($initializer());
|
|
}
|
|
|
|
/**
|
|
* Returns an initializer for the specified FQN or type.
|
|
*
|
|
* @param string $fqnOrType Type of cluster or FQN of a class implementing ClusterConnectionInterface.
|
|
* @param ClientOptionsInterface $options Instance of the client options.
|
|
* @return \Closure
|
|
*/
|
|
protected function getInitializer(ClientOptionsInterface $options, $fqnOrType)
|
|
{
|
|
switch ($fqnOrType) {
|
|
case 'predis':
|
|
return function () {
|
|
return new PredisCluster();
|
|
};
|
|
|
|
case 'redis':
|
|
return function () use ($options) {
|
|
$connectionFactory = $options->connections;
|
|
$cluster = new RedisCluster($connectionFactory);
|
|
|
|
return $cluster;
|
|
};
|
|
|
|
default:
|
|
// TODO: we should not even allow non-string values here.
|
|
if (is_string($fqnOrType) && !class_exists($fqnOrType)) {
|
|
throw new \InvalidArgumentException("Class $fqnOrType does not exist");
|
|
}
|
|
return function () use ($fqnOrType) {
|
|
return new $fqnOrType();
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getDefault(ClientOptionsInterface $options)
|
|
{
|
|
return new PredisCluster();
|
|
}
|
|
}
|