src/Storefront/Theme/ThemeAppLifecycleHandler.php line 58

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme;
  3. use Shopware\Core\Framework\App\Event\AppActivatedEvent;
  4. use Shopware\Core\Framework\App\Event\AppChangedEvent;
  5. use Shopware\Core\Framework\App\Event\AppDeactivatedEvent;
  6. use Shopware\Core\Framework\App\Event\AppUpdatedEvent;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Storefront\Theme\StorefrontPluginConfiguration\AbstractStorefrontPluginConfigurationFactory;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11.  * @internal
  12.  */
  13. #[Package('storefront')]
  14. class ThemeAppLifecycleHandler implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @internal
  18.      */
  19.     public function __construct(private readonly StorefrontPluginRegistryInterface $themeRegistry, private readonly AbstractStorefrontPluginConfigurationFactory $themeConfigFactory, private readonly ThemeLifecycleHandler $themeLifecycleHandler)
  20.     {
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             AppUpdatedEvent::class => 'handleAppActivationOrUpdate',
  26.             AppActivatedEvent::class => 'handleAppActivationOrUpdate',
  27.             AppDeactivatedEvent::class => 'handleUninstall',
  28.         ];
  29.     }
  30.     public function handleAppActivationOrUpdate(AppChangedEvent $event): void
  31.     {
  32.         $app $event->getApp();
  33.         if (!$app->isActive()) {
  34.             return;
  35.         }
  36.         $configurationCollection $this->themeRegistry->getConfigurations();
  37.         $config $configurationCollection->getByTechnicalName($app->getName());
  38.         if (!$config) {
  39.             $config $this->themeConfigFactory->createFromApp($app->getName(), $app->getPath());
  40.             $configurationCollection = clone $configurationCollection;
  41.             $configurationCollection->add($config);
  42.         }
  43.         $this->themeLifecycleHandler->handleThemeInstallOrUpdate(
  44.             $config,
  45.             $configurationCollection,
  46.             $event->getContext()
  47.         );
  48.     }
  49.     public function handleUninstall(AppDeactivatedEvent $event): void
  50.     {
  51.         $config $this->themeRegistry->getConfigurations()->getByTechnicalName($event->getApp()->getName());
  52.         if (!$config) {
  53.             return;
  54.         }
  55.         $this->themeLifecycleHandler->handleThemeUninstall($config$event->getContext());
  56.     }
  57. }