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

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 Kiener\MolliePayments\Repository\CustomFieldSet\CustomFieldSetRepository;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\Migration\MigrationCollection;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  15. use Shopware\Core\Kernel;
  16. use Symfony\Component\DependencyInjection\Container;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. class MolliePayments extends Plugin
  19. {
  20.     const PLUGIN_VERSION '4.10.2';
  21.     /**
  22.      * @param ContainerBuilder $container
  23.      * @throws Exception
  24.      */
  25.     public function build(ContainerBuilder $container): void
  26.     {
  27.         parent::build($container);
  28.         $this->container $container;
  29.         $shopwareVersion $this->container->getParameter('kernel.shopware_version');
  30.         if (!is_string($shopwareVersion)) {
  31.             $shopwareVersionKernel::SHOPWARE_FALLBACK_VERSION;
  32.         }
  33.         # load the dependencies that are compatible
  34.         # with our current shopware version
  35.         $loader = new DependencyLoader($this->container, new VersionCompare($shopwareVersion));
  36.         $loader->loadServices();
  37.         $loader->prepareStorefrontBuild();
  38.     }
  39.     /**
  40.      * @param InstallContext $context
  41.      * @return void
  42.      */
  43.     public function install(InstallContext $context): void
  44.     {
  45.         parent::install($context);
  46.         if ($this->container === null) {
  47.             throw new Exception('Container is not initialized');
  48.         }
  49.         # that's the only part we use the Shopware repository directly,
  50.         # and not our custom one, because our repositories are not yet registered in this function
  51.         /** @var EntityRepository $shopwareRepoCustomFields */
  52.         $shopwareRepoCustomFields $this->container->get('custom_field_set.repository');
  53.         if ($shopwareRepoCustomFields !== null) {
  54.             $mollieRepoCustomFields = new CustomFieldSetRepository($shopwareRepoCustomFields);
  55.         }
  56.         $this->runDbMigrations($context->getMigrationCollection());
  57.     }
  58.     /**
  59.      * @param UpdateContext $context
  60.      * @throws \Doctrine\DBAL\Exception
  61.      * @return void
  62.      */
  63.     public function update(UpdateContext $context): void
  64.     {
  65.         parent::update($context);
  66.         if ($context->getPlugin()->isActive() === true) {
  67.             # only prepare our whole plugin
  68.             # if it is indeed active at the moment.
  69.             # otherwise service would not be found of course
  70.             $this->preparePlugin($context->getContext());
  71.             $this->runDbMigrations($context->getMigrationCollection());
  72.         }
  73.     }
  74.     /**
  75.      * @param ActivateContext $context
  76.      * @throws \Doctrine\DBAL\Exception
  77.      * @return void
  78.      */
  79.     public function activate(ActivateContext $context): void
  80.     {
  81.         parent::activate($context);
  82.         $this->preparePlugin($context->getContext());
  83.         $this->runDbMigrations($context->getMigrationCollection());
  84.     }
  85.     public function boot(): void
  86.     {
  87.         parent::boot();
  88.         if ($this->container === null) {
  89.             return;
  90.         }
  91.         /** @var Container $container */
  92.         $container $this->container;
  93.         
  94.         $shopwareVersion $container->getParameter('kernel.shopware_version');
  95.         if (!is_string($shopwareVersion)) {
  96.             $shopwareVersion Kernel::SHOPWARE_FALLBACK_VERSION;
  97.         }
  98.         # load the dependencies that are compatible
  99.         # with our current shopware version
  100.         $loader = new DependencyLoader($container, new VersionCompare($shopwareVersion));
  101.         $loader->registerFixturesAutoloader();
  102.     }
  103.     /**
  104.      * @param Context $context
  105.      * @throws \Doctrine\DBAL\Exception
  106.      */
  107.     private function preparePlugin(Context $context): void
  108.     {
  109.         if ($this->container === null) {
  110.             throw new Exception('Container is not initialized');
  111.         }
  112.         /** @var PluginInstaller $pluginInstaller */
  113.         $pluginInstaller $this->container->get(PluginInstaller::class);
  114.         $pluginInstaller->install($context);
  115.     }
  116.     /**
  117.      * @param MigrationCollection $migrationCollection
  118.      * @return void
  119.      */
  120.     private function runDbMigrations(MigrationCollection $migrationCollection): void
  121.     {
  122.         $migrationCollection->migrateInPlace();
  123.     }
  124. }