custom/plugins/PickwarePos/src/PickwarePos.php line 43

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\PickwarePos;
  11. use Doctrine\DBAL\Connection;
  12. use Pickware\BundleInstaller\BundleInstaller;
  13. use Pickware\DalBundle\DalBundle;
  14. use Pickware\DebugBundle\ShopwarePluginsDebugBundle;
  15. use Pickware\DocumentBundle\DocumentBundle;
  16. use Pickware\MobileAppAuthBundle\PickwareMobileAppAuthBundle;
  17. use Pickware\PickwareErpReturnOrderBasicAdministrationBundle\PickwareErpReturnOrderBasicAdministrationBundle;
  18. use Pickware\PickwarePos\Installation\PickwarePosInstaller;
  19. use Pickware\ShopwareExtensionsBundle\PickwareShopwareExtensionsBundle;
  20. use Shopware\Core\Framework\Bundle;
  21. use Shopware\Core\Framework\Migration\MigrationCollectionLoader;
  22. use Shopware\Core\Framework\Migration\MigrationRuntime;
  23. use Shopware\Core\Framework\Migration\MigrationSource;
  24. use Shopware\Core\Framework\Parameter\AdditionalBundleParameters;
  25. use Shopware\Core\Framework\Plugin;
  26. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  27. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  28. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  29. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  30. use Shopware\Core\Framework\Struct\Collection;
  31. use Symfony\Bridge\Monolog\Logger;
  32. use Symfony\Component\Config\FileLocator;
  33. use Symfony\Component\DependencyInjection\ContainerBuilder;
  34. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  35. if (file_exists(__DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php')) {
  36.     require_once __DIR__ '/../vendor/pickware/dependency-loader/src/DependencyLoader.php';
  37. }
  38. class PickwarePos extends Plugin
  39. {
  40.     public const PLUGIN_CONFIG_DOMAIN 'PickwarePos.sales-channel-plugin-config';
  41.     public const PLUGIN_CONFIG_KEY_PREFIX self::PLUGIN_CONFIG_DOMAIN '.';
  42.     public const POS_AUTOMATIC_RECEIPT_PRINTING_CONFIG_KEY self::PLUGIN_CONFIG_KEY_PREFIX 'posAutomaticReceiptPrinting';
  43.     public const POS_GROUP_PRODUCT_VARIANTS_CONFIG_KEY self::PLUGIN_CONFIG_KEY_PREFIX 'posGroupProductVariants';
  44.     public const POS_OVERSELLING_WARNING_CONFIG_KEY self::PLUGIN_CONFIG_KEY_PREFIX 'posOversellingWarning';
  45.     public const SALES_CHANNEL_TYPE_ID 'd18beabacf894e14b507767f4358eeb0';
  46.     /**
  47.      * @var class-string<Bundle>[]
  48.      */
  49.     private const ADDITIONAL_BUNDLES = [
  50.         DalBundle::class,
  51.         DocumentBundle::class,
  52.         ShopwarePluginsDebugBundle::class,
  53.         PickwareMobileAppAuthBundle::class,
  54.         PickwareShopwareExtensionsBundle::class,
  55.         PickwareErpReturnOrderBasicAdministrationBundle::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('Address/DependencyInjection/model-extension.xml');
  84.         $loader->load('Address/DependencyInjection/model.xml');
  85.         $loader->load('BranchStore/DependencyInjection/controller.xml');
  86.         $loader->load('BranchStore/DependencyInjection/model.xml');
  87.         $loader->load('BranchStore/DependencyInjection/service.xml');
  88.         $loader->load('CashPointClosing/DependencyInjection/controller.xml');
  89.         $loader->load('CashPointClosing/DependencyInjection/model-extension.xml');
  90.         $loader->load('CashPointClosing/DependencyInjection/model-subscriber.xml');
  91.         $loader->load('CashPointClosing/DependencyInjection/model.xml');
  92.         $loader->load('CashPointClosing/DependencyInjection/service.xml');
  93.         $loader->load('CashRegister/DependencyInjection/model.xml');
  94.         $loader->load('Config/DependencyInjection/service.xml');
  95.         $loader->load('Customer/DependencyInjection/controller.xml');
  96.         $loader->load('Customer/DependencyInjection/service.xml');
  97.         $loader->load('Installation/DependencyInjection/service.xml');
  98.         $loader->load('Order/DependencyInjection/controller.xml');
  99.         $loader->load('Order/DependencyInjection/model-extension.xml');
  100.         $loader->load('Order/DependencyInjection/model.xml');
  101.         $loader->load('Order/DependencyInjection/service.xml');
  102.         $loader->load('OrderDocument/DependencyInjection/controller.xml');
  103.         $loader->load('OrderDocument/DependencyInjection/service.xml');
  104.         $loader->load('OrderDocument/DependencyInjection/twig-extension.xml');
  105.         $loader->load('Product/DependencyInjection/controller.xml');
  106.         $loader->load('Product/DependencyInjection/service.xml');
  107.         $loader->load('ReturnOrder/DependencyInjection/controller.xml');
  108.         $loader->load('SalesChannel/DependencyInjection/controller.xml');
  109.         $loader->load('SalesChannel/DependencyInjection/service.xml');
  110.     }
  111.     public function install(InstallContext $installContext): void
  112.     {
  113.         $this->loadDependenciesForSetup();
  114.         $this->executeMigrationsOfBundles();
  115.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  116.             ->install(self::ADDITIONAL_BUNDLES$installContext);
  117.     }
  118.     public function postInstall(InstallContext $installContext): void
  119.     {
  120.         $installer PickwarePosInstaller::initFromContainer($this->container);
  121.         $installer->postInstall();
  122.     }
  123.     public function update(UpdateContext $updateContext): void
  124.     {
  125.         $this->loadDependenciesForSetup();
  126.         $this->executeMigrationsOfBundles();
  127.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  128.             ->install(self::ADDITIONAL_BUNDLES$updateContext);
  129.     }
  130.     public function postUpdate(UpdateContext $updateContext): void
  131.     {
  132.         $installer PickwarePosInstaller::initFromContainer($this->container);
  133.         $installer->postUpdate();
  134.         if ($updateContext->getPlugin()->isActive()) {
  135.             $this->container
  136.                 ->get('pickware_pos.bundle_supporting_asset_service')
  137.                 ->copyAssetsFromBundle('PickwareMobileAppAuthBundle')
  138.                 ->copyAssetsFromBundle('PickwareErpReturnOrderBasicAdministrationBundle');
  139.             BundleInstaller::createForContainerAndClass($this->containerself::class)
  140.                 ->onAfterActivate(self::ADDITIONAL_BUNDLES$updateContext);
  141.         }
  142.     }
  143.     private function executeMigrationsOfBundles(): void
  144.     {
  145.         // All the services required for migration execution are private in the DI-Container. As a workaround the
  146.         // services are instantiated explicitly here.
  147.         $db $this->container->get(Connection::class);
  148.         // See vendor/symfony/monolog-bundle/Resources/config/monolog.xml on how the logger is defined.
  149.         $logger = new Logger('app');
  150.         $logger->useMicrosecondTimestamps($this->container->getParameter('monolog.use_microseconds'));
  151.         $migrationCollectionLoader = new MigrationCollectionLoader($db, new MigrationRuntime($db$logger));
  152.         $migrationSource = new MigrationSource('PickwarePos');
  153.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  154.             $bundle::registerMigrations($migrationSource);
  155.         }
  156.         $migrationCollectionLoader->addSource($migrationSource);
  157.         foreach ($migrationCollectionLoader->collectAll() as $migrationCollection) {
  158.             $migrationCollection->sync();
  159.             $migrationCollection->migrateInPlace();
  160.         }
  161.     }
  162.     public function uninstall(UninstallContext $uninstallContext): void
  163.     {
  164.         parent::uninstall($uninstallContext);
  165.         if ($uninstallContext->keepUserData()) {
  166.             return;
  167.         }
  168.         $this->loadDependenciesForSetup();
  169.         $this->container->get(Connection::class)->executeStatement('
  170.             SET FOREIGN_KEY_CHECKS = 0;
  171.             -- Migration1619094000CreateAddressSchema.php
  172.             DROP TABLE IF EXISTS `pickware_pos_address`;
  173.             -- Migration1619094846CreateBranchStoreSchema.php
  174.             DROP TABLE IF EXISTS `pickware_pos_branch_store`;
  175.             -- Migration1619101148CreateCashRegisterSchema.php
  176.             DROP TABLE IF EXISTS `pickware_pos_cash_register`;
  177.             DROP TABLE IF EXISTS `pickware_pos_cash_register_fiskaly_configuration`;
  178.             -- Migration1626965594CreateCashPointClosingSchema.php
  179.             DROP TABLE IF EXISTS `pickware_pos_cash_point_closing`;
  180.             DROP TABLE IF EXISTS `pickware_pos_cash_point_closing_transaction`;
  181.             DROP TABLE IF EXISTS `pickware_pos_cash_point_closing_transaction_line_item`;
  182.             -- Migration1638791749CreateOrderBranchStoreMappingSchema.php
  183.             DROP TABLE IF EXISTS `pickware_pos_order_branch_store_mapping`;
  184.             SET FOREIGN_KEY_CHECKS = 1;
  185.         ');
  186.         PickwarePosInstaller::initFromContainer($this->container)->uninstall();
  187.         BundleInstaller::createForContainerAndClass($this->containerself::class)->uninstall($uninstallContext);
  188.     }
  189.     public function activate(ActivateContext $activateContext): void
  190.     {
  191.         $this->container
  192.             ->get('pickware_pos.bundle_supporting_asset_service')
  193.             ->copyAssetsFromBundle('PickwareMobileAppAuthBundle')
  194.             ->copyAssetsFromBundle('PickwareErpReturnOrderBasicAdministrationBundle');
  195.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  196.             ->onAfterActivate(self::ADDITIONAL_BUNDLES$activateContext);
  197.     }
  198.     /**
  199.      * Run the dependency loader for a setup step like install/update/uninstall
  200.      *
  201.      * When executing one of these steps but no Pickware plugin is activated, the dependency loader did never run until
  202.      * the call of the corresponding method. You can trigger it with a call of this method.
  203.      */
  204.     private function loadDependenciesForSetup(): void
  205.     {
  206.         if (isset($GLOBALS['PICKWARE_DEPENDENCY_LOADER'])) {
  207.             $plugins $this->container->get('kernel')->getPluginLoader()->getPluginInfos();
  208.             $projectDir $this->container->getParameter('kernel.project_dir');
  209.             $GLOBALS['PICKWARE_DEPENDENCY_LOADER']->ensureLatestDependenciesOfPluginsLoaded($plugins$projectDir);
  210.         }
  211.     }
  212. }