vendor/shopware/storefront/Theme/ResolvedConfigLoader.php line 53

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme;
  3. use Shopware\Core\Content\Media\MediaCollection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. #[Package('storefront')]
  11. class ResolvedConfigLoader extends AbstractResolvedConfigLoader
  12. {
  13.     /**
  14.      * @internal
  15.      */
  16.     public function __construct(
  17.         private readonly EntityRepository $repository,
  18.         private readonly ThemeService $service
  19.     ) {
  20.     }
  21.     public function getDecorated(): AbstractResolvedConfigLoader
  22.     {
  23.         throw new DecorationPatternException(self::class);
  24.     }
  25.     public function load(string $themeIdSalesChannelContext $context): array
  26.     {
  27.         $config $this->service->getThemeConfiguration($themeIdfalse$context->getContext());
  28.         $resolvedConfig = [];
  29.         $mediaItems = [];
  30.         if (!\array_key_exists('fields'$config)) {
  31.             return [];
  32.         }
  33.         foreach ($config['fields'] as $key => $data) {
  34.             if ($data['type'] === 'media' && $data['value'] && Uuid::isValid($data['value'])) {
  35.                 $mediaItems[$data['value']][] = $key;
  36.             }
  37.             $resolvedConfig[$key] = $data['value'];
  38.         }
  39.         $result = new MediaCollection();
  40.         /** @var array<string> $mediaIds */
  41.         $mediaIds array_keys($mediaItems);
  42.         if (!empty($mediaIds)) {
  43.             $criteria = new Criteria($mediaIds);
  44.             $criteria->setTitle('theme-service::resolve-media');
  45.             $result $this->repository->search($criteria$context->getContext());
  46.         }
  47.         foreach ($result as $media) {
  48.             if (!\array_key_exists($media->getId(), $mediaItems)) {
  49.                 continue;
  50.             }
  51.             foreach ($mediaItems[$media->getId()] as $key) {
  52.                 $resolvedConfig[$key] = $media->getUrl();
  53.             }
  54.         }
  55.         return $resolvedConfig;
  56.     }
  57. }