<?php
/*
* Copyright (c) Pickware GmbH. All rights reserved.
* This file is part of software that is released under a proprietary license.
* You must not copy, modify, distribute, make publicly available, or execute
* its contents or parts thereof without express permission by the copyright
* holder, unless otherwise permitted by law.
*/
declare(strict_types=1);
namespace Pickware\MobileAppAuthBundle;
use Doctrine\DBAL\Connection;
use Pickware\BundleInstaller\BundleInstaller;
use Pickware\DalBundle\DalBundle;
use Pickware\MobileAppAuthBundle\Installation\PickwareMobileAppAuthBundleInstaller;
use Pickware\MobileAppAuthBundle\Installation\Steps\UpsertMobileAppAclRoleInstallationStep;
use Shopware\Core\Framework\Bundle;
use Shopware\Core\Framework\Migration\MigrationSource;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Struct\Collection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
class PickwareMobileAppAuthBundle extends Bundle
{
/**
* @var class-string<Bundle>[]
*/
private const ADDITIONAL_BUNDLES = [DalBundle::class];
private static ?PickwareMobileAppAuthBundle $instance = null;
private static bool $registered = false;
private static bool $migrationsRegistered = false;
public static function register(Collection $bundleCollection): void
{
if (self::$registered) {
return;
}
$bundleCollection->add(self::getInstance());
foreach (self::ADDITIONAL_BUNDLES as $bundle) {
$bundle::register($bundleCollection);
}
self::$registered = true;
}
public static function registerMigrations(MigrationSource $migrationSource): void
{
if (self::$migrationsRegistered) {
return;
}
$migrationsPath = self::getInstance()->getMigrationPath();
$migrationNamespace = self::getInstance()->getMigrationNamespace();
$migrationSource->addDirectory($migrationsPath, $migrationNamespace);
self::$migrationsRegistered = true;
foreach (self::ADDITIONAL_BUNDLES as $bundle) {
$bundle::registerMigrations($migrationSource);
}
}
public static function getInstance(): self
{
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function build(ContainerBuilder $containerBuilder): void
{
parent::build($containerBuilder);
$loader = new XmlFileLoader($containerBuilder, new FileLocator(__DIR__));
$loader->load('DependencyInjection/controller.xml');
$loader->load('DependencyInjection/model.xml');
$loader->load('DemodataGeneration/DependencyInjection/command.xml');
$loader->load('DemodataGeneration/DependencyInjection/demodata-generator.xml');
$loader->load('OAuth/DependencyInjection/service.xml');
$loader->load('OAuth/DependencyInjection/subscriber.xml');
}
public function boot(): void
{
parent::boot();
// Shopware may reboot the kernel under certain circumstances. After the kernel was rebooted, our bundles have
// to be registered again. Since there does not seem to be a way to detect a reboot, we reset the registration
// flag immediately after the bundle has been booted. This will cause the bundles to be registered again when
// the method self::register is called the next time, which will only happen in the case of a kernel reboot.
self::$registered = false;
}
public function install(InstallContext $installContext): void
{
BundleInstaller::createForContainerAndClass($this->container, self::class)
->install(self::ADDITIONAL_BUNDLES, $installContext);
}
public function onAfterActivate(InstallContext $installContext): void
{
$installer = new PickwareMobileAppAuthBundleInstaller($this->container->get(Connection::class));
$installer->install();
BundleInstaller::createForContainerAndClass($this->container, self::class)
->onAfterActivate(self::ADDITIONAL_BUNDLES, $installContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
return;
}
$db = $this->container->get(Connection::class);
$db->executeStatement('
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `pickware_mobile_app_credential`;
SET FOREIGN_KEY_CHECKS=1;
');
// Integrations are created directly by our Pickware apps and should be removed as soon as no authentication
// is used anymore (this bundle is removed).
$db->executeStatement('DELETE FROM `integration` WHERE label LIKE \'Pickware App%\'');
$db->executeStatement('DELETE FROM `acl_role` WHERE id = :aclRoleId', [
'aclRoleId' => UpsertMobileAppAclRoleInstallationStep::MOBILE_APP_ACL_ROLE_ID_BIN,
]);
// We need eight backslashes, as we need to match a single one and double the count for each of the following:
// 1. The PHP parser
// 2. The MySQL parser
// 3. The MySQL pattern matcher (only when using LIKE)
$db->executeStatement("DELETE FROM `migration` WHERE `class` LIKE 'Pickware\\\\\\\\MobileAppAuthBundle\\\\\\\\%'");
BundleInstaller::createForContainerAndClass($this->container, self::class)->uninstall($uninstallContext);
}
}