custom/plugins/BilliePaymentSW6/src/BilliePaymentSW6.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (c) Billie 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 Billie\BilliePayment;
  10. use Billie\BilliePayment\Bootstrap\AbstractBootstrap;
  11. use Billie\BilliePayment\Bootstrap\Database;
  12. use Billie\BilliePayment\Bootstrap\PaymentMethods;
  13. use Billie\BilliePayment\Bootstrap\PluginConfig;
  14. use Billie\Sdk\HttpClient\BillieClient;
  15. use Exception;
  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. class BilliePaymentSW6 extends Plugin
  26. {
  27.     public function install(InstallContext $installContext): void
  28.     {
  29.         $bootstrapper $this->getBootstrapClasses($installContext);
  30.         foreach ($bootstrapper as $bootstrap) {
  31.             $bootstrap->preInstall();
  32.         }
  33.         foreach ($bootstrapper as $bootstrap) {
  34.             $bootstrap->install();
  35.         }
  36.         foreach ($bootstrapper as $bootstrap) {
  37.             $bootstrap->postInstall();
  38.         }
  39.     }
  40.     public function update(UpdateContext $updateContext): void
  41.     {
  42.         $bootstrapper $this->getBootstrapClasses($updateContext);
  43.         foreach ($bootstrapper as $bootstrap) {
  44.             $bootstrap->preUpdate();
  45.         }
  46.         foreach ($bootstrapper as $bootstrap) {
  47.             $bootstrap->update();
  48.         }
  49.         foreach ($bootstrapper as $bootstrap) {
  50.             $bootstrap->postUpdate();
  51.         }
  52.     }
  53.     public function uninstall(UninstallContext $uninstallContext): void
  54.     {
  55.         $bootstrapper $this->getBootstrapClasses($uninstallContext);
  56.         foreach ($bootstrapper as $bootstrap) {
  57.             $bootstrap->preUninstall();
  58.         }
  59.         foreach ($bootstrapper as $bootstrap) {
  60.             $bootstrap->uninstall($uninstallContext->keepUserData());
  61.         }
  62.         foreach ($bootstrapper as $bootstrap) {
  63.             $bootstrap->postUninstall();
  64.         }
  65.     }
  66.     public function deactivate(DeactivateContext $deactivateContext): void
  67.     {
  68.         $bootstrapper $this->getBootstrapClasses($deactivateContext);
  69.         foreach ($bootstrapper as $bootstrap) {
  70.             $bootstrap->preDeactivate();
  71.         }
  72.         foreach ($bootstrapper as $bootstrap) {
  73.             $bootstrap->deactivate();
  74.         }
  75.         foreach ($bootstrapper as $bootstrap) {
  76.             $bootstrap->postDeactivate();
  77.         }
  78.     }
  79.     public function activate(ActivateContext $activateContext): void
  80.     {
  81.         $bootstrapper $this->getBootstrapClasses($activateContext);
  82.         foreach ($bootstrapper as $bootstrap) {
  83.             $bootstrap->preActivate();
  84.         }
  85.         foreach ($bootstrapper as $bootstrap) {
  86.             $bootstrap->activate();
  87.         }
  88.         foreach ($bootstrapper as $bootstrap) {
  89.             $bootstrap->postActivate();
  90.         }
  91.     }
  92.     public function executeComposerCommands(): bool
  93.     {
  94.         return false// shopware sw < 6.5 only supports this by using a feature flag. So we disable this and still using require_once at the end of the plugin-file
  95.     }
  96.     /**
  97.      * @return AbstractBootstrap[]
  98.      */
  99.     protected function getBootstrapClasses(InstallContext $context): array
  100.     {
  101.         /** @var AbstractBootstrap[] $bootstrapper */
  102.         $bootstrapper = [
  103.             new Database(),
  104.             new PaymentMethods(),
  105.             new PluginConfig(),
  106.         ];
  107.         /** @var EntityRepository $pluginRepository */
  108.         $pluginRepository $this->container->get('plugin.repository');
  109.         $plugins $pluginRepository->search(
  110.             (new Criteria())->addFilter(new EqualsFilter('baseClass', static::class)),
  111.             $context->getContext()
  112.         );
  113.         $plugin $plugins->first();
  114.         // $logger = new FileLogger($this->container->getParameter('kernel.logs_dir'));
  115.         foreach ($bootstrapper as $bootstrap) {
  116.             $bootstrap->setInstallContext($context);
  117.             // $bootstrap->setLogger($logger);
  118.             $bootstrap->setContainer($this->container);
  119.             $bootstrap->injectServices();
  120.             $bootstrap->setPlugin($plugin);
  121.         }
  122.         return $bootstrapper;
  123.     }
  124. }
  125. if (!class_exists(BillieClient::class)) {
  126.     $autoloaderPath dirname(__DIR__) . '/vendor/autoload.php';
  127.     if (file_exists($autoloaderPath)) {
  128.         require_once $autoloaderPath;
  129.     } else {
  130.         throw new Exception('Missing Billie dependencies! Please run `composer require billie/shopware6-module` in project directory');
  131.     }
  132. }