浏览代码

[AsseticBundle] updated for Assetic changes

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

+ 55 - 32
src/Symfony/Bundle/AsseticBundle/Templating/AsseticHelper.php

@@ -25,6 +25,7 @@ class AsseticHelper extends Helper
     protected $debug;
     protected $defaultJavascriptsOutput;
     protected $defaultStylesheetsOutput;
+    protected $defaultImageOutput;
 
     /**
      * Constructor.
@@ -33,13 +34,60 @@ class AsseticHelper extends Helper
      * @param Boolean      $debug                    The debug mode
      * @param string       $defaultJavascriptsOutput The default {@link javascripts()} output string
      * @param string       $defaultStylesheetsOutput The default {@link stylesheets()} output string
+     * @param string       $defaultImageOutput       The default {@link image()} output string
      */
-    public function __construct(AssetFactory $factory, $debug = false, $defaultJavascriptsOutput = 'js/*.js', $defaultStylesheetsOutput = 'css/*.css')
+    public function __construct(AssetFactory $factory, $debug = false, $defaultJavascriptsOutput = 'js/*.js', $defaultStylesheetsOutput = 'css/*.css', $defaultImageOutput = 'images/*')
     {
         $this->factory = $factory;
         $this->debug = $debug;
         $this->defaultJavascriptsOutput = $defaultJavascriptsOutput;
         $this->defaultStylesheetsOutput = $defaultStylesheetsOutput;
+        $this->defaultImageOutput = $defaultImageOutput;
+    }
+
+    /**
+     * Returns an array of javascript urls.
+     *
+     * This convenience method wraps {@link assets()} and provides a default
+     * output string.
+     */
+    public function javascripts($inputs = array(), $filters = array(), array $options = array())
+    {
+        if (!isset($options['output'])) {
+            $options['output'] = $this->defaultJavascriptsOutput;
+        }
+
+        return $this->getAssetUrls($inputs, $filters, $options);
+    }
+
+    /**
+     * Returns an array of stylesheet urls.
+     *
+     * This convenience method wraps {@link assets()} and provides a default
+     * output string.
+     */
+    public function stylesheets($inputs = array(), $filters = array(), array $options = array())
+    {
+        if (!isset($options['output'])) {
+            $options['output'] = $this->defaultStylesheetsOutput;
+        }
+
+        return $this->getAssetUrls($inputs, $filters, $options);
+    }
+
+    /**
+     * Returns an array of one image url.
+     *
+     * This convenience method wraps {@link assets()} and provides a default
+     * output string.
+     */
+    public function image($inputs = array(), $filters = array(), array $options = array())
+    {
+        if (!isset($options['output'])) {
+            $options['output'] = $this->defaultImageOutput;
+        }
+
+        return $this->getAssetUrls($inputs, $filters, $options, true);
     }
 
     /**
@@ -57,10 +105,11 @@ class AsseticHelper extends Helper
      * @param array|string $inputs  An array or comma-separated list of input strings
      * @param array|string $filters An array or comma-separated list of filter names
      * @param array        $options An array of options
+     * @param Boolean      $single  Use only the last input string
      *
      * @return array An array of URLs for the asset
      */
-    public function assets($inputs = array(), $filters = array(), array $options = array())
+    private function getAssetUrls($inputs = array(), $filters = array(), array $options = array(), $single = false)
     {
         $explode = function($value)
         {
@@ -79,6 +128,10 @@ class AsseticHelper extends Helper
             $options['debug'] = $this->debug;
         }
 
+        if ($single && 1 < count($inputs)) {
+            $inputs = array_slice($inputs, -1);
+        }
+
         $coll = $this->factory->createAsset($inputs, $filters, $options);
 
         if (!$options['debug']) {
@@ -93,36 +146,6 @@ class AsseticHelper extends Helper
         return $urls;
     }
 
-    /**
-     * Returns an array of javascript urls.
-     *
-     * This convenience method wraps {@link assets()} and provides a default
-     * output string.
-     */
-    public function javascripts($inputs = array(), $filters = array(), array $options = array())
-    {
-        if (!isset($options['output'])) {
-            $options['output'] = $this->defaultJavascriptsOutput;
-        }
-
-        return $this->assets($inputs, $filters, $options);
-    }
-
-    /**
-     * Returns an array of stylesheet urls.
-     *
-     * This convenience method wraps {@link assets()} and provides a default
-     * output string.
-     */
-    public function stylesheets($inputs = array(), $filters = array(), array $options = array())
-    {
-        if (!isset($options['output'])) {
-            $options['output'] = $this->defaultStylesheetsOutput;
-        }
-
-        return $this->assets($inputs, $filters, $options);
-    }
-
     public function getName()
     {
         return 'assetic';

+ 5 - 10
src/Symfony/Bundle/AsseticBundle/Tests/FunctionalTest.php

@@ -50,11 +50,8 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase
     {
         $kernel = new TestKernel('test', $debug);
         $kernel->boot();
-        $container = $kernel->getContainer();
-
-        $names = $container->get('assetic.asset_manager')->getNames();
 
-        $this->assertEquals($count, count($names));
+        $this->assertEquals($count, count($kernel->getContainer()->get('assetic.asset_manager')->getNames()));
     }
 
     /**
@@ -64,12 +61,9 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase
     {
         $kernel = new TestKernel('test', $debug);
         $kernel->boot();
-        $container = $kernel->getContainer();
-
-        $routes = $container->get('router')->getRouteCollection()->all();
 
         $matches = 0;
-        foreach (array_keys($routes) as $name) {
+        foreach (array_keys($kernel->getContainer()->get('router')->getRouteCollection()->all()) as $name) {
             if (0 === strpos($name, 'assetic_')) {
                 ++$matches;
             }
@@ -110,9 +104,10 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase
 
     public function provideDebugAndAssetCount()
     {
+        // totals include assets defined in both php and twig templates
         return array(
-            array(true, 5),
-            array(false, 2),
+            array(true, 8),
+            array(false, 3),
         );
     }
 }

+ 6 - 0
src/Symfony/Bundle/AsseticBundle/Tests/Resources/Resources/views/layout.html.twig

@@ -11,3 +11,9 @@
         <script src="{{ asset_url }}"></script>
     {% endjavascripts %}
 {% endblock %}
+
+{% block content %}
+    {% image 'logo.png' %}
+        <img src="{{ asset_url }}">
+    {% endimage %}
+{% endblock %}

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

@@ -19,3 +19,4 @@ twig:
 assetic:
     use_controller: true
     read_from:      "%kernel.root_dir%/web"
+    bundles:        [TestBundle]

+ 4 - 4
src/Symfony/Bundle/AsseticBundle/Tests/Templating/AsseticHelperTest.php

@@ -31,17 +31,17 @@ class AsseticHelperTest extends \PHPUnit_Framework_TestCase
     public function testUrls($debug, $count, $message)
     {
         $helper = new AsseticHelper(new AssetFactory('/foo', $debug), $debug);
-        $urls = $helper->assets(array('js/jquery.js', 'js/jquery.plugin.js'));
+        $urls = $helper->javascripts(array('js/jquery.js', 'js/jquery.plugin.js'));
 
-        $this->assertInternalType('array', $urls, '->assets() returns an array');
+        $this->assertInternalType('array', $urls, '->javascripts() returns an array');
         $this->assertEquals($count, count($urls), $message);
     }
 
     public function getDebugAndCount()
     {
         return array(
-            array(false, 1, '->assets() returns one url when not in debug mode'),
-            array(true, 2, '->assets() returns many urls when in debug mode'),
+            array(false, 1, '->javascripts() returns one url when not in debug mode'),
+            array(true, 2, '->javascripts() returns many urls when in debug mode'),
         );
     }
 }

+ 4 - 4
src/Symfony/Bundle/AsseticBundle/Twig/DynamicExtension.php

@@ -15,7 +15,7 @@ use Assetic\Extension\Twig\AsseticExtension;
 use Assetic\Factory\AssetFactory;
 
 /**
- * Assetic integration.
+ * The dynamic extension is used when use_controllers is enabled.
  *
  * @author Kris Wallsmith <kris.wallsmith@symfony.com>
  */
@@ -24,9 +24,9 @@ class DynamicExtension extends AsseticExtension
     public function getTokenParsers()
     {
         return array(
-            new DynamicTokenParser($this->factory, $this->debug),
-            new DynamicTokenParser($this->factory, $this->debug, $this->defaultJavascriptsOutput, 'javascripts'),
-            new DynamicTokenParser($this->factory, $this->debug, $this->defaultStylesheetsOutput, 'stylesheets'),
+            new DynamicTokenParser($this->factory, 'javascripts', 'js/*.js', $this->debug),
+            new DynamicTokenParser($this->factory, 'stylesheets', 'css/*.css', $this->debug),
+            new DynamicTokenParser($this->factory, 'image', 'images/*', $this->debug, true),
         );
     }
 }

+ 4 - 4
src/Symfony/Bundle/AsseticBundle/Twig/StaticExtension.php

@@ -15,7 +15,7 @@ use Assetic\Extension\Twig\AsseticExtension;
 use Assetic\Factory\AssetFactory;
 
 /**
- * Assetic integration.
+ * The Static extension is used when use_controllers is disabled.
  *
  * @author Kris Wallsmith <kris.wallsmith@symfony.com>
  */
@@ -24,9 +24,9 @@ class StaticExtension extends AsseticExtension
     public function getTokenParsers()
     {
         return array(
-            new StaticTokenParser($this->factory, $this->debug),
-            new StaticTokenParser($this->factory, $this->debug, $this->defaultJavascriptsOutput, 'javascripts'),
-            new StaticTokenParser($this->factory, $this->debug, $this->defaultStylesheetsOutput, 'stylesheets'),
+            new StaticTokenParser($this->factory, 'javascripts', 'js/*.js', $this->debug),
+            new StaticTokenParser($this->factory, 'stylesheets', 'css/*.css', $this->debug),
+            new StaticTokenParser($this->factory, 'image', 'images/*', $this->debug, true),
         );
     }
 }