custom/plugins/PickwareWms/src/PickwareWms.php line 42

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\BundleInstaller\BundleInstaller;
  13. use Pickware\DalBundle\DalBundle;
  14. use Pickware\DocumentBundle\DocumentBundle;
  15. use Pickware\MobileAppAuthBundle\PickwareMobileAppAuthBundle;
  16. use Pickware\PickwareWms\Installation\PickwareWmsInstaller;
  17. use Pickware\ShopwareExtensionsBundle\PickwareShopwareExtensionsBundle;
  18. use Pickware\ValidationBundle\PickwareValidationBundle;
  19. use Shopware\Core\Framework\Bundle;
  20. use Shopware\Core\Framework\Migration\MigrationCollectionLoader;
  21. use Shopware\Core\Framework\Migration\MigrationRuntime;
  22. use Shopware\Core\Framework\Migration\MigrationSource;
  23. use Shopware\Core\Framework\Parameter\AdditionalBundleParameters;
  24. use Shopware\Core\Framework\Plugin;
  25. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  26. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  27. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  28. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  29. use Shopware\Core\Framework\Struct\Collection;
  30. use Symfony\Bridge\Monolog\Logger;
  31. use Symfony\Component\Config\FileLocator;
  32. use Symfony\Component\DependencyInjection\ContainerBuilder;
  33. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  34. if (file_exists(__DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php')) {
  35.     require_once __DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php';
  36. }
  37. class PickwareWms extends Plugin
  38. {
  39.     public const GLOBAL_PLUGIN_CONFIG_DOMAIN 'PickwareWms.global-plugin-config';
  40.     /**
  41.      * Note that the PickwareShippingBundle is a composer dependency of this plugin. But it is not loaded (it is not
  42.      * part of this bundle list).
  43.      * This is because we have a conflict to older versions of the PickwareShippingBundle (conflict with version <1.5.0)
  44.      * which we cannot properly ensure in the Shopware plugin system. By shipping the code without loading it we ensure
  45.      * that, when a shipping adapter is loading the PickwareShippingBundle, this new version is loaded. See this issue
  46.      * for more details: https://github.com/pickware/shopware-plugins/issues/2755
  47.      *
  48.      * @var class-string<Bundle>[]
  49.      */
  50.     private const ADDITIONAL_BUNDLES = [
  51.         DalBundle::class,
  52.         DocumentBundle::class,
  53.         PickwareMobileAppAuthBundle::class,
  54.         PickwareShopwareExtensionsBundle::class,
  55.         PickwareValidationBundle::class,
  56.     ];
  57.     public function getAdditionalBundles(AdditionalBundleParameters $parameters): array
  58.     {
  59.         // Ensure the bundle classes can be loaded via auto-loading.
  60.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  61.             $kernelParameters $parameters->getKernelParameters();
  62.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded(
  63.                 $kernelParameters['kernel.plugin_infos'],
  64.                 $kernelParameters['kernel.project_dir'],
  65.             );
  66.         }
  67.         // For some reason Collection is abstract
  68.         // phpcs:ignore Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore -- PHP CS does not understand the PHP 7 syntax
  69.         $bundleCollection = new class() extends Collection {};
  70.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  71.             $bundle::register($bundleCollection);
  72.         }
  73.         return $bundleCollection->getElements();
  74.     }
  75.     public static function getDistPackages(): array
  76.     {
  77.         return include __DIR__ '/../Packages.php';
  78.     }
  79.     public function build(ContainerBuilder $containerBuilder): void
  80.     {
  81.         parent::build($containerBuilder);
  82.         $loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
  83.         $loader->load('Config/DependencyInjection/controller.xml');
  84.         $loader->load('Config/DependencyInjection/service.xml');
  85.         $loader->load('Installation/DependencyInjection/service.xml');
  86.         $loader->load('PickingProcess/DependencyInjection/controller.xml');
  87.         $loader->load('PickingProcess/DependencyInjection/model.xml');
  88.         $loader->load('PickingProcess/DependencyInjection/model-extension.xml');
  89.         $loader->load('PickingProcess/DependencyInjection/service.xml');
  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 postInstall(InstallContext $installContext): void
  99.     {
  100.         $installer PickwareWmsInstaller::initFromContainer($this->container);
  101.         $installer->postInstall();
  102.     }
  103.     public function update(UpdateContext $updateContext): void
  104.     {
  105.         $this->loadDependenciesForSetup();
  106.         $this->executeMigrationsOfBundles();
  107.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  108.             ->install(self::ADDITIONAL_BUNDLES$updateContext);
  109.     }
  110.     public function postUpdate(UpdateContext $updateContext): void
  111.     {
  112.         $installer PickwareWmsInstaller::initFromContainer($this->container);
  113.         $installer->postUpdate();
  114.         if ($updateContext->getPlugin()->isActive()) {
  115.             $this->container
  116.                 ->get('pickware_wms.bundle_supporting_asset_service')
  117.                 ->copyAssetsFromBundle('PickwareMobileAppAuthBundle');
  118.             BundleInstaller::createForContainerAndClass($this->containerself::class)
  119.                 ->onAfterActivate(self::ADDITIONAL_BUNDLES$updateContext);
  120.         }
  121.     }
  122.     private function executeMigrationsOfBundles(): void
  123.     {
  124.         // All the services required for migration execution are private in the DI-Container. As a workaround the
  125.         // services are instantiated explicitly here.
  126.         $db $this->container->get(Connection::class);
  127.         // See vendor/symfony/monolog-bundle/Resources/config/monolog.xml on how the logger is defined.
  128.         $logger = new Logger('app');
  129.         $logger->useMicrosecondTimestamps($this->container->getParameter('monolog.use_microseconds'));
  130.         $migrationCollectionLoader = new MigrationCollectionLoader($db, new MigrationRuntime($db$logger));
  131.         $migrationSource = new MigrationSource('PickwareWms');
  132.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  133.             $bundle::registerMigrations($migrationSource);
  134.         }
  135.         $migrationCollectionLoader->addSource($migrationSource);
  136.         foreach ($migrationCollectionLoader->collectAll() as $migrationCollection) {
  137.             $migrationCollection->sync();
  138.             $migrationCollection->migrateInPlace();
  139.         }
  140.     }
  141.     public function uninstall(UninstallContext $uninstallContext): void
  142.     {
  143.         parent::uninstall($uninstallContext);
  144.         if ($uninstallContext->keepUserData()) {
  145.             return;
  146.         }
  147.         $this->loadDependenciesForSetup();
  148.         $this->container->get(Connection::class)->exec('
  149.             SET FOREIGN_KEY_CHECKS = 0;
  150.             -- Migration1649747949AddPickingProcessSchema.php
  151.             DROP TABLE IF EXISTS `pickware_wms_picking_process`;
  152.             DROP TABLE IF EXISTS `pickware_wms_picking_process_tracking_code`;
  153.             DROP TABLE IF EXISTS `pickware_wms_picking_process_reserved_item`;
  154.             DROP TABLE IF EXISTS `pickware_wms_picking_process_order_document_mapping`;
  155.             DROP TABLE IF EXISTS `pickware_wms_picking_process_document_mapping`;
  156.             SET FOREIGN_KEY_CHECKS = 1;
  157.         ');
  158.         BundleInstaller::createForContainerAndClass($this->containerself::class)->uninstall($uninstallContext);
  159.     }
  160.     public function activate(ActivateContext $activateContext): void
  161.     {
  162.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  163.             ->onAfterActivate(self::ADDITIONAL_BUNDLES$activateContext);
  164.         $this->container
  165.             ->get('pickware_wms.bundle_supporting_asset_service')
  166.             ->copyAssetsFromBundle('PickwareMobileAppAuthBundle');
  167.     }
  168.     /**
  169.      * Run the dependency loader for a setup step like install/update/uninstall
  170.      *
  171.      * When executing one of these steps but no Pickware plugin is activated, the dependency loader did never run until
  172.      * the call of the corresponding method. You can trigger it with a call of this method.
  173.      */
  174.     private function loadDependenciesForSetup(): void
  175.     {
  176.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  177.             $plugins $this->container->get('kernel')->getPluginLoader()->getPluginInfos();
  178.             $projectDir $this->container->getParameter('kernel.project_dir');
  179.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded($plugins$projectDir);
  180.         }
  181.     }
  182. }