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

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