Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Templating] Added ability to set a specific version of the asset
  • Loading branch information
hason authored and romainneutron committed Mar 31, 2014
1 parent 7792ba9 commit 6fce503
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 21 deletions.
11 changes: 6 additions & 5 deletions src/Symfony/Bundle/TwigBundle/Extension/AssetsExtension.php
Expand Up @@ -48,15 +48,16 @@ public function getFunctions()
*
* 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
* @param Boolean $absolute Whether to return an absolute URL or a relative one
* @param string $path A public path
* @param string $packageName The name of the asset package to use
* @param Boolean $absolute Whether to return an absolute URL or a relative one
* @param string|Boolean|null $version A specific version
*
* @return string A public path which takes into account the base path and URL path
*/
public function getAssetUrl($path, $packageName = null, $absolute = false)
public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null)
{
$url = $this->container->get('templating.helper.assets')->getUrl($path, $packageName);
$url = $this->container->get('templating.helper.assets')->getUrl($path, $packageName, $version);

if (!$absolute) {
return $url;
Expand Down
20 changes: 14 additions & 6 deletions src/Symfony/Component/Templating/Asset/Package.php
Expand Up @@ -33,34 +33,42 @@ public function __construct($version = null, $format = '')
$this->format = $format ?: '%s?%s';
}

/**
* {@inheritdoc}
*/
public function getVersion()
{
return $this->version;
}

public function getUrl($path)
/**
* {@inheritdoc}
*/
public function getUrl($path, $version = null)
{
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
return $path;
}

return $this->applyVersion($path);
return $this->applyVersion($path, $version);
}

/**
* Applies version to the supplied path.
*
* @param string $path A path
* @param string $path A path
* @param string|Boolean|null $version A specific version
*
* @return string The versionized path
*/
protected function applyVersion($path)
protected function applyVersion($path, $version = null)
{
if (null === $this->version) {
$version = null !== $version ? $version : $this->version;
if (null === $version || false === $version) {
return $path;
}

$versionized = sprintf($this->format, ltrim($path, '/'), $this->version);
$versionized = sprintf($this->format, ltrim($path, '/'), $version);

if ($path && '/' == $path[0]) {
$versionized = '/'.$versionized;
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Templating/Asset/PackageInterface.php
Expand Up @@ -28,9 +28,10 @@ public function getVersion();
/**
* Returns an absolute or root-relative public path.
*
* @param string $path A path
* @param string $path A path
* @prama string|Boolean|null $version A specific version for the path
*
* @return string The public path
*/
public function getUrl($path);
public function getUrl($path, $version = null);
}
7 changes: 5 additions & 2 deletions src/Symfony/Component/Templating/Asset/PathPackage.php
Expand Up @@ -42,13 +42,16 @@ public function __construct($basePath = null, $version = null, $format = null)
}
}

public function getUrl($path)
/**
* {@inheritdoc}
*/
public function getUrl($path, $version = null)
{
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
return $path;
}

$url = $this->applyVersion($path);
$url = $this->applyVersion($path, $version);

// apply the base path
if ('/' !== substr($url, 0, 1)) {
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Templating/Asset/UrlPackage.php
Expand Up @@ -41,13 +41,16 @@ public function __construct($baseUrls = array(), $version = null, $format = null
}
}

public function getUrl($path)
/**
* {@inheritdoc}
*/
public function getUrl($path, $version = null)
{
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
return $path;
}

$url = $this->applyVersion($path);
$url = $this->applyVersion($path, $version);

if ($url && '/' != $url[0]) {
$url = '/'.$url;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Templating/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.2.0
-----

* added ability to set a specific version of the asset

2.1.0
-----

Expand Down
9 changes: 5 additions & 4 deletions src/Symfony/Component/Templating/Helper/CoreAssetsHelper.php
Expand Up @@ -105,14 +105,15 @@ public function getVersion($packageName = null)
*
* 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
* @param string $path A public path
* @param string $packageName The name of the asset package to use
* @param string|Boolean|null $version A specific version
*
* @return string A public path which takes into account the base path and URL path
*/
public function getUrl($path, $packageName = null)
public function getUrl($path, $packageName = null, $version = null)
{
return $this->getPackage($packageName)->getUrl($path);
return $this->getPackage($packageName)->getUrl($path, $version);
}

/**
Expand Down
Expand Up @@ -57,6 +57,14 @@ public function testGetUrl()
$this->assertEquals('/', $helper->getUrl(''), '->getUrl() with empty arg returns the prefix alone');
}

public function testGetUrlWithVersion()
{
$helper = new AssetsHelper(null, array(), '12');
$this->assertEquals('/foo.js?12', $helper->getUrl('foo.js'));
$this->assertEquals('/foo.js?bar', $helper->getUrl('foo.js', null, 'bar'));
$this->assertEquals('/foo.js', $helper->getUrl('foo.js', null, false));
}

public function testGetUrlLeavesProtocolRelativePathsUntouched()
{
$helper = new AssetsHelper(null, 'http://foo.com');
Expand Down

0 comments on commit 6fce503

Please sign in to comment.