/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/RequestMatcher.php

https://bitbucket.org/laborautonomo/laborautonomo-site · PHP · 152 lines · 64 code · 20 blank · 68 comment · 9 complexity · 28f2b82a3204a79f656645efc8f5b76c MD5 · raw file

  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 $ip;
  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|null $ip
  45. * @param array $attributes
  46. */
  47. public function __construct($path = null, $host = null, $methods = null, $ip = null, array $attributes = array())
  48. {
  49. $this->matchPath($path);
  50. $this->matchHost($host);
  51. $this->matchMethod($methods);
  52. $this->matchIp($ip);
  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->ip = $ip;
  83. }
  84. /**
  85. * Adds a check for the HTTP method.
  86. *
  87. * @param string|string[]|null $method An HTTP method or an array of HTTP methods
  88. */
  89. public function matchMethod($method)
  90. {
  91. $this->methods = array_map('strtoupper', (array) $method);
  92. }
  93. /**
  94. * Adds a check for request attribute.
  95. *
  96. * @param string $key The request attribute name
  97. * @param string $regexp A Regexp
  98. */
  99. public function matchAttribute($key, $regexp)
  100. {
  101. $this->attributes[$key] = $regexp;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. *
  106. * @api
  107. */
  108. public function matches(Request $request)
  109. {
  110. if ($this->methods && !in_array($request->getMethod(), $this->methods)) {
  111. return false;
  112. }
  113. foreach ($this->attributes as $key => $pattern) {
  114. if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
  115. return false;
  116. }
  117. }
  118. if (null !== $this->path) {
  119. $path = str_replace('#', '\\#', $this->path);
  120. if (!preg_match('#'.$path.'#', rawurldecode($request->getPathInfo()))) {
  121. return false;
  122. }
  123. }
  124. if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#i', $request->getHost())) {
  125. return false;
  126. }
  127. if (null !== $this->ip && !IpUtils::checkIp($request->getClientIp(), $this->ip)) {
  128. return false;
  129. }
  130. return true;
  131. }
  132. }