控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequestMatcher.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\HttpFoundation;
  11. /**
  12. * RequestMatcher compares a pre-defined set of checks against a Request instance.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class RequestMatcher implements RequestMatcherInterface
  17. {
  18. /**
  19. * @var string|null
  20. */
  21. private $path;
  22. /**
  23. * @var string|null
  24. */
  25. private $host;
  26. /**
  27. * @var int|null
  28. */
  29. private $port;
  30. /**
  31. * @var string[]
  32. */
  33. private $methods = [];
  34. /**
  35. * @var string[]
  36. */
  37. private $ips = [];
  38. /**
  39. * @var array
  40. */
  41. private $attributes = [];
  42. /**
  43. * @var string[]
  44. */
  45. private $schemes = [];
  46. /**
  47. * @param string|string[]|null $methods
  48. * @param string|string[]|null $ips
  49. * @param string|string[]|null $schemes
  50. */
  51. public function __construct(?string $path = null, ?string $host = null, $methods = null, $ips = null, array $attributes = [], $schemes = null, ?int $port = null)
  52. {
  53. $this->matchPath($path);
  54. $this->matchHost($host);
  55. $this->matchMethod($methods);
  56. $this->matchIps($ips);
  57. $this->matchScheme($schemes);
  58. $this->matchPort($port);
  59. foreach ($attributes as $k => $v) {
  60. $this->matchAttribute($k, $v);
  61. }
  62. }
  63. /**
  64. * Adds a check for the HTTP scheme.
  65. *
  66. * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
  67. */
  68. public function matchScheme($scheme)
  69. {
  70. $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : [];
  71. }
  72. /**
  73. * Adds a check for the URL host name.
  74. */
  75. public function matchHost(?string $regexp)
  76. {
  77. $this->host = $regexp;
  78. }
  79. /**
  80. * Adds a check for the URL port.
  81. *
  82. * @param int|null $port The port number to connect to
  83. */
  84. public function matchPort(?int $port)
  85. {
  86. $this->port = $port;
  87. }
  88. /**
  89. * Adds a check for the URL path info.
  90. */
  91. public function matchPath(?string $regexp)
  92. {
  93. $this->path = $regexp;
  94. }
  95. /**
  96. * Adds a check for the client IP.
  97. *
  98. * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  99. */
  100. public function matchIp(string $ip)
  101. {
  102. $this->matchIps($ip);
  103. }
  104. /**
  105. * Adds a check for the client IP.
  106. *
  107. * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  108. */
  109. public function matchIps($ips)
  110. {
  111. $ips = null !== $ips ? (array) $ips : [];
  112. $this->ips = array_reduce($ips, static function (array $ips, string $ip) {
  113. return array_merge($ips, preg_split('/\s*,\s*/', $ip));
  114. }, []);
  115. }
  116. /**
  117. * Adds a check for the HTTP method.
  118. *
  119. * @param string|string[]|null $method An HTTP method or an array of HTTP methods
  120. */
  121. public function matchMethod($method)
  122. {
  123. $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : [];
  124. }
  125. /**
  126. * Adds a check for request attribute.
  127. */
  128. public function matchAttribute(string $key, string $regexp)
  129. {
  130. $this->attributes[$key] = $regexp;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function matches(Request $request)
  136. {
  137. if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) {
  138. return false;
  139. }
  140. if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) {
  141. return false;
  142. }
  143. foreach ($this->attributes as $key => $pattern) {
  144. $requestAttribute = $request->attributes->get($key);
  145. if (!\is_string($requestAttribute)) {
  146. return false;
  147. }
  148. if (!preg_match('{'.$pattern.'}', $requestAttribute)) {
  149. return false;
  150. }
  151. }
  152. if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
  153. return false;
  154. }
  155. if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
  156. return false;
  157. }
  158. if (null !== $this->port && 0 < $this->port && $request->getPort() !== $this->port) {
  159. return false;
  160. }
  161. if (IpUtils::checkIp($request->getClientIp() ?? '', $this->ips)) {
  162. return true;
  163. }
  164. // Note to future implementors: add additional checks above the
  165. // foreach above or else your check might not be run!
  166. return 0 === \count($this->ips);
  167. }
  168. }