vendor/friendsofsymfony/rest-bundle/EventListener/ZoneMatcherListener.php line 37

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the FOSRestBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\RestBundle\EventListener;
  11. use FOS\RestBundle\FOSRestBundle;
  12. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. /**
  15. * Matches FOSRest's zones.
  16. *
  17. * @author Florian Voutzinos <florian@voutzinos.com>
  18. *
  19. * @internal
  20. */
  21. class ZoneMatcherListener
  22. {
  23. private $requestMatchers = [];
  24. public function addRequestMatcher(RequestMatcherInterface $requestMatcher)
  25. {
  26. $this->requestMatchers[] = $requestMatcher;
  27. }
  28. /**
  29. * Adds an optional "_fos_rest_zone" request attribute to be checked for existence by other listeners.
  30. */
  31. public function onKernelRequest(RequestEvent $event): void
  32. {
  33. $request = $event->getRequest();
  34. foreach ($this->requestMatchers as $requestMatcher) {
  35. if ($requestMatcher->matches($request)) {
  36. $request->attributes->set(FOSRestBundle::ZONE_ATTRIBUTE, true);
  37. return;
  38. }
  39. }
  40. $request->attributes->set(FOSRestBundle::ZONE_ATTRIBUTE, false);
  41. }
  42. }