ConnectionParameters.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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;
  11. use Predis\Option\OptionInterface;
  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. 'iterable_multibulk' => false,
  26. 'throw_errors' => true,
  27. );
  28. /**
  29. * @param string|array Connection parameters in the form of an URI string or a named array.
  30. */
  31. public function __construct($parameters = array())
  32. {
  33. if (!is_array($parameters)) {
  34. $parameters = $this->parseURI($parameters);
  35. }
  36. $this->parameters = $this->filter($parameters) + $this->getDefaults();
  37. }
  38. /**
  39. * Returns some default parameters with their values.
  40. *
  41. * @return array
  42. */
  43. protected function getDefaults()
  44. {
  45. return self::$defaults;
  46. }
  47. /**
  48. * Returns validators functions for the values of certain parameters.
  49. *
  50. * @return array
  51. */
  52. protected function getValidators()
  53. {
  54. $bool = function($value) { return (bool) $value; };
  55. $float = function($value) { return (float) $value; };
  56. $int = function($value) { return (int) $value; };
  57. return array(
  58. 'port' => $int,
  59. 'async_connect' => $bool,
  60. 'persistent' => $bool,
  61. 'timeout' => $float,
  62. 'read_write_timeout' => $float,
  63. 'iterable_multibulk' => $bool,
  64. 'throw_errors' => $bool,
  65. );
  66. }
  67. /**
  68. * Parses an URI string and returns an array of connection parameters.
  69. *
  70. * @param string $uri Connection string.
  71. * @return array
  72. */
  73. private function parseURI($uri)
  74. {
  75. if (stripos($uri, 'unix') === 0) {
  76. // Hack to support URIs for UNIX sockets with minimal effort.
  77. $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
  78. }
  79. if (($parsed = @parse_url($uri)) === false || !isset($parsed['host'])) {
  80. throw new ClientException("Invalid URI: $uri");
  81. }
  82. if (isset($parsed['query'])) {
  83. foreach (explode('&', $parsed['query']) as $kv) {
  84. @list($k, $v) = explode('=', $kv);
  85. $parsed[$k] = $v;
  86. }
  87. unset($parsed['query']);
  88. }
  89. return $parsed;
  90. }
  91. /**
  92. * Validates and converts each value of the connection parameters array.
  93. *
  94. * @param array $parameters Connection parameters.
  95. * @return array
  96. */
  97. private function filter(Array $parameters)
  98. {
  99. if (count($parameters) > 0) {
  100. $validators = array_intersect_key($this->getValidators(), $parameters);
  101. foreach ($validators as $parameter => $validator) {
  102. $parameters[$parameter] = $validator($parameters[$parameter]);
  103. }
  104. }
  105. return $parameters;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function __get($parameter)
  111. {
  112. if (isset($this->{$parameter})) {
  113. return $this->parameters[$parameter];
  114. }
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function __isset($parameter)
  120. {
  121. return isset($this->parameters[$parameter]);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function toArray()
  127. {
  128. return $this->parameters;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function __sleep()
  134. {
  135. return array('parameters');
  136. }
  137. }