vendor/sonata-project/seo-bundle/src/Block/Breadcrumb/BaseBreadcrumbMenuBlockService.php line 45

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\Block\Breadcrumb;
  12. use Knp\Menu\FactoryInterface;
  13. use Knp\Menu\ItemInterface;
  14. use Knp\Menu\Provider\MenuProviderInterface;
  15. use Sonata\BlockBundle\Block\BlockContextInterface;
  16. use Sonata\BlockBundle\Block\Service\MenuBlockService;
  17. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. /**
  20.  * Abstract class for breadcrumb menu services.
  21.  *
  22.  * @author Sylvain Deloux <sylvain.deloux@ekino.com>
  23.  */
  24. abstract class BaseBreadcrumbMenuBlockService extends MenuBlockService
  25. {
  26.     /**
  27.      * @var string
  28.      */
  29.     private $context;
  30.     /**
  31.      * @var FactoryInterface
  32.      */
  33.     private $factory;
  34.     /**
  35.      * @param string $context
  36.      * @param string $name
  37.      */
  38.     public function __construct($context$nameEngineInterface $templatingMenuProviderInterface $menuProviderFactoryInterface $factory)
  39.     {
  40.         parent::__construct($name$templating$menuProvider);
  41.         $this->context $context;
  42.         $this->factory $factory;
  43.     }
  44.     /**
  45.      * Return true if current BlockService handles the given context.
  46.      *
  47.      * @param string $context
  48.      *
  49.      * @return bool
  50.      */
  51.     public function handleContext($context)
  52.     {
  53.         return $this->context === $context;
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function getName()
  59.     {
  60.         return sprintf('Breadcrumb %s'$this->context);
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function configureSettings(OptionsResolver $resolver)
  66.     {
  67.         parent::configureSettings($resolver);
  68.         $resolver->setDefaults([
  69.             'menu_template' => '@SonataSeo/Block/breadcrumb.html.twig',
  70.             'include_homepage_link' => true,
  71.             'context' => false,
  72.         ]);
  73.     }
  74.     /**
  75.      * @return FactoryInterface
  76.      */
  77.     protected function getFactory()
  78.     {
  79.         return $this->factory;
  80.     }
  81.     /**
  82.      * @return string
  83.      */
  84.     protected function getContext()
  85.     {
  86.         return $this->context;
  87.     }
  88.     /**
  89.      * Initialize breadcrumb menu.
  90.      *
  91.      * @return ItemInterface
  92.      */
  93.     protected function getRootMenu(BlockContextInterface $blockContext)
  94.     {
  95.         $settings $blockContext->getSettings();
  96.         /*
  97.          * @todo : Use the router to get the homepage URI
  98.          */
  99.         $menu $this->factory->createItem('breadcrumb');
  100.         $menu->setChildrenAttribute('class''breadcrumb');
  101.         if (method_exists($menu'setCurrentUri')) {
  102.             $menu->setCurrentUri($settings['current_uri']);
  103.         }
  104.         if (method_exists($menu'setCurrent')) {
  105.             $menu->setCurrent($settings['current_uri']);
  106.         }
  107.         if ($settings['include_homepage_link']) {
  108.             $menu->addChild('sonata_seo_homepage_breadcrumb', ['uri' => '/']);
  109.         }
  110.         return $menu;
  111.     }
  112. }