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

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. /**
  28.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  29.  */
  30. class StoreApiSeoResolver implements EventSubscriberInterface
  31. {
  32.     private SalesChannelRepositoryInterface $salesChannelRepository;
  33.     private DefinitionInstanceRegistry $definitionInstanceRegistry;
  34.     private SeoUrlRouteRegistry $seoUrlRouteRegistry;
  35.     private SalesChannelDefinitionInstanceRegistry $salesChannelDefinitionInstanceRegistry;
  36.     /**
  37.      * @internal
  38.      */
  39.     public function __construct(
  40.         SalesChannelRepositoryInterface $salesChannelRepository,
  41.         DefinitionInstanceRegistry $definitionInstanceRegistry,
  42.         SalesChannelDefinitionInstanceRegistry $salesChannelDefinitionInstanceRegistry,
  43.         SeoUrlRouteRegistry $seoUrlRouteRegistry
  44.     ) {
  45.         $this->salesChannelRepository $salesChannelRepository;
  46.         $this->definitionInstanceRegistry $definitionInstanceRegistry;
  47.         $this->seoUrlRouteRegistry $seoUrlRouteRegistry;
  48.         $this->salesChannelDefinitionInstanceRegistry $salesChannelDefinitionInstanceRegistry;
  49.     }
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             KernelEvents::RESPONSE => ['addSeoInformation'10000],
  54.         ];
  55.     }
  56.     public function addSeoInformation(ResponseEvent $event): void
  57.     {
  58.         $response $event->getResponse();
  59.         if (!$response instanceof StoreApiResponse) {
  60.             return;
  61.         }
  62.         if (!$event->getRequest()->headers->has(PlatformRequest::HEADER_INCLUDE_SEO_URLS)) {
  63.             return;
  64.         }
  65.         $dataBag = new SeoResolverData();
  66.         $this->find($dataBag$response->getObject());
  67.         $this->enrich($dataBag$event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT));
  68.     }
  69.     private function find(SeoResolverData $dataStruct $struct): void
  70.     {
  71.         if ($struct instanceof AggregationResultCollection) {
  72.             foreach ($struct as $item) {
  73.                 $this->findStruct($data$item);
  74.             }
  75.         }
  76.         if ($struct instanceof EntitySearchResult) {
  77.             foreach ($struct->getEntities() as $entity) {
  78.                 $this->findStruct($data$entity);
  79.             }
  80.         }
  81.         if ($struct instanceof Collection) {
  82.             foreach ($struct as $item) {
  83.                 $this->findStruct($data$item);
  84.             }
  85.         }
  86.         $this->findStruct($data$struct);
  87.     }
  88.     private function findStruct(SeoResolverData $dataStruct $struct): void
  89.     {
  90.         if ($struct instanceof Entity) {
  91.             $definition $this->definitionInstanceRegistry->getByEntityClass($struct) ?? $this->salesChannelDefinitionInstanceRegistry->getByEntityClass($struct);
  92.             if ($definition && $definition->isSeoAware()) {
  93.                 $data->add($definition->getEntityName(), $struct);
  94.             }
  95.         }
  96.         foreach ($struct->getVars() as $item) {
  97.             if ($item instanceof Collection) {
  98.                 foreach ($item as $collectionItem) {
  99.                     if ($collectionItem instanceof Struct) {
  100.                         $this->findStruct($data$collectionItem);
  101.                     }
  102.                 }
  103.             } elseif ($item instanceof Struct) {
  104.                 $this->findStruct($data$item);
  105.             }
  106.         }
  107.     }
  108.     private function enrich(SeoResolverData $dataSalesChannelContext $context): void
  109.     {
  110.         foreach ($data->getEntities() as $definition) {
  111.             $definition = (string) $definition;
  112.             $ids $data->getIds($definition);
  113.             $routes $this->seoUrlRouteRegistry->findByDefinition($definition);
  114.             if (\count($routes) === 0) {
  115.                 continue;
  116.             }
  117.             $routes array_map(static function (SeoUrlRouteConfigRoute $seoUrlRoute) {
  118.                 return $seoUrlRoute->getConfig()->getRouteName();
  119.             }, $routes);
  120.             $criteria = new Criteria();
  121.             $criteria->addFilter(new EqualsFilter('isCanonical'true));
  122.             $criteria->addFilter(new EqualsAnyFilter('routeName'$routes));
  123.             $criteria->addFilter(new EqualsAnyFilter('foreignKey'$ids));
  124.             $criteria->addFilter(new EqualsFilter('languageId'$context->getContext()->getLanguageId()));
  125.             $criteria->addSorting(new FieldSorting('salesChannelId'));
  126.             /** @var SeoUrlEntity $url */
  127.             foreach ($this->salesChannelRepository->search($criteria$context) as $url) {
  128.                 /** @var SalesChannelProductEntity|CategoryEntity $entity */
  129.                 $entity $data->get($definition$url->getForeignKey());
  130.                 if ($entity->getSeoUrls() === null) {
  131.                     $entity->setSeoUrls(new SeoUrlCollection());
  132.                 }
  133.                 /** @phpstan-ignore-next-line - will complain that 'getSeoUrls' might be null, but we will set it if it is null */
  134.                 $entity->getSeoUrls()->add($url);
  135.             }
  136.         }
  137.     }
  138. }