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

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