ConnectionParameters.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis;
  11. use Predis\Option\OptionInterface;
  12. /**
  13. * Handles parsing and validation of connection parameters.
  14. *
  15. * @author Daniele Alessandri <suppakilla@gmail.com>
  16. */
  17. class ConnectionParameters implements ConnectionParametersInterface
  18. {
  19. private $parameters;
  20. private static $defaults = array(
  21. 'scheme' => 'tcp',
  22. 'host' => '127.0.0.1',
  23. 'port' => 6379,
  24. 'timeout' => 5.0,
  25. 'iterable_multibulk' => false,
  26. );
  27. /**
  28. * @param string|array Connection parameters in the form of an URI string or a named array.
  29. */
  30. public function __construct($parameters = array())
  31. {
  32. if (!is_array($parameters)) {
  33. $parameters = $this->parseURI($parameters);
  34. }
  35. $this->parameters = $this->filter($parameters) + $this->getDefaults();
  36. }
  37. /**
  38. * Returns some default parameters with their values.
  39. *
  40. * @return array
  41. */
  42. protected function getDefaults()
  43. {
  44. return self::$defaults;
  45. }
  46. /**
  47. * Returns validators functions for the values of certain parameters.
  48. *
  49. * @return array
  50. */
  51. protected function getValidators()
  52. {
  53. $bool = function($value) { return (bool) $value; };
  54. $float = function($value) { return (float) $value; };
  55. $int = function($value) { return (int) $value; };
  56. return array(
  57. 'port' => $int,
  58. 'async_connect' => $bool,
  59. 'persistent' => $bool,
  60. 'timeout' => $float,
  61. 'read_write_timeout' => $float,
  62. 'iterable_multibulk' => $bool,
  63. );
  64. }
  65. /**
  66. * Parses an URI string and returns an array of connection parameters.
  67. *
  68. * @param string $uri Connection string.
  69. * @return array
  70. */
  71. private function parseURI($uri)
  72. {
  73. if (stripos($uri, 'unix') === 0) {
  74. // Hack to support URIs for UNIX sockets with minimal effort.
  75. $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
  76. }
  77. if (($parsed = @parse_url($uri)) === false || !isset($parsed['host'])) {
  78. throw new ClientException("Invalid URI: $uri");
  79. }
  80. if (isset($parsed['query'])) {
  81. foreach (explode('&', $parsed['query']) as $kv) {
  82. @list($k, $v) = explode('=', $kv);
  83. $parsed[$k] = $v;
  84. }
  85. unset($parsed['query']);
  86. }
  87. return $parsed;
  88. }
  89. /**
  90. * Validates and converts each value of the connection parameters array.
  91. *
  92. * @param array $parameters Connection parameters.
  93. * @return array
  94. */
  95. private function filter(Array $parameters)
  96. {
  97. if (count($parameters) > 0) {
  98. $validators = array_intersect_key($this->getValidators(), $parameters);
  99. foreach ($validators as $parameter => $validator) {
  100. $parameters[$parameter] = $validator($parameters[$parameter]);
  101. }
  102. }
  103. return $parameters;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function __get($parameter)
  109. {
  110. if (isset($this->{$parameter})) {
  111. return $this->parameters[$parameter];
  112. }
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function __isset($parameter)
  118. {
  119. return isset($this->parameters[$parameter]);
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function toArray()
  125. {
  126. return $this->parameters;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function __sleep()
  132. {
  133. return array('parameters');
  134. }
  135. }