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

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