custom/plugins/PickwareErpPro/src/PickwareErpPro.php line 38

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\PickwareErpPro;
  11. use Doctrine\DBAL\Connection;
  12. use Pickware\BundleInstaller\BundleInstaller;
  13. use Pickware\PickwareDhlExpressBundle\PickwareDhlExpressBundle;
  14. use Pickware\PickwareUpsBundle\PickwareUpsBundle;
  15. use Shopware\Core\Framework\Bundle;
  16. use Shopware\Core\Framework\Migration\MigrationCollectionLoader;
  17. use Shopware\Core\Framework\Migration\MigrationRuntime;
  18. use Shopware\Core\Framework\Migration\MigrationSource;
  19. use Shopware\Core\Framework\Parameter\AdditionalBundleParameters;
  20. use Shopware\Core\Framework\Plugin;
  21. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  22. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  23. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  24. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  25. use Shopware\Core\Framework\Struct\Collection;
  26. use Symfony\Bridge\Monolog\Logger;
  27. use Symfony\Component\Config\FileLocator;
  28. use Symfony\Component\DependencyInjection\ContainerBuilder;
  29. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  30. if (file_exists(__DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php')) {
  31.     require_once __DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php';
  32. }
  33. class PickwareErpPro extends Plugin
  34. {
  35.     /**
  36.      * @var class-string<Bundle>[]
  37.      */
  38.     private const ADDITIONAL_BUNDLES = [
  39.         PickwareUpsBundle::class,
  40.         PickwareDhlExpressBundle::class,
  41.     ];
  42.     public function getAdditionalBundles(AdditionalBundleParameters $parameters): array
  43.     {
  44.         // Ensure the bundle classes can be loaded via auto-loading.
  45.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  46.             $kernelParameters $parameters->getKernelParameters();
  47.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded(
  48.                 $kernelParameters['kernel.plugin_infos'],
  49.                 $kernelParameters['kernel.project_dir'],
  50.             );
  51.         }
  52.         // For some reason Collection is abstract
  53.         // phpcs:ignore Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore -- PHP CS does not understand the PHP 7 syntax
  54.         $bundleCollection = new class() extends Collection {};
  55.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  56.             $bundle::register($bundleCollection);
  57.         }
  58.         return $bundleCollection->getElements();
  59.     }
  60.     public static function getDistPackages(): array
  61.     {
  62.         return include __DIR__ '/../Packages.php';
  63.     }
  64.     public function build(ContainerBuilder $containerBuilder): void
  65.     {
  66.         parent::build($containerBuilder);
  67.         $loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
  68.         $loader->load('Installation/DependencyInjection/service.xml');
  69.     }
  70.     public function install(InstallContext $installContext): void
  71.     {
  72.         $this->loadDependenciesForSetup();
  73.         $this->executeMigrationsOfBundles();
  74.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  75.             ->install(self::ADDITIONAL_BUNDLES$installContext);
  76.     }
  77.     public function update(UpdateContext $updateContext): void
  78.     {
  79.         $this->loadDependenciesForSetup();
  80.         $this->executeMigrationsOfBundles();
  81.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  82.             ->install(self::ADDITIONAL_BUNDLES$updateContext);
  83.     }
  84.     public function postUpdate(UpdateContext $updateContext): void
  85.     {
  86.         if ($updateContext->getPlugin()->isActive()) {
  87.             $this->container
  88.                 ->get('pickware_erp_pro.bundle_supporting_asset_service')
  89.                 ->copyAssetsFromBundle('PickwareShippingBundle')
  90.                 ->copyAssetsFromBundle('PickwareUpsBundle')
  91.                 ->copyAssetsFromBundle('PickwareDhlExpressBundle');
  92.             BundleInstaller::createForContainerAndClass($this->containerself::class)
  93.                 ->onAfterActivate(self::ADDITIONAL_BUNDLES$updateContext);
  94.         }
  95.     }
  96.     public function activate(ActivateContext $activateContext): void
  97.     {
  98.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  99.             ->onAfterActivate(self::ADDITIONAL_BUNDLES$activateContext);
  100.         $this->container
  101.             ->get('pickware_erp_pro.bundle_supporting_asset_service')
  102.             ->copyAssetsFromBundle('PickwareShippingBundle')
  103.             ->copyAssetsFromBundle('PickwareUpsBundle')
  104.             ->copyAssetsFromBundle('PickwareDhlExpressBundle');
  105.     }
  106.     public function uninstall(UninstallContext $uninstallContext): void
  107.     {
  108.         parent::uninstall($uninstallContext);
  109.         if ($uninstallContext->keepUserData()) {
  110.             return;
  111.         }
  112.         $this->loadDependenciesForSetup();
  113.         BundleInstaller::createForContainerAndClass($this->containerself::class)->uninstall($uninstallContext);
  114.     }
  115.     private function executeMigrationsOfBundles(): void
  116.     {
  117.         // All the services required for migration execution are private in the DI-Container. As a workaround the
  118.         // services are instantiated explicitly here.
  119.         $connection $this->container->get(Connection::class);
  120.         // See vendor/symfony/monolog-bundle/Resources/config/monolog.xml on how the logger is defined.
  121.         $logger = new Logger('app');
  122.         $logger->useMicrosecondTimestamps($this->container->getParameter('monolog.use_microseconds'));
  123.         $migrationCollectionLoader = new MigrationCollectionLoader($connection, new MigrationRuntime($connection$logger));
  124.         $migrationSource = new MigrationSource('PickwareErpPro');
  125.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  126.             $bundle::registerMigrations($migrationSource);
  127.         }
  128.         $migrationCollectionLoader->addSource($migrationSource);
  129.         foreach ($migrationCollectionLoader->collectAll() as $migrationCollection) {
  130.             $migrationCollection->sync();
  131.             $migrationCollection->migrateInPlace();
  132.         }
  133.     }
  134.     /**
  135.      * Run the dependency loader for a setup step like install/update/uninstall
  136.      *
  137.      * When executing one of these steps but no Pickware plugin is activated, the dependency loader did never run until
  138.      * the call of the corresponding method. You can trigger it with a call of this method.
  139.      */
  140.     private function loadDependenciesForSetup(): void
  141.     {
  142.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  143.             $plugins $this->container->get('kernel')->getPluginLoader()->getPluginInfos();
  144.             $projectDir $this->container->getParameter('kernel.project_dir');
  145.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded($plugins$projectDir);
  146.         }
  147.     }
  148. }