From 98cb838aa1ae73068d2bcfe015dbc54d348c04cb Mon Sep 17 00:00:00 2001 From: Diego Saint Esteben Date: Thu, 17 Dec 2015 00:41:56 -0300 Subject: [PATCH] Added the assets helper again --- .../Resources/config/templating_php.xml | 3 +- .../Templating/Helper/AssetsHelper.php | 67 +++++++++++++++++++ .../Templating/Helper/AssetsHelperTest.php | 44 ++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Templating/Helper/AssetsHelper.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/AssetsHelperTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml index 80148144cb..ba77ba93cb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml @@ -34,8 +34,7 @@ - - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/AssetsHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/AssetsHelper.php new file mode 100644 index 0000000000..072b6e7fc9 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/AssetsHelper.php @@ -0,0 +1,67 @@ + + * + * 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 + */ +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'; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/AssetsHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/AssetsHelperTest.php new file mode 100644 index 0000000000..7e9a25313c --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/AssetsHelperTest.php @@ -0,0 +1,44 @@ + + * + * 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 +{ + private $helper; + + protected function setUp() + { + $fooPackage = new Package(new StaticVersionStrategy('42', '%s?v=%s')); + $barPackage = new Package(new StaticVersionStrategy('22', '%s?%s')); + + $packages = new Packages($fooPackage, ['bar' => $barPackage]); + + $this->helper = new AssetsHelper($packages); + } + + public function testGetUrl() + { + $this->assertEquals('me.png?v=42', $this->helper->getUrl('me.png')); + $this->assertEquals('me.png?22', $this->helper->getUrl('me.png', 'bar')); + } + + public function testGetVersion() + { + $this->assertEquals('42', $this->helper->getVersion('/')); + $this->assertEquals('22', $this->helper->getVersion('/', 'bar')); + } +}