vendor/shopware/storefront/Theme/Subscriber/UpdateSubscriber.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\Plugin\PluginLifecycleService;
  7. use Shopware\Core\Framework\Update\Event\UpdatePostFinishEvent;
  8. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  9. use Shopware\Storefront\Theme\ThemeCollection;
  10. use Shopware\Storefront\Theme\ThemeLifecycleService;
  11. use Shopware\Storefront\Theme\ThemeService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class UpdateSubscriber implements EventSubscriberInterface
  14. {
  15.     private ThemeService $themeService;
  16.     private ThemeLifecycleService $themeLifecycleService;
  17.     private EntityRepositoryInterface $salesChannelRepository;
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(
  22.         ThemeService $themeService,
  23.         ThemeLifecycleService $themeLifecycleService,
  24.         EntityRepositoryInterface $salesChannelRepository
  25.     ) {
  26.         $this->themeService $themeService;
  27.         $this->themeLifecycleService $themeLifecycleService;
  28.         $this->salesChannelRepository $salesChannelRepository;
  29.     }
  30.     /**
  31.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  32.      */
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             UpdatePostFinishEvent::class => 'updateFinished',
  37.         ];
  38.     }
  39.     /**
  40.      * @internal
  41.      */
  42.     public function updateFinished(UpdatePostFinishEvent $event): void
  43.     {
  44.         $context $event->getContext();
  45.         $this->themeLifecycleService->refreshThemes($context);
  46.         if ($context->hasState(PluginLifecycleService::STATE_SKIP_ASSET_BUILDING)) {
  47.             return;
  48.         }
  49.         $criteria = new Criteria();
  50.         $criteria->addFilter(new EqualsFilter('active'true));
  51.         $criteria->getAssociation('themes')
  52.             ->addFilter(new EqualsFilter('active'true));
  53.         $alreadyCompiled = [];
  54.         /** @var SalesChannelEntity $salesChannel */
  55.         foreach ($this->salesChannelRepository->search($criteria$context) as $salesChannel) {
  56.             $themes $salesChannel->getExtension('themes');
  57.             if (!$themes instanceof ThemeCollection) {
  58.                 continue;
  59.             }
  60.             foreach ($themes as $theme) {
  61.                 // NEXT-21735 - his is covered randomly
  62.                 // @codeCoverageIgnoreStart
  63.                 if (\in_array($theme->getId(), $alreadyCompiledtrue) !== false) {
  64.                     continue;
  65.                 }
  66.                 // @codeCoverageIgnoreEnd
  67.                 $alreadyCompiled += $this->themeService->compileThemeById($theme->getId(), $context);
  68.             }
  69.         }
  70.     }
  71. }