浏览代码

[AsseticBundle] added support for Twig functions

Kris Wallsmith 14 年之前
父节点
当前提交
f3728744b4

+ 3 - 0
src/Symfony/Bundle/AsseticBundle/DependencyInjection/AsseticExtension.php

@@ -71,6 +71,9 @@ class AsseticExtension extends Extension
             }
         }
 
+        // twig functions
+        $container->getDefinition('assetic.twig_extension')->replaceArgument(2, $config['twig']['functions']);
+
         // choose dynamic or static
         if ($parameterBag->resolveValue($parameterBag->get('assetic.use_controller'))) {
             $loader->load('controller.xml');

+ 23 - 0
src/Symfony/Bundle/AsseticBundle/DependencyInjection/Configuration.php

@@ -91,6 +91,29 @@ class Configuration implements ConfigurationInterface
                     ->end()
                 ->end()
             ->end()
+
+            // twig
+            ->children()
+                ->arrayNode('twig')
+                    ->addDefaultsIfNotSet()
+                    ->defaultValue(array())
+                    ->fixXmlConfig('function')
+                    ->children()
+                        ->arrayNode('functions')
+                            ->addDefaultsIfNotSet()
+                            ->defaultValue(array())
+                            ->useAttributeAsKey('name')
+                            ->prototype('variable')
+                                ->treatNullLike(array())
+                                ->validate()
+                                    ->ifTrue(function($v) { return !is_array($v); })
+                                    ->thenInvalid('The assetic.twig.functions config %s must be either null or an array.')
+                                ->end()
+                            ->end()
+                        ->end()
+                    ->end()
+                ->end()
+            ->end()
         ;
 
         return $builder;

+ 12 - 0
src/Symfony/Bundle/AsseticBundle/Resources/config/schema/assetic-1.0.xsd

@@ -11,6 +11,7 @@
         <xsd:sequence>
             <xsd:element name="bundle" type="bundle" minOccurs="0" maxOccurs="unbounded" />
             <xsd:element name="filter" type="filter" minOccurs="0" maxOccurs="unbounded" />
+            <xsd:element name="twig" type="twig" minOccurs="0" maxOccurs="unbounded" />
         </xsd:sequence>
         <xsd:attribute name="debug" type="xsd:string" />
         <xsd:attribute name="use-controller" type="xsd:string" />
@@ -32,4 +33,15 @@
         <xsd:attribute name="resource" type="xsd:string" />
         <xsd:anyAttribute namespace="##any" processContents="lax" />
     </xsd:complexType>
+
+    <xsd:complexType name="twig">
+        <xsd:sequence>
+            <xsd:element name="function" type="twig_function" minOccurs="0" maxOccurs="unbounded" />
+        </xsd:sequence>
+    </xsd:complexType>
+
+    <xsd:complexType name="twig_function">
+        <xsd:attribute name="name" type="xsd:string" use="required" />
+        <xsd:anyAttribute namespace="##any" processContents="lax" />
+    </xsd:complexType>
 </xsd:schema>

+ 1 - 1
src/Symfony/Bundle/AsseticBundle/Resources/config/templating_twig.xml

@@ -14,8 +14,8 @@
             <tag name="twig.extension" />
             <tag name="assetic.templating.twig" />
             <argument type="service" id="assetic.asset_factory" />
-            <argument>%assetic.debug%</argument>
             <argument>%assetic.use_controller%</argument>
+            <argument type="collection" />
         </service>
         <service id="assetic.twig_formula_loader" class="%assetic.cached_formula_loader.class%" public="false">
             <tag name="assetic.formula_loader" alias="twig" />

+ 6 - 0
src/Symfony/Bundle/AsseticBundle/Tests/Resources/config/config.yml

@@ -20,3 +20,9 @@ assetic:
     use_controller: true
     read_from:      "%kernel.root_dir%/web"
     bundles:        [TestBundle]
+    filters:
+        yui_css:
+            jar: %kernel.root_dir/java/yui-compressor-2.4.6.jar
+    twig:
+        functions:
+            yui_css: { output: css/*.css }

+ 2 - 2
src/Symfony/Bundle/AsseticBundle/Twig/AsseticExtension.php

@@ -23,9 +23,9 @@ class AsseticExtension extends BaseAsseticExtension
 {
     private $useController;
 
-    public function __construct(AssetFactory $factory, $debug = false, $useController = false)
+    public function __construct(AssetFactory $factory, $useController = false, $functions = array())
     {
-        parent::__construct($factory, $debug);
+        parent::__construct($factory, $functions);
 
         $this->useController = $useController;
     }