Parameters.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 Parameters implements ParametersInterface
  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 $parameters 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 Parameters
  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. parse_str($parsed['query'], $queryarray);
  89. unset($parsed['query']);
  90. $parsed = array_merge($parsed, $queryarray);
  91. }
  92. return $parsed;
  93. }
  94. /**
  95. * Validates and converts each value of the connection parameters array.
  96. *
  97. * @param array $parameters Connection parameters.
  98. * @return array
  99. */
  100. private function filter(array $parameters)
  101. {
  102. if ($parameters) {
  103. $casters = array_intersect_key($this->getValueCasters(), $parameters);
  104. foreach ($casters as $parameter => $caster) {
  105. $parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
  106. }
  107. }
  108. return $parameters ?: array();
  109. }
  110. /**
  111. * Validates value as boolean.
  112. *
  113. * @param mixed $value Input value.
  114. * @return boolean
  115. */
  116. private static function castBoolean($value)
  117. {
  118. return (bool) $value;
  119. }
  120. /**
  121. * Validates value as float.
  122. *
  123. * @param mixed $value Input value.
  124. * @return float
  125. */
  126. private static function castFloat($value)
  127. {
  128. return (float) $value;
  129. }
  130. /**
  131. * Validates value as integer.
  132. *
  133. * @param mixed $value Input value.
  134. * @return int
  135. */
  136. private static function castInteger($value)
  137. {
  138. return (int) $value;
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function __get($parameter)
  144. {
  145. if (isset($this->parameters[$parameter])) {
  146. return $this->parameters[$parameter];
  147. }
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function __isset($parameter)
  153. {
  154. return isset($this->parameters[$parameter]);
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function toArray()
  160. {
  161. return $this->parameters;
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function __sleep()
  167. {
  168. return array('parameters');
  169. }
  170. }