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

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\MoneyBundle\MoneyBundle;
  18. use Pickware\PickwareDhl\Config\DhlConfig;
  19. use Pickware\PickwareDhl\Installation\PickwareDhlInstaller;
  20. use Pickware\ShippingBundle\Carrier\CarrierAdapterRegistryCompilerPass;
  21. use Pickware\ShippingBundle\PickwareShippingBundle;
  22. use Shopware\Core\Framework\Bundle;
  23. use Shopware\Core\Framework\Migration\MigrationCollectionLoader;
  24. use Shopware\Core\Framework\Migration\MigrationRuntime;
  25. use Shopware\Core\Framework\Migration\MigrationSource;
  26. use Shopware\Core\Framework\Parameter\AdditionalBundleParameters;
  27. use Shopware\Core\Framework\Plugin;
  28. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  29. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  30. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  31. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  32. use Shopware\Core\Framework\Struct\Collection;
  33. use Symfony\Bridge\Monolog\Logger;
  34. use Symfony\Component\Config\FileLocator;
  35. use Symfony\Component\DependencyInjection\ContainerBuilder;
  36. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  37. if (file_exists(__DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php')) {
  38.     require_once __DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php';
  39. }
  40. class PickwareDhl extends Plugin
  41. {
  42.     /**
  43.      * @var class-string<Bundle>[]
  44.      */
  45.     private const ADDITIONAL_BUNDLES = [
  46.         DalBundle::class,
  47.         DocumentBundle::class,
  48.         MoneyBundle::class,
  49.         PickwareApiErrorHandlingBundle::class,
  50.         PickwareShippingBundle::class,
  51.         ShopwarePluginsDebugBundle::class,
  52.     ];
  53.     public const CARRIER_TECHNICAL_NAME_DHL 'dhl';
  54.     public function getAdditionalBundles(AdditionalBundleParameters $parameters): array
  55.     {
  56.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  57.             $kernelParameters $parameters->getKernelParameters();
  58.             // Ensure the bundle classes can be loaded via auto-loading.
  59.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded(
  60.                 $kernelParameters['kernel.plugin_infos'],
  61.                 $kernelParameters['kernel.project_dir'],
  62.             );
  63.         }
  64.         // For some reason Collection is abstract
  65.         // phpcs:ignore Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore -- PHP CS does not understand the PHP 7 syntax
  66.         $bundleCollection = new class() extends Collection {};
  67.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  68.             $bundle::register($bundleCollection);
  69.         }
  70.         return $bundleCollection->getElements();
  71.     }
  72.     public static function getDistPackages(): array
  73.     {
  74.         return include __DIR__ '/../Packages.php';
  75.     }
  76.     public function build(ContainerBuilder $containerBuilder): void
  77.     {
  78.         parent::build($containerBuilder);
  79.         $loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
  80.         $loader->load('Adapter/DependencyInjection/service.xml');
  81.         $loader->load('ApiClient/DependencyInjection/service.xml');
  82.         $loader->load('Config/DependencyInjection/service.xml');
  83.         $loader->load('DhlBcpConfigScraper/DependencyInjection/command.xml');
  84.         $loader->load('PreferredDelivery/DependencyInjection/controller.xml');
  85.         $loader->load('PreferredDelivery/DependencyInjection/service.xml');
  86.         $loader->load('SalesChannelContext/DependencyInjection/model.xml');
  87.         $loader->load('SalesChannelContext/DependencyInjection/service.xml');
  88.         $loader->load('LocationFinder/DependencyInjection/controller.xml');
  89.         $loader->load('LocationFinder/DependencyInjection/service.xml');
  90.         $loader->load('Installation/DependencyInjection/service.xml');
  91.         $loader->load('ReturnLabel/DependencyInjection/service.xml');
  92.         $containerBuilder->addCompilerPass(new CarrierAdapterRegistryCompilerPass());
  93.     }
  94.     public function install(InstallContext $installContext): void
  95.     {
  96.         $this->loadDependenciesForSetup();
  97.         $this->executeMigrationsOfBundles();
  98.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  99.             ->install(self::ADDITIONAL_BUNDLES$installContext);
  100.     }
  101.     public function update(UpdateContext $updateContext): void
  102.     {
  103.         $this->loadDependenciesForSetup();
  104.         $this->executeMigrationsOfBundles();
  105.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  106.             ->install(self::ADDITIONAL_BUNDLES$updateContext);
  107.     }
  108.     private function executeMigrationsOfBundles(): void
  109.     {
  110.         // All the services required for migration execution are private in the DI-Container. As a workaround the
  111.         // services are instantiated explicitly here.
  112.         $db $this->container->get(Connection::class);
  113.         // See vendor/symfony/monolog-bundle/Resources/config/monolog.xml on how the logger is defined.
  114.         $logger = new Logger('app');
  115.         $logger->useMicrosecondTimestamps($this->container->getParameter('monolog.use_microseconds'));
  116.         $migrationCollectionLoader = new MigrationCollectionLoader($db, new MigrationRuntime($db$logger));
  117.         $migrationSource = new MigrationSource('PickwareDhl');
  118.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  119.             $bundle::registerMigrations($migrationSource);
  120.         }
  121.         $migrationCollectionLoader->addSource($migrationSource);
  122.         foreach ($migrationCollectionLoader->collectAll() as $migrationCollection) {
  123.             $migrationCollection->sync();
  124.             $migrationCollection->migrateInPlace();
  125.         }
  126.     }
  127.     public function postInstall(InstallContext $installContext): void
  128.     {
  129.         $installer PickwareDhlInstaller::initFromContainer($this->container);
  130.         $installer->postInstall($installContext->getContext());
  131.     }
  132.     public function postUpdate(UpdateContext $updateContext): void
  133.     {
  134.         $installer PickwareDhlInstaller::initFromContainer($this->container);
  135.         $installer->postUpdate($updateContext->getContext());
  136.         if ($updateContext->getPlugin()->isActive()) {
  137.             $this->container
  138.                 ->get('pickware_dhl.bundle_supporting_asset_service')
  139.                 ->copyAssetsFromBundle('PickwareShippingBundle');
  140.             $this->migrateDocumentsOfPluginFileSystemToDocumentBundleFileSystem();
  141.             BundleInstaller::createForContainerAndClass($this->containerself::class)
  142.                 ->onAfterActivate(self::ADDITIONAL_BUNDLES$updateContext);
  143.         }
  144.     }
  145.     public function uninstall(UninstallContext $uninstallContext): void
  146.     {
  147.         if ($uninstallContext->keepUserData()) {
  148.             return;
  149.         }
  150.         $this->loadDependenciesForSetup();
  151.         $db $this->container->get(Connection::class);
  152.         // These are actually only tables from old plugin versions. We still remove them here just in case.
  153.         $db->executeStatement('
  154.             SET FOREIGN_KEY_CHECKS=0;
  155.             DROP TABLE IF EXISTS `pickware_dhl_carrier`;
  156.             DROP TABLE IF EXISTS `pickware_dhl_document`;
  157.             DROP TABLE IF EXISTS `pickware_dhl_document_page_format`;
  158.             DROP TABLE IF EXISTS `pickware_dhl_document_shipment_mapping`;
  159.             DROP TABLE IF EXISTS `pickware_dhl_document_tracking_code_mapping`;
  160.             DROP TABLE IF EXISTS `pickware_dhl_document_type`;
  161.             DROP TABLE IF EXISTS `pickware_dhl_shipment`;
  162.             DROP TABLE IF EXISTS `pickware_dhl_shipment_order_delivery_mapping`;
  163.             DROP TABLE IF EXISTS `pickware_dhl_shipment_order_mapping`;
  164.             DROP TABLE IF EXISTS `pickware_dhl_shipping_method_config`;
  165.             DROP TABLE IF EXISTS `pickware_dhl_tracking_code`;
  166.             DROP TABLE IF EXISTS `pickware_dhl_sales_channel_api_context`;
  167.             SET FOREIGN_KEY_CHECKS=1;
  168.         ');
  169.         $db->executeStatement(
  170.             'DELETE FROM system_config
  171.             WHERE configuration_key LIKE :domain',
  172.             ['domain' => DhlConfig::CONFIG_DOMAIN '.%'],
  173.         );
  174.         PickwareDhlInstaller::initFromContainer($this->container)->uninstall($uninstallContext);
  175.         BundleInstaller::createForContainerAndClass($this->containerself::class)->uninstall($uninstallContext);
  176.     }
  177.     public function activate(ActivateContext $activateContext): void
  178.     {
  179.         $this->container->get('pickware_dhl.bundle_supporting_asset_service')->copyAssetsFromBundle('PickwareShippingBundle');
  180.         $this->migrateDocumentsOfPluginFileSystemToDocumentBundleFileSystem();
  181.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  182.             ->onAfterActivate(self::ADDITIONAL_BUNDLES$activateContext);
  183.     }
  184.     private function migrateDocumentsOfPluginFileSystemToDocumentBundleFileSystem(): void
  185.     {
  186.         $this->container->get(
  187.             'pickware_dhl.plugin_filesystem_to_document_bundle_filesystem_migrator',
  188.         )->moveDirectory('documents');
  189.     }
  190.     /**
  191.      * Run the dependency loader for a setup step like install/update/uninstall
  192.      *
  193.      * When executing one of these steps but no Pickware plugin is activated, the dependency loader did never run until
  194.      * the call of the corresponding method. You can trigger it with a call of this method.
  195.      */
  196.     private function loadDependenciesForSetup(): void
  197.     {
  198.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  199.             $plugins $this->container->get('kernel')->getPluginLoader()->getPluginInfos();
  200.             $projectDir $this->container->getParameter('kernel.project_dir');
  201.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded($plugins$projectDir);
  202.         }
  203.     }
  204. }