[DoctrineMongoDBBundle] set annotation constraint namespace alias to assertMongoDB

This commit is contained in:
Kris Wallsmith 2011-04-19 07:33:51 -07:00
parent 570a100860
commit defb0218fb
4 changed files with 37 additions and 26 deletions

View File

@ -0,0 +1,22 @@
<?php
namespace Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class AddValidatorNamespaceAliasPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('validator.mapping.loader.annotation_loader')) {
return;
}
$loader = $container->getDefinition('validator.mapping.loader.annotation_loader');
$args = $loader->getArguments();
$args[0]['assertMongoDB'] = 'Symfony\\Bundle\\DoctrineMongoDBBundle\\Validator\\Constraints\\';
$loader->setArgument(0, $args[0]);
}
}

View File

@ -71,8 +71,6 @@ class DoctrineMongoDBExtension extends AbstractDoctrineExtension
$config['metadata_cache_driver'],
$container
);
$this->loadConstraints($container);
}
/**
@ -311,19 +309,6 @@ class DoctrineMongoDBExtension extends AbstractDoctrineExtension
$odmConfigDef->addMethodCall('setDocumentNamespaces', array($this->aliasMap));
}
protected function loadConstraints(ContainerBuilder $container)
{
// FIXME: the validator.annotations.namespaces parameter does not exist anymore
// and anyway, it was not available in the FrameworkExtension code
// as each bundle is isolated from the others
if ($container->hasParameter('validator.annotations.namespaces')) {
$container->setParameter('validator.annotations.namespaces', array_merge(
$container->getParameter('validator.annotations.namespaces'),
array('mongodb' => 'Symfony\Bundle\DoctrineMongoDBBundle\Validator\Constraints\\')
));
}
}
protected function getObjectManagerElementName($name)
{
return 'doctrine.odm.mongodb.' . $name;

View File

@ -14,6 +14,7 @@ namespace Symfony\Bundle\DoctrineMongoDBBundle;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler\AddValidatorNamespaceAliasPass;
use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler\CreateHydratorDirectoryPass;
use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler\CreateProxyDirectoryPass;
use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass;
@ -31,6 +32,7 @@ class DoctrineMongoDBBundle extends Bundle
{
parent::build($container);
$container->addCompilerPass(new AddValidatorNamespaceAliasPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
$container->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
$container->addCompilerPass(new CreateProxyDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
$container->addCompilerPass(new CreateHydratorDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\DoctrineMongoDBBundle\Tests\DependencyInjection;
use Symfony\Bundle\DoctrineMongoDBBundle\Tests\TestCase;
use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler\AddValidatorNamespaceAliasPass;
use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
@ -334,25 +335,26 @@ abstract class AbstractMongoDBExtensionTest extends TestCase
public function testRegistersValidatorNamespace()
{
$container = $this->getContainer();
$container->register('validator.mapping.loader.annotation_loader')
->setClass('stdClass')
->addArgument(array('foo' => 'Foo\\'));
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->addCompilerPass(new AddValidatorNamespaceAliasPass());
$container->compile();
$container->setParameter('validator.annotations.namespaces', array('Namespace1\\', 'Namespace2\\'));
$loader = new DoctrineMongoDBExtension();
$loader->load(array(array()), $container);
$definition = $container->getDefinition('validator.mapping.loader.annotation_loader');
$arguments = $definition->getArguments();
$this->assertEquals(array(
'Namespace1\\',
'Namespace2\\',
'mongodb' => 'Symfony\Bundle\DoctrineMongoDBBundle\Validator\Constraints\\',
), $container->getParameter('validator.annotations.namespaces'));
'assertMongoDB' => 'Symfony\\Bundle\\DoctrineMongoDBBundle\\Validator\\Constraints\\',
'foo' => 'Foo\\',
), $arguments[0], 'compiler adds constraint alias to validator');
}
protected function getContainer($bundle = 'YamlBundle')
{
require_once __DIR__.'/Fixtures/Bundles/'.$bundle.'/'.$bundle.'.php';
return new ContainerBuilder(new ParameterBag(array(
'kernel.bundles' => array($bundle => 'DoctrineMongoDBBundle\\Tests\\DependencyInjection\\Fixtures\\Bundles\\'.$bundle.'\\'.$bundle),
'kernel.cache_dir' => sys_get_temp_dir(),