Skip to content

Commit

Permalink
Make manager registry resettable
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Aug 30, 2024
1 parent edfb3da commit 3fe9abd
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/ManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\MongoDBException;
use ProxyManager\Proxy\LazyLoadingInterface;
use Psr\Container\ContainerInterface;
use Symfony\Bridge\Doctrine\ManagerRegistry as BaseManagerRegistry;
use Symfony\Component\VarExporter\LazyObjectInterface;
use Symfony\Contracts\Service\ResetInterface;

use function array_keys;
use function assert;

class ManagerRegistry extends BaseManagerRegistry
class ManagerRegistry extends BaseManagerRegistry implements ResetInterface
{
public function __construct(string $name, array $connections, array $managers, string $defaultConnection, string $defaultManager, string $proxyInterfaceName, ?ContainerInterface $container = null)
{
Expand Down Expand Up @@ -42,4 +46,37 @@ public function getAliasNamespace(string $alias): string

throw MongoDBException::unknownDocumentNamespace($alias);
}

/**
* Clears all document managers.
*/
public function reset(): void
{
foreach ($this->getManagerNames() as $managerName => $serviceId) {
$this->resetOrClearManager($managerName, $serviceId);
}
}

private function resetOrClearManager(string $managerName, string $serviceId): void
{
if (! $this->container->initialized($serviceId)) {
return;
}

$manager = $this->container->get($serviceId);

if ($manager instanceof LazyLoadingInterface || $manager instanceof LazyObjectInterface) {
$this->resetManager($managerName);

return;
}

assert($manager instanceof DocumentManager);

if (! $manager->isOpen()) {
return;
}

$manager->clear();
}
}
71 changes: 71 additions & 0 deletions tests/ManagerRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests;

use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\Persistence\ObjectManager;
use MongoDBManagerRepositoryTestResetContainer;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;

class ManagerRegistryTest extends TestCase
{
public function testReset(): void
{
$container = new ContainerBuilder();
$container->register('manager.default', DocumentManagerStub::class)
->setPublic(true);
$container->register('manager.lazy', DocumentManagerStub::class)
->setPublic(true)
->setLazy(true)
->addTag('proxy', ['interface' => ObjectManager::class]);
$container->compile();

$dumper = new PhpDumper($container);
eval('?' . '>' . $dumper->dump(['class' => 'MongoDBManagerRepositoryTestResetContainer']));

$container = new MongoDBManagerRepositoryTestResetContainer();
$repository = new ManagerRegistry('MongoDB', [], [
'default' => 'manager.default',
'lazy' => 'manager.lazy',
], '', '', '', $container);

Check failure on line 34 in tests/ManagerRegistryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

InvalidArgument

tests/ManagerRegistryTest.php:34:24: InvalidArgument: Argument 7 of Doctrine\Bundle\MongoDBBundle\ManagerRegistry::__construct expects Psr\Container\ContainerInterface|null, but MongoDBManagerRepositoryTestResetContainer provided (see https://psalm.dev/004)

DocumentManagerStub::$clearCount = 0;

$repository->reset();

// Service was not initialized, so reset should not be called
$this->assertSame(0, DocumentManagerStub::$clearCount);

Check failure on line 41 in tests/ManagerRegistryTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

RedundantCondition

tests/ManagerRegistryTest.php:41:16: RedundantCondition: Type 0 for Doctrine\Bundle\MongoDBBundle\Tests\DocumentManagerStub::$clearCount is always =int(0) (see https://psalm.dev/122)

// The lazy service is reinitialized instead of being cleared
$container->get('manager.lazy')->flush();
$repository->reset();
$this->assertSame(0, DocumentManagerStub::$clearCount);

// The default service is cleared when initialized
$container->get('manager.default')->flush();
$repository->reset();
$this->assertSame(1, DocumentManagerStub::$clearCount);
}
}

class DocumentManagerStub extends DocumentManager
{
public static int $clearCount;

public function __construct()
{
}

public function clear($objectName = null): void

Check failure on line 63 in tests/ManagerRegistryTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Method \Doctrine\Bundle\MongoDBBundle\Tests\DocumentManagerStub::clear() does not have parameter type hint nor @param annotation for its parameter $objectName.
{
self::$clearCount++;
}

public function flush(array $options = []): void
{
}
}

0 comments on commit 3fe9abd

Please sign in to comment.