platform/src/Storefront/Theme/Subscriber/UpdateSubscriber.php line 52

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\Update\Event\UpdatePostFinishEvent;
  7. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  8. use Shopware\Storefront\Theme\ThemeCollection;
  9. use Shopware\Storefront\Theme\ThemeLifecycleService;
  10. use Shopware\Storefront\Theme\ThemeService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class UpdateSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var ThemeService
  16.      */
  17.     private $themeService;
  18.     /**
  19.      * @var ThemeLifecycleService
  20.      */
  21.     private $themeLifecycleService;
  22.     /**
  23.      * @var EntityRepositoryInterface
  24.      */
  25.     private $salesChannelRepository;
  26.     public function __construct(
  27.         ThemeService $themeService,
  28.         ThemeLifecycleService $themeLifecycleService,
  29.         EntityRepositoryInterface $salesChannelRepository
  30.     ) {
  31.         $this->themeService $themeService;
  32.         $this->themeLifecycleService $themeLifecycleService;
  33.         $this->salesChannelRepository $salesChannelRepository;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             UpdatePostFinishEvent::class => 'updateFinished',
  39.         ];
  40.     }
  41.     /**
  42.      * @internal
  43.      */
  44.     public function updateFinished(UpdatePostFinishEvent $event): void
  45.     {
  46.         $context $event->getContext();
  47.         $this->themeLifecycleService->refreshThemes($context);
  48.         $criteria = new Criteria();
  49.         $criteria->addFilter(new EqualsFilter('active'true));
  50.         $criteria->getAssociation('themes')
  51.             ->addFilter(new EqualsFilter('active'true));
  52.         /** @var SalesChannelEntity $salesChannel */
  53.         foreach ($this->salesChannelRepository->search($criteria$context) as $salesChannel) {
  54.             $themes $salesChannel->getExtension('themes');
  55.             if (!$themes instanceof ThemeCollection) {
  56.                 continue;
  57.             }
  58.             foreach ($themes as $theme) {
  59.                 $salesChannelId $salesChannel->getId();
  60.                 $this->themeService->compileTheme($salesChannelId$theme->getId(), $context);
  61.             }
  62.         }
  63.     }
  64. }