Skip to content

Commit 58bed5d

Browse files
committed
feature #10593 [Templating] Added ability to set a specific version of the asset (romainneutron)
This PR was merged into the 2.5-dev branch. Discussion ---------- [Templating] Added ability to set a specific version of the asset | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT | Doc PR | https://github.com/symfony/symfony-docs/pull/3742/files This PR replaces #6092 Commits ------- 1642094 [Templating] Update changelog 6fce503 [Templating] Added ability to set a specific version of the asset
2 parents 7792ba9 + 1642094 commit 58bed5d

File tree

8 files changed

+52
-21
lines changed

8 files changed

+52
-21
lines changed

src/Symfony/Bundle/TwigBundle/Extension/AssetsExtension.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@ public function getFunctions()
4848
*
4949
* Absolute paths (i.e. http://...) are returned unmodified.
5050
*
51-
* @param string $path A public path
52-
* @param string $packageName The name of the asset package to use
53-
* @param Boolean $absolute Whether to return an absolute URL or a relative one
51+
* @param string $path A public path
52+
* @param string $packageName The name of the asset package to use
53+
* @param Boolean $absolute Whether to return an absolute URL or a relative one
54+
* @param string|Boolean|null $version A specific version
5455
*
5556
* @return string A public path which takes into account the base path and URL path
5657
*/
57-
public function getAssetUrl($path, $packageName = null, $absolute = false)
58+
public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null)
5859
{
59-
$url = $this->container->get('templating.helper.assets')->getUrl($path, $packageName);
60+
$url = $this->container->get('templating.helper.assets')->getUrl($path, $packageName, $version);
6061

6162
if (!$absolute) {
6263
return $url;

src/Symfony/Component/Templating/Asset/Package.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,42 @@ public function __construct($version = null, $format = '')
3333
$this->format = $format ?: '%s?%s';
3434
}
3535

36+
/**
37+
* {@inheritdoc}
38+
*/
3639
public function getVersion()
3740
{
3841
return $this->version;
3942
}
4043

41-
public function getUrl($path)
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function getUrl($path, $version = null)
4248
{
4349
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
4450
return $path;
4551
}
4652

47-
return $this->applyVersion($path);
53+
return $this->applyVersion($path, $version);
4854
}
4955

5056
/**
5157
* Applies version to the supplied path.
5258
*
53-
* @param string $path A path
59+
* @param string $path A path
60+
* @param string|Boolean|null $version A specific version
5461
*
5562
* @return string The versionized path
5663
*/
57-
protected function applyVersion($path)
64+
protected function applyVersion($path, $version = null)
5865
{
59-
if (null === $this->version) {
66+
$version = null !== $version ? $version : $this->version;
67+
if (null === $version || false === $version) {
6068
return $path;
6169
}
6270

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

6573
if ($path && '/' == $path[0]) {
6674
$versionized = '/'.$versionized;

src/Symfony/Component/Templating/Asset/PackageInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public function getVersion();
2828
/**
2929
* Returns an absolute or root-relative public path.
3030
*
31-
* @param string $path A path
31+
* @param string $path A path
32+
* @prama string|Boolean|null $version A specific version for the path
3233
*
3334
* @return string The public path
3435
*/
35-
public function getUrl($path);
36+
public function getUrl($path, $version = null);
3637
}

src/Symfony/Component/Templating/Asset/PathPackage.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ public function __construct($basePath = null, $version = null, $format = null)
4242
}
4343
}
4444

45-
public function getUrl($path)
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function getUrl($path, $version = null)
4649
{
4750
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
4851
return $path;
4952
}
5053

51-
$url = $this->applyVersion($path);
54+
$url = $this->applyVersion($path, $version);
5255

5356
// apply the base path
5457
if ('/' !== substr($url, 0, 1)) {

src/Symfony/Component/Templating/Asset/UrlPackage.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ public function __construct($baseUrls = array(), $version = null, $format = null
4141
}
4242
}
4343

44-
public function getUrl($path)
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function getUrl($path, $version = null)
4548
{
4649
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
4750
return $path;
4851
}
4952

50-
$url = $this->applyVersion($path);
53+
$url = $this->applyVersion($path, $version);
5154

5255
if ($url && '/' != $url[0]) {
5356
$url = '/'.$url;

src/Symfony/Component/Templating/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.5.0
5+
-----
6+
7+
* added ability to generate versioned URLs
8+
* added ability to generate absolute URLs
9+
410
2.1.0
511
-----
612

src/Symfony/Component/Templating/Helper/CoreAssetsHelper.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,15 @@ public function getVersion($packageName = null)
105105
*
106106
* Absolute paths (i.e. http://...) are returned unmodified.
107107
*
108-
* @param string $path A public path
109-
* @param string $packageName The name of the asset package to use
108+
* @param string $path A public path
109+
* @param string $packageName The name of the asset package to use
110+
* @param string|Boolean|null $version A specific version
110111
*
111112
* @return string A public path which takes into account the base path and URL path
112113
*/
113-
public function getUrl($path, $packageName = null)
114+
public function getUrl($path, $packageName = null, $version = null)
114115
{
115-
return $this->getPackage($packageName)->getUrl($path);
116+
return $this->getPackage($packageName)->getUrl($path, $version);
116117
}
117118

118119
/**

src/Symfony/Component/Templating/Tests/Helper/AssetsHelperTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public function testGetUrl()
5757
$this->assertEquals('/', $helper->getUrl(''), '->getUrl() with empty arg returns the prefix alone');
5858
}
5959

60+
public function testGetUrlWithVersion()
61+
{
62+
$helper = new AssetsHelper(null, array(), '12');
63+
$this->assertEquals('/foo.js?12', $helper->getUrl('foo.js'));
64+
$this->assertEquals('/foo.js?bar', $helper->getUrl('foo.js', null, 'bar'));
65+
$this->assertEquals('/foo.js', $helper->getUrl('foo.js', null, false));
66+
}
67+
6068
public function testGetUrlLeavesProtocolRelativePathsUntouched()
6169
{
6270
$helper = new AssetsHelper(null, 'http://foo.com');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy