Skip to content

Commit

Permalink
[DoctrineMongoDBBundle] set annotation constraint namespace alias to …
Browse files Browse the repository at this point in the history
…assertMongoDB
  • Loading branch information
kriswallsmith committed Apr 19, 2011
1 parent 570a100 commit defb021
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
@@ -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]);
}
}
Expand Up @@ -71,8 +71,6 @@ public function load(array $configs, ContainerBuilder $container)
$config['metadata_cache_driver'],
$container
);

$this->loadConstraints($container);
}

/**
Expand Down Expand Up @@ -311,19 +309,6 @@ protected function loadDocumentManagerBundlesMappingInformation(array $documentM
$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;
Expand Down
Expand Up @@ -14,6 +14,7 @@
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;
Expand All @@ -31,6 +32,7 @@ public function build(ContainerBuilder $container)
{
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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -334,25 +335,26 @@ public function testDependencyInjectionImportsOverrideDefaults()
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(),
Expand Down

0 comments on commit defb021

Please sign in to comment.