Skip to content

Commit 98cb838

Browse files
committed
Added the assets helper again
1 parent 3040977 commit 98cb838

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434

3535
<service id="templating.helper.assets" class="Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper">
3636
<tag name="templating.helper" alias="assets" />
37-
<argument /> <!-- default package -->
38-
<argument type="collection" /> <!-- named packages -->
37+
<argument /> <!-- packages -->
3938
</service>
4039

4140
<service id="templating.helper.actions" class="Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
13+
14+
use Symfony\Component\Asset\Packages;
15+
use Symfony\Component\Templating\Helper\Helper;
16+
17+
/**
18+
* AssetsHelper helps manage asset URLs.
19+
*
20+
* @author Fabien Potencier <fabien@symfony.com>
21+
*/
22+
class AssetsHelper extends Helper
23+
{
24+
private $packages;
25+
26+
public function __construct(Packages $packages)
27+
{
28+
$this->packages = $packages;
29+
}
30+
31+
/**
32+
* Returns the public url/path of an asset.
33+
*
34+
* If the package used to generate the path is an instance of
35+
* UrlPackage, you will always get a URL and not a path.
36+
*
37+
* @param string $path A public path
38+
* @param string $packageName The name of the asset package to use
39+
*
40+
* @return string The public path of the asset
41+
*/
42+
public function getUrl($path, $packageName = null)
43+
{
44+
return $this->packages->getUrl($path, $packageName);
45+
}
46+
47+
/**
48+
* Returns the version of an asset.
49+
*
50+
* @param string $path A public path
51+
* @param string $packageName The name of the asset package to use
52+
*
53+
* @return string The asset version
54+
*/
55+
public function getVersion($path, $packageName = null)
56+
{
57+
return $this->packages->getVersion($path, $packageName);
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function getName()
64+
{
65+
return 'assets';
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper;
13+
14+
use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
15+
use Symfony\Component\Asset\Package;
16+
use Symfony\Component\Asset\Packages;
17+
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
18+
19+
class AssetsHelperTest extends \PHPUnit_Framework_TestCase
20+
{
21+
private $helper;
22+
23+
protected function setUp()
24+
{
25+
$fooPackage = new Package(new StaticVersionStrategy('42', '%s?v=%s'));
26+
$barPackage = new Package(new StaticVersionStrategy('22', '%s?%s'));
27+
28+
$packages = new Packages($fooPackage, ['bar' => $barPackage]);
29+
30+
$this->helper = new AssetsHelper($packages);
31+
}
32+
33+
public function testGetUrl()
34+
{
35+
$this->assertEquals('me.png?v=42', $this->helper->getUrl('me.png'));
36+
$this->assertEquals('me.png?22', $this->helper->getUrl('me.png', 'bar'));
37+
}
38+
39+
public function testGetVersion()
40+
{
41+
$this->assertEquals('42', $this->helper->getVersion('/'));
42+
$this->assertEquals('22', $this->helper->getVersion('/', 'bar'));
43+
}
44+
}

0 commit comments

Comments
 (0)