RequestMatcher.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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|null $path
  48. * @param string|null $host
  49. * @param string|string[]|null $methods
  50. * @param string|string[]|null $ips
  51. * @param array $attributes
  52. * @param string|string[]|null $schemes
  53. */
  54. public function __construct(string $path = null, string $host = null, $methods = null, $ips = null, array $attributes = [], $schemes = null, int $port = null)
  55. {
  56. $this->matchPath($path);
  57. $this->matchHost($host);
  58. $this->matchMethod($methods);
  59. $this->matchIps($ips);
  60. $this->matchScheme($schemes);
  61. $this->matchPort($port);
  62. foreach ($attributes as $k => $v) {
  63. $this->matchAttribute($k, $v);
  64. }
  65. }
  66. /**
  67. * Adds a check for the HTTP scheme.
  68. *
  69. * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
  70. */
  71. public function matchScheme($scheme)
  72. {
  73. $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : [];
  74. }
  75. /**
  76. * Adds a check for the URL host name.
  77. *
  78. * @param string|null $regexp A Regexp
  79. */
  80. public function matchHost($regexp)
  81. {
  82. $this->host = $regexp;
  83. }
  84. /**
  85. * Adds a check for the the URL port.
  86. *
  87. * @param int|null $port The port number to connect to
  88. */
  89. public function matchPort(int $port = null)
  90. {
  91. $this->port = $port;
  92. }
  93. /**
  94. * Adds a check for the URL path info.
  95. *
  96. * @param string|null $regexp A Regexp
  97. */
  98. public function matchPath($regexp)
  99. {
  100. $this->path = $regexp;
  101. }
  102. /**
  103. * Adds a check for the client IP.
  104. *
  105. * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  106. */
  107. public function matchIp($ip)
  108. {
  109. $this->matchIps($ip);
  110. }
  111. /**
  112. * Adds a check for the client IP.
  113. *
  114. * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
  115. */
  116. public function matchIps($ips)
  117. {
  118. $this->ips = null !== $ips ? (array) $ips : [];
  119. }
  120. /**
  121. * Adds a check for the HTTP method.
  122. *
  123. * @param string|string[]|null $method An HTTP method or an array of HTTP methods
  124. */
  125. public function matchMethod($method)
  126. {
  127. $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : [];
  128. }
  129. /**
  130. * Adds a check for request attribute.
  131. *
  132. * @param string $key The request attribute name
  133. * @param string $regexp A Regexp
  134. */
  135. public function matchAttribute($key, $regexp)
  136. {
  137. $this->attributes[$key] = $regexp;
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function matches(Request $request)
  143. {
  144. if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) {
  145. return false;
  146. }
  147. if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) {
  148. return false;
  149. }
  150. foreach ($this->attributes as $key => $pattern) {
  151. if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
  152. return false;
  153. }
  154. }
  155. if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
  156. return false;
  157. }
  158. if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
  159. return false;
  160. }
  161. if (null !== $this->port && 0 < $this->port && $request->getPort() !== $this->port) {
  162. return false;
  163. }
  164. if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
  165. return true;
  166. }
  167. // Note to future implementors: add additional checks above the
  168. // foreach above or else your check might not be run!
  169. return 0 === \count($this->ips);
  170. }
  171. }