ConnectionParameters.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. private function parseURI($uri) {
  70. if (stripos($uri, 'unix') === 0) {
  71. // Hack to support URIs for UNIX sockets with minimal effort.
  72. $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
  73. }
  74. if (($parsed = @parse_url($uri)) === false || !isset($parsed['host'])) {
  75. throw new ClientException("Invalid URI: $uri");
  76. }
  77. if (isset($parsed['query'])) {
  78. foreach (explode('&', $parsed['query']) as $kv) {
  79. @list($k, $v) = explode('=', $kv);
  80. $parsed[$k] = $v;
  81. }
  82. unset($parsed['query']);
  83. }
  84. return $this->filter($parsed);
  85. }
  86. private function filter(Array $parameters) {
  87. if (count($parameters) > 0) {
  88. $validators = self::$_validators;
  89. foreach ($parameters as $parameter => $value) {
  90. if (isset($validators[$parameter])) {
  91. $parameters[$parameter] = $validators[$parameter]($value);
  92. }
  93. }
  94. }
  95. return $parameters;
  96. }
  97. public function __get($parameter) {
  98. $value = $this->_parameters[$parameter];
  99. if ($value instanceof IOption) {
  100. $this->_parameters[$parameter] = ($value = $value->getDefault());
  101. }
  102. return $value;
  103. }
  104. public function __isset($parameter) {
  105. return in_array($parameter, $this->_userDefined);
  106. }
  107. protected function getBaseURI() {
  108. if ($this->scheme === 'unix') {
  109. return "{$this->scheme}://{$this->path}";
  110. }
  111. return "{$this->scheme}://{$this->host}:{$this->port}";
  112. }
  113. protected function getDisallowedURIParts() {
  114. return array('scheme', 'host', 'port', 'password', 'path');
  115. }
  116. public function toArray() {
  117. return $this->_parameters;
  118. }
  119. public function __toString() {
  120. $query = array();
  121. $parameters = $this->toArray();
  122. $reject = $this->getDisallowedURIParts();
  123. foreach ($this->_userDefined as $param) {
  124. if (in_array($param, $reject) || !isset($parameters[$param])) {
  125. continue;
  126. }
  127. $value = $parameters[$param];
  128. $query[] = "$param=" . ($value === false ? '0' : $value);
  129. }
  130. if (count($query) === 0) {
  131. return $this->getBaseURI();
  132. }
  133. return $this->getBaseURI() . '/?' . implode('&', $query);
  134. }
  135. public function __sleep() {
  136. return array('_parameters', '_userDefined');
  137. }
  138. public function __wakeup() {
  139. self::ensureDefaults();
  140. }
  141. }