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

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\Feature;
  5. use Shopware\Core\Framework\Plugin;
  6. use Shopware\Core\Framework\Plugin\Event\PluginLifecycleEvent;
  7. use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
  8. use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivationFailedEvent;
  9. use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
  10. use Shopware\Core\Framework\Plugin\Event\PluginPreActivateEvent;
  11. use Shopware\Core\Framework\Plugin\Event\PluginPreDeactivateEvent;
  12. use Shopware\Core\Framework\Plugin\Event\PluginPreUninstallEvent;
  13. use Shopware\Core\Framework\Plugin\Event\PluginPreUpdateEvent;
  14. use Shopware\Storefront\Theme\Exception\InvalidThemeBundleException;
  15. use Shopware\Storefront\Theme\Exception\ThemeCompileException;
  16. use Shopware\Storefront\Theme\StorefrontPluginConfiguration\AbstractStorefrontPluginConfigurationFactory;
  17. use Shopware\Storefront\Theme\StorefrontPluginConfiguration\StorefrontPluginConfiguration;
  18. use Shopware\Storefront\Theme\StorefrontPluginRegistryInterface;
  19. use Shopware\Storefront\Theme\ThemeLifecycleHandler;
  20. use Shopware\Storefront\Theme\ThemeLifecycleService;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. class PluginLifecycleSubscriber implements EventSubscriberInterface
  23. {
  24.     private StorefrontPluginRegistryInterface $storefrontPluginRegistry;
  25.     private string $projectDirectory;
  26.     private AbstractStorefrontPluginConfigurationFactory $pluginConfigurationFactory;
  27.     private ThemeLifecycleHandler $themeLifecycleHandler;
  28.     private ThemeLifecycleService $themeLifecycleService;
  29.     /**
  30.      * @internal
  31.      */
  32.     public function __construct(
  33.         StorefrontPluginRegistryInterface $storefrontPluginRegistry,
  34.         string $projectDirectory,
  35.         AbstractStorefrontPluginConfigurationFactory $pluginConfigurationFactory,
  36.         ThemeLifecycleHandler $themeLifecycleHandler,
  37.         ThemeLifecycleService $themeLifecycleService
  38.     ) {
  39.         $this->storefrontPluginRegistry $storefrontPluginRegistry;
  40.         $this->projectDirectory $projectDirectory;
  41.         $this->pluginConfigurationFactory $pluginConfigurationFactory;
  42.         $this->themeLifecycleHandler $themeLifecycleHandler;
  43.         $this->themeLifecycleService $themeLifecycleService;
  44.     }
  45.     /**
  46.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  47.      */
  48.     public static function getSubscribedEvents()
  49.     {
  50.         if (Feature::isActive('v6.5.0.0')) {
  51.             return [
  52.                 PluginPostActivateEvent::class => 'pluginPostActivate',
  53.                 PluginPreUpdateEvent::class => 'pluginUpdate',
  54.                 PluginPreDeactivateEvent::class => 'pluginDeactivateAndUninstall',
  55.                 PluginPostDeactivationFailedEvent::class => 'pluginPostDeactivateFailed',
  56.                 PluginPreUninstallEvent::class => 'pluginDeactivateAndUninstall',
  57.                 PluginPostUninstallEvent::class => 'pluginPostUninstall',
  58.             ];
  59.         }
  60.         return [
  61.             PluginPreActivateEvent::class => 'pluginActivate',
  62.             PluginPostActivateEvent::class => 'pluginPostActivate',
  63.             PluginPreUpdateEvent::class => 'pluginUpdate',
  64.             PluginPreDeactivateEvent::class => 'pluginDeactivateAndUninstall',
  65.             PluginPostDeactivationFailedEvent::class => 'pluginPostDeactivateFailed',
  66.             PluginPreUninstallEvent::class => 'pluginDeactivateAndUninstall',
  67.             PluginPostUninstallEvent::class => 'pluginPostUninstall',
  68.         ];
  69.     }
  70.     /**
  71.      * @deprecated tag:v6.5.0 - Method will be removed. use pluginPostActivate instead
  72.      */
  73.     public function pluginActivate(PluginPreActivateEvent $event): void
  74.     {
  75.         Feature::triggerDeprecationOrThrow(
  76.             'v6.5.0.0',
  77.             sprintf('Method pluginActivate of Class %s is deprecated. Use method pluginPostActivate instead', static::class)
  78.         );
  79.         // do nothing
  80.     }
  81.     public function pluginPostActivate(PluginPostActivateEvent $event): void
  82.     {
  83.         $this->doPostActivate($event);
  84.     }
  85.     public function pluginPostDeactivateFailed(PluginPostDeactivationFailedEvent $event): void
  86.     {
  87.         $this->doPostActivate($event);
  88.     }
  89.     public function pluginUpdate(PluginPreUpdateEvent $event): void
  90.     {
  91.         if ($this->skipCompile($event->getContext()->getContext())) {
  92.             return;
  93.         }
  94.         $pluginName $event->getPlugin()->getName();
  95.         $config $this->storefrontPluginRegistry->getConfigurations()->getByTechnicalName($pluginName);
  96.         if (!$config) {
  97.             return;
  98.         }
  99.         $this->themeLifecycleHandler->handleThemeInstallOrUpdate(
  100.             $config,
  101.             $this->storefrontPluginRegistry->getConfigurations(),
  102.             $event->getContext()->getContext()
  103.         );
  104.     }
  105.     /**
  106.      * @param PluginPreDeactivateEvent|PluginPreUninstallEvent $event
  107.      */
  108.     public function pluginDeactivateAndUninstall($event): void
  109.     {
  110.         if ($this->skipCompile($event->getContext()->getContext())) {
  111.             return;
  112.         }
  113.         $pluginName $event->getPlugin()->getName();
  114.         $config $this->storefrontPluginRegistry->getConfigurations()->getByTechnicalName($pluginName);
  115.         if (!$config) {
  116.             return;
  117.         }
  118.         $this->themeLifecycleHandler->handleThemeUninstall($config$event->getContext()->getContext());
  119.     }
  120.     public function pluginPostUninstall(PluginPostUninstallEvent $event): void
  121.     {
  122.         if ($event->getContext()->keepUserData()) {
  123.             return;
  124.         }
  125.         $this->themeLifecycleService->removeTheme($event->getPlugin()->getName(), $event->getContext()->getContext());
  126.     }
  127.     /**
  128.      * @throws ThemeCompileException
  129.      * @throws InvalidThemeBundleException
  130.      */
  131.     private function createConfigFromClassName(string $pluginPathstring $className): StorefrontPluginConfiguration
  132.     {
  133.         /** @var Plugin $plugin */
  134.         $plugin = new $className(true$pluginPath$this->projectDirectory);
  135.         if (!$plugin instanceof Plugin) {
  136.             throw new \RuntimeException(
  137.                 sprintf('Plugin class "%s" must extend "%s"', \get_class($plugin), Plugin::class)
  138.             );
  139.         }
  140.         return $this->pluginConfigurationFactory->createFromBundle($plugin);
  141.     }
  142.     private function doPostActivate(PluginLifecycleEvent $event): void
  143.     {
  144.         if (!($event instanceof PluginPostActivateEvent) && !($event instanceof PluginPostDeactivationFailedEvent)) {
  145.             return;
  146.         }
  147.         if ($this->skipCompile($event->getContext()->getContext())) {
  148.             return;
  149.         }
  150.         // create instance of the plugin to create a configuration
  151.         // (the kernel boot is already finished and the activated plugin is missing)
  152.         $storefrontPluginConfig $this->createConfigFromClassName(
  153.             $event->getPlugin()->getPath() ?: '',
  154.             $event->getPlugin()->getBaseClass()
  155.         );
  156.         // add plugin configuration to the list of all active plugin configurations
  157.         $configurationCollection = clone $this->storefrontPluginRegistry->getConfigurations();
  158.         $configurationCollection->add($storefrontPluginConfig);
  159.         $this->themeLifecycleHandler->handleThemeInstallOrUpdate(
  160.             $storefrontPluginConfig,
  161.             $configurationCollection,
  162.             $event->getContext()->getContext()
  163.         );
  164.     }
  165.     private function skipCompile(Context $context): bool
  166.     {
  167.         return $context->hasState(Plugin\PluginLifecycleService::STATE_SKIP_ASSET_BUILDING);
  168.     }
  169. }