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

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 <[email protected]>
  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\Block\BlockServiceInterface;
  13. use Sonata\BlockBundle\Event\BlockEvent;
  14. use Sonata\BlockBundle\Model\Block;
  15. use Sonata\SeoBundle\BreadcrumbInterface;
  16. /**
  17.  * BreadcrumbListener for Block Event.
  18.  *
  19.  * @author Sylvain Deloux <[email protected]>
  20.  */
  21. class BreadcrumbListener
  22. {
  23.     /**
  24.      * @var array
  25.      */
  26.     protected $blockServices = [];
  27.     /**
  28.      * Add a renderer to the status services list.
  29.      *
  30.      * @param string              $type
  31.      * @param BreadcrumbInterface $breadcrumb
  32.      *
  33.      * NEXT_MAJOR: Require BreadcrumbInterface instead of BlockServiceInterface
  34.      */
  35.     public function addBlockService($typeBlockServiceInterface $breadcrumb)
  36.     {
  37.         if (!$breadcrumb instanceof BreadcrumbInterface) {
  38.             @trigger_error(
  39.                 sprintf('Passing a %s class is deprecated since sonata-project/seo-bundle 2.12, pass a %s instead.'BlockServiceInterface::class, BreadcrumbInterface::class),
  40.                 \E_USER_DEPRECATED
  41.             );
  42.         }
  43.         $this->blockServices[$type] = $breadcrumb;
  44.     }
  45.     /**
  46.      * Add context related BlockService, if found.
  47.      */
  48.     public function onBlock(BlockEvent $event)
  49.     {
  50.         $context $event->getSetting('context'null);
  51.         if (null === $context) {
  52.             return;
  53.         }
  54.         foreach ($this->blockServices as $type => $blockService) {
  55.             if ($blockService->handleContext($context)) {
  56.                 $block = new Block();
  57.                 $block->setId(uniqid());
  58.                 $block->setSettings($event->getSettings());
  59.                 $block->setType($type);
  60.                 $event->addBlock($block);
  61.                 return;
  62.             }
  63.         }
  64.     }
  65. }