custom/plugins/PickwareWms/vendor/pickware/mobile-app-auth-bundle/src/PickwareMobileAppAuthBundle.php line 29

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\MobileAppAuthBundle;
  11. use Doctrine\DBAL\Connection;
  12. use Pickware\BundleInstaller\BundleInstaller;
  13. use Pickware\DalBundle\DalBundle;
  14. use Pickware\InstallationLibrary\DependencyAwareTableDropper;
  15. use Pickware\MobileAppAuthBundle\Installation\PickwareMobileAppAuthBundleInstaller;
  16. use Pickware\MobileAppAuthBundle\Installation\Steps\UpsertMobileAppAclRoleInstallationStep;
  17. use Shopware\Core\Framework\Bundle;
  18. use Shopware\Core\Framework\Migration\MigrationSource;
  19. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  20. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  21. use Shopware\Core\Framework\Struct\Collection;
  22. use Symfony\Component\Config\FileLocator;
  23. use Symfony\Component\DependencyInjection\ContainerBuilder;
  24. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  25. class PickwareMobileAppAuthBundle extends Bundle
  26. {
  27.     /**
  28.      * @var class-string<Bundle>[]
  29.      */
  30.     private const ADDITIONAL_BUNDLES = [DalBundle::class];
  31.     private static ?PickwareMobileAppAuthBundle $instance null;
  32.     private static bool $registered false;
  33.     private static bool $migrationsRegistered false;
  34.     public static function register(Collection $bundleCollection): void
  35.     {
  36.         if (self::$registered) {
  37.             return;
  38.         }
  39.         $bundleCollection->add(self::getInstance());
  40.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  41.             $bundle::register($bundleCollection);
  42.         }
  43.         self::$registered true;
  44.     }
  45.     public static function registerMigrations(MigrationSource $migrationSource): void
  46.     {
  47.         if (self::$migrationsRegistered) {
  48.             return;
  49.         }
  50.         $migrationsPath self::getInstance()->getMigrationPath();
  51.         $migrationNamespace self::getInstance()->getMigrationNamespace();
  52.         $migrationSource->addDirectory($migrationsPath$migrationNamespace);
  53.         self::$migrationsRegistered true;
  54.         foreach (self::ADDITIONAL_BUNDLES as $bundle) {
  55.             $bundle::registerMigrations($migrationSource);
  56.         }
  57.     }
  58.     public static function getInstance(): self
  59.     {
  60.         if (!self::$instance) {
  61.             self::$instance = new self();
  62.         }
  63.         return self::$instance;
  64.     }
  65.     public function build(ContainerBuilder $containerBuilder): void
  66.     {
  67.         parent::build($containerBuilder);
  68.         $loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
  69.         $loader->load('DependencyInjection/controller.xml');
  70.         $loader->load('DependencyInjection/model.xml');
  71.         $loader->load('DemodataGeneration/DependencyInjection/command.xml');
  72.         $loader->load('DemodataGeneration/DependencyInjection/demodata-generator.xml');
  73.         $loader->load('OAuth/DependencyInjection/service.xml');
  74.         $loader->load('OAuth/DependencyInjection/subscriber.xml');
  75.     }
  76.     public function shutdown(): void
  77.     {
  78.         parent::shutdown();
  79.         // Shopware may reboot the kernel under certain circumstances (e.g. plugin un-/installation) within a single
  80.         // request. After the kernel was rebooted, our bundles have to be registered again.
  81.         // We reset the registration flag when the kernel is shut down. This will cause the bundles to be registered
  82.         // again in the (re)boot process.
  83.         self::$registered false;
  84.     }
  85.     public function install(InstallContext $installContext): void
  86.     {
  87.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  88.             ->install(self::ADDITIONAL_BUNDLES$installContext);
  89.     }
  90.     public function onAfterActivate(InstallContext $installContext): void
  91.     {
  92.         $installer = new PickwareMobileAppAuthBundleInstaller($this->container->get(Connection::class));
  93.         $installer->install();
  94.         BundleInstaller::createForContainerAndClass($this->containerself::class)
  95.             ->onAfterActivate(self::ADDITIONAL_BUNDLES$installContext);
  96.     }
  97.     public function uninstall(UninstallContext $uninstallContext): void
  98.     {
  99.         if ($uninstallContext->keepUserData()) {
  100.             return;
  101.         }
  102.         DependencyAwareTableDropper::createForContainer($this->container)->dropTables(['pickware_mobile_app_credential']);
  103.         $db $this->container->get(Connection::class);
  104.         // Integrations are created directly by our Pickware apps and should be removed as soon as no authentication
  105.         // is used anymore (this bundle is removed).
  106.         $db->executeStatement('DELETE FROM `integration` WHERE label LIKE \'Pickware App%\'');
  107.         $db->executeStatement('DELETE FROM `acl_role` WHERE id = :aclRoleId', [
  108.             'aclRoleId' => UpsertMobileAppAclRoleInstallationStep::MOBILE_APP_ACL_ROLE_ID_BIN,
  109.         ]);
  110.         // We need eight backslashes, as we need to match a single one and double the count for each of the following:
  111.         // 1. The PHP parser
  112.         // 2. The MySQL parser
  113.         // 3. The MySQL pattern matcher (only when using LIKE)
  114.         $db->executeStatement("DELETE FROM `migration` WHERE `class` LIKE 'Pickware\\\\\\\\MobileAppAuthBundle\\\\\\\\%'");
  115.         BundleInstaller::createForContainerAndClass($this->containerself::class)->uninstall($uninstallContext);
  116.     }
  117. }