RequestMatcher.php 3.7 KB

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