vendor/shopware/storefront/Theme/ConfigLoader/StaticFileConfigDumper.php line 56

  1. <?php
  2. declare(strict_types=1);
  3. namespace Shopware\Storefront\Theme\ConfigLoader;
  4. use League\Flysystem\FilesystemOperator;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Storefront\Theme\Event\ThemeAssignedEvent;
  8. use Shopware\Storefront\Theme\Event\ThemeConfigChangedEvent;
  9. use Shopware\Storefront\Theme\Event\ThemeConfigResetEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use function json_encode;
  12. use function sprintf;
  13. use const JSON_THROW_ON_ERROR;
  14. /**
  15.  * @internal
  16.  */
  17. #[Package('storefront')]
  18. class StaticFileConfigDumper implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @internal
  22.      */
  23.     public function __construct(
  24.         private readonly AbstractConfigLoader $configLoader,
  25.         private readonly AbstractAvailableThemeProvider $availableThemeProvider,
  26.         private readonly FilesystemOperator $filesystem
  27.     ) {
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             ThemeConfigChangedEvent::class => 'dumpConfigFromEvent',
  33.             ThemeAssignedEvent::class => 'dumpConfigFromEvent',
  34.             ThemeConfigResetEvent::class => 'dumpConfigFromEvent',
  35.         ];
  36.     }
  37.     public function dumpConfig(Context $context): void
  38.     {
  39.         $salesChannelToTheme $this->availableThemeProvider->load($contextfalse);
  40.         $this->filesystem->write(StaticFileAvailableThemeProvider::THEME_INDEXjson_encode($salesChannelToThemeJSON_THROW_ON_ERROR));
  41.         foreach ($salesChannelToTheme as $themeId) {
  42.             $struct $this->configLoader->load($themeId$context);
  43.             $path sprintf('theme-config/%s.json'$themeId);
  44.             $this->filesystem->write($pathjson_encode($struct->jsonSerialize(), JSON_THROW_ON_ERROR));
  45.         }
  46.     }
  47.     public function dumpConfigFromEvent(): void
  48.     {
  49.         $this->dumpConfig(Context::createDefaultContext());
  50.     }
  51. }