8000 [Templating] Added ability to set a specific version of the asset by hason · Pull Request #6092 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Templating] Added ability to set a specific version of the asset #6092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Symfony/Bundle/TwigBundle/Extension/AssetsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ 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 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 getAssetUrl($path, $packageName = null)
public function getAssetUrl($path, $packageName = null, $version = null)
{
return $this->container->get('templating.helper.assets')->getUrl($path, $packageName);
return $this->container->get('templating.helper.assets')->getUrl($path, $packageName, $version);
}

/**
Expand Down
20 changes: 14 additions & 6 deletions src/Symfony/Component/Templating/Asset/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,42 @@ public function __construct($version = null, $format = null)
$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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why false ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to disable generating versioned url.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.2.0
-----

* added ability to set a specific version of the asset

2.1.0
-----

Expand Down
9E88
9 changes: 5 additions & 4 deletions src/Symfony/Component/Templating/Helper/CoreAssetsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,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
6B37
Original file line number Diff line number Diff line change
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