Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make manager registry resettable #852

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e">
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
<file src="config/value_resolver.php">
<UndefinedClass>
<code><![CDATA[ExpressionLanguage]]></code>
Expand All @@ -21,15 +21,14 @@
<code><![CDATA[children]]></code>
</UndefinedMethod>
</file>
<file src="tests/DocumentValueResolverFunctionalTest.php">
<InvalidArgument>
<code><![CDATA[[DocumentValueResolverController::class, 'showUserByDefault']]]></code>
<code><![CDATA[[DocumentValueResolverController::class, 'showUserWithMapping']]]></code>
</InvalidArgument>
</file>
<file src="tests/Form/Type/GuesserTestType.php">
<MissingTemplateParam>
<code><![CDATA[GuesserTestType]]></code>
</MissingTemplateParam>
</file>
<file src="tests/ManagerRegistryTest.php">
<RedundantCondition>
<code><![CDATA[assertSame]]></code>
</RedundantCondition>
</file>
</files>
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();
}
}
74 changes: 74 additions & 0 deletions tests/ManagerRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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 Symfony\Component\DependencyInjection\Container;
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();

/** @var class-string<Container> $containerClass */
$containerClass = 'MongoDBManagerRepositoryTestResetContainer';
$dumper = new PhpDumper($container);
eval('?' . '>' . $dumper->dump(['class' => $containerClass]));
Copy link
Member Author

@GromNaN GromNaN Aug 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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

DocumentManagerStub::$clearCount = 0;

$repository->reset();

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

// 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()
{
}

/** {@inheritDoc} */
public function clear($objectName = null): void
{
self::$clearCount++;
}

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