platform/src/Storefront/Theme/Subscriber/PluginLifecycleSubscriber.php line 121

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.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             PluginPreActivateEvent::class => 'pluginActivate',
  42.             PluginPreUpdateEvent::class => 'pluginUpdate',
  43.             PluginPreDeactivateEvent::class => 'pluginDeactivateAndUninstall',
  44.             PluginPreUninstallEvent::class => 'pluginDeactivateAndUninstall',
  45.             PluginPostUninstallEvent::class => 'pluginPostUninstall',
  46.         ];
  47.     }
  48.     public function pluginActivate(PluginPreActivateEvent $event): void
  49.     {
  50.         if ($this->skipCompile($event->getContext()->getContext())) {
  51.             return;
  52.         }
  53.         // create instance of the plugin to create a configuration
  54.         // (the kernel boot is already finished and the activated plugin is missing)
  55.         $storefrontPluginConfig $this->createConfigFromClassName(
  56.             $event->getPlugin()->getPath(),
  57.             $event->getPlugin()->getBaseClass()
  58.         );
  59.         // add plugin configuration to the list of all active plugin configurations
  60.         $configurationCollection = clone $this->storefrontPluginRegistry->getConfigurations();
  61.         $configurationCollection->add($storefrontPluginConfig);
  62.         $this->themeLifecycleHandler->handleThemeInstallOrUpdate(
  63.             $storefrontPluginConfig,
  64.             $configurationCollection,
  65.             $event->getContext()->getContext()
  66.         );
  67.     }
  68.     public function pluginUpdate(PluginPreUpdateEvent $event): void
  69.     {
  70.         if ($this->skipCompile($event->getContext()->getContext())) {
  71.             return;
  72.         }
  73.         $pluginName $event->getPlugin()->getName();
  74.         $config $this->storefrontPluginRegistry->getConfigurations()->getByTechnicalName($pluginName);
  75.         if (!$config) {
  76.             return;
  77.         }
  78.         $this->themeLifecycleHandler->handleThemeInstallOrUpdate(
  79.             $config,
  80.             $this->storefrontPluginRegistry->getConfigurations(),
  81.             $event->getContext()->getContext()
  82.         );
  83.     }
  84.     /**
  85.      * @param PluginPreDeactivateEvent|PluginPreUninstallEvent $event
  86.      */
  87.     public function pluginDeactivateAndUninstall($event): void
  88.     {
  89.         if ($this->skipCompile($event->getContext()->getContext())) {
  90.             return;
  91.         }
  92.         $pluginName $event->getPlugin()->getName();
  93.         $config $this->storefrontPluginRegistry->getConfigurations()->getByTechnicalName($pluginName);
  94.         if (!$config) {
  95.             return;
  96.         }
  97.         $this->themeLifecycleHandler->handleThemeUninstall($config$event->getContext()->getContext());
  98.     }
  99.     public function pluginPostUninstall(PluginPostUninstallEvent $event): void
  100.     {
  101.         if ($event->getContext()->keepUserData()) {
  102.             return;
  103.         }
  104.         $this->themeLifecycleService->removeTheme($event->getPlugin()->getName(), $event->getContext()->getContext());
  105.     }
  106.     /**
  107.      * @throws ThemeCompileException
  108.      * @throws InvalidThemeBundleException
  109.      */
  110.     private function createConfigFromClassName(string $pluginPathstring $className): StorefrontPluginConfiguration
  111.     {
  112.         /** @var Plugin $plugin */
  113.         $plugin = new $className(true$pluginPath$this->projectDirectory);
  114.         if (!$plugin instanceof Plugin) {
  115.             throw new \RuntimeException(
  116.                 sprintf('Plugin class "%s" must extend "%s"'\get_class($plugin), Plugin::class)
  117.             );
  118.         }
  119.         return $this->pluginConfigurationFactory->createFromBundle($plugin);
  120.     }
  121.     private function skipCompile(Context $context): bool
  122.     {
  123.         return $context->hasState(Plugin\PluginLifecycleService::STATE_SKIP_ASSET_BUILDING);
  124.     }
  125. }