RequestMatcher.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. * @param string|null $regexp A Regexp
  76. */
  77. public function matchHost($regexp)
  78. {
  79. $this->host = $regexp;
  80. }
  81. /**
  82. * Adds a check for the the URL port.
  83. *
  84. * @param int|null $port The port number to connect to
  85. */
  86. public function matchPort(?int $port)
  87. {
  88. $this->port = $port;
  89. }
  90. /**
  91. * Adds a check for the URL path info.
  92. *
  93. * @param string|null $regexp A Regexp
  94. */
  95. public function matchPath($regexp)
  96. {
  97. $this->path = $regexp;
  98. }
  99. /**
  100. * Adds a check for the client IP.
  101. *
  102. * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  103. */
  104. public function matchIp($ip)
  105. {
  106. $this->matchIps($ip);
  107. }
  108. /**
  109. * Adds a check for the client IP.
  110. *
  111. * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  112. */
  113. public function matchIps($ips)
  114. {
  115. $this->ips = null !== $ips ? (array) $ips : [];
  116. }
  117. /**
  118. * Adds a check for the HTTP method.
  119. *
  120. * @param string|string[]|null $method An HTTP method or an array of HTTP methods
  121. */
  122. public function matchMethod($method)
  123. {
  124. $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : [];
  125. }
  126. /**
  127. * Adds a check for request attribute.
  128. *
  129. * @param string $key The request attribute name
  130. * @param string $regexp A Regexp
  131. */
  132. public function matchAttribute($key, $regexp)
  133. {
  134. $this->attributes[$key] = $regexp;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function matches(Request $request)
  140. {
  141. if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) {
  142. return false;
  143. }
  144. if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) {
  145. return false;
  146. }
  147. foreach ($this->attributes as $key => $pattern) {
  148. if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
  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. }