ConnectionParameters.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 $parameters 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. 'iterable_multibulk' => 'self::castBoolean',
  59. );
  60. }
  61. /**
  62. * Validates value as boolean.
  63. *
  64. * @param mixed $value Input value.
  65. * @return bool
  66. */
  67. private static function castBoolean($value)
  68. {
  69. return (bool) $value;
  70. }
  71. /**
  72. * Validates value as float.
  73. *
  74. * @param mixed $value Input value.
  75. * @return float
  76. */
  77. private static function castFloat($value)
  78. {
  79. return (float) $value;
  80. }
  81. /**
  82. * Validates value as integer.
  83. *
  84. * @param mixed $value Input value.
  85. * @return int
  86. */
  87. private static function castInteger($value)
  88. {
  89. return (int) $value;
  90. }
  91. /**
  92. * Parses an URI string and returns an array of connection parameters.
  93. *
  94. * @param string $uri Connection string.
  95. * @return array
  96. */
  97. public static function parseURI($uri)
  98. {
  99. if (stripos($uri, 'unix') === 0) {
  100. // Hack to support URIs for UNIX sockets with minimal effort.
  101. $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
  102. }
  103. if (!($parsed = @parse_url($uri)) || !isset($parsed['host'])) {
  104. throw new ClientException("Invalid URI: $uri");
  105. }
  106. if (isset($parsed['query'])) {
  107. parse_str($parsed['query'], $queryarray);
  108. unset($parsed['query']);
  109. $parsed = array_merge($parsed, $queryarray);
  110. }
  111. return $parsed;
  112. }
  113. /**
  114. * Validates and converts each value of the connection parameters array.
  115. *
  116. * @param array $parameters Connection parameters.
  117. * @return array
  118. */
  119. private function filter(Array $parameters)
  120. {
  121. if ($parameters) {
  122. $casters = array_intersect_key($this->getValueCasters(), $parameters);
  123. foreach ($casters as $parameter => $caster) {
  124. $parameters[$parameter] = call_user_func($caster, $parameters[$parameter]);
  125. }
  126. }
  127. return $parameters;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function __get($parameter)
  133. {
  134. if (isset($this->{$parameter})) {
  135. return $this->parameters[$parameter];
  136. }
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function __isset($parameter)
  142. {
  143. return isset($this->parameters[$parameter]);
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function toArray()
  149. {
  150. return $this->parameters;
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function __sleep()
  156. {
  157. return array('parameters');
  158. }
  159. }