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

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