vendor/sonata-project/seo-bundle/src/Event/BreadcrumbListener.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\SeoBundle\Event;
  12. use Sonata\BlockBundle\Event\BlockEvent;
  13. use Sonata\BlockBundle\Model\Block;
  14. use Sonata\SeoBundle\Block\Breadcrumb\BreadcrumbBlockService;
  15. /**
  16.  * BreadcrumbListener for Block Event.
  17.  *
  18.  * @author Sylvain Deloux <sylvain.deloux@ekino.com>
  19.  */
  20. final class BreadcrumbListener
  21. {
  22.     /**
  23.      * @var array<string, BreadcrumbBlockService>
  24.      */
  25.     private array $blockServices = [];
  26.     /**
  27.      * Add a renderer to the status services list.
  28.      */
  29.     public function addBlockService(string $typeBreadcrumbBlockService $breadcrumb): void
  30.     {
  31.         $this->blockServices[$type] = $breadcrumb;
  32.     }
  33.     /**
  34.      * Add context related BlockService, if found.
  35.      */
  36.     public function onBlock(BlockEvent $event): void
  37.     {
  38.         $context $event->getSetting('context'null);
  39.         if (null === $context) {
  40.             return;
  41.         }
  42.         foreach ($this->blockServices as $type => $blockService) {
  43.             if ($blockService->handleContext($context)) {
  44.                 $block = new Block();
  45.                 $block->setId(uniqid());
  46.                 $block->setSettings($event->getSettings());
  47.                 $block->setType($type);
  48.                 $event->addBlock($block);
  49.                 return;
  50.             }
  51.         }
  52.     }
  53. }