Parameters.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * The actual list of supported connection parameters depends on the features
  16. * supported by each connection backend class (please refer to their specific
  17. * documentation), furthermore developers can pass their own custom parameters.
  18. *
  19. * These are the most common parameters used through the library:
  20. *
  21. * @property-read string scheme Connection scheme, such as 'tcp' or 'unix'.
  22. * @property-read string host IP address or hostname of Redis.
  23. * @property-read int port TCP port on which Redis is listening to.
  24. * @property-read string path Path of a UNIX domain socket file.
  25. * @property-read float timeout Timeout for the connect() operation.
  26. * @property-read float read_write_timeout Timeout for read() and write() operations.
  27. * @property-read bool async_connect Performs the connect() operation asynchronously.
  28. * @property-read bool tcp_nodelay Toggles the Nagle's algorithm for coalescing.
  29. * @property-read bool persistent Leaves the connection open after a GC collection.
  30. * @property-read string password Password to access Redis (see the AUTH command).
  31. * @property-read string database Database index (see the SELECT command).
  32. *
  33. * @author Daniele Alessandri <suppakilla@gmail.com>
  34. */
  35. class Parameters implements ParametersInterface
  36. {
  37. private $parameters;
  38. private static $defaults = array(
  39. 'scheme' => 'tcp',
  40. 'host' => '127.0.0.1',
  41. 'port' => 6379,
  42. 'timeout' => 5.0,
  43. );
  44. private static $casters = array(
  45. 'port' => 'self::castInteger',
  46. 'async_connect' => 'self::castBoolean',
  47. 'persistent' => 'self::castBoolean',
  48. 'timeout' => 'self::castFloat',
  49. 'read_write_timeout' => 'self::castFloat',
  50. );
  51. /**
  52. * @param array $parameters Named array of connection parameters.
  53. */
  54. public function __construct(array $parameters = array())
  55. {
  56. $this->parameters = $this->filter($parameters) + $this->getDefaults();
  57. }
  58. /**
  59. * Returns some default parameters with their values.
  60. *
  61. * @return array
  62. */
  63. protected function getDefaults()
  64. {
  65. return self::$defaults;
  66. }
  67. /**
  68. * Returns cast functions for user-supplied parameter values.
  69. *
  70. * @return array
  71. */
  72. protected function getValueCasters()
  73. {
  74. return self::$casters;
  75. }
  76. /**
  77. * Creates a new instance by supplying the initial parameters either in the
  78. * form of an URI string or a named array.
  79. *
  80. * @param array|string $parameters Set of connection parameters.
  81. * @return Parameters
  82. */
  83. public static function create($parameters)
  84. {
  85. if (is_string($parameters)) {
  86. $parameters = self::parse($parameters);
  87. }
  88. return new self($parameters ?: array());
  89. }
  90. /**
  91. * Parses an URI string returning an array of connection parameters.
  92. *
  93. * @param string $uri URI string.
  94. * @return array
  95. */
  96. public static function parse($uri)
  97. {
  98. if (stripos($uri, 'unix') === 0) {
  99. // Hack to support URIs for UNIX sockets with minimal effort.
  100. $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
  101. }
  102. if (!($parsed = parse_url($uri)) || !isset($parsed['host'])) {
  103. throw new InvalidArgumentException("Invalid parameters URI: $uri");
  104. }
  105. if (isset($parsed['query'])) {
  106. parse_str($parsed['query'], $queryarray);
  107. unset($parsed['query']);
  108. $parsed = array_merge($parsed, $queryarray);
  109. }
  110. return $parsed;
  111. }
  112. /**
  113. * Validates and converts each value of the connection parameters array.
  114. *
  115. * @param array $parameters Connection parameters.
  116. * @return array
  117. */
  118. private function filter(array $parameters)
  119. {
  120. if ($parameters) {
  121. $casters = array_intersect_key($this->getValueCasters(), $parameters);
  122. foreach ($casters as $parameter => $caster) {
  123. $parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
  124. }
  125. }
  126. return $parameters ?: array();
  127. }
  128. /**
  129. * Validates value as boolean.
  130. *
  131. * @param mixed $value Input value.
  132. * @return boolean
  133. */
  134. private static function castBoolean($value)
  135. {
  136. return (bool) $value;
  137. }
  138. /**
  139. * Validates value as float.
  140. *
  141. * @param mixed $value Input value.
  142. * @return float
  143. */
  144. private static function castFloat($value)
  145. {
  146. return (float) $value;
  147. }
  148. /**
  149. * Validates value as integer.
  150. *
  151. * @param mixed $value Input value.
  152. * @return int
  153. */
  154. private static function castInteger($value)
  155. {
  156. return (int) $value;
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function __get($parameter)
  162. {
  163. if (isset($this->parameters[$parameter])) {
  164. return $this->parameters[$parameter];
  165. }
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function __isset($parameter)
  171. {
  172. return isset($this->parameters[$parameter]);
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function toArray()
  178. {
  179. return $this->parameters;
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function __sleep()
  185. {
  186. return array('parameters');
  187. }
  188. }