No Description

RequestMatcher.php 4.3KB

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