custom/plugins/NetiNextEasyCouponDesigns/src/NetiNextEasyCouponDesigns.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCouponDesigns;
  4. use Doctrine\DBAL\DBALException;
  5. use NetInventors\NetiNextEasyCouponDesigns\Components\RuleBasedContainerFile;
  6. use NetInventors\NetiNextEasyCouponDesigns\Components\RuleBasedContainerFileLoader;
  7. use NetInventors\NetiNextEasyCouponDesigns\Service\CheckService;
  8. use Shopware\Core\Framework\Parameter\AdditionalBundleParameters;
  9. use Shopware\Core\Framework\Plugin;
  10. use NetInventors\NetiNextEasyCouponDesigns\Components\Setup;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Throwable;
  16. use function is_file;
  17. use function version_compare;
  18. class NetiNextEasyCouponDesigns extends Plugin
  19. {
  20.     public const DOCUMENT_TYPE_ID 'B41C26FF07FA4F7A9D0E42A293A1CF0A';
  21.     public const DOCUMENT_TYPE    'easy_coupon';
  22.     private bool $autoloaderInjected false;
  23.     public function getAdditionalBundles(AdditionalBundleParameters $parameters): array
  24.     {
  25.         // This is not a nice solution, but there is no other way to inject the autoloader
  26.         if (!$this->autoloaderInjected) {
  27.             $this->injectAutoloader();
  28.             $this->autoloaderInjected true;
  29.         }
  30.         return parent::getAdditionalBundles($parameters);
  31.     }
  32.     public function build(ContainerBuilder $container): void
  33.     {
  34.         parent::build($container);
  35.         $ruleBasedContainerFileLoader = new RuleBasedContainerFileLoader(
  36.             $container,
  37.             $this->getPath()
  38.         );
  39.         /**
  40.          * @var string
  41.          *
  42.          * @psalm-suppress UndefinedDocblockClass
  43.          *                 Dockblock-defined type UnitEnum is available in PHP 8 >= 8.1.0
  44.          */
  45.         $version $container->getParameter('kernel.shopware_version');
  46.         $ruleBasedContainerFileLoader->load(
  47.             new RuleBasedContainerFile(
  48.                 $this->getPath() . '/Resources/config/services/flow.xml',
  49.                 function () use ($version) {
  50.                     return version_compare($versionCheckService::MINIMUM_FLOW_SHOPWARE_VERSION'>=');
  51.                 }
  52.             )
  53.         );
  54.     }
  55.     /**
  56.      * @throws Throwable
  57.      */
  58.     public function install(InstallContext $installContext): void
  59.     {
  60.         parent::install($installContext);
  61.         if (!$this->container instanceof ContainerInterface)  {
  62.             return;
  63.         }
  64.         (new Setup($installContext$this->container))->install();
  65.     }
  66.     /**
  67.      * @throws Throwable
  68.      */
  69.     public function update(Plugin\Context\UpdateContext $updateContext): void
  70.     {
  71.         parent::install($updateContext);
  72.         if (!$this->container instanceof ContainerInterface)  {
  73.             return;
  74.         }
  75.         (new Setup($updateContext$this->container))->update();
  76.     }
  77.     /**
  78.      * @throws DBALException
  79.      */
  80.     public function uninstall(UninstallContext $uninstallContext): void
  81.     {
  82.         parent::uninstall($uninstallContext);
  83.         if ($uninstallContext->keepUserData() || !$this->container instanceof ContainerInterface) {
  84.             return;
  85.         }
  86.         if (!$this->container instanceof ContainerInterface)  {
  87.             return;
  88.         }
  89.         (new Setup($uninstallContext$this->container))->uninstall();
  90.     }
  91.     private function injectAutoloader(): void
  92.     {
  93.         // For development
  94.         if (is_file(__DIR__ '/Resources/build/vendor/autoload.php')) {
  95.             require __DIR__ '/Resources/build/vendor/autoload.php';
  96.             return;
  97.         }
  98.         // For production
  99.         if (is_file(__DIR__ '/Resources/vendor/autoload.php')) {
  100.             require __DIR__ '/Resources/vendor/autoload.php';
  101.             return;
  102.         }
  103.     }
  104. }