Browse Source

[Templating] introduced concept of asset packages so base URLs and asset versions can be set more granularly

Kris Wallsmith 14 years ago
parent
commit
e273a709f1

+ 63 - 0
src/Symfony/Component/Templating/Asset/AssetPackage.php

@@ -0,0 +1,63 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Templating\Asset;
+
+/**
+ * An asset package.
+ *
+ * @author Kris Wallsmith <kris@symfony.com>
+ */
+class AssetPackage implements AssetPackageInterface
+{
+    private $baseUrls;
+    private $version;
+
+    /**
+     * Constructor.
+     *
+     * @param array|string $baseUrls The domain URL or an array of domain URLs
+     * @param string       $version  The version
+     */
+    public function __construct($baseUrls = array(), $version = null)
+    {
+        $this->baseUrls = array();
+        $this->version = $version;
+
+        if (!is_array($baseUrls)) {
+            $baseUrls = (array) $baseUrls;
+        }
+
+        foreach ($baseUrls as $baseUrl) {
+            $this->baseUrls[] = rtrim($baseUrl, '/');
+        }
+    }
+
+    public function getVersion()
+    {
+        return $this->version;
+    }
+
+    public function getBaseUrl($path)
+    {
+        $count = count($this->baseUrls);
+
+        if (0 === $count) {
+            return '';
+        }
+
+        if (1 === $count) {
+            return $this->baseUrls[0];
+        }
+
+        return $this->baseUrls[fmod(hexdec(substr(md5($path), 0, 10)), $count)];
+    }
+}

+ 36 - 0
src/Symfony/Component/Templating/Asset/AssetPackageInterface.php

@@ -0,0 +1,36 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Templating\Asset;
+
+/**
+ * Asset package interface.
+ *
+ * @author Kris Wallsmith <kris@symfony.com>
+ */
+interface AssetPackageInterface
+{
+    /**
+     * Returns the asset package version.
+     *
+     * @return string The version string
+     */
+    function getVersion();
+
+    /**
+     * Returns a base URL for the supplied path.
+     *
+     * @param string $path An asset path
+     *
+     * @return string A base URL
+     */
+    function getBaseUrl($path);
+}

+ 59 - 61
src/Symfony/Component/Templating/Helper/AssetsHelper.php

@@ -11,6 +11,9 @@
 
 namespace Symfony\Component\Templating\Helper;
 
+use Symfony\Component\Templating\Asset\AssetPackage;
+use Symfony\Component\Templating\Asset\AssetPackageInterface;
+
 /**
  * AssetsHelper is the base class for all helper classes that manages assets.
  *
@@ -22,11 +25,11 @@ namespace Symfony\Component\Templating\Helper;
  *
  * @author Fabien Potencier <fabien@symfony.com>
  */
-class AssetsHelper extends Helper
+class AssetsHelper extends Helper implements AssetPackageInterface
 {
     protected $version;
-    protected $baseURLs;
-    protected $basePath;
+    protected $defaultPackage;
+    protected $packages;
 
     /**
      * Constructor.
@@ -34,32 +37,62 @@ class AssetsHelper extends Helper
      * @param string       $basePath The base path
      * @param string|array $baseURLs The domain URL or an array of domain URLs
      * @param string       $version  The version
+     * @param array        $packages Asset packages indexed by name
      */
-    public function __construct($basePath = null, $baseURLs = array(), $version = null)
+    public function __construct($basePath = null, $baseUrls = array(), $version = null, $packages = array())
     {
         $this->setBasePath($basePath);
-        $this->setBaseURLs($baseURLs);
-        $this->version = $version;
+        $this->defaultPackage = new AssetPackage($baseUrls, $version);
+        $this->packages = array();
+
+        foreach ($packages as $name => $package) {
+            $this->setPackage($name, $package);
+        }
     }
 
     /**
-     * Gets the version to add to public URL.
+     * Adds an asset package to the helper.
      *
-     * @return string The current version
+     * @param string                $name    The package name
+     * @param AssetPackageInterface $package The package
+     */
+    public function setPackage($name, AssetPackageInterface $package)
+    {
+        $this->packages[$name] = $package;
+    }
+
+    /**
+     * Returns an asset package.
+     *
+     * @param string $name The name of the package or null for the default package
+     *
+     * @return AssetPackageInterface An asset package
+     *
+     * @throws InvalidArgumentException If there is no package by that name
      */
-    public function getVersion()
+    public function getPackage($name = null)
     {
-        return $this->version;
+        if (null === $name) {
+            return $this->defaultPackage;
+        }
+
+        if (!isset($this->packages[$name])) {
+            throw new \InvalidArgumentException(sprintf('There is no "%s" asset package.', $name));
+        }
+
+        return $this->packages[$name];
     }
 
     /**
-     * Sets the version that is added to each public URL.
+     * Gets the version to add to public URL.
+     *
+     * @param string $package A package name
      *
-     * @param string $id The version
+     * @return string The current version
      */
-    public function setVersion($version)
+    public function getVersion($packageName = null)
     {
-        $this->version = $version;
+        return $this->getPackage($packageName)->getVersion();
     }
 
     /**
@@ -96,71 +129,36 @@ class AssetsHelper extends Helper
      *
      * @return string The base URL
      */
-    public function getBaseURL($path)
-    {
-        $count = count($this->baseURLs);
-
-        if (0 === $count) {
-            return '';
-        }
-
-        if (1 === $count) {
-            return $this->baseURLs[0];
-        }
-
-        return $this->baseURLs[fmod(hexdec(substr(md5($path), 0, 10)), $count)];
-
-    }
-
-    /**
-     * Gets the base URLs.
-     *
-     * @return array The base URLs
-     */
-    public function getBaseURLs()
-    {
-        return $this->baseURLs;
-    }
-
-    /**
-     * Sets the base URLs.
-     *
-     * If you pass an array, the getBaseURL() will return a
-     * randomly pick one to use for each asset.
-     *
-     * @param string|array $baseURLs The base URLs
-     */
-    public function setBaseURLs($baseURLs)
+    public function getBaseUrl($path, $packageName = null)
     {
-        if (!is_array($baseURLs)) {
-            $baseURLs = array($baseURLs);
-        }
-
-        $this->baseURLs = array();
-        foreach ($baseURLs as $URL) {
-            $this->baseURLs[] = rtrim($URL, '/');
-        }
+        return $this->getPackage($packageName)->getBaseUrl($path);
     }
 
     /**
      * Returns the public path.
      *
-     * @param string $path A public path
+     * Absolute paths (i.e. http://...) are returned unmodified.
+     *
+     * @param string $path        A public path
+     * @param string $packageName The name of the asset package to use
      *
      * @return string A public path which takes into account the base path and URL path
      */
-    public function getUrl($path)
+    public function getUrl($path, $packageName = null)
     {
         if (false !== strpos($path, '://')) {
             return $path;
         }
 
-        $base = $this->getBaseURL($path);
+        $package = $this->getPackage($packageName);
+        $base    = $package->getBaseUrl($path);
+        $version = $package->getVersion();
+
         if (0 !== strpos($path, '/')) {
             $path = $base ? '/'.$path : $this->basePath.$path;
         }
 
-        return $base.$path.($this->version ? '?'.$this->version : '');
+        return $base.$path.($version ? '?'.$version : '');
     }
 
     /**

+ 5 - 23
tests/Symfony/Tests/Component/Templating/Helper/AssetsTest.php

@@ -11,6 +11,7 @@
 
 namespace Symfony\Tests\Component\Templating\Helper;
 
+use Symfony\Component\Templating\Asset\AssetPackage;
 use Symfony\Component\Templating\Helper\AssetsHelper;
 
 class AssetsHelperTest extends \PHPUnit_Framework_TestCase
@@ -19,8 +20,7 @@ class AssetsHelperTest extends \PHPUnit_Framework_TestCase
     {
         $helper = new AssetsHelper('foo', 'http://www.example.com', 'abcd');
         $this->assertEquals('/foo/', $helper->getBasePath(), '__construct() takes a base path as its first argument');
-        $this->assertEquals(array('http://www.example.com'), $helper->getBaseURLs(), '__construct() takes a base URL as its second argument');
-        $this->assertEquals('abcd', $helper->getVersion(), '__construct() takes a version as its thrid argument');
+        $this->assertEquals(new AssetPackage('http://www.example.com', 'abcd'), $helper->getPackage(), '->__construct() creates a default asset package');
     }
 
     public function testGetSetBasePath()
@@ -36,28 +36,10 @@ class AssetsHelperTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('/0/', $helper->getBasePath(), '->setBasePath() returns /0/ if 0 is given');
     }
 
-    public function testGetSetVersion()
+    public function testGetVersion()
     {
-        $helper = new AssetsHelper();
-        $helper->setVersion('foo');
-        $this->assertEquals('foo', $helper->getVersion(), '->setVersion() sets the version');
-    }
-
-    public function testSetGetBaseURLs()
-    {
-        $helper = new AssetsHelper();
-        $helper->setBaseURLs('http://www.example.com/');
-        $this->assertEquals(array('http://www.example.com'), $helper->getBaseURLs(), '->setBaseURLs() removes the / at the of an absolute base path');
-        $helper->setBaseURLs(array('http://www1.example.com/', 'http://www2.example.com/'));
-        $URLs = array();
-        for ($i = 0; $i < 20; $i++) {
-            $URLs[] = $helper->getBaseURL($i);
-        }
-        $URLs = array_values(array_unique($URLs));
-        sort($URLs);
-        $this->assertEquals(array('http://www1.example.com', 'http://www2.example.com'), $URLs, '->getBaseURL() returns a random base URL if several are given');
-        $helper->setBaseURLs('');
-        $this->assertEquals('', $helper->getBaseURL(1), '->getBaseURL() returns an empty string if no base URL exist');
+        $helper = new AssetsHelper(null, array(), 'foo');
+        $this->assertEquals('foo', $helper->getVersion(), '->getVersion() returns the version');
     }
 
     public function testGetUrl()