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

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