ConnectionParameters.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Predis;
  3. use Predis\IConnectionParameters;
  4. use Predis\Options\IOption;
  5. class ConnectionParameters implements IConnectionParameters {
  6. private static $_defaultParameters;
  7. private static $_validators;
  8. private $_parameters;
  9. private $_userDefined;
  10. public function __construct($parameters = array()) {
  11. self::ensureDefaults();
  12. $extractor = is_array($parameters) ? 'filter' : 'parseURI';
  13. $parameters = $this->$extractor($parameters);
  14. $this->_userDefined = array_keys($parameters);
  15. $this->_parameters = array_merge(self::$_defaultParameters, $parameters);
  16. }
  17. private static function ensureDefaults() {
  18. if (!isset(self::$_defaultParameters)) {
  19. self::$_defaultParameters = array(
  20. 'scheme' => 'tcp',
  21. 'host' => '127.0.0.1',
  22. 'port' => 6379,
  23. 'database' => null,
  24. 'password' => null,
  25. 'connection_async' => false,
  26. 'connection_persistent' => false,
  27. 'connection_timeout' => 5.0,
  28. 'read_write_timeout' => null,
  29. 'alias' => null,
  30. 'weight' => null,
  31. 'path' => null,
  32. 'iterable_multibulk' => false,
  33. 'throw_errors' => true,
  34. );
  35. }
  36. if (!isset(self::$_validators)) {
  37. $boolValidator = function($value) { return (bool) $value; };
  38. $floatValidator = function($value) { return (float) $value; };
  39. $intValidator = function($value) { return (int) $value; };
  40. self::$_validators = array(
  41. 'port' => $intValidator,
  42. 'connection_async' => $boolValidator,
  43. 'connection_persistent' => $boolValidator,
  44. 'connection_timeout' => $floatValidator,
  45. 'read_write_timeout' => $floatValidator,
  46. 'iterable_multibulk' => $boolValidator,
  47. 'throw_errors' => $boolValidator,
  48. );
  49. }
  50. }
  51. public static function define($parameter, $default, $callable = null) {
  52. self::ensureDefaults();
  53. self::$_defaultParameters[$parameter] = $default;
  54. if ($default instanceof IOption) {
  55. self::$_validators[$parameter] = $default;
  56. return;
  57. }
  58. if (!isset($callable)) {
  59. unset(self::$_validators[$parameter]);
  60. return;
  61. }
  62. if (!is_callable($callable)) {
  63. throw new \InvalidArgumentException(
  64. "The validator for $parameter must be a callable object"
  65. );
  66. }
  67. self::$_validators[$parameter] = $callable;
  68. }
  69. public static function undefine($parameter) {
  70. self::ensureDefaults();
  71. unset(self::$_defaultParameters[$parameter], self::$_validators[$parameter]);
  72. }
  73. private function parseURI($uri) {
  74. if (stripos($uri, 'unix') === 0) {
  75. // Hack to support URIs for UNIX sockets with minimal effort.
  76. $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
  77. }
  78. if (($parsed = @parse_url($uri)) === false || !isset($parsed['host'])) {
  79. throw new ClientException("Invalid URI: $uri");
  80. }
  81. if (isset($parsed['query'])) {
  82. foreach (explode('&', $parsed['query']) as $kv) {
  83. @list($k, $v) = explode('=', $kv);
  84. $parsed[$k] = $v;
  85. }
  86. unset($parsed['query']);
  87. }
  88. return $this->filter($parsed);
  89. }
  90. private function filter(Array $parameters) {
  91. if (count($parameters) > 0) {
  92. $validators = self::$_validators;
  93. foreach ($parameters as $parameter => $value) {
  94. if (isset($validators[$parameter])) {
  95. $parameters[$parameter] = $validators[$parameter]($value);
  96. }
  97. }
  98. }
  99. return $parameters;
  100. }
  101. public function __get($parameter) {
  102. $value = $this->_parameters[$parameter];
  103. if ($value instanceof IOption) {
  104. $this->_parameters[$parameter] = ($value = $value->getDefault());
  105. }
  106. return $value;
  107. }
  108. public function __isset($parameter) {
  109. return in_array($parameter, $this->_userDefined);
  110. }
  111. protected function getBaseURI() {
  112. if ($this->scheme === 'unix') {
  113. return "{$this->scheme}://{$this->path}";
  114. }
  115. return "{$this->scheme}://{$this->host}:{$this->port}";
  116. }
  117. protected function getDisallowedURIParts() {
  118. return array('scheme', 'host', 'port', 'password', 'path');
  119. }
  120. public function toArray() {
  121. return $this->_parameters;
  122. }
  123. public function __toString() {
  124. $query = array();
  125. $parameters = $this->toArray();
  126. $reject = $this->getDisallowedURIParts();
  127. foreach ($this->_userDefined as $param) {
  128. if (in_array($param, $reject) || !isset($parameters[$param])) {
  129. continue;
  130. }
  131. $value = $parameters[$param];
  132. $query[] = "$param=" . ($value === false ? '0' : $value);
  133. }
  134. if (count($query) === 0) {
  135. return $this->getBaseURI();
  136. }
  137. return $this->getBaseURI() . '/?' . implode('&', $query);
  138. }
  139. public function __sleep() {
  140. return array('_parameters', '_userDefined');
  141. }
  142. public function __wakeup() {
  143. self::ensureDefaults();
  144. }
  145. }