Jelajahi Sumber

[FrameworkBundle/Routing] Add "type" option for main Router resource (and expose this in FrameworkExtension config)

In routing files, import statements allow an optional "type" option to hint the resources' type (e.g. for ambiguous file extensions). This adds the same type option to the FrameworkExtension config, which defines the main routing resource.
Jeremy Mikola 14 tahun lalu
induk
melakukan
b3cb02adf2

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

@@ -96,6 +96,7 @@ class Configuration
                 ->canBeUnset()
                 ->canBeUnset()
                 ->scalarNode('cache_warmer')->end()
                 ->scalarNode('cache_warmer')->end()
                 ->scalarNode('resource')->isRequired()->end()
                 ->scalarNode('resource')->isRequired()->end()
+                ->scalarNode('type')->end()
                 ->end()
                 ->end()
         ;
         ;
     }
     }

+ 4 - 0
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

@@ -234,6 +234,10 @@ class FrameworkExtension extends Extension
 
 
         $container->setParameter('routing.resource', $config['resource']);
         $container->setParameter('routing.resource', $config['resource']);
 
 
+        if (isset($config['type'])) {
+            $container->setParameter('router.options.resource_type', $config['type']);
+        }
+
         if (isset($config['cache_warmer']) && $config['cache_warmer']) {
         if (isset($config['cache_warmer']) && $config['cache_warmer']) {
             $container->getDefinition('router.cache_warmer')->addTag('kernel.cache_warmer');
             $container->getDefinition('router.cache_warmer')->addTag('kernel.cache_warmer');
             $container->setAlias('router', 'router.cached');
             $container->setAlias('router', 'router.cached');

+ 2 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

@@ -22,6 +22,7 @@
         <parameter key="router.cache_warmer.class">Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer</parameter>
         <parameter key="router.cache_warmer.class">Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer</parameter>
         <parameter key="router.options.matcher.cache_class">%kernel.name%%kernel.environment%UrlMatcher</parameter>
         <parameter key="router.options.matcher.cache_class">%kernel.name%%kernel.environment%UrlMatcher</parameter>
         <parameter key="router.options.generator.cache_class">%kernel.name%%kernel.environment%UrlGenerator</parameter>
         <parameter key="router.options.generator.cache_class">%kernel.name%%kernel.environment%UrlGenerator</parameter>
+        <parameter key="router.options.resource_type" />
     </parameters>
     </parameters>
 
 
     <services>
     <services>
@@ -71,6 +72,7 @@
                 <argument key="matcher_base_class">%router.options.matcher_base_class%</argument>
                 <argument key="matcher_base_class">%router.options.matcher_base_class%</argument>
                 <argument key="matcher_dumper_class">%router.options.matcher_dumper_class%</argument>
                 <argument key="matcher_dumper_class">%router.options.matcher_dumper_class%</argument>
                 <argument key="matcher_cache_class">%router.options.matcher.cache_class%</argument>
                 <argument key="matcher_cache_class">%router.options.matcher.cache_class%</argument>
+                <argument key="resource_type">%router.options.resource_type%</argument>
             </argument>
             </argument>
         </service>
         </service>
 
 

+ 2 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

@@ -63,8 +63,9 @@
     </xsd:complexType>
     </xsd:complexType>
 
 
     <xsd:complexType name="router">
     <xsd:complexType name="router">
-        <xsd:attribute name="resource" type="xsd:string" />
         <xsd:attribute name="cache-warmer" type="cache_warmer" />
         <xsd:attribute name="cache-warmer" type="cache_warmer" />
+        <xsd:attribute name="resource" type="xsd:string" />
+        <xsd:attribute name="type" type="xsd:string" />
     </xsd:complexType>
     </xsd:complexType>
 
 
     <xsd:complexType name="session">
     <xsd:complexType name="session">

+ 2 - 1
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

@@ -13,8 +13,9 @@ $container->loadFromExtension('app', 'config', array(
         'only_exceptions' => true,
         'only_exceptions' => true,
     ),
     ),
     'router' => array(
     'router' => array(
-        'resource'     => '%kernel.root_dir%/config/routing.xml',
         'cache_warmer' => true,
         'cache_warmer' => true,
+        'resource'     => '%kernel.root_dir%/config/routing.xml',
+        'type'         => 'xml',
     ),
     ),
     'session' => array(
     'session' => array(
         'auto_start'     => true,
         'auto_start'     => true,

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

@@ -10,7 +10,7 @@
         <app:csrf-protection enabled="true" field-name="_csrf" secret="s3cr3t" />
         <app:csrf-protection enabled="true" field-name="_csrf" secret="s3cr3t" />
         <app:esi enabled="true" />
         <app:esi enabled="true" />
         <app:profiler only-exceptions="true" />
         <app:profiler only-exceptions="true" />
-        <app:router resource="%kernel.root_dir%/config/routing.xml" cache-warmer="true" />
+        <app:router cache-warmer="true" resource="%kernel.root_dir%/config/routing.xml" type="xml" />
         <app:session auto-start="true" class="Session" default-locale="fr" storage-id="native" name="_SYMFONY" lifetime="86400" path="/" domain="example.com" secure="true" httponly="true" />
         <app:session auto-start="true" class="Session" default-locale="fr" storage-id="native" name="_SYMFONY" lifetime="86400" path="/" domain="example.com" secure="true" httponly="true" />
         <app:templating assets-version="SomeVersionScheme" assets-base-urls="http://cdn.example.com" cache-warmer="true">
         <app:templating assets-version="SomeVersionScheme" assets-base-urls="http://cdn.example.com" cache-warmer="true">
             <app:loader>loader.foo</app:loader>
             <app:loader>loader.foo</app:loader>

+ 2 - 1
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

@@ -8,8 +8,9 @@ app.config:
     profiler:
     profiler:
         only_exceptions: true
         only_exceptions: true
     router:
     router:
-        resource:     %kernel.root_dir%/config/routing.xml
         cache_warmer: true
         cache_warmer: true
+        resource:     %kernel.root_dir%/config/routing.xml
+        type:         xml
     session:
     session:
         auto_start:     true
         auto_start:     true
         class:          Session
         class:          Session

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

@@ -51,6 +51,7 @@ abstract class FrameworkExtensionTest extends TestCase
 
 
         $this->assertTrue($container->hasDefinition('router.real'), '->registerRouterConfiguration() loads routing.xml');
         $this->assertTrue($container->hasDefinition('router.real'), '->registerRouterConfiguration() loads routing.xml');
         $this->assertEquals($container->getParameter('kernel.root_dir').'/config/routing.xml', $container->getParameter('routing.resource'), '->registerRouterConfiguration() sets routing resource');
         $this->assertEquals($container->getParameter('kernel.root_dir').'/config/routing.xml', $container->getParameter('routing.resource'), '->registerRouterConfiguration() sets routing resource');
+        $this->assertEquals('xml', $container->getParameter('router.options.resource_type'), '->registerRouterConfiguration() sets routing resource type');
         $this->assertTrue($container->getDefinition('router.cache_warmer')->hasTag('kernel.cache_warmer'), '->registerRouterConfiguration() tags router cache warmer if cache warming is set');
         $this->assertTrue($container->getDefinition('router.cache_warmer')->hasTag('kernel.cache_warmer'), '->registerRouterConfiguration() tags router cache warmer if cache warming is set');
         $this->assertEquals('router.cached', (string) $container->getAlias('router'), '->registerRouterConfiguration() changes router alias to cached if cache warming is set');
         $this->assertEquals('router.cached', (string) $container->getAlias('router'), '->registerRouterConfiguration() changes router alias to cached if cache warming is set');
     }
     }

+ 5 - 3
src/Symfony/Component/Routing/Router.php

@@ -35,8 +35,9 @@ class Router implements RouterInterface
      *
      *
      * Available options:
      * Available options:
      *
      *
-     *   * cache_dir: The cache directory (or null to disable caching)
-     *   * debug:     Whether to enable debugging or not (false by default)
+     *   * cache_dir:     The cache directory (or null to disable caching)
+     *   * debug:         Whether to enable debugging or not (false by default)
+     *   * resource_type: Type hint for the main resource (optional)
      *
      *
      * @param LoaderInterface $loader A LoaderInterface instance
      * @param LoaderInterface $loader A LoaderInterface instance
      * @param mixed           $resource The main resource to load
      * @param mixed           $resource The main resource to load
@@ -63,6 +64,7 @@ class Router implements RouterInterface
             'matcher_base_class'     => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
             'matcher_base_class'     => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
             'matcher_dumper_class'   => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
             'matcher_dumper_class'   => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
             'matcher_cache_class'    => 'ProjectUrlMatcher',
             'matcher_cache_class'    => 'ProjectUrlMatcher',
+            'resource_type'          => null,
         );
         );
 
 
         // check option names
         // check option names
@@ -81,7 +83,7 @@ class Router implements RouterInterface
     public function getRouteCollection()
     public function getRouteCollection()
     {
     {
         if (null === $this->collection) {
         if (null === $this->collection) {
-            $this->collection = $this->loader->load($this->resource);
+            $this->collection = $this->loader->load($this->resource, $this->options['resource_type']);
         }
         }
 
 
         return $this->collection;
         return $this->collection;