custom/plugins/RpayPayments/src/RpayPayments.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (c) Ratepay GmbH
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace Ratepay\RpayPayments;
  10. use Exception;
  11. use RatePAY\RequestBuilder;
  12. use Ratepay\RpayPayments\Bootstrap\AbstractBootstrap;
  13. use Ratepay\RpayPayments\Bootstrap\Database;
  14. use Ratepay\RpayPayments\Bootstrap\PaymentMethods;
  15. use Shopware\Core\Framework\Context;
  16. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  19. use Shopware\Core\Framework\Plugin;
  20. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  21. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  22. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  23. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  24. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  25. use Shopware\Core\Kernel;
  26. use Symfony\Component\Config\FileLocator;
  27. use Symfony\Component\DependencyInjection\ContainerBuilder;
  28. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  29. class RpayPayments extends Plugin
  30. {
  31.     public function install(InstallContext $context): void
  32.     {
  33.         $bootstrapper $this->getBootstrapClasses($context);
  34.         foreach ($bootstrapper as $bootstrap) {
  35.             $bootstrap->preInstall();
  36.         }
  37.         foreach ($bootstrapper as $bootstrap) {
  38.             $bootstrap->install();
  39.         }
  40.         foreach ($bootstrapper as $bootstrap) {
  41.             $bootstrap->postInstall();
  42.         }
  43.     }
  44.     public function update(UpdateContext $context): void
  45.     {
  46.         $bootstrapper $this->getBootstrapClasses($context);
  47.         foreach ($bootstrapper as $bootstrap) {
  48.             $bootstrap->preUpdate();
  49.         }
  50.         foreach ($bootstrapper as $bootstrap) {
  51.             $bootstrap->update();
  52.         }
  53.         foreach ($bootstrapper as $bootstrap) {
  54.             $bootstrap->postUpdate();
  55.         }
  56.     }
  57.     public function uninstall(UninstallContext $context): void
  58.     {
  59.         $bootstrapper $this->getBootstrapClasses($context);
  60.         foreach ($bootstrapper as $bootstrap) {
  61.             $bootstrap->preUninstall();
  62.         }
  63.         foreach ($bootstrapper as $bootstrap) {
  64.             $bootstrap->uninstall($context->keepUserData());
  65.         }
  66.         foreach ($bootstrapper as $bootstrap) {
  67.             $bootstrap->postUninstall();
  68.         }
  69.     }
  70.     public function deactivate(DeactivateContext $context): void
  71.     {
  72.         $bootstrapper $this->getBootstrapClasses($context);
  73.         foreach ($bootstrapper as $bootstrap) {
  74.             $bootstrap->preDeactivate();
  75.         }
  76.         foreach ($bootstrapper as $bootstrap) {
  77.             $bootstrap->deactivate();
  78.         }
  79.         foreach ($bootstrapper as $bootstrap) {
  80.             $bootstrap->postDeactivate();
  81.         }
  82.     }
  83.     public function activate(ActivateContext $context): void
  84.     {
  85.         $bootstrapper $this->getBootstrapClasses($context);
  86.         foreach ($bootstrapper as $bootstrap) {
  87.             $bootstrap->preActivate();
  88.         }
  89.         foreach ($bootstrapper as $bootstrap) {
  90.             $bootstrap->activate();
  91.         }
  92.         foreach ($bootstrapper as $bootstrap) {
  93.             $bootstrap->postActivate();
  94.         }
  95.     }
  96.     public function boot(): void
  97.     {
  98.         parent::boot();
  99.         if (!class_exists(RequestBuilder::class)) {
  100.             $autoloaderPath dirname(__DIR__) . '/vendor/autoload.php';
  101.             if (file_exists($autoloaderPath)) {
  102.                 /** @noinspection PhpIncludeInspection */
  103.                 require_once $autoloaderPath;
  104.             } else {
  105.                 throw new Exception('Missing Ratepay dependencies! Please run `composer require ratepay/shopware6-module` in project directory');
  106.             }
  107.         }
  108.     }
  109.     public function build(ContainerBuilder $containerBuilder): void
  110.     {
  111.         parent::build($containerBuilder);
  112.         $componentContainerFiles = [
  113.             'services.xml',
  114.             'models.xml',
  115.             'controller.xml',
  116.             'subscriber.xml',
  117.         ];
  118.         $loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
  119.         foreach (array_filter(glob(__DIR__ '/Components/*'), 'is_dir') as $dir) {
  120.             foreach ($componentContainerFiles as $fileName) {
  121.                 $file $dir '/DependencyInjection/' $fileName;
  122.                 if (file_exists($file)) {
  123.                     $loader->load($file);
  124.                 }
  125.             }
  126.         }
  127.         $containerBuilder->addCompilerPass(new PluginVersionCompilerPass(__DIR__ '/../'));
  128.     }
  129.     /**
  130.      * @return AbstractBootstrap[]
  131.      */
  132.     protected function getBootstrapClasses(InstallContext $context): array
  133.     {
  134.         /** @var AbstractBootstrap[] $bootstrapper */
  135.         $bootstrapper = [
  136.             new Database(),
  137.             new PaymentMethods(),
  138.         ];
  139.         /** @var EntityRepository $pluginRepository */
  140.         $pluginRepository $this->container->get('plugin.repository');
  141.         $plugins $pluginRepository->search((new Criteria())->addFilter(new EqualsFilter('baseClass', static::class)), Context::createDefaultContext());
  142.         $plugin $plugins->first();
  143.         // $logger = new FileLogger($this->container->getParameter('kernel.logs_dir'));
  144.         foreach ($bootstrapper as $bootstrap) {
  145.             $bootstrap->setInstallContext($context);
  146.             // $bootstrap->setLogger($logger);
  147.             $bootstrap->setContainer($this->container);
  148.             $bootstrap->injectServices();
  149.             $bootstrap->setPlugin($plugin);
  150.         }
  151.         return $bootstrapper;
  152.     }
  153. }