Skip to content

Commit

Permalink
Merge pull request #569 from alcaeus/fix-command-logger-registration
Browse files Browse the repository at this point in the history
Fix registration of command subscribers
  • Loading branch information
alcaeus authored Sep 24, 2019
2 parents c4435f5 + 17657d2 commit 4173faf
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 15 deletions.
40 changes: 40 additions & 0 deletions APM/CommandLoggerRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\APM;

use Doctrine\ODM\MongoDB\APM\CommandLoggerInterface;
use function array_map;

final class CommandLoggerRegistry
{
/** @var CommandLoggerInterface[] */
private $commandLoggers = [];

public function __construct(iterable $commandLoggers)
{
foreach ($commandLoggers as $commandLogger) {
$this->addLogger($commandLogger);
}
}

public function register() : void
{
array_map(static function (CommandLoggerInterface $commandLogger) {
$commandLogger->register();
}, $this->commandLoggers);
}

public function unregister() : void
{
array_map(static function (CommandLoggerInterface $commandLogger) {
$commandLogger->unregister();
}, $this->commandLoggers);
}

private function addLogger(CommandLoggerInterface $logger) : void
{
$this->commandLoggers[] = $logger;
}
}
4 changes: 2 additions & 2 deletions DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa
// logging
if ($container->getParameterBag()->resolveValue($documentManager['logging'])) {
$logger = $container->getDefinition('doctrine_mongodb.odm.command_logger');
$logger->addMethodCall('register');
$logger->addTag('doctrine_mongodb.odm.command_logger');
}

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

$container
->getDefinition('doctrine_mongodb.odm.data_collector')
Expand Down
21 changes: 20 additions & 1 deletion DoctrineMongoDBBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function boot()
$registry = $this->container->get('doctrine_mongodb');

$this->registerAutoloader($registry->getManager());
$this->registerCommandLoggers();
}

private function registerAutoloader(DocumentManager $documentManager) : void
Expand All @@ -69,7 +70,7 @@ private function registerAutoloader(DocumentManager $documentManager) : void
spl_autoload_register($this->autoloader);
}

public function shutdown()
private function unregisterAutoloader() : void
{
if ($this->autoloader === null) {
return;
Expand All @@ -78,4 +79,22 @@ public function shutdown()
spl_autoload_unregister($this->autoloader);
$this->autoloader = null;
}

private function registerCommandLoggers() : void
{
$commandLoggerRegistry = $this->container->get('doctrine_mongodb.odm.command_logger_registry');
$commandLoggerRegistry->register();
}

private function unregisterCommandLoggers() : void
{
$commandLoggerRegistry = $this->container->get('doctrine_mongodb.odm.command_logger_registry');
$commandLoggerRegistry->unregister();
}

public function shutdown()
{
$this->unregisterAutoloader();
$this->unregisterCommandLoggers();
}
}
4 changes: 4 additions & 0 deletions Resources/config/mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@
<service id="doctrine_mongodb.odm.cache.array" class="%doctrine_mongodb.odm.cache.array.class%" />

<!-- logger -->
<service id="doctrine_mongodb.odm.command_logger_registry" class="Doctrine\Bundle\MongoDBBundle\APM\CommandLoggerRegistry" public="true">
<argument type="tagged" tag="doctrine_mongodb.odm.command_logger" />
</service>

<service id="doctrine_mongodb.odm.command_logger" class="Doctrine\Bundle\MongoDBBundle\APM\PSRCommandLogger" public="false">
<argument type="service" id="logger" on-invalid="null" />

Expand Down
4 changes: 2 additions & 2 deletions Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testLoggerConfig(bool $expected, array $config, bool $debug)
$this->extension->load([$config], $this->container);

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

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

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

$dataCollectorDefinition = $this->container->getDefinition('doctrine_mongodb.odm.data_collector');
$this->assertSame($expected, $dataCollectorDefinition->hasTag('data_collector'));
Expand Down
18 changes: 9 additions & 9 deletions Tests/FixtureIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function setUp()

public function testFixturesLoader() : void
{
$kernel = new IntegrationTestKernel('dev', true);
$kernel = new IntegrationTestKernel('dev', false);
$kernel->addServices(static function (ContainerBuilder $c) : void {
$c->autowire(OtherFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
Expand Down Expand Up @@ -74,7 +74,7 @@ public function testFixturesLoaderWhenFixtureHasDependencyThatIsNotYetLoaded() :
{
// See https://github.com/doctrine/DoctrineFixturesBundle/issues/215

$kernel = new IntegrationTestKernel('dev', true);
$kernel = new IntegrationTestKernel('dev', false);
$kernel->addServices(static function (ContainerBuilder $c) : void {
$c->autowire(WithDependenciesFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
Expand Down Expand Up @@ -116,7 +116,7 @@ public function testExceptionWithDependenciesWithRequiredArguments() : void
$this->markTestSkipped();
}

$kernel = new IntegrationTestKernel('dev', true);
$kernel = new IntegrationTestKernel('dev', false);
$kernel->addServices(static function (ContainerBuilder $c) {
$c->autowire(DependentOnRequiredConstructorArgsFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
Expand Down Expand Up @@ -147,7 +147,7 @@ public function testExceptionIfDependentFixtureNotWired() : void
$this->markTestSkipped();
}

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

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

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

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

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

public function testLoadFixturesByShortName() : void
{
$kernel = new IntegrationTestKernel('dev', true);
$kernel = new IntegrationTestKernel('dev', false);
$kernel->addServices(static function (ContainerBuilder $c) : void {
// has a "staging" group via the getGroups() method
$c->autowire(OtherFixtures::class)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"require": {
"php": "^7.2",
"doctrine/annotations": "^1.2",
"doctrine/mongodb-odm": "^2.0.0-beta1",
"doctrine/mongodb-odm": "^2.0.0-RC1",
"psr/log": "^1.0",
"symfony/console": "^3.4 || ^4.0",
"symfony/dependency-injection": "^3.4 || ^4.0",
Expand Down

0 comments on commit 4173faf

Please sign in to comment.