ConnectionParameters.php 4.6 KB

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