Skip to content

Commit

Permalink
Merge pull request #29 from dantleech/mapperpass
Browse files Browse the repository at this point in the history
Added Mapper pass and expanded config
  • Loading branch information
dbu committed Nov 11, 2012
2 parents aaaa0d7 + dc523b5 commit 175e7a7
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
29 changes: 29 additions & 0 deletions DependencyInjection/Compiler/MapperPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Symfony\Cmf\Bundle\RoutingExtraBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

/**
* This compiler pass adds additional controller mappers
* to the dynamic router.
*
* @author Daniel Leech <[email protected]>
*/
class MapperPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('symfony_cmf_routing_extra.dynamic_router')) {
return;
}

$router = $container->getDefinition('symfony_cmf_routing_extra.dynamic_router');

foreach ($container->findTaggedServiceIds('dynamic_router_controller_mapper') as $id => $attributes) {
$router->addMethodCall('addControllerMapper', array(new Reference($id)));
}
}
}
2 changes: 2 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function getConfigTreeBuilder()
->end()
->scalarNode('manager_registry')->defaultValue('doctrine_phpcr')->end()
->scalarNode('manager_name')->defaultValue('default')->end()
->scalarNode('route_repository_class')->defaultNull()->end()
->scalarNode('content_repository_class')->defaultNull()->end()
->scalarNode('routing_repositoryroot')->defaultValue('/cms/routes')->end()
->end()
->end()
Expand Down
9 changes: 9 additions & 0 deletions DependencyInjection/SymfonyCmfRoutingExtraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
$loader->load('cmf_routing.xml');
$container->setParameter($this->getAlias() . '.routing_repositoryroot', $config['routing_repositoryroot']);

if (null !== $config['route_repository_class']) {
$container->setParameter('symfony_cmf_routing_extra.route_repository_class', $config['route_repository_class']);
}

if (null !== $config['content_repository_class']) {
$container->setParameter('symfony_cmf_routing_extra.content_repository_class', $config['content_repository_class']);
}


$managerRegistry = $container->getDefinition($this->getAlias() . '.manager_registry');
$managerRegistry->setFactoryService(new Reference($config['manager_registry']));
$managerRegistry->replaceArgument(0, $config['manager_name']);
Expand Down
2 changes: 2 additions & 0 deletions SymfonyCmfRoutingExtraBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Cmf\Bundle\RoutingExtraBundle\DependencyInjection\Compiler\ChainRouterPass;
use Symfony\Cmf\Bundle\RoutingExtraBundle\DependencyInjection\Compiler\MapperPass;

/**
* Bundle class
Expand All @@ -18,5 +19,6 @@ public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new ChainRouterPass());
$container->addCompilerPass(new MapperPass());
}
}
41 changes: 41 additions & 0 deletions Tests/DependencyInjection/Compiler/MapperPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Symfony\Cmf\Bundle\RoutingExtraBundle\Tests\DependencyInjection\Compiler;

use Symfony\Cmf\Bundle\RoutingExtraBundle\DependencyInjection\Compiler\MapperPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class MapperPassTest extends \PHPUnit_Framework_TestCase
{
public function testMapperPass()
{
$serviceIds = array(
'test_mapper' => array(
0 => array(
'id' => 'foo_mapper'
)
),
);

$definition = new Definition('router');
$builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
$builder->expects($this->at(0))
->method('hasDefinition')
->will($this->returnValue(true));
$builder->expects($this->once())
->method('findTaggedServiceIds')
->will($this->returnValue($serviceIds));
$builder->expects($this->once())
->method('getDefinition')
->with('symfony_cmf_routing_extra.dynamic_router')
->will($this->returnValue($definition));

$pass = new MapperPass();
$pass->process($builder);
$calls = $definition->getMethodCalls();
$this->assertEquals(1, count($calls));
$this->assertEquals('addControllerMapper', $calls[0][0]);
}
}

62 changes: 62 additions & 0 deletions Tests/DependencyInjection/SymfonyCmfRoutingExtraExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Symfony\Cmf\Bundle\RoutingExtraBundle\Tests\DependencyInjection;

use Symfony\Cmf\Bundle\RoutingExtraBundle\DependencyInjection\SymfonyCmfRoutingExtraExtension;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class SymfonyCmfRoutingExtraExtensionTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->ext = new SymfonyCmfRoutingExtraExtension;
$this->builder = new ContainerBuilder;

$paramBag = $this->getMock('Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface');
}

public function testLoadDefault()
{
$config = array(
array(
'dynamic' => array(
),
'use_sonata_admin' => false,
)
);

$this->ext->load($config, $this->builder);
$this->assertEquals(
'Symfony\Cmf\Bundle\RoutingExtraBundle\Document\RouteRepository',
$this->builder->getParameter('symfony_cmf_routing_extra.route_repository_class')
);
$this->assertEquals(
'Symfony\Cmf\Bundle\RoutingExtraBundle\Document\ContentRepository',
$this->builder->getParameter('symfony_cmf_routing_extra.content_repository_class')
);
}

public function testLoadPopulated()
{
$config = array(
array(
'dynamic' => array(
'route_repository_class' => 'TestRouteRepository',
'content_repository_class' => 'TestContentRepository',
),
'use_sonata_admin' => false,
)
);

$this->ext->load($config, $this->builder);
$this->assertEquals(
'TestRouteRepository',
$this->builder->getParameter('symfony_cmf_routing_extra.route_repository_class')
);
$this->assertEquals(
'TestContentRepository',
$this->builder->getParameter('symfony_cmf_routing_extra.content_repository_class')
);
}
}

0 comments on commit 175e7a7

Please sign in to comment.