custom/plugins/MolliePayments/src/MolliePayments.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Kiener\MolliePayments;
  4. use Kiener\MolliePayments\Compatibility\DependencyLoader;
  5. use Kiener\MolliePayments\Compatibility\VersionCompare;
  6. use Kiener\MolliePayments\Components\Installer\PluginInstaller;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\Migration\MigrationCollection;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  13. use Shopware\Core\Kernel;
  14. use Symfony\Component\DependencyInjection\Container;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. class MolliePayments extends Plugin
  17. {
  18.     public const PLUGIN_VERSION '4.16.0';
  19.     /**
  20.      * @throws \Exception
  21.      */
  22.     public function build(ContainerBuilder $container): void
  23.     {
  24.         parent::build($container);
  25.         $this->container $container;
  26.         $shopwareVersion $this->container->getParameter('kernel.shopware_version');
  27.         if (! is_string($shopwareVersion)) {
  28.             $shopwareVersion Kernel::SHOPWARE_FALLBACK_VERSION;
  29.         }
  30.         // load the dependencies that are compatible
  31.         // with our current shopware version
  32.         $loader = new DependencyLoader($this->container, new VersionCompare($shopwareVersion));
  33.         $loader->loadServices();
  34.         $loader->prepareStorefrontBuild();
  35.     }
  36.     public function install(InstallContext $context): void
  37.     {
  38.         parent::install($context);
  39.         if ($this->container === null) {
  40.             throw new \Exception('Container is not initialized');
  41.         }
  42.         $this->runDbMigrations($context->getMigrationCollection());
  43.     }
  44.     /**
  45.      * @throws \Doctrine\DBAL\Exception
  46.      */
  47.     public function update(UpdateContext $context): void
  48.     {
  49.         parent::update($context);
  50.         if ($context->getPlugin()->isActive() === true) {
  51.             // only prepare our whole plugin
  52.             // if it is indeed active at the moment.
  53.             // otherwise service would not be found of course
  54.             $this->preparePlugin($context->getContext());
  55.             $this->runDbMigrations($context->getMigrationCollection());
  56.         }
  57.     }
  58.     /**
  59.      * @throws \Doctrine\DBAL\Exception
  60.      */
  61.     public function activate(ActivateContext $context): void
  62.     {
  63.         parent::activate($context);
  64.         $this->preparePlugin($context->getContext());
  65.         $this->runDbMigrations($context->getMigrationCollection());
  66.     }
  67.     public function boot(): void
  68.     {
  69.         parent::boot();
  70.         if ($this->container === null) {
  71.             return;
  72.         }
  73.         /** @var Container $container */
  74.         $container $this->container;
  75.         $shopwareVersion $container->getParameter('kernel.shopware_version');
  76.         if (! is_string($shopwareVersion)) {
  77.             $shopwareVersion Kernel::SHOPWARE_FALLBACK_VERSION;
  78.         }
  79.         // load the dependencies that are compatible
  80.         // with our current shopware version
  81.         $loader = new DependencyLoader($container, new VersionCompare($shopwareVersion));
  82.         $loader->registerDependencies();
  83.     }
  84.     /**
  85.      * @throws \Doctrine\DBAL\Exception
  86.      */
  87.     private function preparePlugin(Context $context): void
  88.     {
  89.         if ($this->container === null) {
  90.             throw new \Exception('Container is not initialized');
  91.         }
  92.         /** @var PluginInstaller $pluginInstaller */
  93.         $pluginInstaller $this->container->get(PluginInstaller::class);
  94.         $pluginInstaller->install($context);
  95.     }
  96.     private function runDbMigrations(MigrationCollection $migrationCollection): void
  97.     {
  98.         $migrationCollection->migrateInPlace();
  99.     }
  100. }