vendor/asprega/breadcrumb-bundle/EventListener/BreadcrumbListener.php line 35

Open in your IDE?
  1. <?php
  2. namespace AndreaSprega\Bundle\BreadcrumbBundle\EventListener;
  3. use AndreaSprega\Bundle\BreadcrumbBundle\Annotation\Breadcrumb;
  4. use AndreaSprega\Bundle\BreadcrumbBundle\Service\BreadcrumbBuilder;
  5. use Doctrine\Common\Annotations\Reader;
  6. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  7. class BreadcrumbListener
  8. {
  9.     /**
  10.      * @var Reader
  11.      */
  12.     private $annotationReader;
  13.     /**
  14.      * @var BreadcrumbBuilder
  15.      */
  16.     private $breadcrumbBuilder;
  17.     /**
  18.      * @param BreadcrumbBuilder $breadcrumbBuilder
  19.      * @param Reader $annotationReader
  20.      */
  21.     public function __construct(BreadcrumbBuilder $breadcrumbBuilderReader $annotationReader)
  22.     {
  23.         $this->breadcrumbBuilder $breadcrumbBuilder;
  24.         $this->annotationReader $annotationReader;
  25.     }
  26.     /**
  27.      * @param FilterControllerEvent $event
  28.      */
  29.     public function onKernelController(FilterControllerEvent $event)
  30.     {
  31.         // In case controller is not an array (e.g. a closure or an invokable class), we can't do anything.
  32.         if (!is_array($event->getController())) {
  33.             return;
  34.         }
  35.         list($controller$action) = $event->getController();
  36.         $class = new \ReflectionClass($controller);
  37.         $method = new \ReflectionMethod($controller$action);
  38.         $annotations = [];
  39.         if (($classAnnotation $this->annotationReader->getClassAnnotation($classBreadcrumb::class))) {
  40.             $annotations[] = $classAnnotation;
  41.         }
  42.         if ($methodAnnotation $this->annotationReader->getMethodAnnotation($methodBreadcrumb::class)) {
  43.             $annotations[] = $methodAnnotation;
  44.         }
  45.         foreach ($annotations as $annotation) {
  46.             foreach ($annotation->items as $item) {
  47.                 $this->breadcrumbBuilder->addItem(
  48.                     $item['label'],
  49.                     isset($item['route']) ? $item['route'] : null,
  50.                     isset($item['params']) ? $item['params'] : null
  51.                 );
  52.             }
  53.         }
  54.     }
  55. }