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

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\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. * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  16. */
  17. #[Package('storefront')]
  18. class UpdateSubscriber implements EventSubscriberInterface
  19. {
  20. private ThemeService $themeService;
  21. private ThemeLifecycleService $themeLifecycleService;
  22. private EntityRepositoryInterface $salesChannelRepository;
  23. /**
  24. * @internal
  25. */
  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. /**
  36. * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  37. */
  38. public static function getSubscribedEvents()
  39. {
  40. return [
  41. UpdatePostFinishEvent::class => 'updateFinished',
  42. ];
  43. }
  44. /**
  45. * @internal
  46. */
  47. public function updateFinished(UpdatePostFinishEvent $event): void
  48. {
  49. $context = $event->getContext();
  50. $this->themeLifecycleService->refreshThemes($context);
  51. if ($context->hasState(PluginLifecycleService::STATE_SKIP_ASSET_BUILDING)) {
  52. return;
  53. }
  54. $criteria = new Criteria();
  55. $criteria->addFilter(new EqualsFilter('active', true));
  56. $criteria->getAssociation('themes')
  57. ->addFilter(new EqualsFilter('active', true));
  58. $alreadyCompiled = [];
  59. /** @var SalesChannelEntity $salesChannel */
  60. foreach ($this->salesChannelRepository->search($criteria, $context) as $salesChannel) {
  61. $themes = $salesChannel->getExtension('themes');
  62. if (!$themes instanceof ThemeCollection) {
  63. continue;
  64. }
  65. foreach ($themes as $theme) {
  66. // NEXT-21735 - his is covered randomly
  67. // @codeCoverageIgnoreStart
  68. if (\in_array($theme->getId(), $alreadyCompiled, true) !== false) {
  69. continue;
  70. }
  71. // @codeCoverageIgnoreEnd
  72. $alreadyCompiled += $this->themeService->compileThemeById($theme->getId(), $context);
  73. }
  74. }
  75. }
  76. }