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

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