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

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