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

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