custom/plugins/PickwareDhl/vendor/pickware/shipping-bundle/src/PickwareShippingBundle.php line 32

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\ShippingBundle;
  11. use Doctrine\DBAL\Connection;
  12. use Pickware\BundleInstaller\BundleInstaller;
  13. use Pickware\DalBundle\DalBundle;
  14. use Pickware\DocumentBundle\DocumentBundle;
  15. use Pickware\MoneyBundle\MoneyBundle;
  16. use Pickware\ShippingBundle\Carrier\CarrierAdapterRegistryCompilerPass;
  17. use Pickware\ShippingBundle\Installation\PickwareShippingBundleInstaller;
  18. use Pickware\ShopwareExtensionsBundle\PickwareShopwareExtensionsBundle;
  19. use Pickware\ShopwarePlugins\ShopwareIntegrationTestPlugin\ShopwareIntegrationTestPlugin;
  20. use Shopware\Core\Framework\Bundle;
  21. use Shopware\Core\Framework\Migration\MigrationSource;
  22. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  23. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  24. use Shopware\Core\Framework\Struct\Collection;
  25. use Symfony\Component\Config\FileLocator;
  26. use Symfony\Component\DependencyInjection\ContainerBuilder;
  27. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  28. class PickwareShippingBundle extends Bundle
  29. {
  30.     /**
  31.      * @var class-string<Bundle>[]
  32.      */
  33.     private const ADDITIONAL_BUNDLES = [
  34.         DalBundle::class,
  35.         DocumentBundle::class,
  36.         MoneyBundle::class,
  37.         PickwareShopwareExtensionsBundle::class,
  38.     ];
  39.     public const DOCUMENT_TYPE_TECHNICAL_NAME_DESCRIPTION_MAPPING = [
  40.         PickwareShippingBundle::DOCUMENT_TYPE_TECHNICAL_NAME_SHIPPING_LABEL => 'Versandetikett',
  41.         PickwareShippingBundle::DOCUMENT_TYPE_TECHNICAL_NAME_RETURN_LABEL => 'Retourenetikett',
  42.         PickwareShippingBundle::DOCUMENT_TYPE_TECHNICAL_NAME_CUSTOMS_DECLARATION_CN23 => 'Zollinhaltserklärung CN23',
  43.         PickwareShippingBundle::DOCUMENT_TYPE_TECHNICAL_NAME_STAMP => 'Briefmarke',
  44.     ];
  45.     public const DOCUMENT_TYPE_TECHNICAL_NAME_STAMP 'stamp';
  46.     public const DOCUMENT_TYPE_TECHNICAL_NAME_CUSTOMS_DECLARATION_CN23 'customs_declaration_cn23';
  47.     public const DOCUMENT_TYPE_TECHNICAL_NAME_SHIPPING_LABEL 'shipping_label';
  48.     public const DOCUMENT_TYPE_TECHNICAL_NAME_RETURN_LABEL 'return_label';
  49.     private static ?PickwareShippingBundle $instance null;
  50.     private static bool $registered false;
  51.     private static bool $migrationsRegistered false;
  52.     public static function register(Collection $bundleCollection): void
  53.     {
  54.         if (self::$registered) {
  55.             return;
  56.         }
  57.         $bundleCollection->add(self::getInstance());
  58.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  59.             $bundle::register($bundleCollection);
  60.         }
  61.         self::$registered true;
  62.     }
  63.     public static function registerMigrations(MigrationSource $migrationSource): void
  64.     {
  65.         if (self::$migrationsRegistered) {
  66.             return;
  67.         }
  68.         $migrationsPath self::getInstance()->getMigrationPath();
  69.         $migrationNamespace self::getInstance()->getMigrationNamespace();
  70.         $migrationSource->addDirectory($migrationsPath$migrationNamespace);
  71.         self::$migrationsRegistered true;
  72.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  73.             $bundle::registerMigrations($migrationSource);
  74.         }
  75.     }
  76.     public static function getInstance(): self
  77.     {
  78.         if (!self::$instance) {
  79.             self::$instance = new self();
  80.         }
  81.         return self::$instance;
  82.     }
  83.     public function build(ContainerBuilder $containerBuilder): void
  84.     {
  85.         parent::build($containerBuilder);
  86.         $loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
  87.         $loader->load('Carrier/DependencyInjection/model.xml');
  88.         $loader->load('Carrier/DependencyInjection/service.xml');
  89.         $loader->load('Carrier/DependencyInjection/subscriber.xml');
  90.         $loader->load('Config/DependencyInjection/command.xml');
  91.         $loader->load('Config/DependencyInjection/model.xml');
  92.         $loader->load('Config/DependencyInjection/service.xml');
  93.         $loader->load('DemodataGeneration/DependencyInjection/command.xml');
  94.         $loader->load('Logging/DependencyInjection/service.xml');
  95.         $loader->load('Mail/DependencyInjection/controller.xml');
  96.         $loader->load('Mail/DependencyInjection/service.xml');
  97.         $loader->load('Notifications/DependencyInjection/service.xml');
  98.         $loader->load('ParcelPacking/DependencyInjection/service.xml');
  99.         $loader->load('ParcelHydration/DependencyInjection/service.xml');
  100.         $loader->load('Shipment/DependencyInjection/controller.xml');
  101.         $loader->load('Shipment/DependencyInjection/model.xml');
  102.         $loader->load('Shipment/DependencyInjection/service.xml');
  103.         $containerBuilder->addCompilerPass(new CarrierAdapterRegistryCompilerPass());
  104.         // Register test services. Should never be loaded in production.
  105.         if (in_array(ShopwareIntegrationTestPlugin::class, $containerBuilder->getParameter('kernel.bundles'), true)) {
  106.             $loader->load('../test/TestEntityCreation/DependencyInjection/service.xml');
  107.         }
  108.     }
  109.     public function shutdown(): void
  110.     {
  111.         parent::shutdown();
  112.         // Shopware may reboot the kernel under certain circumstances (e.g. plugin un-/installation) within a single
  113.         // request. After the kernel was rebooted, our bundles have to be registered again.
  114.         // We reset the registration flag when the kernel is shut down. This will cause the bundles to be registered
  115.         // again in the (re)boot process.
  116.         self::$registered false;
  117.     }
  118.     public function install(InstallContext $installContext): void
  119.     {
  120.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  121.             ->install(self::ADDITIONAL_BUNDLES$installContext);
  122.     }
  123.     public function onAfterActivate(InstallContext $installContext): void
  124.     {
  125.         PickwareShippingBundleInstaller::createFromContainer($this->container)->install();
  126.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  127.             ->onAfterActivate(self::ADDITIONAL_BUNDLES$installContext);
  128.     }
  129.     public function uninstall(UninstallContext $uninstallContext): void
  130.     {
  131.         if ($uninstallContext->keepUserData()) {
  132.             return;
  133.         }
  134.         $db $this->container->get(Connection::class);
  135.         $db->executeStatement('
  136.             SET FOREIGN_KEY_CHECKS=0;
  137.             DROP TABLE IF EXISTS `pickware_shipping_carrier`;
  138.             DROP TABLE IF EXISTS `pickware_shipping_document_shipment_mapping`;
  139.             DROP TABLE IF EXISTS `pickware_shipping_document_tracking_code_mapping`;
  140.             DROP TABLE IF EXISTS `pickware_shipping_shipment`;
  141.             DROP TABLE IF EXISTS `pickware_shipping_shipment_order_mapping`;
  142.             DROP TABLE IF EXISTS `pickware_shipping_shipping_method_config`;
  143.             DROP TABLE IF EXISTS `pickware_shipping_tracking_code`;
  144.             SET FOREIGN_KEY_CHECKS=1;
  145.         ');
  146.         // We need eight backslashes, as we need to match a single one and double the count for each of the following:
  147.         // 1. The PHP parser
  148.         // 2. The MySQL parser
  149.         // 3. The MySQL pattern matcher (only when using LIKE)
  150.         $db->executeStatement("DELETE FROM `migration` WHERE `class` LIKE 'Pickware\\\\\\\\ShippingBundle\\\\\\\\%'");
  151.         PickwareShippingBundleInstaller::createFromContainer($this->container)->uninstall();
  152.         BundleInstaller::createForContainerAndClass($this->containerself::class)->uninstall($uninstallContext);
  153.     }
  154. }