vendor/flagception/flagception-bundle/src/Listener/AnnotationSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. namespace Flagception\Bundle\FlagceptionBundle\Listener;
  3. use Flagception\Bundle\FlagceptionBundle\Annotations\Feature;
  4. use Flagception\Manager\FeatureManagerInterface;
  5. use Doctrine\Common\Annotations\Reader;
  6. use ReflectionClass;
  7. use ReflectionException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. /**
  13.  * Class AnnotationSubscriber
  14.  *
  15.  * @author Michel Chowanski <michel.chowanski@bestit-online.de>
  16.  * @package Flagception\Bundle\FlagceptionBundle\Listener
  17.  */
  18. class AnnotationSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * Annotation reader
  22.      *
  23.      * @var Reader
  24.      */
  25.     private $reader;
  26.     /**
  27.      * The feature manager
  28.      *
  29.      * @var FeatureManagerInterface
  30.      */
  31.     private $manager;
  32.     /**
  33.      * FeatureListener constructor.
  34.      *
  35.      * @param Reader $reader
  36.      * @param FeatureManagerInterface $manager
  37.      */
  38.     public function __construct(Reader $readerFeatureManagerInterface $manager)
  39.     {
  40.         $this->reader $reader;
  41.         $this->manager $manager;
  42.     }
  43.     /**
  44.      * Filter on controller / method
  45.      *
  46.      * @param ControllerEvent $event
  47.      *
  48.      * @return void
  49.      *
  50.      * @throws NotFoundHttpException
  51.      * @throws ReflectionException
  52.      */
  53.     public function onKernelController(ControllerEvent $event)
  54.     {
  55.         $eventController $event->getController();
  56.         $controller =  is_array($eventController) === false && method_exists($eventController'__invoke')
  57.             ? [$eventController'__invoke']
  58.             : $eventController;
  59.         /*
  60.          * $controller passed can be either a class or a Closure.
  61.          * This is not usual in Symfony2 but it may happen.
  62.          * If it is a class, it comes in array format
  63.          */
  64.         if (!is_array($controller)) {
  65.             return;
  66.         }
  67.         $object = new ReflectionClass($controller[0]);
  68.         foreach ($this->reader->getClassAnnotations($object) as $annotation) {
  69.             if ($annotation instanceof Feature) {
  70.                 if (!$this->manager->isActive($annotation->name)) {
  71.                     throw new NotFoundHttpException('Feature for this class is not active.');
  72.                 }
  73.             }
  74.         }
  75.         $method $object->getMethod($controller[1]);
  76.         foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
  77.             if ($annotation instanceof Feature) {
  78.                 if (!$this->manager->isActive($annotation->name)) {
  79.                     throw new NotFoundHttpException('Feature for this method is not active.');
  80.                 }
  81.             }
  82.         }
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public static function getSubscribedEvents(): array
  88.     {
  89.         return [
  90.             KernelEvents::CONTROLLER => 'onKernelController',
  91.         ];
  92.     }
  93. }