ConnectionParameters.php 4.2 KB

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