ConnectionParameters.php 4.4 KB

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