ソースを参照

[Form] Add the FormHelper configuration

Victor Berchet 14 年 前
コミット
5d46e63089

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

@@ -205,6 +205,23 @@ class Configuration implements ConfigurationInterface
                     ->children()
                         ->scalarNode('assets_version')->defaultValue(null)->end()
                         ->scalarNode('assets_version_format')->defaultValue(null)->end()
+                        ->arrayNode('form')
+                            ->addDefaultsIfNotSet()
+                            ->fixXmlConfig('resource')
+                            ->children()
+                                ->arrayNode('resources')
+                                    ->addDefaultsIfNotSet()
+                                    ->defaultValue(array('FrameworkBundle:Form'))
+                                    ->validate()
+                                        ->ifTrue(function($v) {return !in_array('FrameworkBundle:Form', $v); })
+                                        ->then(function($v){
+                                            return array_merge(array('FrameworkBundle:Form'), $v);
+                                        })
+                                    ->end()
+                                    ->prototype('scalar')->end()
+                                ->end()
+                            ->end()
+                        ->end()
                     ->end()
                     ->fixXmlConfig('assets_base_url')
                     ->children()

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

@@ -314,6 +314,7 @@ class FrameworkExtension extends Extension
         );
 
         $container->setParameter('templating.helper.code.file_link_format', str_replace('%', '%%', isset($links[$ide]) ? $links[$ide] : $ide));
+        $container->setParameter('templating.helper.form.resources', $config['form']['resources']);
 
         if ($container->getParameter('kernel.debug')) {
             $loader->load('templating_debug.xml');

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

@@ -90,6 +90,7 @@
             <xsd:element name="engine" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
             <xsd:element name="assets-base-url" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
             <xsd:element name="package" type="package" minOccurs="0" maxOccurs="unbounded" />
+            <xsd:element name="form" type="form-resources" minOccurs="0" maxOccurs="1" />
         </xsd:sequence>
 
         <xsd:attribute name="assets-version" type="xsd:string" />
@@ -97,6 +98,12 @@
         <xsd:attribute name="cache" type="xsd:string" />
     </xsd:complexType>
 
+    <xsd:complexType name="form-resources">
+        <xsd:choice minOccurs="1" maxOccurs="unbounded">
+            <xsd:element name="resource" type="xsd:string" />
+        </xsd:choice>
+    </xsd:complexType>
+
     <xsd:complexType name="package">
         <xsd:sequence>
             <xsd:element name="base-url" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
@@ -117,7 +124,7 @@
         <xsd:attribute name="cache" type="xsd:string" />
         <xsd:attribute name="enable-annotations" type="xsd:boolean" />
     </xsd:complexType>
-    
+
     <xsd:complexType name="annotations">
         <xsd:attribute name="cache" type="xsd:string" />
         <xsd:attribute name="debug" type="xsd:string" />

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

@@ -96,6 +96,7 @@
         <service id="templating.helper.form" class="%templating.helper.form.class%">
             <tag name="templating.helper" alias="form" />
             <argument type="service" id="templating.engine.php" />
+            <argument>%templating.helper.form.resources%</argument>
         </service>
 
         <service id="templating.globals" class="%templating.globals.class%">

+ 2 - 4
src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

@@ -43,16 +43,14 @@ class FormHelper extends Helper
      * @param EngineInterface $engine    The templating engine
      * @param array           $resources An array of theme name
      */
-    public function __construct(EngineInterface $engine, array $resources = array())
+    public function __construct(EngineInterface $engine, array $resources)
     {
         $this->engine = $engine;
+        $this->resources = $resources;
         $this->varStack = array();
         $this->context = array();
         $this->templates = array();
         $this->themes = array();
-
-        $this->resources = 0 == count($resources) ? array('FrameworkBundle:Form') : $resources;
-
     }
 
     public function isChoiceGroup($label)

+ 3 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

@@ -46,6 +46,9 @@ $container->loadFromExtension('framework', array(
                 'base_urls' => array('http://bar1.example.com', 'http://bar2.example.com'),
             ),
         ),
+        'form'              => array(
+            'resources'     => array('theme1', 'theme2')
+        ),
     ),
     'translator' => array(
         'enabled'  => true,

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

@@ -28,6 +28,10 @@
                 <framework:base-url>http://bar1.example.com</framework:base-url>
                 <framework:base-url>http://bar2.example.com</framework:base-url>
             </framework:package>
+            <framework:form>
+                 <framework:resource>theme1</framework:resource>
+                 <framework:resource>theme2</framework:resource>
+            </framework:form>
         </framework:templating>
         <framework:translator enabled="true" fallback="fr" />
         <framework:validation enabled="true" cache="apc" />

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

@@ -35,6 +35,8 @@ framework:
                 version: 1.0.0
             bar:
                 base_urls: ["http://images1.example.com", "http://images2.example.com"]
+        form:
+            resources:    [theme1, theme2]
     translator:
         enabled:  true
         fallback: fr

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

@@ -121,6 +121,8 @@ abstract class FrameworkExtensionTest extends TestCase
         $this->assertEquals('/path/to/cache', $container->getParameter('templating.loader.cache.path'));
 
         $this->assertEquals(array('php', 'twig'), $container->getParameter('templating.engines'), '->registerTemplatingConfiguration() sets a templating.engines parameter');
+
+        $this->assertEquals(array('FrameworkBundle:Form', 'theme1', 'theme2'), $container->getParameter('templating.helper.form.resources'), '->registerTemplatingConfiguration() registers the theme and adds the base theme');
     }
 
     public function testTranslator()

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php

@@ -38,7 +38,7 @@ class FormHelperDivLayoutTest extends AbstractDivLayoutTest
         $loader = new FilesystemLoader(array());
         $engine = new PhpEngine($templateNameParser, $loader);
 
-        $this->helper = new FormHelper($engine);
+        $this->helper = new FormHelper($engine, array('FrameworkBundle:Form'));
 
         $engine->setHelpers(array(
             $this->helper,