custom/plugins/PickwareWms/src/PickwareWms.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\PickwareWms;
  11. use Doctrine\DBAL\Connection;
  12. use Pickware\ApiErrorHandlingBundle\PickwareApiErrorHandlingBundle;
  13. use Pickware\ApiVersioningBundle\PickwareApiVersioningBundle;
  14. use Pickware\BundleInstaller\BundleInstaller;
  15. use Pickware\DalBundle\DalBundle;
  16. use Pickware\DocumentBundle\DocumentBundle;
  17. use Pickware\InstallationLibrary\DependencyAwareTableDropper;
  18. use Pickware\MobileAppAuthBundle\PickwareMobileAppAuthBundle;
  19. use Pickware\PickwareWms\Installation\PickwareWmsInstaller;
  20. use Pickware\ShopwareExtensionsBundle\PickwareShopwareExtensionsBundle;
  21. use Pickware\ShopwarePlugins\ShopwareIntegrationTestPlugin\ShopwareIntegrationTestPlugin;
  22. use Pickware\ValidationBundle\PickwareValidationBundle;
  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 PickwareWms extends Plugin
  42. {
  43.     public const GLOBAL_PLUGIN_CONFIG_DOMAIN 'PickwareWms.global-plugin-config';
  44.     /**
  45.      * Note that the PickwareShippingBundle is a composer dependency of this plugin. But it is not loaded (it is not
  46.      * part of this bundle list).
  47.      * This is because we have a conflict to older versions of the PickwareShippingBundle (conflict with version <1.5.0)
  48.      * which we cannot properly ensure in the Shopware plugin system. By shipping the code without loading it we ensure
  49.      * that, when a shipping adapter is loading the PickwareShippingBundle, this new version is loaded. See this issue
  50.      * for more details: https://github.com/pickware/shopware-plugins/issues/2755
  51.      *
  52.      * @var array<class-string<Bundle>>
  53.      */
  54.     private const ADDITIONAL_BUNDLES = [
  55.         DalBundle::class,
  56.         DocumentBundle::class,
  57.         PickwareApiErrorHandlingBundle::class,
  58.         PickwareApiVersioningBundle::class,
  59.         PickwareMobileAppAuthBundle::class,
  60.         PickwareShopwareExtensionsBundle::class,
  61.         PickwareValidationBundle::class,
  62.     ];
  63.     public function getAdditionalBundles(AdditionalBundleParameters $parameters): array
  64.     {
  65.         // Ensure the bundle classes can be loaded via auto-loading.
  66.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  67.             $kernelParameters $parameters->getKernelParameters();
  68.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded(
  69.                 $kernelParameters['kernel.plugin_infos'],
  70.                 $kernelParameters['kernel.project_dir'],
  71.             );
  72.         }
  73.         // For some reason Collection is abstract
  74.         // phpcs:ignore Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore -- PHP CS does not understand the PHP 7 syntax
  75.         $bundleCollection = new class() extends Collection {};
  76.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  77.             $bundle::register($bundleCollection);
  78.         }
  79.         return $bundleCollection->getElements();
  80.     }
  81.     public static function getDistPackages(): array
  82.     {
  83.         return include __DIR__ '/../Packages.php';
  84.     }
  85.     public function build(ContainerBuilder $containerBuilder): void
  86.     {
  87.         parent::build($containerBuilder);
  88.         $loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
  89.         $loader->load('Config/DependencyInjection/api-versioning.xml');
  90.         $loader->load('Config/DependencyInjection/controller.xml');
  91.         $loader->load('Config/DependencyInjection/service.xml');
  92.         $loader->load('Delivery/DependencyInjection/controller.xml');
  93.         $loader->load('Delivery/DependencyInjection/model.xml');
  94.         $loader->load('Delivery/DependencyInjection/model-extension.xml');
  95.         $loader->load('Delivery/DependencyInjection/service.xml');
  96.         $loader->load('DeliveryNote/DependencyInjection/service.xml');
  97.         $loader->load('DocumentPrintingConfig/DependencyInjection/model.xml');
  98.         $loader->load('DocumentPrintingConfig/DependencyInjection/model-extension.xml');
  99.         $loader->load('DemodataGeneration/DependencyInjection/command.xml');
  100.         $loader->load('Installation/DependencyInjection/service.xml');
  101.         $loader->load('OrderDocument/DependencyInjection/twig-extension.xml');
  102.         $loader->load('OrderStateValidation/DependencyInjection/service.xml');
  103.         $loader->load('PickingProcess/DependencyInjection/controller.xml');
  104.         $loader->load('PickingProcess/DependencyInjection/model.xml');
  105.         $loader->load('PickingProcess/DependencyInjection/model-extension.xml');
  106.         $loader->load('PickingProcess/DependencyInjection/service.xml');
  107.         $loader->load('StockContainerClearing/DependencyInjection/service.xml');
  108.         $loader->load('StockReservation/DependencyInjection/service.xml');
  109.         $loader->load('WmsAppVersionValidation/DependencyInjection/subscriber.xml');
  110.         // Register test services. Should never be loaded in production.
  111.         if (in_array(ShopwareIntegrationTestPlugin::class, $containerBuilder->getParameter('kernel.bundles'), true)) {
  112.             $loader->load('../test/TestEntityCreation/DependencyInjection/service.xml');
  113.         }
  114.     }
  115.     public function install(InstallContext $installContext): void
  116.     {
  117.         $this->loadDependenciesForSetup();
  118.         $this->executeMigrationsOfBundles();
  119.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  120.             ->install(self::ADDITIONAL_BUNDLES$installContext);
  121.     }
  122.     public function postInstall(InstallContext $installContext): void
  123.     {
  124.         PickwareWmsInstaller::initFromContainer($this->container)->postInstall();
  125.     }
  126.     public function update(UpdateContext $updateContext): void
  127.     {
  128.         $this->loadDependenciesForSetup();
  129.         $this->executeMigrationsOfBundles();
  130.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  131.             ->install(self::ADDITIONAL_BUNDLES$updateContext);
  132.     }
  133.     public function postUpdate(UpdateContext $updateContext): void
  134.     {
  135.         PickwareWmsInstaller::initFromContainer($this->container)->postUpdate($updateContext);
  136.         if ($updateContext->getPlugin()->isActive()) {
  137.             $this->container
  138.                 ->get('pickware_wms.bundle_supporting_asset_service')
  139.                 ->copyAssetsFromBundle('PickwareMobileAppAuthBundle');
  140.             BundleInstaller::createForContainerAndClass($this->containerself::class)
  141.                 ->onAfterActivate(self::ADDITIONAL_BUNDLES$updateContext);
  142.         }
  143.     }
  144.     private function executeMigrationsOfBundles(): void
  145.     {
  146.         // All the services required for migration execution are private in the DI-Container. As a workaround the
  147.         // services are instantiated explicitly here.
  148.         $db $this->container->get(Connection::class);
  149.         // See vendor/symfony/monolog-bundle/Resources/config/monolog.xml on how the logger is defined.
  150.         $logger = new Logger('app');
  151.         $logger->useMicrosecondTimestamps($this->container->getParameter('monolog.use_microseconds'));
  152.         $migrationCollectionLoader = new MigrationCollectionLoader($db, new MigrationRuntime($db$logger));
  153.         $migrationSource = new MigrationSource('PickwareWms');
  154.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  155.             $bundle::registerMigrations($migrationSource);
  156.         }
  157.         $migrationCollectionLoader->addSource($migrationSource);
  158.         foreach ($migrationCollectionLoader->collectAll() as $migrationCollection) {
  159.             $migrationCollection->sync();
  160.             $migrationCollection->migrateInPlace();
  161.         }
  162.     }
  163.     public function uninstall(UninstallContext $uninstallContext): void
  164.     {
  165.         parent::uninstall($uninstallContext);
  166.         if ($uninstallContext->keepUserData()) {
  167.             return;
  168.         }
  169.         $this->loadDependenciesForSetup();
  170.         DependencyAwareTableDropper::createForContainer($this->container)->dropTables([
  171.             'pickware_wms_picking_process',
  172.             'pickware_wms_picking_process_tracking_code',
  173.             'pickware_wms_picking_process_reserved_item',
  174.             'pickware_wms_picking_process_order_document_mapping',
  175.             'pickware_wms_picking_process_document_mapping',
  176.             'pickware_wms_delivery',
  177.             'pickware_wms_delivery_tracking_code',
  178.             'pickware_wms_delivery_order_document_mapping',
  179.             'pickware_wms_delivery_document_mapping',
  180.             'pickware_wms_delivery_line_item',
  181.         ]);
  182.         PickwareWmsInstaller::initFromContainer($this->container)->uninstall($uninstallContext);
  183.         BundleInstaller::createForContainerAndClass($this->containerself::class)->uninstall($uninstallContext);
  184.     }
  185.     public function activate(ActivateContext $activateContext): void
  186.     {
  187.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  188.             ->onAfterActivate(self::ADDITIONAL_BUNDLES$activateContext);
  189.         $this->container
  190.             ->get('pickware_wms.bundle_supporting_asset_service')
  191.             ->copyAssetsFromBundle('PickwareMobileAppAuthBundle');
  192.     }
  193.     /**
  194.      * Run the dependency loader for a setup step like install/update/uninstall
  195.      *
  196.      * When executing one of these steps but no Pickware plugin is activated, the dependency loader did never run until
  197.      * the call of the corresponding method. You can trigger it with a call of this method.
  198.      */
  199.     private function loadDependenciesForSetup(): void
  200.     {
  201.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  202.             $plugins $this->container->get('kernel')->getPluginLoader()->getPluginInfos();
  203.             $projectDir $this->container->getParameter('kernel.project_dir');
  204.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded($plugins$projectDir);
  205.         }
  206.     }
  207. }