123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace Predis\Connection;
- use Predis\ClientException;
- use Predis\Option\OptionInterface;
- class ConnectionParameters implements ConnectionParametersInterface
- {
- private $parameters;
- private static $defaults = array(
- 'scheme' => 'tcp',
- 'host' => '127.0.0.1',
- 'port' => 6379,
- 'timeout' => 5.0,
- 'iterable_multibulk' => false,
- );
-
- public function __construct($parameters = array())
- {
- if (!is_array($parameters)) {
- $parameters = $this->parseURI($parameters);
- }
- $this->parameters = $this->filter($parameters) + $this->getDefaults();
- }
-
- protected function getDefaults()
- {
- return self::$defaults;
- }
-
- protected function getValueCasters()
- {
- return array(
- 'port' => 'self::castInteger',
- 'async_connect' => 'self::castBoolean',
- 'persistent' => 'self::castBoolean',
- 'timeout' => 'self::castFloat',
- 'read_write_timeout' => 'self::castFloat',
- 'iterable_multibulk' => 'self::castBoolean',
- );
- }
-
- private static function castBoolean($value)
- {
- return (bool) $value;
- }
-
- private static function castFloat($value)
- {
- return (float) $value;
- }
-
- private static function castInteger($value)
- {
- return (int) $value;
- }
-
- private function parseURI($uri)
- {
- if (stripos($uri, 'unix') === 0) {
-
- $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
- }
- if (($parsed = @parse_url($uri)) === false || !isset($parsed['host'])) {
- throw new ClientException("Invalid URI: $uri");
- }
- if (isset($parsed['query'])) {
- foreach (explode('&', $parsed['query']) as $kv) {
- @list($k, $v) = explode('=', $kv);
- $parsed[$k] = $v;
- }
- unset($parsed['query']);
- }
- return $parsed;
- }
-
- private function filter(Array $parameters)
- {
- if (count($parameters) > 0) {
- $casters = array_intersect_key($this->getValueCasters(), $parameters);
- foreach ($casters as $parameter => $caster) {
- $parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
- }
- }
- return $parameters;
- }
-
- public function __get($parameter)
- {
- if (isset($this->{$parameter})) {
- return $this->parameters[$parameter];
- }
- }
-
- public function __isset($parameter)
- {
- return isset($this->parameters[$parameter]);
- }
-
- public function toArray()
- {
- return $this->parameters;
- }
-
- public function __sleep()
- {
- return array('parameters');
- }
- }
|