Skip to content

Commit 17657d2

Browse files
committed
Fix registration of command subscribers
1 parent 64fb75c commit 17657d2

File tree

6 files changed

+77
-14
lines changed

6 files changed

+77
-14
lines changed

APM/CommandLoggerRegistry.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Bundle\MongoDBBundle\APM;
6+
7+
use Doctrine\ODM\MongoDB\APM\CommandLoggerInterface;
8+
use function array_map;
9+
10+
final class CommandLoggerRegistry
11+
{
12+
/** @var CommandLoggerInterface[] */
13+
private $commandLoggers = [];
14+
15+
public function __construct(iterable $commandLoggers)
16+
{
17+
foreach ($commandLoggers as $commandLogger) {
18+
$this->addLogger($commandLogger);
19+
}
20+
}
21+
22+
public function register() : void
23+
{
24+
array_map(static function (CommandLoggerInterface $commandLogger) {
25+
$commandLogger->register();
26+
}, $this->commandLoggers);
27+
}
28+
29+
public function unregister() : void
30+
{
31+
array_map(static function (CommandLoggerInterface $commandLogger) {
32+
$commandLogger->unregister();
33+
}, $this->commandLoggers);
34+
}
35+
36+
private function addLogger(CommandLoggerInterface $logger) : void
37+
{
38+
$this->commandLoggers[] = $logger;
39+
}
40+
}

DependencyInjection/DoctrineMongoDBExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa
214214
// logging
215215
if ($container->getParameterBag()->resolveValue($documentManager['logging'])) {
216216
$logger = $container->getDefinition('doctrine_mongodb.odm.command_logger');
217-
$logger->addMethodCall('register');
217+
$logger->addTag('doctrine_mongodb.odm.command_logger');
218218
}
219219

220220
// profiler
221221
if ($container->getParameterBag()->resolveValue($documentManager['profiler']['enabled'])) {
222222
$logger = $container->getDefinition('doctrine_mongodb.odm.data_collector.command_logger');
223-
$logger->addMethodCall('register');
223+
$logger->addTag('doctrine_mongodb.odm.command_logger');
224224

225225
$container
226226
->getDefinition('doctrine_mongodb.odm.data_collector')

DoctrineMongoDBBundle.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function boot()
5555
$registry = $this->container->get('doctrine_mongodb');
5656

5757
$this->registerAutoloader($registry->getManager());
58+
$this->registerCommandLoggers();
5859
}
5960

6061
private function registerAutoloader(DocumentManager $documentManager) : void
@@ -69,7 +70,7 @@ private function registerAutoloader(DocumentManager $documentManager) : void
6970
spl_autoload_register($this->autoloader);
7071
}
7172

72-
public function shutdown()
73+
private function unregisterAutoloader() : void
7374
{
7475
if ($this->autoloader === null) {
7576
return;
@@ -78,4 +79,22 @@ public function shutdown()
7879
spl_autoload_unregister($this->autoloader);
7980
$this->autoloader = null;
8081
}
82+
83+
private function registerCommandLoggers() : void
84+
{
85+
$commandLoggerRegistry = $this->container->get('doctrine_mongodb.odm.command_logger_registry');
86+
$commandLoggerRegistry->register();
87+
}
88+
89+
private function unregisterCommandLoggers() : void
90+
{
91+
$commandLoggerRegistry = $this->container->get('doctrine_mongodb.odm.command_logger_registry');
92+
$commandLoggerRegistry->unregister();
93+
}
94+
95+
public function shutdown()
96+
{
97+
$this->unregisterAutoloader();
98+
$this->unregisterCommandLoggers();
99+
}
81100
}

Resources/config/mongodb.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@
132132
<service id="doctrine_mongodb.odm.cache.array" class="%doctrine_mongodb.odm.cache.array.class%" />
133133

134134
<!-- logger -->
135+
<service id="doctrine_mongodb.odm.command_logger_registry" class="Doctrine\Bundle\MongoDBBundle\APM\CommandLoggerRegistry" public="true">
136+
<argument type="tagged" tag="doctrine_mongodb.odm.command_logger" />
137+
</service>
138+
135139
<service id="doctrine_mongodb.odm.command_logger" class="Doctrine\Bundle\MongoDBBundle\APM\PSRCommandLogger" public="false">
136140
<argument type="service" id="logger" on-invalid="null" />
137141

Tests/ContainerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testLoggerConfig(bool $expected, array $config, bool $debug)
4646
$this->extension->load([$config], $this->container);
4747

4848
$definition = $this->container->getDefinition('doctrine_mongodb.odm.command_logger');
49-
$this->assertSame($expected, $definition->hasMethodCall('register'));
49+
$this->assertSame($expected, $definition->hasTag('doctrine_mongodb.odm.command_logger'));
5050
}
5151

5252
public function provideLoggerConfigs()
@@ -90,7 +90,7 @@ public function testDataCollectorConfig(bool $expected, array $config, bool $deb
9090
$this->extension->load([$config], $this->container);
9191

9292
$loggerDefinition = $this->container->getDefinition('doctrine_mongodb.odm.data_collector.command_logger');
93-
$this->assertSame($expected, $loggerDefinition->hasMethodCall('register'));
93+
$this->assertSame($expected, $loggerDefinition->hasTag('doctrine_mongodb.odm.command_logger'));
9494

9595
$dataCollectorDefinition = $this->container->getDefinition('doctrine_mongodb.odm.data_collector');
9696
$this->assertSame($expected, $dataCollectorDefinition->hasTag('data_collector'));

Tests/FixtureIntegrationTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function setUp()
4141

4242
public function testFixturesLoader() : void
4343
{
44-
$kernel = new IntegrationTestKernel('dev', true);
44+
$kernel = new IntegrationTestKernel('dev', false);
4545
$kernel->addServices(static function (ContainerBuilder $c) : void {
4646
$c->autowire(OtherFixtures::class)
4747
->addTag(FixturesCompilerPass::FIXTURE_TAG);
@@ -74,7 +74,7 @@ public function testFixturesLoaderWhenFixtureHasDependencyThatIsNotYetLoaded() :
7474
{
7575
// See https://github.com/doctrine/DoctrineFixturesBundle/issues/215
7676

77-
$kernel = new IntegrationTestKernel('dev', true);
77+
$kernel = new IntegrationTestKernel('dev', false);
7878
$kernel->addServices(static function (ContainerBuilder $c) : void {
7979
$c->autowire(WithDependenciesFixtures::class)
8080
->addTag(FixturesCompilerPass::FIXTURE_TAG);
@@ -116,7 +116,7 @@ public function testExceptionWithDependenciesWithRequiredArguments() : void
116116
$this->markTestSkipped();
117117
}
118118

119-
$kernel = new IntegrationTestKernel('dev', true);
119+
$kernel = new IntegrationTestKernel('dev', false);
120120
$kernel->addServices(static function (ContainerBuilder $c) {
121121
$c->autowire(DependentOnRequiredConstructorArgsFixtures::class)
122122
->addTag(FixturesCompilerPass::FIXTURE_TAG);
@@ -147,7 +147,7 @@ public function testExceptionIfDependentFixtureNotWired() : void
147147
$this->markTestSkipped();
148148
}
149149

150-
$kernel = new IntegrationTestKernel('dev', true);
150+
$kernel = new IntegrationTestKernel('dev', false);
151151
$kernel->addServices(static function (ContainerBuilder $c) : void {
152152
$c->autowire(DependentOnRequiredConstructorArgsFixtures::class)
153153
->addTag(FixturesCompilerPass::FIXTURE_TAG);
@@ -165,7 +165,7 @@ public function testExceptionIfDependentFixtureNotWired() : void
165165

166166
public function testFixturesLoaderWithGroupsOptionViaInterface() : void
167167
{
168-
$kernel = new IntegrationTestKernel('dev', true);
168+
$kernel = new IntegrationTestKernel('dev', false);
169169
$kernel->addServices(static function (ContainerBuilder $c) : void {
170170
// has a "staging" group via the getGroups() method
171171
$c->autowire(OtherFixtures::class)
@@ -197,7 +197,7 @@ public function testFixturesLoaderWithGroupsOptionViaInterface() : void
197197

198198
public function testFixturesLoaderWithGroupsOptionViaTag() : void
199199
{
200-
$kernel = new IntegrationTestKernel('dev', true);
200+
$kernel = new IntegrationTestKernel('dev', false);
201201
$kernel->addServices(static function (ContainerBuilder $c) : void {
202202
// has a "staging" group via the getGroups() method
203203
$c->autowire(OtherFixtures::class)
@@ -224,7 +224,7 @@ public function testFixturesLoaderWithGroupsOptionViaTag() : void
224224

225225
public function testLoadFixturesViaGroupWithMissingDependency() : void
226226
{
227-
$kernel = new IntegrationTestKernel('dev', true);
227+
$kernel = new IntegrationTestKernel('dev', false);
228228
$kernel->addServices(static function (ContainerBuilder $c) : void {
229229
// has a "staging" group via the getGroups() method
230230
$c->autowire(OtherFixtures::class)
@@ -250,7 +250,7 @@ public function testLoadFixturesViaGroupWithMissingDependency() : void
250250

251251
public function testLoadFixturesViaGroupWithFulfilledDependency() : void
252252
{
253-
$kernel = new IntegrationTestKernel('dev', true);
253+
$kernel = new IntegrationTestKernel('dev', false);
254254
$kernel->addServices(static function (ContainerBuilder $c) : void {
255255
// has a "staging" group via the getGroups() method
256256
$c->autowire(OtherFixtures::class)
@@ -283,7 +283,7 @@ public function testLoadFixturesViaGroupWithFulfilledDependency() : void
283283

284284
public function testLoadFixturesByShortName() : void
285285
{
286-
$kernel = new IntegrationTestKernel('dev', true);
286+
$kernel = new IntegrationTestKernel('dev', false);
287287
$kernel->addServices(static function (ContainerBuilder $c) : void {
288288
// has a "staging" group via the getGroups() method
289289
$c->autowire(OtherFixtures::class)

0 commit comments

Comments
 (0)