ConnectionParameters.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 Predis\ClientException;
  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 = array())
  30. {
  31. if (!is_array($parameters)) {
  32. $parameters = self::parseURI($parameters);
  33. }
  34. $this->parameters = $this->filter($parameters) + $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 and returns an array of connection parameters.
  92. *
  93. * @param string $uri Connection string.
  94. * @return array
  95. */
  96. public static function parseURI($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 ClientException("Invalid URI: $uri");
  104. }
  105. if (isset($parsed['query'])) {
  106. foreach (explode('&', $parsed['query']) as $kv) {
  107. @list($k, $v) = explode('=', $kv);
  108. $parsed[$k] = $v;
  109. }
  110. unset($parsed['query']);
  111. }
  112. return $parsed;
  113. }
  114. /**
  115. * Validates and converts each value of the connection parameters array.
  116. *
  117. * @param array $parameters Connection parameters.
  118. * @return array
  119. */
  120. private function filter(Array $parameters)
  121. {
  122. if ($parameters) {
  123. $casters = array_intersect_key($this->getValueCasters(), $parameters);
  124. foreach ($casters as $parameter => $caster) {
  125. $parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
  126. }
  127. }
  128. return $parameters;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function __get($parameter)
  134. {
  135. if (isset($this->parameters[$parameter])) {
  136. return $this->parameters[$parameter];
  137. }
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function __isset($parameter)
  143. {
  144. return isset($this->parameters[$parameter]);
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function toArray()
  150. {
  151. return $this->parameters;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function __sleep()
  157. {
  158. return array('parameters');
  159. }
  160. }