ConnectionParameters.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /**
  27. * @param string|array Connection parameters in the form of an URI string or a named array.
  28. */
  29. public function __construct($parameters = null)
  30. {
  31. if (is_string($parameters)) {
  32. $parameters = self::parse($parameters);
  33. }
  34. $this->parameters = $this->filter($parameters ?: array()) + $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. );
  59. }
  60. /**
  61. * Validates value as boolean.
  62. *
  63. * @param mixed $value Input value.
  64. * @return boolean
  65. */
  66. private static function castBoolean($value)
  67. {
  68. return (bool) $value;
  69. }
  70. /**
  71. * Validates value as float.
  72. *
  73. * @param mixed $value Input value.
  74. * @return float
  75. */
  76. private static function castFloat($value)
  77. {
  78. return (float) $value;
  79. }
  80. /**
  81. * Validates value as integer.
  82. *
  83. * @param mixed $value Input value.
  84. * @return int
  85. */
  86. private static function castInteger($value)
  87. {
  88. return (int) $value;
  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. foreach (explode('&', $parsed['query']) as $kv) {
  107. $kv = explode('=', $kv, 2);
  108. if (isset($kv[0], $kv[1])) {
  109. $parsed[$kv[0]] = $kv[1];
  110. }
  111. }
  112. unset($parsed['query']);
  113. }
  114. return $parsed;
  115. }
  116. /**
  117. * Validates and converts each value of the connection parameters array.
  118. *
  119. * @param array $parameters Connection parameters.
  120. * @return array
  121. */
  122. private function filter(array $parameters)
  123. {
  124. if ($parameters) {
  125. $casters = array_intersect_key($this->getValueCasters(), $parameters);
  126. foreach ($casters as $parameter => $caster) {
  127. $parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
  128. }
  129. }
  130. return $parameters;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function __get($parameter)
  136. {
  137. if (isset($this->parameters[$parameter])) {
  138. return $this->parameters[$parameter];
  139. }
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function __isset($parameter)
  145. {
  146. return isset($this->parameters[$parameter]);
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function toArray()
  152. {
  153. return $this->parameters;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function __sleep()
  159. {
  160. return array('parameters');
  161. }
  162. }