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

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