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

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