RequestMatcher.php 4.2 KB

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