custom/plugins/NetiNextEasyCoupon/src/NetiNextEasyCoupon.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCoupon;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\DBALException;
  6. use NetInventors\NetiNextEasyCoupon\Components\RuleBasedContainerFile;
  7. use NetInventors\NetiNextEasyCoupon\Components\RuleBasedContainerFileLoader;
  8. use NetInventors\NetiNextEasyCoupon\Components\Setup;
  9. use NetInventors\NetiNextEasyCoupon\Service\CheckService;
  10. use NetInventors\NetiNextEasyCoupon\Service\VoucherCodeGenerator\ValidatorPass as VoucherCodeGeneratorValidatorPass;
  11. use NetInventors\NetiNextEasyCoupon\Service\VoucherRedemption\ValidatorPass as VoucherRedemptionValidatorPass;
  12. use Shopware\Core\Framework\Plugin;
  13. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  16. use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
  17. use Symfony\Component\Config\FileLocator;
  18. use Symfony\Component\Config\Loader\DelegatingLoader;
  19. use Symfony\Component\Config\Loader\LoaderResolver;
  20. use Symfony\Component\DependencyInjection\ContainerBuilder;
  21. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  22. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  23. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  24. class NetiNextEasyCoupon extends Plugin
  25. {
  26.     public function build(ContainerBuilder $container): void
  27.     {
  28.         parent::build($container);
  29.         $container->addCompilerPass(new VoucherCodeGeneratorValidatorPass());
  30.         $container->addCompilerPass(new VoucherRedemptionValidatorPass());
  31.         $ruleBasedContainerFileLoader = new RuleBasedContainerFileLoader(
  32.             $container,
  33.             $this->getPath()
  34.         );
  35.         /** @var string */
  36.         $version $container->getParameter('kernel.shopware_version');
  37.         $ruleBasedContainerFileLoader->load(new RuleBasedContainerFile(
  38.             $this->getPath() . '/Resources/config/migrations/6.3.5.0/NEXT-12478-after.xml',
  39.             function () use ($version) {
  40.                 return \version_compare($version'6.3.5.0''>=')
  41.                     && \version_compare($version'6.4.3.0''<');
  42.             }
  43.         ));
  44.         $ruleBasedContainerFileLoader->load(new RuleBasedContainerFile(
  45.             $this->getPath() . '/Resources/config/migrations/6.4.3.0/NEXT-15687.xml',
  46.             function () use ($version) {
  47.                 return \version_compare($version'6.4.3.0''>=');
  48.             }
  49.         ));
  50.         $this->registerEnvBasedContainerFile($container);
  51.         $ruleBasedContainerFileLoader->load(new RuleBasedContainerFile(
  52.             $this->getPath() . '/Resources/config/services/flow.xml',
  53.             function () use ($version) {
  54.                 return \version_compare($versionCheckService::MINIMUM_FLOW_SHOPWARE_VERSION'>=');
  55.             }
  56.         ));
  57.     }
  58.     public function install(InstallContext $installContext): void
  59.     {
  60.         parent::install($installContext);
  61.         $setup = new Setup($this->container$installContext);
  62.         $setup->install();
  63.         $setup->installImportExportProfile($installContext->getContext());
  64.     }
  65.     /**
  66.      * @param UninstallContext $uninstallContext
  67.      *
  68.      * @throws DBALException
  69.      */
  70.     public function uninstall(UninstallContext $uninstallContext): void
  71.     {
  72.         parent::uninstall($uninstallContext);
  73.         if (false === $uninstallContext->keepUserData()) {
  74.             $connection $this->container->get(Connection::class);
  75.             if (!$connection instanceof Connection) {
  76.                 return;
  77.             }
  78.             $setup = new Setup($this->container$uninstallContext);
  79.             $setup->uninstall();
  80.         }
  81.     }
  82.     public function update(UpdateContext $updateContext): void
  83.     {
  84.         $setup = new Setup($this->container$updateContext);
  85.         $setup->installImportExportProfile($updateContext->getContext());
  86.         $setup->update();
  87.     }
  88.     private function registerEnvBasedContainerFile(ContainerBuilder $container): void
  89.     {
  90.         /**
  91.          * Suppress weird Psalm issue, that only appears in ci/cd pipelines.
  92.          *
  93.          * @psalm-suppress UndefinedDocblockClass
  94.          */
  95.         $env $container->getParameter('kernel.environment');
  96.         if (!\is_string($env) || '' === $env) {
  97.             return;
  98.         }
  99.         $fileLocator    = new FileLocator($this->getPath());
  100.         $loaderResolver = new LoaderResolver([
  101.             new XmlFileLoader($container$fileLocator),
  102.             new YamlFileLoader($container$fileLocator),
  103.             new PhpFileLoader($container$fileLocator),
  104.         ]);
  105.         $delegatingLoader = new DelegatingLoader($loaderResolver);
  106.         foreach (glob($this->getPath() . "/Resources/config/{$env}/*") as $path) {
  107.             try {
  108.                 $fileLocator->locate($path);
  109.                 $delegatingLoader->load($path);
  110.             } catch (FileLocatorFileNotFoundException $exception) {
  111.                 // Nothing to catch here
  112.             }
  113.         }
  114.     }
  115. }