Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\CmfRoutingExtension::setupDynamicRouter PHP Method

setupDynamicRouter() private method

Set up the DynamicRouter - only to be called if enabled is set to true.
private setupDynamicRouter ( array $config, ContainerBuilder $container, Symfony\Component\Config\Loader\LoaderInterface $loader )
$config array the compiled configuration for the dynamic router
$container Symfony\Component\DependencyInjection\ContainerBuilder the container builder
$loader Symfony\Component\Config\Loader\LoaderInterface the configuration loader
    private function setupDynamicRouter(array $config, ContainerBuilder $container, LoaderInterface $loader)
    {
        $loader->load('routing-dynamic.xml');
        // strip whitespace (XML support)
        foreach (array('controllers_by_type', 'controllers_by_class', 'templates_by_class', 'route_filters_by_id') as $option) {
            $config[$option] = array_map('trim', $config[$option]);
        }
        $defaultController = $config['default_controller'];
        if (null === $defaultController) {
            $defaultController = $config['generic_controller'];
        }
        $container->setParameter('cmf_routing.default_controller', $defaultController);
        $locales = $config['locales'];
        if (0 === count($locales) && $config['auto_locale_pattern']) {
            throw new InvalidConfigurationException('It makes no sense to activate auto_locale_pattern when no locales are configured.');
        }
        $this->configureParameters($container, $config, array('generic_controller' => 'generic_controller', 'controllers_by_type' => 'controllers_by_type', 'controllers_by_class' => 'controllers_by_class', 'templates_by_class' => 'templates_by_class', 'uri_filter_regexp' => 'uri_filter_regexp', 'route_collection_limit' => 'route_collection_limit', 'limit_candidates' => 'dynamic.limit_candidates', 'locales' => 'dynamic.locales', 'auto_locale_pattern' => 'dynamic.auto_locale_pattern'));
        $hasProvider = false;
        $hasContentRepository = false;
        if ($config['persistence']['phpcr']['enabled']) {
            $this->loadPhpcrProvider($config['persistence']['phpcr'], $loader, $container, $locales, $config['match_implicit_locale']);
            $hasProvider = $hasContentRepository = true;
        }
        if ($config['persistence']['orm']['enabled']) {
            $this->loadOrmProvider($config['persistence']['orm'], $loader, $container, $locales, $config['match_implicit_locale']);
            $hasProvider = $hasContentRepository = true;
        }
        if (isset($config['route_provider_service_id'])) {
            $container->setAlias('cmf_routing.route_provider', $config['route_provider_service_id']);
            $hasProvider = true;
        }
        if (!$hasProvider) {
            throw new InvalidConfigurationException('When the dynamic router is enabled, you need to either enable one of the persistence layers or set the cmf_routing.dynamic.route_provider_service_id option');
        }
        if (isset($config['content_repository_service_id'])) {
            $container->setAlias('cmf_routing.content_repository', $config['content_repository_service_id']);
            $hasContentRepository = true;
        }
        // content repository is optional
        if ($hasContentRepository) {
            $generator = $container->getDefinition('cmf_routing.generator');
            $generator->addMethodCall('setContentRepository', array(new Reference('cmf_routing.content_repository')));
            $container->getDefinition('cmf_routing.enhancer.content_repository')->addTag('dynamic_router_route_enhancer', array('priority' => 100));
        }
        $dynamic = $container->getDefinition('cmf_routing.dynamic_router');
        // if any mappings are defined, set the respective route enhancer
        if (count($config['controllers_by_type']) > 0) {
            $container->getDefinition('cmf_routing.enhancer.controllers_by_type')->addTag('dynamic_router_route_enhancer', array('priority' => 60));
        }
        if (count($config['controllers_by_class']) > 0) {
            $container->getDefinition('cmf_routing.enhancer.controllers_by_class')->addTag('dynamic_router_route_enhancer', array('priority' => 50));
        }
        if (count($config['templates_by_class']) > 0) {
            $container->getDefinition('cmf_routing.enhancer.templates_by_class')->addTag('dynamic_router_route_enhancer', array('priority' => 40));
            /*
             * The CoreBundle prepends the controller from ContentBundle if the
             * ContentBundle is present in the project.
             * If you are sure you do not need a generic controller, set the field
             * to false to disable this check explicitly. But you would need
             * something else like the default_controller to set the controller,
             * as no controller will be set here.
             */
            if (null === $config['generic_controller']) {
                throw new InvalidConfigurationException('If you want to configure templates_by_class, you need to configure the generic_controller option.');
            }
            // if the content class defines the template, we also need to make sure we use the generic controller for those routes
            $controllerForTemplates = array();
            foreach ($config['templates_by_class'] as $key => $value) {
                $controllerForTemplates[$key] = $config['generic_controller'];
            }
            $definition = $container->getDefinition('cmf_routing.enhancer.controller_for_templates_by_class');
            $definition->replaceArgument(2, $controllerForTemplates);
            $container->getDefinition('cmf_routing.enhancer.controller_for_templates_by_class')->addTag('dynamic_router_route_enhancer', array('priority' => 30));
        }
        if (null !== $config['generic_controller'] && $defaultController !== $config['generic_controller']) {
            $container->getDefinition('cmf_routing.enhancer.explicit_template')->addTag('dynamic_router_route_enhancer', array('priority' => 10));
        }
        if (null !== $defaultController) {
            $container->getDefinition('cmf_routing.enhancer.default_controller')->addTag('dynamic_router_route_enhancer', array('priority' => -100));
        }
        if (count($config['route_filters_by_id']) > 0) {
            $matcher = $container->getDefinition('cmf_routing.nested_matcher');
            foreach ($config['route_filters_by_id'] as $id => $priority) {
                $matcher->addMethodCall('addRouteFilter', array(new Reference($id), $priority));
            }
        }
        $dynamic->replaceArgument(2, new Reference($config['url_generator']));
    }