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

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\Components\Installer\PluginInstaller;
  6. use Kiener\MolliePayments\Repository\CustomFieldSet\CustomFieldSetRepository;
  7. use Psr\Container\ContainerInterface;
  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\DeactivateContext;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  17. use Shopware\Core\Kernel;
  18. use Symfony\Component\DependencyInjection\Container;
  19. use Symfony\Component\DependencyInjection\ContainerBuilder;
  20. use Symfony\Component\Filesystem\Filesystem;
  21. use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
  22. class MolliePayments extends Plugin
  23. {
  24.     const PLUGIN_VERSION '4.6.0';
  25.     /**
  26.      * @param ContainerBuilder $container
  27.      * @throws Exception
  28.      */
  29.     public function build(ContainerBuilder $container): void
  30.     {
  31.         parent::build($container);
  32.         $this->container $container;
  33.         # load the dependencies that are compatible
  34.         # with our current shopware version
  35.         $loader = new DependencyLoader($this->container);
  36.         $loader->loadServices();
  37.         $loader->prepareStorefrontBuild();
  38.     }
  39.     /**
  40.      * @return void
  41.      */
  42.     public function boot(): void
  43.     {
  44.         parent::boot();
  45.     }
  46.     /**
  47.      * @param RoutingConfigurator $routes
  48.      * @param string $environment
  49.      * @return void
  50.      */
  51.     public function configureRoutes(RoutingConfigurator $routesstring $environment): void
  52.     {
  53.         if (!$this->isActive()) {
  54.             return;
  55.         }
  56.         /** @var Container $container */
  57.         $container $this->container;
  58.         $loader = new DependencyLoader($container);
  59.         $routeDir $loader->getRoutesPath($this->getPath());
  60.         $fileSystem = new Filesystem();
  61.         if ($fileSystem->exists($routeDir)) {
  62.             $routes->import($routeDir '/{routes}/*' Kernel::CONFIG_EXTS'glob');
  63.             $routes->import($routeDir '/{routes}/' $environment '/**/*' Kernel::CONFIG_EXTS'glob');
  64.             $routes->import($routeDir '/{routes}' Kernel::CONFIG_EXTS'glob');
  65.             $routes->import($routeDir '/{routes}_' $environment Kernel::CONFIG_EXTS'glob');
  66.         }
  67.     }
  68.     /**
  69.      * @param InstallContext $context
  70.      * @return void
  71.      */
  72.     public function install(InstallContext $context): void
  73.     {
  74.         parent::install($context);
  75.         if ($this->container === null) {
  76.             throw new Exception('Container is not initialized');
  77.         }
  78.         # that's the only part we use the Shopware repository directly,
  79.         # and not our custom one, because our repositories are not yet registered in this function
  80.         /** @var EntityRepository $shopwareRepoCustomFields */
  81.         $shopwareRepoCustomFields $this->container->get('custom_field_set.repository');
  82.         if ($shopwareRepoCustomFields !== null) {
  83.             $mollieRepoCustomFields = new CustomFieldSetRepository($shopwareRepoCustomFields);
  84.         }
  85.         $this->runDbMigrations($context->getMigrationCollection());
  86.     }
  87.     /**
  88.      * @param UpdateContext $context
  89.      * @throws \Doctrine\DBAL\Exception
  90.      * @return void
  91.      */
  92.     public function update(UpdateContext $context): void
  93.     {
  94.         parent::update($context);
  95.         if ($context->getPlugin()->isActive() === true) {
  96.             # only prepare our whole plugin
  97.             # if it is indeed active at the moment.
  98.             # otherwise service would not be found of course
  99.             $this->preparePlugin($context->getContext());
  100.             $this->runDbMigrations($context->getMigrationCollection());
  101.         }
  102.     }
  103.     /**
  104.      * @param InstallContext $context
  105.      * @return void
  106.      */
  107.     public function postInstall(InstallContext $context): void
  108.     {
  109.         parent::postInstall($context);
  110.     }
  111.     /**
  112.      * @param UninstallContext $context
  113.      * @return void
  114.      */
  115.     public function uninstall(UninstallContext $context): void
  116.     {
  117.         parent::uninstall($context);
  118.     }
  119.     /**
  120.      * @param ActivateContext $context
  121.      * @throws \Doctrine\DBAL\Exception
  122.      * @return void
  123.      */
  124.     public function activate(ActivateContext $context): void
  125.     {
  126.         parent::activate($context);
  127.         $this->preparePlugin($context->getContext());
  128.         $this->runDbMigrations($context->getMigrationCollection());
  129.     }
  130.     /**
  131.      * @param DeactivateContext $context
  132.      * @return void
  133.      */
  134.     public function deactivate(DeactivateContext $context): void
  135.     {
  136.         parent::deactivate($context);
  137.     }
  138.     /**
  139.      * @param Context $context
  140.      * @throws \Doctrine\DBAL\Exception
  141.      */
  142.     private function preparePlugin(Context $context): void
  143.     {
  144.         if ($this->container === null) {
  145.             throw new Exception('Container is not initialized');
  146.         }
  147.         /** @var PluginInstaller $pluginInstaller */
  148.         $pluginInstaller $this->container->get(PluginInstaller::class);
  149.         $pluginInstaller->install($context);
  150.     }
  151.     /**
  152.      * @param MigrationCollection $migrationCollection
  153.      * @return void
  154.      */
  155.     private function runDbMigrations(MigrationCollection $migrationCollection): void
  156.     {
  157.         $migrationCollection->migrateInPlace();
  158.     }
  159. }