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

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