diff --git a/DataCollector/StandardDataCollector.php b/DataCollector/StandardDataCollector.php index 2f5dde22..cdb867bb 100644 --- a/DataCollector/StandardDataCollector.php +++ b/DataCollector/StandardDataCollector.php @@ -44,6 +44,15 @@ public function collect(Request $request, Response $response, \Exception $except $this->data['queries'] = array_map('json_encode', $this->queries); } + public function reset() + { + $this->queries = []; + $this->data = [ + 'nb_queries' => 0, + 'queries' => [], + ]; + } + public function getQueryCount() { return $this->data['nb_queries']; diff --git a/DependencyInjection/DoctrineMongoDBExtension.php b/DependencyInjection/DoctrineMongoDBExtension.php index c9005abc..f54a3545 100644 --- a/DependencyInjection/DoctrineMongoDBExtension.php +++ b/DependencyInjection/DoctrineMongoDBExtension.php @@ -259,6 +259,7 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa $odmDmDef = new Definition('%doctrine_mongodb.odm.document_manager.class%', $odmDmArgs); $odmDmDef->setFactory(['%doctrine_mongodb.odm.document_manager.class%', 'create']); $odmDmDef->addTag('doctrine_mongodb.odm.document_manager'); + $odmDmDef->setPublic(true); $container ->setDefinition(sprintf('doctrine_mongodb.odm.%s_document_manager', $documentManager['name']), $odmDmDef) @@ -270,6 +271,8 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa 'doctrine_mongodb.odm.document_manager', new Alias(sprintf('doctrine_mongodb.odm.%s_document_manager', $documentManager['name'])) ); + $container->getAlias('doctrine_mongodb.odm.document_manager')->setPublic(true); + $container->setAlias( 'doctrine_mongodb.odm.event_manager', new Alias(sprintf('doctrine_mongodb.odm.%s_connection.event_manager', $connectionName)) @@ -302,6 +305,7 @@ protected function loadConnections(array $connections, ContainerBuilder $contain $this->normalizeDriverOptions($connection), ]; $odmConnDef = new Definition('%doctrine_mongodb.odm.connection.class%', $odmConnArgs); + $odmConnDef->setPublic(true); $id = sprintf('doctrine_mongodb.odm.%s_connection', $name); $container->setDefinition($id, $odmConnDef); $cons[$name] = $id; diff --git a/ManagerRegistry.php b/ManagerRegistry.php index a0061dba..0ec338be 100644 --- a/ManagerRegistry.php +++ b/ManagerRegistry.php @@ -16,9 +16,25 @@ use Doctrine\ODM\MongoDB\MongoDBException; use Symfony\Bridge\Doctrine\ManagerRegistry as BaseManagerRegistry; +use Symfony\Component\DependencyInjection\ContainerAwareTrait; +use Symfony\Component\DependencyInjection\ContainerInterface; class ManagerRegistry extends BaseManagerRegistry { + public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName, ContainerInterface $container = null) + { + $parentTraits = class_uses(parent::class); + if (isset($parentTraits[ContainerAwareTrait::class])) { + // this case should be removed when Symfony 3.4 becomes the lowest supported version + // and then also, the constructor should type-hint Psr\Container\ContainerInterface + $this->setContainer($container); + } else { + $this->container = $container; + } + + parent::__construct($name, $connections, $managers, $defaultConnection, $defaultManager, $proxyInterfaceName); + } + /** * Resolves a registered namespace alias to the full namespace. * diff --git a/Resources/config/mongodb.xml b/Resources/config/mongodb.xml index a082bca0..5b07267d 100644 --- a/Resources/config/mongodb.xml +++ b/Resources/config/mongodb.xml @@ -180,16 +180,14 @@ - + MongoDB %doctrine_mongodb.odm.connections% %doctrine_mongodb.odm.document_managers% %doctrine_mongodb.odm.default_connection% %doctrine_mongodb.odm.default_document_manager% Doctrine\ODM\MongoDB\Proxy\Proxy - - - + diff --git a/Tests/DataCollector/StandardDataCollectorTest.php b/Tests/DataCollector/StandardDataCollectorTest.php index aaa2bae0..a16e8693 100644 --- a/Tests/DataCollector/StandardDataCollectorTest.php +++ b/Tests/DataCollector/StandardDataCollectorTest.php @@ -30,4 +30,17 @@ public function testCollect() $this->assertEquals(1, $collector->getQueryCount()); $this->assertEquals(['{"foo":"bar"}'], $collector->getQueries()); } + + public function testReset() + { + $collector = new StandardDataCollector(); + $collector->logQuery(['foo' => 'bar']); + $collector->collect(new Request(), new Response()); + + $collector->reset(); + $collector->collect(new Request(), new Response()); + + $this->assertEquals([], $collector->getQueries()); + $this->assertEquals(0, $collector->getQueryCount()); + } } diff --git a/Tests/DependencyInjection/DoctrineMongoDBExtensionTest.php b/Tests/DependencyInjection/DoctrineMongoDBExtensionTest.php index b3c09de3..55f0b2ab 100644 --- a/Tests/DependencyInjection/DoctrineMongoDBExtensionTest.php +++ b/Tests/DependencyInjection/DoctrineMongoDBExtensionTest.php @@ -225,4 +225,14 @@ public function testFactoriesAreRegistered() [new Reference('persistent_collection_factory_service')] ], $configDm1->getMethodCalls()); } + + public function testPublicServicesAndAliases() + { + $loader = new DoctrineMongoDBExtension(); + $loader->load(self::buildConfiguration(), $container = $this->buildMinimalContainer()); + + $this->assertTrue($container->getDefinition('doctrine_mongodb')->isPublic()); + $this->assertTrue($container->getDefinition('doctrine_mongodb.odm.dummy_document_manager')->isPublic()); + $this->assertTrue($container->getAlias('doctrine_mongodb.odm.document_manager')->isPublic()); + } }