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

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\Framework\DataAbstractionLayer\Util\AfterSort;
  12. use Shopware\Core\System\Annotation\Concept\ExtensionPattern\Decoratable;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  16. /**
  17.  * @Decoratable()
  18.  */
  19. class NavigationLoader implements NavigationLoaderInterface
  20. {
  21.     /**
  22.      * @var TreeItem
  23.      */
  24.     private $treeItem;
  25.     /**
  26.      * @var EventDispatcherInterface
  27.      */
  28.     private $eventDispatcher;
  29.     /**
  30.      * @var AbstractNavigationRoute
  31.      */
  32.     private $navigationRoute;
  33.     /**
  34.      * @internal
  35.      */
  36.     public function __construct(
  37.         EventDispatcherInterface $eventDispatcher,
  38.         AbstractNavigationRoute $navigationRoute
  39.     ) {
  40.         $this->treeItem = new TreeItem(null, []);
  41.         $this->eventDispatcher $eventDispatcher;
  42.         $this->navigationRoute $navigationRoute;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      *
  47.      * @throws CategoryNotFoundException
  48.      */
  49.     public function load(string $activeIdSalesChannelContext $contextstring $rootIdint $depth 2): Tree
  50.     {
  51.         $request = new Request();
  52.         $request->query->set('buildTree''false');
  53.         $request->query->set('depth', (string) $depth);
  54.         $criteria = new Criteria();
  55.         $criteria->setTitle('header::navigation');
  56.         $categories $this->navigationRoute
  57.             ->load($activeId$rootId$request$context$criteria)
  58.             ->getCategories();
  59.         $navigation $this->getTree($rootId$categories$categories->get($activeId));
  60.         $event = new NavigationLoadedEvent($navigation$context);
  61.         $this->eventDispatcher->dispatch($event);
  62.         return $event->getNavigation();
  63.     }
  64.     private function getTree(?string $rootIdCategoryCollection $categories, ?CategoryEntity $active): Tree
  65.     {
  66.         $parents = [];
  67.         $items = [];
  68.         foreach ($categories as $category) {
  69.             $item = clone $this->treeItem;
  70.             $item->setCategory($category);
  71.             $parents[$category->getParentId()][$category->getId()] = $item;
  72.             $items[$category->getId()] = $item;
  73.         }
  74.         foreach ($parents as $parentId => $children) {
  75.             if (empty($parentId)) {
  76.                 continue;
  77.             }
  78.             $sorted AfterSort::sort($children);
  79.             $filtered \array_filter($sorted, static function (TreeItem $filter) {
  80.                 return $filter->getCategory()->getActive() && $filter->getCategory()->getVisible();
  81.             });
  82.             if (!isset($items[$parentId])) {
  83.                 continue;
  84.             }
  85.             $item $items[$parentId];
  86.             $item->setChildren($filtered);
  87.         }
  88.         $root $parents[$rootId] ?? [];
  89.         $root AfterSort::sort($root);
  90.         $filtered = [];
  91.         /** @var TreeItem $item */
  92.         foreach ($root as $key => $item) {
  93.             if (!$item->getCategory()->getActive() || !$item->getCategory()->getVisible()) {
  94.                 continue;
  95.             }
  96.             $filtered[$key] = $item;
  97.         }
  98.         return new Tree($active$filtered);
  99.     }
  100. }