custom/plugins/PickwareGls/src/PickwareGls.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\PickwareGls;
  11. use Doctrine\DBAL\Connection;
  12. use Pickware\ApiErrorHandlingBundle\PickwareApiErrorHandlingBundle;
  13. use Pickware\BundleInstaller\BundleInstaller;
  14. use Pickware\DalBundle\DalBundle;
  15. use Pickware\DebugBundle\ShopwarePluginsDebugBundle;
  16. use Pickware\DocumentBundle\DocumentBundle;
  17. use Pickware\PickwareGls\Config\GlsConfig;
  18. use Pickware\PickwareGls\Installation\GlsCarrier;
  19. use Pickware\ShippingBundle\Config\ConfigService;
  20. use Pickware\ShippingBundle\Installation\CarrierInstaller;
  21. use Pickware\ShippingBundle\Installation\CarrierUninstaller;
  22. use Pickware\ShippingBundle\PickwareShippingBundle;
  23. use Shopware\Core\Framework\Bundle;
  24. use Shopware\Core\Framework\Migration\MigrationCollectionLoader;
  25. use Shopware\Core\Framework\Migration\MigrationRuntime;
  26. use Shopware\Core\Framework\Migration\MigrationSource;
  27. use Shopware\Core\Framework\Parameter\AdditionalBundleParameters;
  28. use Shopware\Core\Framework\Plugin;
  29. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  30. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  31. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  32. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  33. use Shopware\Core\Framework\Struct\Collection;
  34. use Shopware\Core\System\SystemConfig\SystemConfigService;
  35. use Symfony\Bridge\Monolog\Logger;
  36. use Symfony\Component\Config\FileLocator;
  37. use Symfony\Component\DependencyInjection\ContainerBuilder;
  38. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  39. if (file_exists(__DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php')) {
  40.     require_once __DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php';
  41. }
  42. class PickwareGls extends Plugin
  43. {
  44.     /**
  45.      * @var class-string<Bundle>[]
  46.      */
  47.     private const ADDITIONAL_BUNDLES = [
  48.         DalBundle::class,
  49.         DocumentBundle::class,
  50.         PickwareApiErrorHandlingBundle::class,
  51.         PickwareShippingBundle::class,
  52.         ShopwarePluginsDebugBundle::class,
  53.     ];
  54.     public const CARRIER_TECHNICAL_NAME_GLS 'gls';
  55.     public function getAdditionalBundles(AdditionalBundleParameters $parameters): array
  56.     {
  57.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  58.             $kernelParameters $parameters->getKernelParameters();
  59.             // Ensure the bundle classes can be loaded via auto-loading.
  60.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded(
  61.                 $kernelParameters['kernel.plugin_infos'],
  62.                 $kernelParameters['kernel.project_dir'],
  63.             );
  64.         }
  65.         // For some reason Collection is abstract
  66.         // phpcs:ignore Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore -- PHP CS does not understand the PHP 7 syntax
  67.         $bundleCollection = new class() extends Collection {};
  68.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  69.             $bundle::register($bundleCollection);
  70.         }
  71.         return $bundleCollection->getElements();
  72.     }
  73.     public static function getDistPackages(): array
  74.     {
  75.         return include __DIR__ '/../Packages.php';
  76.     }
  77.     public function build(ContainerBuilder $containerBuilder): void
  78.     {
  79.         parent::build($containerBuilder);
  80.         $loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
  81.         $loader->load('Adapter/DependencyInjection/service.xml');
  82.         $loader->load('Api/DependencyInjection/controller.xml');
  83.         $loader->load('Api/DependencyInjection/service.xml');
  84.         $loader->load('Installation/DependencyInjection/service.xml');
  85.     }
  86.     public function install(InstallContext $installContext): void
  87.     {
  88.         $this->loadDependenciesForSetup();
  89.         $this->executeMigrationsOfBundles();
  90.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  91.             ->install(self::ADDITIONAL_BUNDLES$installContext);
  92.     }
  93.     public function update(UpdateContext $updateContext): void
  94.     {
  95.         $this->loadDependenciesForSetup();
  96.         $this->executeMigrationsOfBundles();
  97.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  98.             ->install(self::ADDITIONAL_BUNDLES$updateContext);
  99.     }
  100.     private function executeMigrationsOfBundles(): void
  101.     {
  102.         // All the services required for migration execution are private in the DI-Container. As a workaround the
  103.         // services are instantiated explicitly here.
  104.         $db $this->container->get(Connection::class);
  105.         // See vendor/symfony/monolog-bundle/Resources/config/monolog.xml on how the logger is defined.
  106.         $logger = new Logger('app');
  107.         $logger->useMicrosecondTimestamps($this->container->getParameter('monolog.use_microseconds'));
  108.         $migrationCollectionLoader = new MigrationCollectionLoader($db, new MigrationRuntime($db$logger));
  109.         $migrationSource = new MigrationSource('PickwareGls');
  110.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  111.             $bundle::registerMigrations($migrationSource);
  112.         }
  113.         $migrationCollectionLoader->addSource($migrationSource);
  114.         foreach ($migrationCollectionLoader->collectAll() as $migrationCollection) {
  115.             $migrationCollection->sync();
  116.             $migrationCollection->migrateInPlace();
  117.         }
  118.     }
  119.     public function postInstall(InstallContext $installContext): void
  120.     {
  121.         $this->upsertCarrier($this->container->get(Connection::class));
  122.         $this->upsertDefaultConfiguration();
  123.     }
  124.     public function postUpdate(UpdateContext $updateContext): void
  125.     {
  126.         $this->upsertCarrier($this->container->get(Connection::class));
  127.         $this->upsertDefaultConfiguration();
  128.         if ($updateContext->getPlugin()->isActive()) {
  129.             $this->container->get('pickware_gls.bundle_supporting_asset_service')->copyAssetsFromBundle('PickwareShippingBundle');
  130.             BundleInstaller::createForContainerAndClass($this->containerself::class)
  131.                 ->onAfterActivate(self::ADDITIONAL_BUNDLES$updateContext);
  132.         }
  133.     }
  134.     public function uninstall(UninstallContext $uninstallContext): void
  135.     {
  136.         if ($uninstallContext->keepUserData()) {
  137.             return;
  138.         }
  139.         $this->loadDependenciesForSetup();
  140.         $db $this->container->get(Connection::class);
  141.         $db->executeStatement(
  142.             'DELETE FROM system_config
  143.             WHERE configuration_key LIKE :domain',
  144.             [
  145.                 'domain' => GlsConfig::CONFIG_DOMAIN '.%',
  146.             ],
  147.         );
  148.         CarrierUninstaller::createForContainer($this->container)->uninstallCarrier(GlsCarrier::TECHNICAL_NAME);
  149.         BundleInstaller::createForContainerAndClass($this->containerself::class)->uninstall($uninstallContext);
  150.     }
  151.     public function activate(ActivateContext $activateContext): void
  152.     {
  153.         $this->container->get('pickware_gls.bundle_supporting_asset_service')->copyAssetsFromBundle('PickwareShippingBundle');
  154.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  155.             ->onAfterActivate(self::ADDITIONAL_BUNDLES$activateContext);
  156.     }
  157.     /**
  158.      * Run the dependency loader for a setup step like install/update/uninstall
  159.      *
  160.      * When executing one of this steps but no Pickware plugin is activated, the dependency loader did never run until
  161.      * the call of the corresponding method. You can trigger it with a call of this method.
  162.      */
  163.     private function loadDependenciesForSetup(): void
  164.     {
  165.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  166.             $plugins $this->container->get('kernel')->getPluginLoader()->getPluginInfos();
  167.             $projectDir $this->container->getParameter('kernel.project_dir');
  168.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded($plugins$projectDir);
  169.         }
  170.     }
  171.     private function upsertCarrier(Connection $db): void
  172.     {
  173.         $carrierInstaller = new CarrierInstaller($db);
  174.         $carrierInstaller->installCarrier(new GlsCarrier());
  175.     }
  176.     private function upsertDefaultConfiguration(): void
  177.     {
  178.         $systemConfigService $this->container->get(SystemConfigService::class);
  179.         $shippingConfigService = new ConfigService($systemConfigService);
  180.         $currentConfig $shippingConfigService->getConfigForSalesChannel(GlsConfig::CONFIG_DOMAINnull);
  181.         $defaultConfig GlsConfig::createDefault();
  182.         $defaultConfig->apply(new GlsConfig($currentConfig));
  183.         $shippingConfigService->saveConfigForSalesChannel($defaultConfig->getConfig(), null);
  184.     }
  185. }