custom/plugins/TmmsDropDownMenu/src/Subscriber/SystemConfigSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tmms\DropDownMenu\Subscriber;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
  8. use Shopware\Core\System\SalesChannel\SalesChannelCollection;
  9. use Shopware\Core\System\SystemConfig\Event\SystemConfigChangedEvent;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Theme\ThemeCollection;
  12. use Shopware\Storefront\Theme\ThemeService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class SystemConfigSubscriber implements EventSubscriberInterface
  15. {
  16.     private bool $pluginConfigSaved false;
  17.     private SystemConfigService $systemConfig;
  18.     private ThemeService $themeService;
  19.     private EntityRepositoryInterface $salesChannelRepository;
  20.     private string $pluginDomain;
  21.     public function __construct(
  22.         SystemConfigService $systemConfig,
  23.         ThemeService $themeService,
  24.         EntityRepositoryInterface $salesChannelRepository,
  25.         string $pluginDomain
  26.     ) {
  27.         $this->systemConfig $systemConfig;
  28.         $this->pluginDomain $pluginDomain;
  29.         $this->themeService $themeService;
  30.         $this->salesChannelRepository $salesChannelRepository;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             SystemConfigChangedEvent::class => 'onSaveTheme',
  36.             BeforeSendResponseEvent::class => 'compileTheme',
  37.         ];
  38.     }
  39.     public function onSaveTheme(SystemConfigChangedEvent $event): void
  40.     {
  41.         $isPluginConfigKey mb_strpos($event->getKey(), $this->pluginDomain);
  42.         if (!$this->pluginConfigSaved && $isPluginConfigKey === 0) {
  43.             $this->pluginConfigSaved true;
  44.         }
  45.     }
  46.     public function compileTheme(BeforeSendResponseEvent $event): void
  47.     {
  48.         if (!$this->pluginConfigSaved) {
  49.             return;
  50.         }
  51.         $context Context::createDefaultContext();
  52.         $salesChannels $this->getSalesChannels($context);
  53.         foreach ($salesChannels as $salesChannel) {
  54.             $isThemeCompileActive $this->systemConfig
  55.                 ->get($this->pluginDomain 'dropdownMenuCompileThemeOnSave'$salesChannel->getId());
  56.             if (!$isThemeCompileActive) {
  57.                 continue;
  58.             }
  59.             /** @var ThemeCollection|null $themes */
  60.             $themes $salesChannel->getExtensionOfType('themes'ThemeCollection::class);
  61.             if (!$themes || !$theme $themes->first()) {
  62.                 continue;
  63.             }
  64.             $this->themeService->compileTheme($salesChannel->getId(), $theme->getId(), $contextnullfalse);
  65.         }
  66.     }
  67.     private function getSalesChannels(Context $context): SalesChannelCollection
  68.     {
  69.         $criteria = new Criteria();
  70.         $criteria->addAssociation('themes');
  71.         /** @var SalesChannelCollection $result */
  72.         $result $this->salesChannelRepository->search($criteria$context)->getEntities();
  73.         return $result;
  74.     }
  75. }