vendor/friendsofsymfony/http-cache-bundle/src/EventListener/Php8AttributesListener.php line 40

Open in your IDE?
  1. <?php
  2. namespace FOS\HttpCacheBundle\EventListener;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  6. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\Kernel;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. if (Kernel::MAJOR_VERSION >= 5) {
  11.     class_alias(RequestEvent::class, 'FOS\HttpCacheBundle\EventListener\AttributeRequestEvent');
  12. } else {
  13.     class_alias(GetResponseEvent::class, 'FOS\HttpCacheBundle\EventListener\AttributeRequestEvent');
  14. }
  15. /**
  16.  * On kernel.request event, this event handler fetch PHP8 attributes.
  17.  * It is available from PHP 8.0.0.
  18.  *
  19.  * @author Yoann Chocteau <yoann@kezaweb.fr>
  20.  */
  21. class Php8AttributesListener implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var ControllerResolverInterface
  25.      */
  26.     private $controllerResolver;
  27.     public function __construct(ControllerResolverInterface $controllerResolver)
  28.     {
  29.         if (\PHP_VERSION_ID 80000) {
  30.             throw new \Exception(sprintf('Php8AttributesListener must not be loaded for PHP %s'phpversion()));
  31.         }
  32.         $this->controllerResolver $controllerResolver;
  33.     }
  34.     public function onKernelRequest(AttributeRequestEvent $event)
  35.     {
  36.         $request $event->getRequest();
  37.         $controller $this->controllerResolver->getController($request);
  38.         if (!is_array($controller) || !== count($controller)) {
  39.             return;
  40.         }
  41.         $class = new \ReflectionClass($controller[0]);
  42.         $method $class->getMethod($controller[1]);
  43.         $attributes = [];
  44.         $addAttributes = function ($instance) use (&$attributes) {
  45.             if (
  46.                 $instance instanceof ConfigurationInterface &&
  47.                 in_array(
  48.                     $instance->getAliasName(), [
  49.                     'tag''invalidate_path''invalidate_route',
  50.                 ])
  51.             ) {
  52.                 $attributes['_'.$instance->getAliasName()][] = $instance;
  53.             }
  54.         };
  55.         foreach ($class->getAttributes() as $classAttribute) {
  56.             $addAttributes($classAttribute->newInstance());
  57.         }
  58.         foreach ($method->getAttributes() as $methodAttribute) {
  59.             $addAttributes($methodAttribute->newInstance());
  60.         }
  61.         foreach ($attributes as $key => $attr) {
  62.             $request->attributes->set(
  63.                 $key,
  64.                 array_merge($attr$request->attributes->get($key, []))
  65.             );
  66.         }
  67.     }
  68.     public static function getSubscribedEvents()
  69.     {
  70.         return [
  71.             KernelEvents::REQUEST => 'onKernelRequest',
  72.         ];
  73.     }
  74. }