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

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\ControllerEvent;
  7. class BreadcrumbListener
  8. {
  9.     /**
  10.      * @var Reader
  11.      */
  12.     private $annotationReader;
  13.     /**
  14.      * @var BreadcrumbBuilder
  15.      */
  16.     private $breadcrumbBuilder;
  17.     public function __construct(BreadcrumbBuilder $breadcrumbBuilderReader $annotationReader)
  18.     {
  19.         $this->breadcrumbBuilder $breadcrumbBuilder;
  20.         $this->annotationReader $annotationReader;
  21.     }
  22.     public function onKernelController(ControllerEvent $event)
  23.     {
  24.         // In case controller is not an array (e.g. a closure or an invokable class), we can't do anything.
  25.         if (!is_array($event->getController())) {
  26.             return;
  27.         }
  28.         list($controller$action) = $event->getController();
  29.         $class = new \ReflectionClass($controller);
  30.         $method = new \ReflectionMethod($controller$action);
  31.         $annotations = [];
  32.         if (($classAnnotation $this->annotationReader->getClassAnnotation($classBreadcrumb::class))) {
  33.             $annotations[] = $classAnnotation;
  34.         }
  35.         if ($methodAnnotation $this->annotationReader->getMethodAnnotation($methodBreadcrumb::class)) {
  36.             $annotations[] = $methodAnnotation;
  37.         }
  38.         foreach ($annotations as $annotation) {
  39.             foreach ($annotation->items as $item) {
  40.                 $this->breadcrumbBuilder->addItem(
  41.                     $item['label'],
  42.                     isset($item['route']) ? $item['route'] : null,
  43.                     isset($item['params']) ? $item['params'] : null,
  44.                     isset($item['translationDomain']) ? $item['translationDomain'] : null
  45.                 );
  46.             }
  47.         }
  48.     }
  49. }