/foody.blogtamsudev.vn/vendor/predis/predis/src/Connection/Parameters.php

https://gitlab.com/ntphuc/BackendFeedy · PHP · 176 lines · 85 code · 22 blank · 69 comment · 14 complexity · 8b25263a9360613fb43c18c2acf195e1 MD5 · raw file

  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. /**
  12. * Container for connection parameters used to initialize connections to Redis.
  13. *
  14. * {@inheritdoc}
  15. *
  16. * @author Daniele Alessandri <suppakilla@gmail.com>
  17. */
  18. class Parameters implements ParametersInterface
  19. {
  20. private $parameters;
  21. private static $defaults = array(
  22. 'scheme' => 'tcp',
  23. 'host' => '127.0.0.1',
  24. 'port' => 6379,
  25. 'timeout' => 5.0,
  26. );
  27. /**
  28. * @param array $parameters Named array of connection parameters.
  29. */
  30. public function __construct(array $parameters = array())
  31. {
  32. $this->parameters = $this->filter($parameters) + $this->getDefaults();
  33. }
  34. /**
  35. * Returns some default parameters with their values.
  36. *
  37. * @return array
  38. */
  39. protected function getDefaults()
  40. {
  41. return self::$defaults;
  42. }
  43. /**
  44. * Creates a new instance by supplying the initial parameters either in the
  45. * form of an URI string or a named array.
  46. *
  47. * @param array|string $parameters Set of connection parameters.
  48. *
  49. * @return Parameters
  50. */
  51. public static function create($parameters)
  52. {
  53. if (is_string($parameters)) {
  54. $parameters = static::parse($parameters);
  55. }
  56. return new static($parameters ?: array());
  57. }
  58. /**
  59. * Parses an URI string returning an array of connection parameters.
  60. *
  61. * When using the "redis" and "rediss" schemes the URI is parsed according
  62. * to the rules defined by the provisional registration documents approved
  63. * by IANA. If the URI has a password in its "user-information" part or a
  64. * database number in the "path" part these values override the values of
  65. * "password" and "database" if they are present in the "query" part.
  66. *
  67. * @link http://www.iana.org/assignments/uri-schemes/prov/redis
  68. * @link http://www.iana.org/assignments/uri-schemes/prov/redis
  69. *
  70. * @param string $uri URI string.
  71. *
  72. * @throws \InvalidArgumentException
  73. *
  74. * @return array
  75. */
  76. public static function parse($uri)
  77. {
  78. if (stripos($uri, 'unix') === 0) {
  79. // Hack to support URIs for UNIX sockets with minimal effort.
  80. $uri = str_ireplace('unix:///', 'unix://localhost/', $uri);
  81. }
  82. if (!$parsed = parse_url($uri)) {
  83. throw new \InvalidArgumentException("Invalid parameters URI: $uri");
  84. }
  85. if (
  86. isset($parsed['host'])
  87. && false !== strpos($parsed['host'], '[')
  88. && false !== strpos($parsed['host'], ']')
  89. ) {
  90. $parsed['host'] = substr($parsed['host'], 1, -1);
  91. }
  92. if (isset($parsed['query'])) {
  93. parse_str($parsed['query'], $queryarray);
  94. unset($parsed['query']);
  95. $parsed = array_merge($parsed, $queryarray);
  96. }
  97. if (stripos($uri, 'redis') === 0) {
  98. if (isset($parsed['pass'])) {
  99. $parsed['password'] = $parsed['pass'];
  100. unset($parsed['pass']);
  101. }
  102. if (isset($parsed['path']) && preg_match('/^\/(\d+)(\/.*)?/', $parsed['path'], $path)) {
  103. $parsed['database'] = $path[1];
  104. if (isset($path[2])) {
  105. $parsed['path'] = $path[2];
  106. } else {
  107. unset($parsed['path']);
  108. }
  109. }
  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. *
  118. * @return array
  119. */
  120. protected function filter(array $parameters)
  121. {
  122. return $parameters ?: array();
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function __get($parameter)
  128. {
  129. if (isset($this->parameters[$parameter])) {
  130. return $this->parameters[$parameter];
  131. }
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function __isset($parameter)
  137. {
  138. return isset($this->parameters[$parameter]);
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function toArray()
  144. {
  145. return $this->parameters;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function __sleep()
  151. {
  152. return array('parameters');
  153. }
  154. }