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

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