Skip to content

Commit

Permalink
refactored routing management (it's now possible to disable the defau…
Browse files Browse the repository at this point in the history
…lt routing)

 * removed the Kernel::registerRoutes() method
 * added a router entry in <web:config> (replaces the registerRoutes() method)
       <web:config>
           <web:router resource="%kernel.root_dir%/config/routing.xml" />
       </web:config>
 * refactored routing configuration in its own routing.xml file (leverages the new routing component API),
   which is loaded only if <web:router> is defined in the configuration
  • Loading branch information
fabpot committed Jul 20, 2010
1 parent 14cecd5 commit 4e3e86c
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 35 deletions.
Expand Up @@ -30,6 +30,7 @@ class WebExtension extends Extension
protected $resources = array(
'templating' => 'templating.xml',
'web' => 'web.xml',
'routing' => 'routing.xml',
// validation.xml conflicts with the naming convention for XML
// validation mapping files, so call it validator.xml
'validation' => 'validator.xml',
Expand All @@ -52,15 +53,24 @@ public function __construct(array $bundleDirs, array $bundles)
*/
public function configLoad($config, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');

if (!$container->hasDefinition('controller_manager')) {
$loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
$loader->load($this->resources['web']);
}

if (isset($config['ide']) && 'textmate' === $config['ide']) {
$container->setParameter('debug.file_link_format', 'txmt://open?url=file://%%f&line=%%l');
}

if (isset($config['router'])) {
if (!$container->hasDefinition('router')) {
$loader->load($this->resources['routing']);
}

$container->setParameter('routing.resource', $config['router']['resource']);
}

if (isset($config['toolbar']) && $config['toolbar']) {
$config['profiler'] = true;
}
Expand Down
51 changes: 51 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml
@@ -0,0 +1,51 @@
<?xml version="1.0" ?>

<container xmlns="http://www.symfony-project.org/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="router.class">Symfony\Components\Routing\Router</parameter>
<parameter key="routing.loader.class">Symfony\Components\Routing\Loader\DelegatingLoader</parameter>
<parameter key="routing.resolver.class">Symfony\Bundle\FrameworkBundle\Routing\LoaderResolver</parameter>
<parameter key="routing.loader.xml.class">Symfony\Components\Routing\Loader\XmlFileLoader</parameter>
<parameter key="routing.loader.yml.class">Symfony\Components\Routing\Loader\YamlFileLoader</parameter>
<parameter key="routing.loader.php.class">Symfony\Components\Routing\Loader\PhpFileLoader</parameter>
</parameters>

<services>
<service id="routing.resolver" class="%routing.resolver.class%">
<argument type="service" id="service_container" />
</service>

<service id="routing.loader.xml" class="%routing.loader.xml.class%">
<annotation name="routing.loader" />
<argument>%kernel.bundle_dirs%</argument>
</service>

<service id="routing.loader.yml" class="%routing.loader.yml.class%">
<annotation name="routing.loader" />
<argument>%kernel.bundle_dirs%</argument>
</service>

<service id="routing.loader.php" class="%routing.loader.php.class%">
<annotation name="routing.loader" />
<argument>%kernel.bundle_dirs%</argument>
</service>

<service id="routing.loader" class="%routing.loader.class%">
<argument type="service" id="routing.resolver" />
</service>

<service id="router" class="%router.class%">
<argument type="service" id="routing.loader" />
<argument>%routing.resource%</argument>
<argument type="collection">
<argument key="cache_dir">%kernel.cache_dir%</argument>
<argument key="debug">%kernel.debug%</argument>
<argument key="matcher_cache_class">%kernel.name%UrlMatcher</argument>
<argument key="generator_cache_class">%kernel.name%UrlGenerator</argument>
</argument>
</service>
</services>
</container>
Expand Up @@ -10,11 +10,19 @@
<xsd:element name="user" type="user" />

<xsd:complexType name="config">
<xsd:sequence>
<xsd:element name="router" type="router" minOccurs="0" maxOccurs="1" />
</xsd:sequence>

<xsd:attribute name="ide" type="xsd:string" />
<xsd:attribute name="profiler" type="xsd:boolean" />
<xsd:attribute name="toolbar" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="router">
<xsd:attribute name="resource" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="user">
<xsd:sequence>
<xsd:element name="session" type="session" minOccurs="0" maxOccurs="1" />
Expand Down
14 changes: 0 additions & 14 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml
Expand Up @@ -8,7 +8,6 @@
<parameter key="request_listener.class">Symfony\Bundle\FrameworkBundle\RequestListener</parameter>
<parameter key="controller_manager.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerManager</parameter>
<parameter key="controller_loader_listener.class">Symfony\Components\HttpKernel\Controller\ControllerLoaderListener</parameter>
<parameter key="router.class">Symfony\Components\Routing\Router</parameter>
<parameter key="response_listener.class">Symfony\Components\HttpKernel\ResponseListener</parameter>
<parameter key="exception_listener.class">Symfony\Bundle\FrameworkBundle\Controller\ExceptionListener</parameter>
<parameter key="exception_listener.controller">FrameworkBundle:Exception:exception</parameter>
Expand All @@ -35,19 +34,6 @@
<argument type="service" id="logger" on-invalid="ignore" />
</service>

<service id="router" class="%router.class%">
<argument type="collection">
<argument type="service" id="kernel" />
<argument>registerRoutes</argument>
</argument>
<argument type="collection">
<argument key="cache_dir">%kernel.cache_dir%</argument>
<argument key="debug">%kernel.debug%</argument>
<argument key="matcher_cache_class">%kernel.name%UrlMatcher</argument>
<argument key="generator_cache_class">%kernel.name%UrlGenerator</argument>
</argument>
</service>

<service id="esi" class="%esi.class%" />

<service id="esi_listener" class="%esi_listener.class%">
Expand Down
Expand Up @@ -4,7 +4,6 @@

use Symfony\Framework\Kernel;
use Symfony\Components\DependencyInjection\Loader\XmlFileLoader as ContainerLoader;
use Symfony\Components\Routing\Loader\XmlFileLoader as RoutingLoader;
use Symfony\Components\DependencyInjection\ContainerBuilder;

use Symfony\Framework\Bundle\KernelBundle;
Expand Down Expand Up @@ -57,11 +56,4 @@ public function registerContainerConfiguration()

return $container;
}

public function registerRoutes()
{
$loader = new RoutingLoader($this->getBundleDirs());

return $loader->load(__DIR__.'/config/routing.xml');
}
}
Expand Up @@ -22,7 +22,9 @@
error_handler_level="null"
/>

<web:config />
<web:config>
<web:router resource="%kernel.root_dir%/config/routing.xml" />
</web:config>

<web:templating
escaping="htmlspecialchars"
Expand Down
Expand Up @@ -4,7 +4,6 @@

use Symfony\Framework\Kernel;
use Symfony\Components\DependencyInjection\Loader\YamlFileLoader as ContainerLoader;
use Symfony\Components\Routing\Loader\YamlFileLoader as RoutingLoader;
use Symfony\Components\DependencyInjection\ContainerBuilder;

use Symfony\Framework\Bundle\KernelBundle;
Expand Down Expand Up @@ -57,11 +56,4 @@ public function registerContainerConfiguration()

return $container;
}

public function registerRoutes()
{
$loader = new RoutingLoader($this->getBundleDirs());

return $loader->load(__DIR__.'/config/routing.yml');
}
}
Expand Up @@ -5,7 +5,8 @@ kernel.config:
charset: UTF-8
error_handler_level: null

web.config: ~
web.config:
router: { resource: "%kernel.root_dir%/config/routing.yml" }

web.templating:
escaping: htmlspecialchars
Expand Down
65 changes: 65 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Routing/LoaderResolver.php
@@ -0,0 +1,65 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Routing;

use Symfony\Components\Routing\Loader\LoaderResolver as BaseLoaderResolver;
use Symfony\Components\DependencyInjection\ContainerInterface;

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* This loader resolver automatically registers routing loaders from
* the container.
*
* If also lazy-loads them.
*
* @package Symfony
* @subpackage Bundle_FrameworkBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class LoaderResolver extends BaseLoaderResolver
{
protected $services;
protected $container;

/**
* Constructor.
*
* @param \Symfony\Components\DependencyInjection\ContainerInterface $container A ContainerInterface instance
* @param \Symfony\Components\Routing\Loader\LoaderInterface[] $loaders An array of loaders
*/
public function __construct(ContainerInterface $container, array $loaders = array())
{
parent::__construct($loaders);

$this->container = $container;
foreach ($container->findAnnotatedServiceIds('routing.loader') as $id => $attributes) {
$this->services[] = $id;
}
}

/**
* Returns a loader able to load the resource.
*
* @param mixed $resource A resource
*
* @return Symfony\Components\Routing\Loader\LoaderInterface A LoaderInterface instance
*/
public function resolve($resource)
{
if (count($this->services)) {
while ($id = array_shift($this->services)) {
$this->addLoader($this->container->get($id));
}
}

return parent::resolve($resource);
}
}
2 changes: 0 additions & 2 deletions src/Symfony/Framework/Kernel.php
Expand Up @@ -85,8 +85,6 @@ abstract public function registerBundleDirs();

abstract public function registerContainerConfiguration();

abstract public function registerRoutes();

/**
* Checks whether the current kernel has been booted or not.
*
Expand Down

0 comments on commit 4e3e86c

Please sign in to comment.