diff --git a/DependencyInjection/Compiler/MapperPass.php b/DependencyInjection/Compiler/MapperPass.php new file mode 100644 index 00000000..07e55341 --- /dev/null +++ b/DependencyInjection/Compiler/MapperPass.php @@ -0,0 +1,29 @@ + + */ +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))); + } + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index a05586b3..9255e6f7 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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() diff --git a/DependencyInjection/SymfonyCmfRoutingExtraExtension.php b/DependencyInjection/SymfonyCmfRoutingExtraExtension.php index 3b41bc99..87accffa 100644 --- a/DependencyInjection/SymfonyCmfRoutingExtraExtension.php +++ b/DependencyInjection/SymfonyCmfRoutingExtraExtension.php @@ -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']); diff --git a/SymfonyCmfRoutingExtraBundle.php b/SymfonyCmfRoutingExtraBundle.php index bb388c66..c919ce76 100644 --- a/SymfonyCmfRoutingExtraBundle.php +++ b/SymfonyCmfRoutingExtraBundle.php @@ -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 @@ -18,5 +19,6 @@ public function build(ContainerBuilder $container) { parent::build($container); $container->addCompilerPass(new ChainRouterPass()); + $container->addCompilerPass(new MapperPass()); } } diff --git a/Tests/DependencyInjection/Compiler/MapperPassTest.php b/Tests/DependencyInjection/Compiler/MapperPassTest.php new file mode 100644 index 00000000..7b730917 --- /dev/null +++ b/Tests/DependencyInjection/Compiler/MapperPassTest.php @@ -0,0 +1,41 @@ + 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]); + } +} + diff --git a/Tests/DependencyInjection/SymfonyCmfRoutingExtraExtensionTest.php b/Tests/DependencyInjection/SymfonyCmfRoutingExtraExtensionTest.php new file mode 100644 index 00000000..f2820446 --- /dev/null +++ b/Tests/DependencyInjection/SymfonyCmfRoutingExtraExtensionTest.php @@ -0,0 +1,62 @@ +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') + ); + } +}