mirror of
https://github.com/predis/predis.git
synced 2026-08-28 03:02:04 +00:00
Partially rewrite the client options bits and remove some madness.
The concept is now similar to a little dependency-injection system.
This commit is contained in:
@@ -12,9 +12,11 @@
|
||||
namespace Predis;
|
||||
|
||||
use Predis\Commands\ICommand;
|
||||
use Predis\Options\IClientOptions;
|
||||
use Predis\Network\IConnection;
|
||||
use Predis\Network\IConnectionSingle;
|
||||
use Predis\Profiles\IServerProfile;
|
||||
use Predis\Options\ClientOptions;
|
||||
use Predis\Profiles\ServerProfile;
|
||||
use Predis\PubSub\PubSubContext;
|
||||
use Predis\Pipeline\PipelineContext;
|
||||
@@ -72,7 +74,7 @@ class Client
|
||||
if (is_array($options)) {
|
||||
return new ClientOptions($options);
|
||||
}
|
||||
if ($options instanceof ClientOptions) {
|
||||
if ($options instanceof IClientOptions) {
|
||||
return $options;
|
||||
}
|
||||
if ($options instanceof IServerProfile) {
|
||||
|
||||
@@ -41,7 +41,7 @@ class ClientCluster extends Option
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value)
|
||||
public function validate(IClientOptions $options, $value)
|
||||
{
|
||||
if (is_callable($value)) {
|
||||
return $this->checkInstance(call_user_func($value));
|
||||
@@ -73,7 +73,7 @@ class ClientCluster extends Option
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefault()
|
||||
public function getDefault(IClientOptions $options)
|
||||
{
|
||||
return new PredisCluster();
|
||||
}
|
||||
|
||||
@@ -24,13 +24,13 @@ class ClientConnectionFactory extends Option
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value)
|
||||
public function validate(IClientOptions $options, $value)
|
||||
{
|
||||
if ($value instanceof IConnectionFactory) {
|
||||
return $value;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
$factory = $this->getDefault();
|
||||
$factory = $this->getDefault($options);
|
||||
foreach ($value as $scheme => $initializer) {
|
||||
$factory->define($scheme, $initializer);
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class ClientConnectionFactory extends Option
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefault()
|
||||
public function getDefault(IClientOptions $options)
|
||||
{
|
||||
return new ConnectionFactory();
|
||||
}
|
||||
|
||||
@@ -9,26 +9,17 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis;
|
||||
|
||||
use Predis\Options\IOption;
|
||||
use Predis\Options\ClientPrefix;
|
||||
use Predis\Options\ClientProfile;
|
||||
use Predis\Options\ClientCluster;
|
||||
use Predis\Options\ClientConnectionFactory;
|
||||
namespace Predis\Options;
|
||||
|
||||
/**
|
||||
* Class that manages validation and conversion of client options.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class ClientOptions
|
||||
class ClientOptions implements IClientOptions
|
||||
{
|
||||
private static $sharedOptions;
|
||||
|
||||
private $handlers;
|
||||
private $defined;
|
||||
|
||||
private $options = array();
|
||||
|
||||
/**
|
||||
@@ -37,7 +28,7 @@ class ClientOptions
|
||||
public function __construct(Array $options = array())
|
||||
{
|
||||
$this->handlers = $this->initialize($options);
|
||||
$this->defined = array_keys($options);
|
||||
$this->defined = array_fill_keys(array_keys($options), true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,43 +36,14 @@ class ClientOptions
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function getSharedOptions()
|
||||
protected function getDefaultOptions()
|
||||
{
|
||||
if (isset(self::$sharedOptions)) {
|
||||
return self::$sharedOptions;
|
||||
}
|
||||
|
||||
self::$sharedOptions = array(
|
||||
return array(
|
||||
'profile' => new ClientProfile(),
|
||||
'connections' => new ClientConnectionFactory(),
|
||||
'cluster' => new ClientCluster(),
|
||||
'prefix' => new ClientPrefix(),
|
||||
);
|
||||
|
||||
return self::$sharedOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines an option handler or overrides an existing one.
|
||||
*
|
||||
* @param string $option Name of the option.
|
||||
* @param IOption $handler Handler for the option.
|
||||
*/
|
||||
public static function define($option, IOption $handler)
|
||||
{
|
||||
self::getSharedOptions();
|
||||
self::$sharedOptions[$option] = $handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Undefines the handler for the specified option.
|
||||
*
|
||||
* @param string $option Name of the option.
|
||||
*/
|
||||
public static function undefine($option)
|
||||
{
|
||||
self::getSharedOptions();
|
||||
unset(self::$sharedOptions[$option]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,17 +52,20 @@ class ClientOptions
|
||||
* @param array $options List of client options values.
|
||||
* @return array
|
||||
*/
|
||||
private function initialize($options)
|
||||
protected function initialize(Array $options)
|
||||
{
|
||||
$handlers = self::getSharedOptions();
|
||||
$handlers = $this->getDefaultOptions();
|
||||
|
||||
foreach ($options as $option => $value) {
|
||||
if (isset($handlers[$option])) {
|
||||
$handler = $handlers[$option];
|
||||
$handlers[$option] = function() use($handler, $value) {
|
||||
return $handler->validate($value);
|
||||
$handlers[$option] = function($options) use($handler, $value) {
|
||||
return $handler->validate($options, $value);
|
||||
};
|
||||
}
|
||||
else {
|
||||
$this->options[$option] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $handlers;
|
||||
@@ -114,7 +79,7 @@ class ClientOptions
|
||||
*/
|
||||
public function __isset($option)
|
||||
{
|
||||
return in_array($option, $this->defined);
|
||||
return isset($this->defined[$option]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,7 +96,7 @@ class ClientOptions
|
||||
|
||||
if (isset($this->handlers[$option])) {
|
||||
$handler = $this->handlers[$option];
|
||||
$value = $handler instanceof IOption ? $handler->getDefault() : $handler();
|
||||
$value = $handler instanceof IOption ? $handler->getDefault($this) : $handler($this);
|
||||
$this->options[$option] = $value;
|
||||
|
||||
return $value;
|
||||
@@ -23,7 +23,7 @@ class ClientPrefix extends Option
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value)
|
||||
public function validate(IClientOptions $options, $value)
|
||||
{
|
||||
return new KeyPrefixProcessor($value);
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class ClientProfile extends Option
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value)
|
||||
public function validate(IClientOptions $options, $value)
|
||||
{
|
||||
if ($value instanceof IServerProfile) {
|
||||
return $value;
|
||||
@@ -42,7 +42,7 @@ class ClientProfile extends Option
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefault()
|
||||
public function getDefault(IClientOptions $options)
|
||||
{
|
||||
return ServerProfile::getDefault();
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ class CustomOption implements IOption
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value)
|
||||
public function validate(IClientOptions $options, $value)
|
||||
{
|
||||
if (isset($value)) {
|
||||
if ($this->validate === null) {
|
||||
@@ -61,32 +61,32 @@ class CustomOption implements IOption
|
||||
}
|
||||
$validator = $this->validate;
|
||||
|
||||
return $validator($value);
|
||||
return $validator($options, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefault()
|
||||
public function getDefault(IClientOptions $options)
|
||||
{
|
||||
if (!isset($this->default)) {
|
||||
return;
|
||||
}
|
||||
$default = $this->default;
|
||||
|
||||
return $default();
|
||||
return $default($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __invoke($value)
|
||||
public function __invoke(IClientOptions $options, $value)
|
||||
{
|
||||
if (isset($value)) {
|
||||
return $this->validate($value);
|
||||
return $this->validate($options, $value);
|
||||
}
|
||||
|
||||
return $this->getDefault();
|
||||
return $this->getDefault($options);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Marker interface defining a client options bag.
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
interface IClientOptions
|
||||
{
|
||||
}
|
||||
@@ -24,7 +24,7 @@ interface IOption
|
||||
* @param mixed $value Input value.
|
||||
* @return mixed
|
||||
*/
|
||||
public function validate($value);
|
||||
public function validate(IClientOptions $options, $value);
|
||||
|
||||
/**
|
||||
* Returns a default value for the option.
|
||||
@@ -32,7 +32,7 @@ interface IOption
|
||||
* @param mixed $value Input value.
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDefault();
|
||||
public function getDefault(IClientOptions $options);
|
||||
|
||||
/**
|
||||
* Validates a value and, if no value is specified, returns
|
||||
@@ -41,5 +41,5 @@ interface IOption
|
||||
* @param mixed $value Input value.
|
||||
* @return mixed
|
||||
*/
|
||||
public function __invoke($value);
|
||||
public function __invoke(IClientOptions $options, $value);
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ class Option implements IOption
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value)
|
||||
public function validate(IClientOptions $options, $value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
@@ -29,7 +29,7 @@ class Option implements IOption
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefault()
|
||||
public function getDefault(IClientOptions $options)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@@ -37,12 +37,12 @@ class Option implements IOption
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __invoke($value)
|
||||
public function __invoke(IClientOptions $options, $value)
|
||||
{
|
||||
if (isset($value)) {
|
||||
return $this->validate($value);
|
||||
return $this->validate($options, $value);
|
||||
}
|
||||
|
||||
return $this->getDefault();
|
||||
return $this->getDefault($options);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user