Initialize client options in a lazy way.

This commit is contained in:
Daniele Alessandri
2011-05-06 15:20:40 +02:00
parent 0255261cd5
commit 893618c985
2 changed files with 27 additions and 12 deletions
+4 -3
View File
@@ -15,10 +15,11 @@ class Client {
public function __construct($parameters = null, $options = null) {
$options = $this->filterOptions($options);
$this->_options = $options;
$this->_profile = $options->profile;
if ($prefixer = $options->prefix) {
$this->_profile->setProcessor($prefixer);
$profile = $options->profile;
if (isset($options->prefix)) {
$profile->setProcessor($options->prefix);
}
$this->_profile = $profile;
$this->_connectionFactory = $options->connections;
$this->_connection = $this->initializeConnection($parameters);
}
+23 -9
View File
@@ -9,11 +9,14 @@ use Predis\Options\ClientCluster;
use Predis\Options\ClientConnectionFactory;
class ClientOptions {
private $_handlers, $_options;
private static $_sharedOptions;
private $_options = array();
private $_handlers;
private $_defined;
public function __construct(Array $options = array()) {
$this->initialize($options);
$this->_handlers = $this->initialize($options);
$this->_defined = array_keys($options);
}
private static function getSharedOptions() {
@@ -40,13 +43,20 @@ class ClientOptions {
}
private function initialize($options) {
$this->_handlers = self::getSharedOptions();
$handlers = self::getSharedOptions();
foreach ($options as $option => $value) {
if (isset($this->_handlers[$option])) {
$handler = $this->_handlers[$option];
$this->_options[$option] = $handler($value);
if (isset($handlers[$option])) {
$handler = $handlers[$option];
$handlers[$option] = function() use($handler, $value) {
return $handler->validate($value);
};
}
}
return $handlers;
}
public function __isset($option) {
return in_array($option, $this->_defined);
}
public function __get($option) {
@@ -54,11 +64,15 @@ class ClientOptions {
return $this->_options[$option];
}
if (isset($this->_handlers[$option])) {
$opts = self::getSharedOptions();
$value = $opts[$option]->getDefault();
$handler = $this->_handlers[$option];
if ($handler instanceof IOption) {
$value = $handler->getDefault();
}
else {
$value = $handler();
}
$this->_options[$option] = $value;
return $value;
}
return null;
}
}