ConnectionParameters.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\ClientException;
  12. use Predis\Option\OptionInterface;
  13. /**
  14. * Handles parsing and validation of connection parameters.
  15. *
  16. * @author Daniele Alessandri <suppakilla@gmail.com>
  17. */
  18. class ConnectionParameters implements ConnectionParametersInterface
  19. {
  20. private $parameters;
  21. private static $defaults = array(
  22. 'scheme' => 'tcp',
  23. 'host' => '127.0.0.1',
  24. 'port' => 6379,
  25. 'timeout' => 5.0,
  26. 'iterable_multibulk' => false,
  27. );
  28. /**
  29. * @param string|array Connection parameters in the form of an URI string or a named array.
  30. */
  31. public function __construct($parameters = array())
  32. {
  33. if (!is_array($parameters)) {
  34. $parameters = $this->parseURI($parameters);
  35. }
  36. $this->parameters = $this->filter($parameters) + $this->getDefaults();
  37. }
  38. /**
  39. * Returns some default parameters with their values.
  40. *
  41. * @return array
  42. */
  43. protected function getDefaults()
  44. {
  45. return self::$defaults;
  46. }
  47. /**
  48. * Returns cast functions for user-supplied parameter values.
  49. *
  50. * @return array
  51. */
  52. protected function getValueCasters()
  53. {
  54. return array(
  55. 'port' => 'self::castInteger',
  56. 'async_connect' => 'self::castBoolean',
  57. 'persistent' => 'self::castBoolean',
  58. 'timeout' => 'self::castFloat',
  59. 'read_write_timeout' => 'self::castFloat',
  60. 'iterable_multibulk' => 'self::castBoolean',
  61. );
  62. }
  63. /**
  64. * Validates value as boolean.
  65. *
  66. * @param mixed $value Input value.
  67. * @return boolean
  68. */
  69. private static function castBoolean($value)
  70. {
  71. return (bool) $value;
  72. }
  73. /**
  74. * Validates value as float.
  75. *
  76. * @param mixed $value Input value.
  77. * @return float
  78. */
  79. private static function castFloat($value)
  80. {
  81. return (float) $value;
  82. }
  83. /**
  84. * Validates value as integer.
  85. *
  86. * @param mixed $value Input value.
  87. * @return int
  88. */
  89. private static function castInteger($value)
  90. {
  91. return (int) $value;
  92. }
  93. /**
  94. * Parses an URI string and returns an array of connection parameters.
  95. *
  96. * @param string $uri Connection string.
  97. * @return array
  98. */
  99. private function parseURI($uri)
  100. {
  101. if (stripos($uri, 'unix') === 0) {
  102. // Hack to support URIs for UNIX sockets with minimal effort.
  103. $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
  104. }
  105. if (($parsed = @parse_url($uri)) === false || !isset($parsed['host'])) {
  106. throw new ClientException("Invalid URI: $uri");
  107. }
  108. if (isset($parsed['query'])) {
  109. foreach (explode('&', $parsed['query']) as $kv) {
  110. @list($k, $v) = explode('=', $kv);
  111. $parsed[$k] = $v;
  112. }
  113. unset($parsed['query']);
  114. }
  115. return $parsed;
  116. }
  117. /**
  118. * Validates and converts each value of the connection parameters array.
  119. *
  120. * @param array $parameters Connection parameters.
  121. * @return array
  122. */
  123. private function filter(Array $parameters)
  124. {
  125. if (count($parameters) > 0) {
  126. $casters = array_intersect_key($this->getValueCasters(), $parameters);
  127. foreach ($casters as $parameter => $caster) {
  128. $parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
  129. }
  130. }
  131. return $parameters;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function __get($parameter)
  137. {
  138. if (isset($this->{$parameter})) {
  139. return $this->parameters[$parameter];
  140. }
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function __isset($parameter)
  146. {
  147. return isset($this->parameters[$parameter]);
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function toArray()
  153. {
  154. return $this->parameters;
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function __sleep()
  160. {
  161. return array('parameters');
  162. }
  163. }