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

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