Skip to content

Commit

Permalink
Merge pull request #197 from doctrine/3.0-ResolveTargetDocumentListener
Browse files Browse the repository at this point in the history
Expose ResolveTargetDocumentListener in configuration
  • Loading branch information
jmikola committed May 16, 2014
2 parents ba17a28 + aff3828 commit e022b83
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 46 deletions.
21 changes: 21 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function getConfigTreeBuilder()

$this->addDocumentManagersSection($rootNode);
$this->addConnectionsSection($rootNode);
$this->addResolveTargetDocumentsSection($rootNode);

$rootNode
->children()
Expand Down Expand Up @@ -240,4 +241,24 @@ private function addConnectionsSection(ArrayNodeDefinition $rootNode)
->end()
;
}

/**
* Adds the "resolve_target_documents" config section.
*
* @param ArrayNodeDefinition $rootNode
*/
private function addResolveTargetDocumentsSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->fixXmlConfig('resolve_target_document')
->children()
->arrayNode('resolve_target_documents')
->useAttributeAsKey('interface')
->prototype('scalar')
->cannotBeEmpty()
->end()
->end()
->end()
;
}
}
8 changes: 8 additions & 0 deletions DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public function load(array $configs, ContainerBuilder $container)
$container
);

if ($config['resolve_target_documents']) {
$def = $container->findDefinition('doctrine_mongodb.odm.listeners.resolve_target_document');
foreach ($config['resolve_target_documents'] as $name => $implementation) {
$def->addMethodCall('addResolveTargetDocument', array($name, $implementation, array()));
}
$def->addTag('doctrine_mongodb.odm.event_listener', array('event' => 'loadClassMetadata'));
}

// BC Aliases for Document Manager
$container->setAlias('doctrine.odm.mongodb.document_manager', new Alias('doctrine_mongodb.odm.document_manager'));
}
Expand Down
6 changes: 6 additions & 0 deletions Resources/config/mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@

<!-- logger -->
<parameter key="doctrine_mongodb.odm.logger.batch_insert_threshold">4</parameter>

<!-- listeners -->
<parameter key="doctrine_mongodb.odm.listeners.resolve_target_document.class">Doctrine\ODM\MongoDB\Tools\ResolveTargetDocumentListener</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -145,5 +148,8 @@
<argument type="service" id="service_container" />
</call>
</service>

<!-- listeners -->
<service id="doctrine_mongodb.odm.listeners.resolve_target_document" class="%doctrine_mongodb.odm.listeners.resolve_target_document.class%" public="false" />
</services>
</container>
9 changes: 9 additions & 0 deletions Resources/config/schema/mongodb-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<xsd:element name="default-commit-options" type="default-commit-options" minOccurs="0" maxOccurs="1" />
<xsd:element name="connection" type="connection" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="document-manager" type="document-manager" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="resolve-target-document" type="resolve_target_document" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="auto-generate-hydrator-classes" type="xsd:boolean" />
<xsd:attribute name="auto-generate-proxy-classes" type="xsd:boolean" />
Expand All @@ -33,6 +34,14 @@
<xsd:attribute name="wtimeout" type="xsd:integer" />
</xsd:complexType>

<xsd:complexType name="resolve_target_document">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="interface" type="xsd:string" use="required" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>

<xsd:complexType name="connection">
<xsd:sequence>
<xsd:element name="options" type="connection-options" minOccurs="0" maxOccurs="1" />
Expand Down
45 changes: 45 additions & 0 deletions Tests/DependencyInjection/AbstractMongoDBExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,51 @@ public function testDependencyInjectionImportsOverrideDefaults()
$this->assertTrue($container->getParameter('doctrine_mongodb.odm.auto_generate_proxy_classes'));
}

public function testResolveTargetDocument()
{
$container = $this->getContainer('YamlBundle');
$loader = new DoctrineMongoDBExtension();
$container->registerExtension($loader);

$this->loadFromFile($container, 'odm_resolve_target_document');

$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->compile();

$definition = $container->getDefinition('doctrine_mongodb.odm.listeners.resolve_target_document');
$this->assertDICDefinitionMethodCallOnce($definition, 'addResolveTargetDocument', array('Symfony\Component\Security\Core\User\UserInterface', 'MyUserClass', array()));
$this->assertEquals(array('doctrine_mongodb.odm.event_listener' => array(array('event' => 'loadClassMetadata'))), $definition->getTags());
}

/**
* Assertion for the DI Container, check if the given definition contains a method call with the given parameters.
*
* @param \Symfony\Component\DependencyInjection\Definition $definition
* @param string $methodName
* @param array $params
*/
protected function assertDICDefinitionMethodCallOnce($definition, $methodName, array $params = null)
{
$calls = $definition->getMethodCalls();
$called = false;
foreach ($calls as $call) {
if ($call[0] == $methodName) {
if ($called) {
$this->fail("Method '" . $methodName . "' is expected to be called only once, a second call was registered though.");
} else {
$called = true;
if ($params !== null) {
$this->assertEquals($params, $call[1], "Expected parameters to methods '" . $methodName . "' do not match the actual parameters.");
}
}
}
}
if (!$called) {
$this->fail("Method '" . $methodName . "' is expected to be called once, definition does not contain a call though.");
}
}

protected function getContainer($bundle = 'YamlBundle')
{
require_once __DIR__.'/Fixtures/Bundles/'.$bundle.'/'.$bundle.'.php';
Expand Down
24 changes: 14 additions & 10 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function testDefaults()
'document_managers' => array(),
'connections' => array(),
'proxy_dir' => '%kernel.cache_dir%/doctrine/odm/mongodb/Proxies',
'resolve_target_documents' => array(),
'proxy_namespace' => 'MongoDBODMProxies',
'hydrator_dir' => '%kernel.cache_dir%/doctrine/odm/mongodb/Hydrators',
'hydrator_namespace' => 'Hydrators',
Expand Down Expand Up @@ -156,23 +157,26 @@ public function testFullConfiguration($config)
'retry_query' => 0,
),
),
'resolve_target_documents' => array(
'Foo\BarInterface' => 'Bar\FooClass'
),
);

$this->assertEquals($expected, $options);
}

public function provideFullConfiguration()
{
$yaml = Yaml::parse(__DIR__.'/Fixtures/config/yml/full.yml');
$yaml = $yaml['doctrine_mongodb'];
$xml = XmlUtils::loadFile(__DIR__.'/Fixtures/config/xml/full.xml');
$xml = XmlUtils::convertDomElementToArray($xml->getElementsByTagName('config')->item(0));
return array(
array($yaml),
array($xml),
);
$yaml = Yaml::parse(__DIR__.'/Fixtures/config/yml/full.yml');
$yaml = $yaml['doctrine_mongodb'];

$xml = XmlUtils::loadFile(__DIR__.'/Fixtures/config/xml/full.xml');
$xml = XmlUtils::convertDomElementToArray($xml->getElementsByTagName('config')->item(0));

return array(
array($yaml),
array($xml),
);
}

/**
Expand Down
66 changes: 34 additions & 32 deletions Tests/DependencyInjection/Fixtures/config/xml/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:doctrine_mongodb="http://symfony.com/schema/dic/doctrine/odm/mongodb"
xmlns:doctrine="http://symfony.com/schema/dic/doctrine/odm/mongodb"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/doctrine/odm/mongodb http://symfony.com/schema/dic/doctrine/odm/mongodb/mongodb-1.0.xsd">

<doctrine_mongodb:config
<doctrine:config
auto-generate-hydrator-classes="true"
auto-generate-proxy-classes="true"
default-connection="conn1"
Expand All @@ -18,7 +18,7 @@
proxy-namespace="Test_Proxies"
>

<doctrine_mongodb:default-commit-options
<doctrine:default-commit-options
j="false"
timeout="10"
w="majority"
Expand All @@ -27,8 +27,8 @@
safe="2"
/>

<doctrine_mongodb:connection id="conn1" server="mongodb://localhost">
<doctrine_mongodb:options
<doctrine:connection id="conn1" server="mongodb://localhost">
<doctrine:options
connect="true"
connectTimeoutMS="500"
db="database_val"
Expand All @@ -43,46 +43,48 @@
w="majority"
wTimeoutMS="1000"
>
<doctrine_mongodb:readPreferenceTags>
<doctrine_mongodb:readPreferenceTag name="dc" value="east" />
<doctrine_mongodb:readPreferenceTag name="use" value="reporting" />
</doctrine_mongodb:readPreferenceTags>
<doctrine_mongodb:readPreferenceTags>
<doctrine_mongodb:readPreferenceTag name="dc" value="west" />
</doctrine_mongodb:readPreferenceTags>
<doctrine_mongodb:readPreferenceTags />
</doctrine_mongodb:options>
</doctrine_mongodb:connection>
<doctrine:readPreferenceTags>
<doctrine:readPreferenceTag name="dc" value="east" />
<doctrine:readPreferenceTag name="use" value="reporting" />
</doctrine:readPreferenceTags>
<doctrine:readPreferenceTags>
<doctrine:readPreferenceTag name="dc" value="west" />
</doctrine:readPreferenceTags>
<doctrine:readPreferenceTags />
</doctrine:options>
</doctrine:connection>

<doctrine_mongodb:connection id="conn2" server="mongodb://otherhost" />
<doctrine:connection id="conn2" server="mongodb://otherhost" />

<doctrine_mongodb:document-manager id="dm1">
<doctrine_mongodb:mapping name="FooBundle" type="annotation" />
<doctrine_mongodb:metadata-cache-driver type="memcache">
<doctrine_mongodb:class>fooClass</doctrine_mongodb:class>
<doctrine_mongodb:host>host_val</doctrine_mongodb:host>
<doctrine_mongodb:port>1234</doctrine_mongodb:port>
<doctrine_mongodb:instance-class>instance_val</doctrine_mongodb:instance-class>
</doctrine_mongodb:metadata-cache-driver>
<doctrine_mongodb:profiler enabled="true" pretty="false" />
<doctrine_mongodb:filter name="test_filter" enabled="true" class="TestClass" />
</doctrine_mongodb:document-manager>
<doctrine:document-manager id="dm1">
<doctrine:mapping name="FooBundle" type="annotation" />
<doctrine:metadata-cache-driver type="memcache">
<doctrine:class>fooClass</doctrine:class>
<doctrine:host>host_val</doctrine:host>
<doctrine:port>1234</doctrine:port>
<doctrine:instance-class>instance_val</doctrine:instance-class>
</doctrine:metadata-cache-driver>
<doctrine:profiler enabled="true" pretty="false" />
<doctrine:filter name="test_filter" enabled="true" class="TestClass" />
</doctrine:document-manager>

<doctrine_mongodb:document-manager
<doctrine:document-manager
id="dm2"
connection="dm2_connection"
database="db1"
metadata-cache-driver="apc"
logging="true"
>
<doctrine_mongodb:mapping
<doctrine:mapping
name="BarBundle"
type="yml"
dir="%kernel.cache_dir%"
prefix="prefix_val"
alias="alias_val"
is-bundle="false"
/>
</doctrine_mongodb:document-manager>
</doctrine_mongodb:config>
</container>
</doctrine:document-manager>

<doctrine:resolve-target-document interface="Foo\BarInterface">Bar\FooClass</doctrine:resolve-target-document>
</doctrine:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
<doctrine:document-manager id="dm1" metadata-cache-driver="xcache" connection="conn1" />
<doctrine:document-manager id="dm2" connection="conn2" metadata-cache-driver="apc" />
</doctrine:mongodb>
</container>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
</doctrine:metadata-cache-driver>
</doctrine:document-manager>
</doctrine:mongodb>
</container>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
</doctrine:document-manager>

</doctrine:mongodb>
</container>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:doctrine="http://symfony.com/schema/dic/doctrine/odm/mongodb"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/doctrine/odm/mongodb http://symfony.com/schema/dic/doctrine/odm/mongodb/mongodb-1.0.xsd">

<doctrine:config>
<doctrine:resolve-target-document interface="Symfony\Component\Security\Core\User\UserInterface">MyUserClass</doctrine:resolve-target-document>
</doctrine:config>
</container>
3 changes: 3 additions & 0 deletions Tests/DependencyInjection/Fixtures/config/yml/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ doctrine_mongodb:
proxy_dir: %kernel.cache_dir%/doctrine/odm/mongodb/Test_Proxies
proxy_namespace: Test_Proxies

resolve_target_documents:
Foo\BarInterface: Bar\FooClass

default_commit_options:
j: false
timeout: 10
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
doctrine_mongodb:
resolve_target_documents:
Symfony\Component\Security\Core\User\UserInterface: MyUserClass
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": ">=5.3.2",
"doctrine/mongodb-odm": "~1.0.0-beta5@dev",
"doctrine/mongodb-odm": "~1.0.0-beta10@dev",
"symfony/options-resolver": "~2.1",
"symfony/doctrine-bridge": "~2.1",
"symfony/framework-bundle": "~2.1"
Expand Down

0 comments on commit e022b83

Please sign in to comment.