vendor/shopware/storefront/Theme/Subscriber/PluginLifecycleSubscriber.php line 124

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Theme\Subscriber;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\Plugin;
  5. use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
  6. use Shopware\Core\Framework\Plugin\Event\PluginPreActivateEvent;
  7. use Shopware\Core\Framework\Plugin\Event\PluginPreDeactivateEvent;
  8. use Shopware\Core\Framework\Plugin\Event\PluginPreUninstallEvent;
  9. use Shopware\Core\Framework\Plugin\Event\PluginPreUpdateEvent;
  10. use Shopware\Storefront\Theme\Exception\InvalidThemeBundleException;
  11. use Shopware\Storefront\Theme\Exception\ThemeCompileException;
  12. use Shopware\Storefront\Theme\StorefrontPluginConfiguration\AbstractStorefrontPluginConfigurationFactory;
  13. use Shopware\Storefront\Theme\StorefrontPluginConfiguration\StorefrontPluginConfiguration;
  14. use Shopware\Storefront\Theme\StorefrontPluginRegistryInterface;
  15. use Shopware\Storefront\Theme\ThemeLifecycleHandler;
  16. use Shopware\Storefront\Theme\ThemeLifecycleService;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class PluginLifecycleSubscriber implements EventSubscriberInterface
  19. {
  20.     private StorefrontPluginRegistryInterface $storefrontPluginRegistry;
  21.     private string $projectDirectory;
  22.     private AbstractStorefrontPluginConfigurationFactory $pluginConfigurationFactory;
  23.     private ThemeLifecycleHandler $themeLifecycleHandler;
  24.     private ThemeLifecycleService $themeLifecycleService;
  25.     public function __construct(
  26.         StorefrontPluginRegistryInterface $storefrontPluginRegistry,
  27.         string $projectDirectory,
  28.         AbstractStorefrontPluginConfigurationFactory $pluginConfigurationFactory,
  29.         ThemeLifecycleHandler $themeLifecycleHandler,
  30.         ThemeLifecycleService $themeLifecycleService
  31.     ) {
  32.         $this->storefrontPluginRegistry $storefrontPluginRegistry;
  33.         $this->projectDirectory $projectDirectory;
  34.         $this->pluginConfigurationFactory $pluginConfigurationFactory;
  35.         $this->themeLifecycleHandler $themeLifecycleHandler;
  36.         $this->themeLifecycleService $themeLifecycleService;
  37.     }
  38.     /**
  39.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  40.      */
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             PluginPreActivateEvent::class => 'pluginActivate',
  45.             PluginPreUpdateEvent::class => 'pluginUpdate',
  46.             PluginPreDeactivateEvent::class => 'pluginDeactivateAndUninstall',
  47.             PluginPreUninstallEvent::class => 'pluginDeactivateAndUninstall',
  48.             PluginPostUninstallEvent::class => 'pluginPostUninstall',
  49.         ];
  50.     }
  51.     public function pluginActivate(PluginPreActivateEvent $event): void
  52.     {
  53.         if ($this->skipCompile($event->getContext()->getContext())) {
  54.             return;
  55.         }
  56.         // create instance of the plugin to create a configuration
  57.         // (the kernel boot is already finished and the activated plugin is missing)
  58.         $storefrontPluginConfig $this->createConfigFromClassName(
  59.             $event->getPlugin()->getPath(),
  60.             $event->getPlugin()->getBaseClass()
  61.         );
  62.         // add plugin configuration to the list of all active plugin configurations
  63.         $configurationCollection = clone $this->storefrontPluginRegistry->getConfigurations();
  64.         $configurationCollection->add($storefrontPluginConfig);
  65.         $this->themeLifecycleHandler->handleThemeInstallOrUpdate(
  66.             $storefrontPluginConfig,
  67.             $configurationCollection,
  68.             $event->getContext()->getContext()
  69.         );
  70.     }
  71.     public function pluginUpdate(PluginPreUpdateEvent $event): void
  72.     {
  73.         if ($this->skipCompile($event->getContext()->getContext())) {
  74.             return;
  75.         }
  76.         $pluginName $event->getPlugin()->getName();
  77.         $config $this->storefrontPluginRegistry->getConfigurations()->getByTechnicalName($pluginName);
  78.         if (!$config) {
  79.             return;
  80.         }
  81.         $this->themeLifecycleHandler->handleThemeInstallOrUpdate(
  82.             $config,
  83.             $this->storefrontPluginRegistry->getConfigurations(),
  84.             $event->getContext()->getContext()
  85.         );
  86.     }
  87.     /**
  88.      * @param PluginPreDeactivateEvent|PluginPreUninstallEvent $event
  89.      */
  90.     public function pluginDeactivateAndUninstall($event): void
  91.     {
  92.         if ($this->skipCompile($event->getContext()->getContext())) {
  93.             return;
  94.         }
  95.         $pluginName $event->getPlugin()->getName();
  96.         $config $this->storefrontPluginRegistry->getConfigurations()->getByTechnicalName($pluginName);
  97.         if (!$config) {
  98.             return;
  99.         }
  100.         $this->themeLifecycleHandler->handleThemeUninstall($config$event->getContext()->getContext());
  101.     }
  102.     public function pluginPostUninstall(PluginPostUninstallEvent $event): void
  103.     {
  104.         if ($event->getContext()->keepUserData()) {
  105.             return;
  106.         }
  107.         $this->themeLifecycleService->removeTheme($event->getPlugin()->getName(), $event->getContext()->getContext());
  108.     }
  109.     /**
  110.      * @throws ThemeCompileException
  111.      * @throws InvalidThemeBundleException
  112.      */
  113.     private function createConfigFromClassName(string $pluginPathstring $className): StorefrontPluginConfiguration
  114.     {
  115.         /** @var Plugin $plugin */
  116.         $plugin = new $className(true$pluginPath$this->projectDirectory);
  117.         if (!$plugin instanceof Plugin) {
  118.             throw new \RuntimeException(
  119.                 sprintf('Plugin class "%s" must extend "%s"', \get_class($plugin), Plugin::class)
  120.             );
  121.         }
  122.         return $this->pluginConfigurationFactory->createFromBundle($plugin);
  123.     }
  124.     private function skipCompile(Context $context): bool
  125.     {
  126.         return $context->hasState(Plugin\PluginLifecycleService::STATE_SKIP_ASSET_BUILDING);
  127.     }
  128. }