custom/plugins/PickwareDhl/src/PickwareDhl.php line 46

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\PickwareDhl;
  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\InstallationLibrary\DependencyAwareTableDropper;
  18. use Pickware\MoneyBundle\MoneyBundle;
  19. use Pickware\PickwareDhl\Config\DhlConfig;
  20. use Pickware\PickwareDhl\Installation\PickwareDhlInstaller;
  21. use Pickware\ShippingBundle\Carrier\CarrierAdapterRegistryCompilerPass;
  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 Symfony\Bridge\Monolog\Logger;
  35. use Symfony\Component\Config\FileLocator;
  36. use Symfony\Component\DependencyInjection\ContainerBuilder;
  37. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  38. if (file_exists(__DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php')) {
  39.     require_once __DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php';
  40. }
  41. class PickwareDhl extends Plugin
  42. {
  43.     /**
  44.      * @var class-string<Bundle>[]
  45.      */
  46.     private const ADDITIONAL_BUNDLES = [
  47.         DalBundle::class,
  48.         DocumentBundle::class,
  49.         MoneyBundle::class,
  50.         PickwareApiErrorHandlingBundle::class,
  51.         PickwareShippingBundle::class,
  52.         ShopwarePluginsDebugBundle::class,
  53.     ];
  54.     public const CARRIER_TECHNICAL_NAME_DHL 'dhl';
  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('ApiClient/DependencyInjection/service.xml');
  83.         $loader->load('Config/DependencyInjection/service.xml');
  84.         $loader->load('DhlBcpConfigScraper/DependencyInjection/command.xml');
  85.         $loader->load('PreferredDelivery/DependencyInjection/controller.xml');
  86.         $loader->load('PreferredDelivery/DependencyInjection/service.xml');
  87.         $loader->load('SalesChannelContext/DependencyInjection/model.xml');
  88.         $loader->load('SalesChannelContext/DependencyInjection/service.xml');
  89.         $loader->load('LocationFinder/DependencyInjection/controller.xml');
  90.         $loader->load('LocationFinder/DependencyInjection/service.xml');
  91.         $loader->load('Installation/DependencyInjection/service.xml');
  92.         $loader->load('ReturnLabel/DependencyInjection/service.xml');
  93.         $containerBuilder->addCompilerPass(new CarrierAdapterRegistryCompilerPass());
  94.     }
  95.     public function install(InstallContext $installContext): void
  96.     {
  97.         $this->loadDependenciesForSetup();
  98.         $this->executeMigrationsOfBundles();
  99.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  100.             ->install(self::ADDITIONAL_BUNDLES$installContext);
  101.     }
  102.     public function update(UpdateContext $updateContext): void
  103.     {
  104.         $this->loadDependenciesForSetup();
  105.         $this->executeMigrationsOfBundles();
  106.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  107.             ->install(self::ADDITIONAL_BUNDLES$updateContext);
  108.     }
  109.     private function executeMigrationsOfBundles(): void
  110.     {
  111.         // All the services required for migration execution are private in the DI-Container. As a workaround the
  112.         // services are instantiated explicitly here.
  113.         $db $this->container->get(Connection::class);
  114.         // See vendor/symfony/monolog-bundle/Resources/config/monolog.xml on how the logger is defined.
  115.         $logger = new Logger('app');
  116.         $logger->useMicrosecondTimestamps($this->container->getParameter('monolog.use_microseconds'));
  117.         $migrationCollectionLoader = new MigrationCollectionLoader($db, new MigrationRuntime($db$logger));
  118.         $migrationSource = new MigrationSource('PickwareDhl');
  119.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  120.             $bundle::registerMigrations($migrationSource);
  121.         }
  122.         $migrationCollectionLoader->addSource($migrationSource);
  123.         foreach ($migrationCollectionLoader->collectAll() as $migrationCollection) {
  124.             $migrationCollection->sync();
  125.             $migrationCollection->migrateInPlace();
  126.         }
  127.     }
  128.     public function postInstall(InstallContext $installContext): void
  129.     {
  130.         $installer PickwareDhlInstaller::initFromContainer($this->container);
  131.         $installer->postInstall($installContext->getContext());
  132.     }
  133.     public function postUpdate(UpdateContext $updateContext): void
  134.     {
  135.         $installer PickwareDhlInstaller::initFromContainer($this->container);
  136.         $installer->postUpdate($updateContext->getContext());
  137.         if ($updateContext->getPlugin()->isActive()) {
  138.             $this->container
  139.                 ->get('pickware_dhl.bundle_supporting_asset_service')
  140.                 ->copyAssetsFromBundle('PickwareShippingBundle');
  141.             $this->migrateDocumentsOfPluginFileSystemToDocumentBundleFileSystem();
  142.             BundleInstaller::createForContainerAndClass($this->containerself::class)
  143.                 ->onAfterActivate(self::ADDITIONAL_BUNDLES$updateContext);
  144.         }
  145.     }
  146.     public function uninstall(UninstallContext $uninstallContext): void
  147.     {
  148.         if ($uninstallContext->keepUserData()) {
  149.             return;
  150.         }
  151.         $this->loadDependenciesForSetup();
  152.         DependencyAwareTableDropper::createForContainer($this->container)->dropTables([
  153.             'pickware_dhl_sales_channel_api_context',
  154.             // These are actually only tables from old plugin versions. We still remove them here just in case.
  155.             'pickware_dhl_carrier',
  156.             'pickware_dhl_document',
  157.             'pickware_dhl_document_page_format',
  158.             'pickware_dhl_document_shipment_mapping',
  159.             'pickware_dhl_document_tracking_code_mapping',
  160.             'pickware_dhl_document_type',
  161.             'pickware_dhl_shipment',
  162.             'pickware_dhl_shipment_order_delivery_mapping',
  163.             'pickware_dhl_shipment_order_mapping',
  164.             'pickware_dhl_shipping_method_config',
  165.             'pickware_dhl_tracking_code',
  166.         ]);
  167.         $this->container->get(Connection::class)->executeStatement(
  168.             'DELETE FROM system_config
  169.             WHERE configuration_key LIKE :domain',
  170.             ['domain' => DhlConfig::CONFIG_DOMAIN '.%'],
  171.         );
  172.         PickwareDhlInstaller::initFromContainer($this->container)->uninstall($uninstallContext);
  173.         BundleInstaller::createForContainerAndClass($this->containerself::class)->uninstall($uninstallContext);
  174.     }
  175.     public function activate(ActivateContext $activateContext): void
  176.     {
  177.         $this->container->get('pickware_dhl.bundle_supporting_asset_service')->copyAssetsFromBundle('PickwareShippingBundle');
  178.         $this->migrateDocumentsOfPluginFileSystemToDocumentBundleFileSystem();
  179.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  180.             ->onAfterActivate(self::ADDITIONAL_BUNDLES$activateContext);
  181.     }
  182.     private function migrateDocumentsOfPluginFileSystemToDocumentBundleFileSystem(): void
  183.     {
  184.         $this->container->get(
  185.             'pickware_dhl.plugin_filesystem_to_document_bundle_filesystem_migrator',
  186.         )->moveDirectory('documents');
  187.     }
  188.     /**
  189.      * Run the dependency loader for a setup step like install/update/uninstall
  190.      *
  191.      * When executing one of these steps but no Pickware plugin is activated, the dependency loader did never run until
  192.      * the call of the corresponding method. You can trigger it with a call of this method.
  193.      */
  194.     private function loadDependenciesForSetup(): void
  195.     {
  196.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  197.             $plugins $this->container->get('kernel')->getPluginLoader()->getPluginInfos();
  198.             $projectDir $this->container->getParameter('kernel.project_dir');
  199.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded($plugins$projectDir);
  200.         }
  201.     }
  202. }