ConnectionParameters.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * 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. 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 string|array Connection parameters in the form of an URI string or a named array.
  35. */
  36. public function __construct($parameters = null)
  37. {
  38. if (is_string($parameters)) {
  39. $parameters = self::parse($parameters);
  40. }
  41. $this->parameters = $this->filter($parameters ?: array()) + $this->getDefaults();
  42. }
  43. /**
  44. * Returns some default parameters with their values.
  45. *
  46. * @return array
  47. */
  48. protected function getDefaults()
  49. {
  50. return self::$defaults;
  51. }
  52. /**
  53. * Returns cast functions for user-supplied parameter values.
  54. *
  55. * @return array
  56. */
  57. protected function getValueCasters()
  58. {
  59. return self::$casters;
  60. }
  61. /**
  62. * Parses an URI string returning an array of connection parameters.
  63. *
  64. * @param string $uri URI string.
  65. * @return array
  66. */
  67. public static function parse($uri)
  68. {
  69. if (stripos($uri, 'unix') === 0) {
  70. // Hack to support URIs for UNIX sockets with minimal effort.
  71. $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
  72. }
  73. if (!($parsed = parse_url($uri)) || !isset($parsed['host'])) {
  74. throw new InvalidArgumentException("Invalid parameters URI: $uri");
  75. }
  76. if (isset($parsed['query'])) {
  77. foreach (explode('&', $parsed['query']) as $kv) {
  78. $kv = explode('=', $kv, 2);
  79. if (isset($kv[0], $kv[1])) {
  80. $parsed[$kv[0]] = $kv[1];
  81. }
  82. }
  83. unset($parsed['query']);
  84. }
  85. return $parsed;
  86. }
  87. /**
  88. * Validates and converts each value of the connection parameters array.
  89. *
  90. * @param array $parameters Connection parameters.
  91. * @return array
  92. */
  93. private function filter(array $parameters)
  94. {
  95. if ($parameters) {
  96. $casters = array_intersect_key($this->getValueCasters(), $parameters);
  97. foreach ($casters as $parameter => $caster) {
  98. $parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
  99. }
  100. }
  101. return $parameters;
  102. }
  103. /**
  104. * Validates value as boolean.
  105. *
  106. * @param mixed $value Input value.
  107. * @return boolean
  108. */
  109. private static function castBoolean($value)
  110. {
  111. return (bool) $value;
  112. }
  113. /**
  114. * Validates value as float.
  115. *
  116. * @param mixed $value Input value.
  117. * @return float
  118. */
  119. private static function castFloat($value)
  120. {
  121. return (float) $value;
  122. }
  123. /**
  124. * Validates value as integer.
  125. *
  126. * @param mixed $value Input value.
  127. * @return int
  128. */
  129. private static function castInteger($value)
  130. {
  131. return (int) $value;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function __get($parameter)
  137. {
  138. if (isset($this->parameters[$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. }