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 Kiener\MolliePayments\Service\CustomFieldService;
  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.0.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.         # that's the only part we use the Shopware repository directly,
  76.         # and not our custom one, because our repositories are not yet registered in this function
  77.         /** @var EntityRepository $shopwareRepoCustomFields */
  78.         $shopwareRepoCustomFields $this->container->get('custom_field_set.repository');
  79.         $mollieRepoCustomFields = new CustomFieldSetRepository($shopwareRepoCustomFields);
  80.         $this->runDbMigrations($context->getMigrationCollection());
  81.     }
  82.     /**
  83.      * @param UpdateContext $context
  84.      * @throws \Doctrine\DBAL\Exception
  85.      * @return void
  86.      */
  87.     public function update(UpdateContext $context): void
  88.     {
  89.         parent::update($context);
  90.         if ($context->getPlugin()->isActive() === true) {
  91.             # only prepare our whole plugin
  92.             # if it is indeed active at the moment.
  93.             # otherwise service would not be found of course
  94.             $this->preparePlugin($context->getContext());
  95.             $this->runDbMigrations($context->getMigrationCollection());
  96.         }
  97.     }
  98.     /**
  99.      * @param InstallContext $context
  100.      * @return void
  101.      */
  102.     public function postInstall(InstallContext $context): void
  103.     {
  104.         parent::postInstall($context);
  105.     }
  106.     /**
  107.      * @param UninstallContext $context
  108.      * @return void
  109.      */
  110.     public function uninstall(UninstallContext $context): void
  111.     {
  112.         parent::uninstall($context);
  113.     }
  114.     /**
  115.      * @param ActivateContext $context
  116.      * @throws \Doctrine\DBAL\Exception
  117.      * @return void
  118.      */
  119.     public function activate(ActivateContext $context): void
  120.     {
  121.         parent::activate($context);
  122.         $this->preparePlugin($context->getContext());
  123.         $this->runDbMigrations($context->getMigrationCollection());
  124.     }
  125.     /**
  126.      * @param DeactivateContext $context
  127.      * @return void
  128.      */
  129.     public function deactivate(DeactivateContext $context): void
  130.     {
  131.         parent::deactivate($context);
  132.     }
  133.     /**
  134.      * @param Context $context
  135.      * @throws \Doctrine\DBAL\Exception
  136.      */
  137.     private function preparePlugin(Context $context): void
  138.     {
  139.         /** @var PluginInstaller $pluginInstaller */
  140.         $pluginInstaller $this->container->get(PluginInstaller::class);
  141.         $pluginInstaller->install($context);
  142.     }
  143.     /**
  144.      * @param MigrationCollection $migrationCollection
  145.      * @return void
  146.      */
  147.     private function runDbMigrations(MigrationCollection $migrationCollection): void
  148.     {
  149.         $migrationCollection->migrateInPlace();
  150.     }
  151. }