vendor/shopware/core/Content/Category/Service/NavigationLoader.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\Service;
  3. use Shopware\Core\Content\Category\CategoryCollection;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\Category\Event\NavigationLoadedEvent;
  6. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  7. use Shopware\Core\Content\Category\SalesChannel\AbstractNavigationRoute;
  8. use Shopware\Core\Content\Category\Tree\Tree;
  9. use Shopware\Core\Content\Category\Tree\TreeItem;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\System\Annotation\Concept\ExtensionPattern\Decoratable;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  15. /**
  16.  * @Decoratable()
  17.  */
  18. class NavigationLoader implements NavigationLoaderInterface
  19. {
  20.     /**
  21.      * @var TreeItem
  22.      */
  23.     private $treeItem;
  24.     /**
  25.      * @var EventDispatcherInterface
  26.      */
  27.     private $eventDispatcher;
  28.     /**
  29.      * @var AbstractNavigationRoute
  30.      */
  31.     private $navigationRoute;
  32.     public function __construct(
  33.         EventDispatcherInterface $eventDispatcher,
  34.         AbstractNavigationRoute $navigationRoute
  35.     ) {
  36.         $this->treeItem = new TreeItem(null, []);
  37.         $this->eventDispatcher $eventDispatcher;
  38.         $this->navigationRoute $navigationRoute;
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      *
  43.      * @throws CategoryNotFoundException
  44.      */
  45.     public function load(string $activeIdSalesChannelContext $contextstring $rootIdint $depth 2): Tree
  46.     {
  47.         $request = new Request();
  48.         $request->query->set('buildTree''false');
  49.         $request->query->set('depth', (string) $depth);
  50.         $criteria = new Criteria();
  51.         $criteria->setTitle('header::navigation');
  52.         $categories $this->navigationRoute
  53.             ->load($activeId$rootId$request$context$criteria)
  54.             ->getCategories();
  55.         $navigation $this->getTree($rootId$categories$categories->get($activeId));
  56.         $event = new NavigationLoadedEvent($navigation$context);
  57.         $this->eventDispatcher->dispatch($event);
  58.         return $event->getNavigation();
  59.     }
  60.     private function getTree(?string $parentIdCategoryCollection $categories, ?CategoryEntity $active): Tree
  61.     {
  62.         $tree $this->buildTree($parentId$categories->getElements());
  63.         return new Tree($active$tree);
  64.     }
  65.     /**
  66.      * @param CategoryEntity[] $categories
  67.      *
  68.      * @return TreeItem[]
  69.      */
  70.     private function buildTree(?string $parentId, array $categories): array
  71.     {
  72.         $children = new CategoryCollection();
  73.         foreach ($categories as $key => $category) {
  74.             if ($category->getParentId() !== $parentId) {
  75.                 continue;
  76.             }
  77.             unset($categories[$key]);
  78.             $children->add($category);
  79.         }
  80.         $children->sortByPosition();
  81.         $items = [];
  82.         foreach ($children as $child) {
  83.             if (!$child->getActive() || !$child->getVisible()) {
  84.                 continue;
  85.             }
  86.             $item = clone $this->treeItem;
  87.             $item->setCategory($child);
  88.             $item->setChildren(
  89.                 $this->buildTree($child->getId(), $categories)
  90.             );
  91.             $items[$child->getId()] = $item;
  92.         }
  93.         return $items;
  94.     }
  95. }