vendor/shopware/core/Content/Seo/SalesChannel/StoreApiSeoResolver.php line 71

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Seo\SalesChannel;
  3. use Shopware\Core\Content\Category\CategoryEntity;
  4. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  5. use Shopware\Core\Content\Seo\SeoUrl\SeoUrlCollection;
  6. use Shopware\Core\Content\Seo\SeoUrl\SeoUrlEntity;
  7. use Shopware\Core\Content\Seo\SeoUrlRoute\SeoUrlRouteInterface as SeoUrlRouteConfigRoute;
  8. use Shopware\Core\Content\Seo\SeoUrlRoute\SeoUrlRouteRegistry;
  9. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  17. use Shopware\Core\Framework\Struct\Collection;
  18. use Shopware\Core\Framework\Struct\Struct;
  19. use Shopware\Core\PlatformRequest;
  20. use Shopware\Core\System\SalesChannel\Entity\SalesChannelDefinitionInstanceRegistry;
  21. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  22. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  23. use Shopware\Core\System\SalesChannel\StoreApiResponse;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  26. use Symfony\Component\HttpKernel\KernelEvents;
  27. class StoreApiSeoResolver implements EventSubscriberInterface
  28. {
  29.     /**
  30.      * @var SalesChannelRepositoryInterface
  31.      */
  32.     private $salesChannelRepository;
  33.     /**
  34.      * @var DefinitionInstanceRegistry
  35.      */
  36.     private $definitionInstanceRegistry;
  37.     /**
  38.      * @var SeoUrlRouteRegistry
  39.      */
  40.     private $seoUrlRouteRegistry;
  41.     /**
  42.      * @var SalesChannelDefinitionInstanceRegistry
  43.      */
  44.     private $salesChannelDefinitionInstanceRegistry;
  45.     public function __construct(
  46.         SalesChannelRepositoryInterface $salesChannelRepository,
  47.         DefinitionInstanceRegistry $definitionInstanceRegistry,
  48.         SalesChannelDefinitionInstanceRegistry $salesChannelDefinitionInstanceRegistry,
  49.         SeoUrlRouteRegistry $seoUrlRouteRegistry
  50.     ) {
  51.         $this->salesChannelRepository $salesChannelRepository;
  52.         $this->definitionInstanceRegistry $definitionInstanceRegistry;
  53.         $this->seoUrlRouteRegistry $seoUrlRouteRegistry;
  54.         $this->salesChannelDefinitionInstanceRegistry $salesChannelDefinitionInstanceRegistry;
  55.     }
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             KernelEvents::RESPONSE => ['addSeoInformation'10000],
  60.         ];
  61.     }
  62.     public function addSeoInformation(ResponseEvent $event): void
  63.     {
  64.         $response $event->getResponse();
  65.         if (!$response instanceof StoreApiResponse) {
  66.             return;
  67.         }
  68.         if (!$event->getRequest()->headers->has(PlatformRequest::HEADER_INCLUDE_SEO_URLS)) {
  69.             return;
  70.         }
  71.         $dataBag = new SeoResolverData();
  72.         $this->find($dataBag$response->getObject());
  73.         $this->enrich($dataBag$event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT));
  74.     }
  75.     private function find(SeoResolverData $dataStruct $struct): void
  76.     {
  77.         if ($struct instanceof AggregationResultCollection) {
  78.             foreach ($struct as $item) {
  79.                 $this->findStruct($data$item);
  80.             }
  81.         }
  82.         if ($struct instanceof EntitySearchResult) {
  83.             foreach ($struct->getEntities() as $entity) {
  84.                 $this->findStruct($data$entity);
  85.             }
  86.         }
  87.         if ($struct instanceof Collection) {
  88.             foreach ($struct as $item) {
  89.                 $this->findStruct($data$item);
  90.             }
  91.         }
  92.         $this->findStruct($data$struct);
  93.     }
  94.     private function findStruct(SeoResolverData $dataStruct $struct): void
  95.     {
  96.         if ($struct instanceof Entity) {
  97.             $definition $this->definitionInstanceRegistry->getByEntityClass($struct) ?? $this->salesChannelDefinitionInstanceRegistry->getByEntityClass($struct);
  98.             if ($definition && $definition->isSeoAware()) {
  99.                 $data->add($definition->getEntityName(), $struct);
  100.             }
  101.         }
  102.         foreach ($struct->getVars() as $item) {
  103.             if ($item instanceof Collection) {
  104.                 foreach ($item as $collectionItem) {
  105.                     if ($collectionItem instanceof Struct) {
  106.                         $this->findStruct($data$collectionItem);
  107.                     }
  108.                 }
  109.             } elseif ($item instanceof Struct) {
  110.                 $this->findStruct($data$item);
  111.             }
  112.         }
  113.     }
  114.     private function enrich(SeoResolverData $dataSalesChannelContext $context): void
  115.     {
  116.         foreach ($data->getEntities() as $definition) {
  117.             $ids $data->getIds($definition);
  118.             $routes $this->seoUrlRouteRegistry->findByDefinition($definition);
  119.             if (\count($routes) === 0) {
  120.                 continue;
  121.             }
  122.             $routes array_map(static function (SeoUrlRouteConfigRoute $seoUrlRoute) {
  123.                 return $seoUrlRoute->getConfig()->getRouteName();
  124.             }, $routes);
  125.             $criteria = new Criteria();
  126.             $criteria->addFilter(new EqualsFilter('isCanonical'true));
  127.             $criteria->addFilter(new EqualsAnyFilter('routeName'$routes));
  128.             $criteria->addFilter(new EqualsAnyFilter('foreignKey'$ids));
  129.             $criteria->addFilter(new EqualsFilter('languageId'$context->getContext()->getLanguageId()));
  130.             $criteria->addSorting(new FieldSorting('salesChannelId'));
  131.             /** @var SeoUrlEntity $url */
  132.             foreach ($this->salesChannelRepository->search($criteria$context) as $url) {
  133.                 /** @var SalesChannelProductEntity|CategoryEntity $entity */
  134.                 $entity $data->get($definition$url->getForeignKey());
  135.                 if ($entity->getSeoUrls() === null) {
  136.                     $entity->setSeoUrls(new SeoUrlCollection());
  137.                 }
  138.                 $entity->getSeoUrls()->add($url);
  139.             }
  140.         }
  141.     }
  142. }