vendor/friendsofsymfony/rest-bundle/EventListener/VersionListener.php line 32

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 FOS\RestBundle\Version\VersionResolverInterface;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. /**
  15. * @internal
  16. */
  17. class VersionListener
  18. {
  19. private $versionResolver;
  20. private $defaultVersion;
  21. public function __construct(VersionResolverInterface $versionResolver, ?string $defaultVersion = null)
  22. {
  23. $this->versionResolver = $versionResolver;
  24. $this->defaultVersion = $defaultVersion;
  25. }
  26. public function onKernelRequest(RequestEvent $event): void
  27. {
  28. $request = $event->getRequest();
  29. if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
  30. return;
  31. }
  32. $version = $this->versionResolver->resolve($request);
  33. if (null === $version && null !== $this->defaultVersion) {
  34. $version = $this->defaultVersion;
  35. }
  36. // Return if nothing to do
  37. if (null === $version) {
  38. return;
  39. }
  40. $request->attributes->set('version', $version);
  41. }
  42. }