Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[3.0][FrameworkBundle] Add assets helper #14972

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 1 commit 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
2 changes: 1 addition & 1 deletion 2 UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ UPGRADE FROM 2.x to 3.0
service instead.

* The `templating.helper.assets` was moved to `templating_php.xml`. You can
use the `assets.package` service instead.
use the `assets.packages` service instead.

Before:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@

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

<service id="templating.helper.actions" class="Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\Bundle\FrameworkBundle\Templating\Helper;

use Symfony\Component\Asset\Packages;
use Symfony\Component\Templating\Helper\Helper;

/**
* AssetsHelper helps manage asset URLs.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class AssetsHelper extends Helper
{
private $packages;

public function __construct(Packages $packages)
{
$this->packages = $packages;
}

/**
* Returns the public url/path of an asset.
*
* If the package used to generate the path is an instance of
* UrlPackage, you will always get a URL and not a path.
*
* @param string $path A public path
* @param string $packageName The name of the asset package to use
*
* @return string The public path of the asset
*/
public function getUrl($path, $packageName = null)
{
return $this->packages->getUrl($path, $packageName);
}

/**
* Returns the version of an asset.
*
* @param string $path A public path
* @param string $packageName The name of the asset package to use
*
* @return string The asset version
*/
public function getVersion($path, $packageName = null)
{
return $this->packages->getVersion($path, $packageName);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'assets';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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\Bundle\FrameworkBundle\Tests\Templating\Helper;

use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;

class AssetsHelperTest extends \PHPUnit_Framework_TestCase
{
public function testGetUrl()
{
$package = new Package(new StaticVersionStrategy('22', '%s?version=%s'));
$packages = new Packages($package);
$helper = new AssetsHelper($packages);

$this->assertEquals('me.png?version=22', $helper->getUrl('me.png'));
}

public function testGetVersion()
{
$package = new Package(new StaticVersionStrategy('22'));
$imagePackage = new Package(new StaticVersionStrategy('42'));
$packages = new Packages($package, array('images' => $imagePackage));
$helper = new AssetsHelper($packages);

$this->assertEquals('22', $helper->getVersion('/foo'));
$this->assertEquals('42', $helper->getVersion('/foo', 'images'));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.