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

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