ConnectionParameters.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\Connection;
  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 cast functions for user-supplied parameter values.
  48. *
  49. * @return array
  50. */
  51. protected function getValueCasters()
  52. {
  53. return array(
  54. 'port' => 'self::castInteger',
  55. 'async_connect' => 'self::castBoolean',
  56. 'persistent' => 'self::castBoolean',
  57. 'timeout' => 'self::castFloat',
  58. 'read_write_timeout' => 'self::castFloat',
  59. 'iterable_multibulk' => 'self::castBoolean',
  60. );
  61. }
  62. /**
  63. * Validates value as boolean.
  64. *
  65. * @param mixed $value Input value.
  66. * @return boolean
  67. */
  68. private static function castBoolean($value)
  69. {
  70. return (bool) $value;
  71. }
  72. /**
  73. * Validates value as float.
  74. *
  75. * @param mixed $value Input value.
  76. * @return float
  77. */
  78. private static function castFloat($value)
  79. {
  80. return (float) $value;
  81. }
  82. /**
  83. * Validates value as integer.
  84. *
  85. * @param mixed $value Input value.
  86. * @return int
  87. */
  88. private static function castInteger($value)
  89. {
  90. return (int) $value;
  91. }
  92. /**
  93. * Parses an URI string and returns an array of connection parameters.
  94. *
  95. * @param string $uri Connection string.
  96. * @return array
  97. */
  98. private function parseURI($uri)
  99. {
  100. if (stripos($uri, 'unix') === 0) {
  101. // Hack to support URIs for UNIX sockets with minimal effort.
  102. $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
  103. }
  104. if (($parsed = @parse_url($uri)) === false || !isset($parsed['host'])) {
  105. throw new ClientException("Invalid URI: $uri");
  106. }
  107. if (isset($parsed['query'])) {
  108. foreach (explode('&', $parsed['query']) as $kv) {
  109. @list($k, $v) = explode('=', $kv);
  110. $parsed[$k] = $v;
  111. }
  112. unset($parsed['query']);
  113. }
  114. return $parsed;
  115. }
  116. /**
  117. * Validates and converts each value of the connection parameters array.
  118. *
  119. * @param array $parameters Connection parameters.
  120. * @return array
  121. */
  122. private function filter(Array $parameters)
  123. {
  124. if (count($parameters) > 0) {
  125. $casters = array_intersect_key($this->getValueCasters(), $parameters);
  126. foreach ($casters as $parameter => $caster) {
  127. $parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
  128. }
  129. }
  130. return $parameters;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function __get($parameter)
  136. {
  137. if (isset($this->{$parameter})) {
  138. return $this->parameters[$parameter];
  139. }
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function __isset($parameter)
  145. {
  146. return isset($this->parameters[$parameter]);
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function toArray()
  152. {
  153. return $this->parameters;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function __sleep()
  159. {
  160. return array('parameters');
  161. }
  162. }