removed deprecated asset feature
This commit is contained in:
parent
bdd1ed2bb8
commit
8018d4bf6a
@ -22,16 +22,10 @@ use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
|
||||
class AssetExtension extends \Twig_Extension
|
||||
{
|
||||
private $packages;
|
||||
private $foundationExtension;
|
||||
|
||||
/**
|
||||
* Passing an HttpFoundationExtension instance as a second argument must not be relied on
|
||||
* as it's only there to maintain BC with older Symfony version. It will be removed in 3.0.
|
||||
*/
|
||||
public function __construct(Packages $packages, HttpFoundationExtension $foundationExtension = null)
|
||||
public function __construct(Packages $packages)
|
||||
{
|
||||
$this->packages = $packages;
|
||||
$this->foundationExtension = $foundationExtension;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -42,7 +36,6 @@ class AssetExtension extends \Twig_Extension
|
||||
return array(
|
||||
new \Twig_SimpleFunction('asset', array($this, 'getAssetUrl')),
|
||||
new \Twig_SimpleFunction('asset_version', array($this, 'getAssetVersion')),
|
||||
new \Twig_SimpleFunction('assets_version', array($this, 'getAssetsVersion')),
|
||||
);
|
||||
}
|
||||
|
||||
@ -57,20 +50,8 @@ class AssetExtension extends \Twig_Extension
|
||||
*
|
||||
* @return string The public path of the asset
|
||||
*/
|
||||
public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null)
|
||||
public function getAssetUrl($path, $packageName = null)
|
||||
{
|
||||
// BC layer to be removed in 3.0
|
||||
if (2 < $count = func_num_args()) {
|
||||
trigger_error('Generating absolute URLs with the Twig asset() function was deprecated in 2.7 and will be removed in 3.0. Please use absolute_url() instead.', E_USER_DEPRECATED);
|
||||
if (4 === $count) {
|
||||
trigger_error('Forcing a version with the Twig asset() function was deprecated in 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
$args = func_get_args();
|
||||
|
||||
return $this->getLegacyAssetUrl($path, $packageName, $args[2], isset($args[3]) ? $args[3] : null);
|
||||
}
|
||||
|
||||
return $this->packages->getUrl($path, $packageName);
|
||||
}
|
||||
|
||||
@ -87,51 +68,6 @@ class AssetExtension extends \Twig_Extension
|
||||
return $this->packages->getVersion($path, $packageName);
|
||||
}
|
||||
|
||||
public function getAssetsVersion($packageName = null)
|
||||
{
|
||||
trigger_error('The Twig assets_version() function was deprecated in 2.7 and will be removed in 3.0. Please use asset_version() instead.', E_USER_DEPRECATED);
|
||||
|
||||
return $this->packages->getVersion('/', $packageName);
|
||||
}
|
||||
|
||||
private function getLegacyAssetUrl($path, $packageName = null, $absolute = false, $version = null)
|
||||
{
|
||||
if ($version) {
|
||||
$package = $this->packages->getPackage($packageName);
|
||||
|
||||
$v = new \ReflectionProperty($package, 'versionStrategy');
|
||||
$v->setAccessible(true);
|
||||
|
||||
$currentVersionStrategy = $v->getValue($package);
|
||||
|
||||
$f = new \ReflectionProperty($currentVersionStrategy, 'format');
|
||||
$f->setAccessible(true);
|
||||
$format = $f->getValue($currentVersionStrategy);
|
||||
|
||||
$v->setValue($package, new StaticVersionStrategy($version, $format));
|
||||
}
|
||||
|
||||
try {
|
||||
$url = $this->packages->getUrl($path, $packageName);
|
||||
} catch (\Exception $e) {
|
||||
if ($version) {
|
||||
$v->setValue($package, $currentVersionStrategy);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if ($version) {
|
||||
$v->setValue($package, $currentVersionStrategy);
|
||||
}
|
||||
|
||||
if ($absolute) {
|
||||
return $this->foundationExtension->generateAbsoluteUrl($url);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the extension.
|
||||
*
|
||||
|
@ -1,40 +0,0 @@
|
||||
<?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\Bridge\Twig\Tests\Extension;
|
||||
|
||||
use Symfony\Bridge\Twig\Extension\AssetExtension;
|
||||
use Symfony\Component\Asset\Package;
|
||||
use Symfony\Component\Asset\Packages;
|
||||
use Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
|
||||
|
||||
class AssetExtensionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testLegacyGetAssetUrl()
|
||||
{
|
||||
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
|
||||
|
||||
$foundationExtension = $this->getMockBuilder('Symfony\Bridge\Twig\Extension\HttpFoundationExtension')->disableOriginalConstructor()->getMock();
|
||||
$foundationExtension
|
||||
->expects($this->any())
|
||||
->method('generateAbsoluteUrl')
|
||||
->will($this->returnCallback(function ($arg) { return 'http://localhost/'.$arg; }))
|
||||
;
|
||||
|
||||
$package = new Package(new StaticVersionStrategy('22', '%s?version=%s'));
|
||||
$packages = new Packages($package);
|
||||
$extension = new AssetExtension($packages, $foundationExtension);
|
||||
|
||||
$this->assertEquals('me.png?version=42', $extension->getAssetUrl('me.png', null, false, '42'));
|
||||
$this->assertEquals('http://localhost/me.png?version=22', $extension->getAssetUrl('me.png', null, true));
|
||||
$this->assertEquals('http://localhost/me.png?version=42', $extension->getAssetUrl('me.png', null, true, '42'));
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
<?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\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
trigger_error('The '.__NAMESPACE__.'\TemplatingAssetHelperPass class is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* @deprecated since 2.7, will be removed in 3.0
|
||||
*/
|
||||
class TemplatingAssetHelperPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->hasDefinition('templating.helper.assets')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$assetsHelperDefinition = $container->getDefinition('templating.helper.assets');
|
||||
$args = $assetsHelperDefinition->getArguments();
|
||||
|
||||
if ('request' === $this->getPackageScope($container, $args[0])) {
|
||||
$assetsHelperDefinition->setScope('request');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!array_key_exists(1, $args)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_array($args[1])) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($args[1] as $arg) {
|
||||
if ('request' === $this->getPackageScope($container, $arg)) {
|
||||
$assetsHelperDefinition->setScope('request');
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getPackageScope(ContainerBuilder $container, $package)
|
||||
{
|
||||
if ($package instanceof Reference) {
|
||||
return $container->findDefinition((string) $package)->getScope();
|
||||
}
|
||||
|
||||
if ($package instanceof Definition) {
|
||||
return $package->getScope();
|
||||
}
|
||||
|
||||
// Someone did some voodoo with a compiler pass. So we ignore this
|
||||
// 'package'. Can we be sure, it's a package anyway?
|
||||
}
|
||||
}
|
@ -53,44 +53,6 @@ class Configuration implements ConfigurationInterface
|
||||
return $v;
|
||||
})
|
||||
->end()
|
||||
->validate()
|
||||
->ifTrue(function ($v) { return isset($v['templating']); })
|
||||
->then(function ($v) {
|
||||
if ($v['templating']['assets_version']
|
||||
|| count($v['templating']['assets_base_urls']['http'])
|
||||
|| count($v['templating']['assets_base_urls']['ssl'])
|
||||
|| count($v['templating']['packages'])
|
||||
) {
|
||||
trigger_error('The assets settings under framework.templating are deprecated since version 2.7 and will be removed in 3.0. Use the framework.assets configuration key instead', E_USER_DEPRECATED);
|
||||
|
||||
// convert the old configuration to the new one
|
||||
if (isset($v['assets'])) {
|
||||
throw new LogicException('You cannot use assets settings under "templating.templating" and "assets" configurations in the same project.');
|
||||
}
|
||||
|
||||
$v['assets'] = array(
|
||||
'version' => $v['templating']['assets_version'],
|
||||
'version_format' => $v['templating']['assets_version_format'],
|
||||
'base_path' => '',
|
||||
'base_urls' => array_values(array_unique(array_merge($v['templating']['assets_base_urls']['http'], $v['templating']['assets_base_urls']['ssl']))),
|
||||
'packages' => array(),
|
||||
);
|
||||
|
||||
foreach ($v['templating']['packages'] as $name => $config) {
|
||||
$v['assets']['packages'][$name] = array(
|
||||
'version' => (string) $config['version'],
|
||||
'version_format' => $config['version_format'],
|
||||
'base_path' => '',
|
||||
'base_urls' => array_values(array_unique(array_merge($config['base_urls']['http'], $config['base_urls']['ssl']))),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
unset($v['templating']['assets_version'], $v['templating']['assets_version_format'], $v['templating']['assets_base_urls'], $v['templating']['packages']);
|
||||
|
||||
return $v;
|
||||
})
|
||||
->end()
|
||||
->children()
|
||||
->scalarNode('secret')->end()
|
||||
->scalarNode('http_method_override')
|
||||
@ -360,35 +322,14 @@ class Configuration implements ConfigurationInterface
|
||||
|
||||
private function addTemplatingSection(ArrayNodeDefinition $rootNode)
|
||||
{
|
||||
$organizeUrls = function ($urls) {
|
||||
$urls += array(
|
||||
'http' => array(),
|
||||
'ssl' => array(),
|
||||
);
|
||||
|
||||
foreach ($urls as $i => $url) {
|
||||
if (is_integer($i)) {
|
||||
if (0 === strpos($url, 'https://') || 0 === strpos($url, '//')) {
|
||||
$urls['http'][] = $urls['ssl'][] = $url;
|
||||
} else {
|
||||
$urls['http'][] = $url;
|
||||
}
|
||||
unset($urls[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
return $urls;
|
||||
};
|
||||
|
||||
$rootNode
|
||||
->children()
|
||||
->arrayNode('templating')
|
||||
->info('templating configuration')
|
||||
->canBeUnset()
|
||||
->children()
|
||||
->scalarNode('assets_version')->defaultNull()->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')->end()
|
||||
->scalarNode('assets_version_format')->defaultValue('%%s?%%s')->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')->end()
|
||||
->scalarNode('hinclude_default_template')->defaultNull()->end()
|
||||
->scalarNode('cache')->end()
|
||||
->arrayNode('form')
|
||||
->addDefaultsIfNotSet()
|
||||
->fixXmlConfig('resource')
|
||||
@ -406,31 +347,6 @@ class Configuration implements ConfigurationInterface
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->fixXmlConfig('assets_base_url')
|
||||
->children()
|
||||
->arrayNode('assets_base_urls')
|
||||
->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')
|
||||
->performNoDeepMerging()
|
||||
->addDefaultsIfNotSet()
|
||||
->beforeNormalization()
|
||||
->ifTrue(function ($v) { return !is_array($v); })
|
||||
->then(function ($v) { return array($v); })
|
||||
->end()
|
||||
->beforeNormalization()
|
||||
->always()
|
||||
->then($organizeUrls)
|
||||
->end()
|
||||
->children()
|
||||
->arrayNode('http')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->arrayNode('ssl')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->scalarNode('cache')->end()
|
||||
->end()
|
||||
->fixXmlConfig('engine')
|
||||
->children()
|
||||
->arrayNode('engines')
|
||||
@ -454,40 +370,6 @@ class Configuration implements ConfigurationInterface
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
->fixXmlConfig('package')
|
||||
->children()
|
||||
->arrayNode('packages')
|
||||
->info('Deprecated since 2.7, will be removed in 3.0. Use the new assets entry instead.')
|
||||
->useAttributeAsKey('name')
|
||||
->prototype('array')
|
||||
->fixXmlConfig('base_url')
|
||||
->children()
|
||||
->scalarNode('version')->defaultNull()->end()
|
||||
->scalarNode('version_format')->defaultValue('%%s?%%s')->end()
|
||||
->arrayNode('base_urls')
|
||||
->performNoDeepMerging()
|
||||
->addDefaultsIfNotSet()
|
||||
->beforeNormalization()
|
||||
->ifTrue(function ($v) { return !is_array($v); })
|
||||
->then(function ($v) { return array($v); })
|
||||
->end()
|
||||
->beforeNormalization()
|
||||
->always()
|
||||
->then($organizeUrls)
|
||||
->end()
|
||||
->children()
|
||||
->arrayNode('http')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->arrayNode('ssl')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
|
@ -152,13 +152,9 @@
|
||||
<xsd:sequence>
|
||||
<xsd:element name="loader" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
|
||||
<xsd:element name="engine" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
|
||||
<xsd:element name="assets-base-url" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
|
||||
<xsd:element name="package" type="old-package" minOccurs="0" maxOccurs="unbounded" />
|
||||
<xsd:element name="form" type="form-resources" minOccurs="0" maxOccurs="1" />
|
||||
</xsd:sequence>
|
||||
|
||||
<xsd:attribute name="assets-version" type="xsd:string" />
|
||||
<xsd:attribute name="assets-version-format" type="xsd:string" />
|
||||
<xsd:attribute name="cache" type="xsd:string" />
|
||||
<xsd:attribute name="hinclude-default-template" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
@ -169,16 +165,6 @@
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="old-package">
|
||||
<xsd:sequence>
|
||||
<xsd:element name="base-url" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
|
||||
</xsd:sequence>
|
||||
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="version" type="xsd:string" />
|
||||
<xsd:attribute name="version-format" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="translator">
|
||||
<xsd:sequence>
|
||||
<xsd:element name="fallback" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
|
||||
|
@ -1,49 +0,0 @@
|
||||
<?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\Asset;
|
||||
|
||||
trigger_error('The Symfony\Bundle\FrameworkBundle\Templating\Asset\PackageFactory is deprecated since version 2.7 and will be removed in 3.0. Use the Asset component instead.', E_USER_DEPRECATED);
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Templating\Asset\PackageInterface;
|
||||
|
||||
/**
|
||||
* Creates packages based on whether the current request is secure.
|
||||
*
|
||||
* @author Kris Wallsmith <kris@symfony.com>
|
||||
*
|
||||
* @deprecated since 2.7, will be removed in 3.0. Use the Asset component instead.
|
||||
*/
|
||||
class PackageFactory
|
||||
{
|
||||
private $container;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns either the HTTP or SSL version of an asset package.
|
||||
*
|
||||
* @param Request $request The current request
|
||||
* @param string $httpId The id for the package to use when the current request is HTTP
|
||||
* @param string $sslId The id for the package to use when the current request is SSL
|
||||
*
|
||||
* @return PackageInterface The package
|
||||
*/
|
||||
public function getPackage(Request $request, $httpId, $sslId)
|
||||
{
|
||||
return $this->container->get($request->isSecure() ? $sslId : $httpId);
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
<?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\Asset;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Templating\Asset\PathPackage as BasePathPackage;
|
||||
|
||||
trigger_error('The Symfony\Bundle\FrameworkBundle\Templating\Asset\PathPackage is deprecated since version 2.7 and will be removed in 3.0. Use the Asset component instead.', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* The path packages adds a version and a base path to asset URLs.
|
||||
*
|
||||
* @author Kris Wallsmith <kris@symfony.com>
|
||||
*
|
||||
* @deprecated since 2.7, will be removed in 3.0. Use the Asset component instead.
|
||||
*/
|
||||
class PathPackage extends BasePathPackage
|
||||
{
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Request $request The current request
|
||||
* @param string $version The version
|
||||
* @param string $format The version format
|
||||
*/
|
||||
public function __construct(Request $request, $version = null, $format = null)
|
||||
{
|
||||
parent::__construct($request->getBasePath(), $version, $format);
|
||||
}
|
||||
}
|
@ -1,126 +0,0 @@
|
||||
<?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\Asset\VersionStrategy\StaticVersionStrategy;
|
||||
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, $version = null)
|
||||
{
|
||||
// BC layer to be removed in 3.0
|
||||
if (3 === $count = func_num_args()) {
|
||||
trigger_error('Forcing a version for an asset was deprecated in 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
|
||||
|
||||
$args = func_get_args();
|
||||
|
||||
return $this->getLegacyAssetUrl($path, $packageName, $args[2]);
|
||||
}
|
||||
|
||||
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 = null, $packageName = null)
|
||||
{
|
||||
// no arguments means old getVersion() for default package
|
||||
if (null === $path) {
|
||||
trigger_error('The getVersion() method requires a path as a first argument since 2.7 and will be enforced as of 3.0.', E_USER_DEPRECATED);
|
||||
|
||||
return $this->packages->getVersion('/', $packageName);
|
||||
}
|
||||
|
||||
// path and packageName can only be for the new version
|
||||
if (null !== $packageName) {
|
||||
return $this->packages->getVersion($path, $packageName);
|
||||
}
|
||||
|
||||
// packageName is null and path not, so path is a path or a packageName
|
||||
try {
|
||||
$package = $this->packages->getPackage($path);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
// path is not a package, so it should be a path
|
||||
return $this->packages->getVersion($path);
|
||||
}
|
||||
|
||||
// path is a packageName, old version
|
||||
trigger_error('The getVersion() method requires a path as a first argument since 2.7 and will be enforced as of 3.0.', E_USER_DEPRECATED);
|
||||
|
||||
return $this->packages->getVersion('/', $path);
|
||||
}
|
||||
|
||||
private function getLegacyAssetUrl($path, $packageName = null, $version = null)
|
||||
{
|
||||
if ($version) {
|
||||
$package = $this->packages->getPackage($packageName);
|
||||
|
||||
$v = new \ReflectionProperty($package, 'versionStrategy');
|
||||
$v->setAccessible(true);
|
||||
|
||||
$currentVersionStrategy = $v->getValue($package);
|
||||
|
||||
$f = new \ReflectionProperty($currentVersionStrategy, 'format');
|
||||
$f->setAccessible(true);
|
||||
$format = $f->getValue($currentVersionStrategy);
|
||||
|
||||
$v->setValue($package, new StaticVersionStrategy($version, $format));
|
||||
}
|
||||
|
||||
$url = $this->packages->getUrl($path, $packageName);
|
||||
|
||||
if ($version) {
|
||||
$v->setValue($package, $currentVersionStrategy);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'assets';
|
||||
}
|
||||
}
|
@ -1,121 +0,0 @@
|
||||
<?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\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingAssetHelperPass;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
class LegacyTemplatingAssetHelperPassTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function getScopesTests()
|
||||
{
|
||||
return array(
|
||||
array('container'),
|
||||
array('request'),
|
||||
);
|
||||
}
|
||||
|
||||
/** @dataProvider getScopesTests */
|
||||
public function testFindLowestScopeInDefaultPackageWithReference($scope)
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$defaultPackage = new Definition('stdClass');
|
||||
$defaultPackage->setScope($scope);
|
||||
$container->setDefinition('default_package', $defaultPackage);
|
||||
|
||||
$definition = new Definition('stdClass', array(new Reference('default_package')));
|
||||
$container->setDefinition('templating.helper.assets', $definition);
|
||||
|
||||
$profilerPass = new TemplatingAssetHelperPass();
|
||||
$profilerPass->process($container);
|
||||
|
||||
$this->assertSame($scope, $definition->getScope());
|
||||
}
|
||||
|
||||
/** @dataProvider getScopesTests */
|
||||
public function testFindLowestScopeInDefaultPackageWithDefinition($scope)
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$defaultPackage = new Definition('stdClass');
|
||||
$defaultPackage->setScope($scope);
|
||||
|
||||
$definition = new Definition('stdClass', array($defaultPackage));
|
||||
$container->setDefinition('templating.helper.assets', $definition);
|
||||
|
||||
$profilerPass = new TemplatingAssetHelperPass();
|
||||
$profilerPass->process($container);
|
||||
|
||||
$this->assertSame($scope, $definition->getScope());
|
||||
}
|
||||
|
||||
/** @dataProvider getScopesTests */
|
||||
public function testFindLowestScopeInNamedPackageWithReference($scope)
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$defaultPackage = new Definition('stdClass');
|
||||
$container->setDefinition('default_package', $defaultPackage);
|
||||
|
||||
$aPackage = new Definition('stdClass');
|
||||
$container->setDefinition('a_package', $aPackage);
|
||||
|
||||
$bPackage = new Definition('stdClass');
|
||||
$bPackage->setScope($scope);
|
||||
$container->setDefinition('b_package', $bPackage);
|
||||
|
||||
$cPackage = new Definition('stdClass');
|
||||
$container->setDefinition('c_package', $cPackage);
|
||||
|
||||
$definition = new Definition('stdClass', array(new Reference('default_package'), array(
|
||||
new Reference('a_package'),
|
||||
new Reference('b_package'),
|
||||
new Reference('c_package'),
|
||||
)));
|
||||
$container->setDefinition('templating.helper.assets', $definition);
|
||||
|
||||
$profilerPass = new TemplatingAssetHelperPass();
|
||||
$profilerPass->process($container);
|
||||
|
||||
$this->assertSame($scope, $definition->getScope());
|
||||
}
|
||||
|
||||
/** @dataProvider getScopesTests */
|
||||
public function testFindLowestScopeInNamedPackageWithDefinition($scope)
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$defaultPackage = new Definition('stdClass');
|
||||
|
||||
$aPackage = new Definition('stdClass');
|
||||
|
||||
$bPackage = new Definition('stdClass');
|
||||
$bPackage->setScope($scope);
|
||||
|
||||
$cPackage = new Definition('stdClass');
|
||||
|
||||
$definition = new Definition('stdClass', array($defaultPackage, array(
|
||||
$aPackage,
|
||||
$bPackage,
|
||||
$cPackage,
|
||||
)));
|
||||
$container->setDefinition('templating.helper.assets', $definition);
|
||||
|
||||
$profilerPass = new TemplatingAssetHelperPass();
|
||||
$profilerPass->process($container);
|
||||
|
||||
$this->assertSame($scope, $definition->getScope());
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
$container->loadFromExtension('framework', array(
|
||||
'templating' => array(
|
||||
'engines' => array('php'),
|
||||
'assets_version' => 'SomeVersionScheme',
|
||||
'assets_base_urls' => 'http://cdn.example.com',
|
||||
'assets_version_format' => '%%s?version=%%s',
|
||||
'packages' => array(
|
||||
'images' => array(
|
||||
'version' => '1.0.0',
|
||||
'base_urls' => array('http://images1.example.com', 'http://images2.example.com'),
|
||||
),
|
||||
'foo' => array(
|
||||
'version' => '1.0.0',
|
||||
'version_format' => '%%s-%%s',
|
||||
),
|
||||
'bar' => array(
|
||||
'base_urls' => array('https://bar2.example.com'),
|
||||
),
|
||||
),
|
||||
),
|
||||
));
|
@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
$container->loadFromExtension('framework', array(
|
||||
'secret' => 's3cr3t',
|
||||
'templating' => array(
|
||||
'assets_base_urls' => 'https://cdn.example.com',
|
||||
'engines' => array('php', 'twig'),
|
||||
'packages' => array(
|
||||
'images' => array(
|
||||
'base_urls' => 'https://images.example.com',
|
||||
),
|
||||
),
|
||||
),
|
||||
));
|
@ -1,15 +0,0 @@
|
||||
framework:
|
||||
templating:
|
||||
engines: [php]
|
||||
assets_version: SomeVersionScheme
|
||||
assets_version_format: %%s?version=%%s
|
||||
assets_base_urls: http://cdn.example.com
|
||||
packages:
|
||||
images:
|
||||
version: 1.0.0
|
||||
base_urls: ["http://images1.example.com", "http://images2.example.com"]
|
||||
foo:
|
||||
version: 1.0.0
|
||||
version_format: %%s-%%s
|
||||
bar:
|
||||
base_urls: "https://bar2.example.com"
|
@ -1,8 +0,0 @@
|
||||
framework:
|
||||
secret: s3cr3t
|
||||
templating:
|
||||
assets_base_urls: https://cdn.example.com
|
||||
engines: [php, twig]
|
||||
packages:
|
||||
images:
|
||||
base_urls: https://images.example.com
|
@ -203,16 +203,30 @@ abstract class FrameworkExtensionTest extends TestCase
|
||||
$this->assertEquals('global_hinclude_template', $container->getParameter('fragment.renderer.hinclude.global_template'), '->registerTemplatingConfiguration() registers the global hinclude.js template');
|
||||
}
|
||||
|
||||
public function testLegacyTemplatingAssets()
|
||||
{
|
||||
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
|
||||
|
||||
$this->checkAssetsPackages($this->createContainerFromFile('legacy_templating_assets'), true);
|
||||
}
|
||||
|
||||
public function testAssets()
|
||||
{
|
||||
$this->checkAssetsPackages($this->createContainerFromFile('assets'));
|
||||
$container = $this->createContainerFromFile('assets');
|
||||
$packages = $container->getDefinition('assets.packages');
|
||||
|
||||
// default package
|
||||
$defaultPackage = $container->getDefinition($packages->getArgument(0));
|
||||
$this->assertUrlPackage($container, $defaultPackage, array('http://cdn.example.com'), 'SomeVersionScheme', '%%s?version=%%s');
|
||||
|
||||
// packages
|
||||
$packages = $packages->getArgument(1);
|
||||
$this->assertCount(4, $packages);
|
||||
|
||||
$package = $container->getDefinition($packages['images_path']);
|
||||
$this->assertPathPackage($container, $package, '/foo', 'SomeVersionScheme', '%%s?version=%%s');
|
||||
|
||||
$package = $container->getDefinition($packages['images']);
|
||||
$this->assertUrlPackage($container, $package, array('http://images1.example.com', 'http://images2.example.com'), '1.0.0', '%%s?version=%%s');
|
||||
|
||||
$package = $container->getDefinition($packages['foo']);
|
||||
$this->assertPathPackage($container, $package, '', '1.0.0', '%%s-%%s');
|
||||
|
||||
$package = $container->getDefinition($packages['bar']);
|
||||
$this->assertUrlPackage($container, $package, array('https://bar2.example.com'), 'SomeVersionScheme', '%%s?version=%%s');
|
||||
}
|
||||
|
||||
public function testTranslator()
|
||||
@ -535,33 +549,6 @@ abstract class FrameworkExtensionTest extends TestCase
|
||||
return $container;
|
||||
}
|
||||
|
||||
private function checkAssetsPackages(ContainerBuilder $container, $legacy = false)
|
||||
{
|
||||
$packages = $container->getDefinition('assets.packages');
|
||||
|
||||
// default package
|
||||
$defaultPackage = $container->getDefinition($packages->getArgument(0));
|
||||
$this->assertUrlPackage($container, $defaultPackage, array('http://cdn.example.com'), 'SomeVersionScheme', '%%s?version=%%s');
|
||||
|
||||
// packages
|
||||
$packages = $packages->getArgument(1);
|
||||
$this->assertCount($legacy ? 3 : 4, $packages);
|
||||
|
||||
if (!$legacy) {
|
||||
$package = $container->getDefinition($packages['images_path']);
|
||||
$this->assertPathPackage($container, $package, '/foo', 'SomeVersionScheme', '%%s?version=%%s');
|
||||
}
|
||||
|
||||
$package = $container->getDefinition($packages['images']);
|
||||
$this->assertUrlPackage($container, $package, array('http://images1.example.com', 'http://images2.example.com'), '1.0.0', $legacy ? '%%s?%%s' : '%%s?version=%%s');
|
||||
|
||||
$package = $container->getDefinition($packages['foo']);
|
||||
$this->assertPathPackage($container, $package, '', '1.0.0', '%%s-%%s');
|
||||
|
||||
$package = $container->getDefinition($packages['bar']);
|
||||
$this->assertUrlPackage($container, $package, array('https://bar2.example.com'), $legacy ? '' : 'SomeVersionScheme', $legacy ? '%%s?%%s' : '%%s?version=%%s');
|
||||
}
|
||||
|
||||
private function assertPathPackage(ContainerBuilder $container, Definition $package, $basePath, $version, $format)
|
||||
{
|
||||
$this->assertEquals('assets.path_package', $package->getParent());
|
||||
|
@ -1,46 +0,0 @@
|
||||
<?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 testLegacyGetUrl()
|
||||
{
|
||||
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
|
||||
|
||||
$package = new Package(new StaticVersionStrategy('22', '%s?version=%s'));
|
||||
$packages = new Packages($package);
|
||||
$helper = new AssetsHelper($packages);
|
||||
|
||||
$this->assertEquals('me.png?version=42', $helper->getUrl('me.png', null, '42'));
|
||||
}
|
||||
|
||||
public function testLegacyGetVersion()
|
||||
{
|
||||
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
|
||||
|
||||
$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());
|
||||
$this->assertEquals('22', $helper->getVersion('/foo'));
|
||||
$this->assertEquals('42', $helper->getVersion('images'));
|
||||
$this->assertEquals('42', $helper->getVersion('/foo', 'images'));
|
||||
}
|
||||
}
|
@ -1,129 +0,0 @@
|
||||
<?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\TwigBundle\Extension;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
trigger_error('The '.__NAMESPACE__.'\AssetsExtension class is deprecated since version 2.7 and will be removed in 3.0. Use the Symfony\Bridge\Twig\Extension\AssetExtension class instead.', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* Twig extension for Symfony assets helper.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* @deprecated since 2.7, to be removed in 3.0. Use Symfony\Component\Twig\Extension\AssetExtension instead.
|
||||
*/
|
||||
class AssetsExtension extends \Twig_Extension
|
||||
{
|
||||
private $container;
|
||||
private $context;
|
||||
|
||||
public function __construct(ContainerInterface $container, RequestContext $requestContext = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->context = $requestContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of functions to add to the existing list.
|
||||
*
|
||||
* @return array An array of functions
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
new \Twig_SimpleFunction('asset', array($this, 'getAssetUrl')),
|
||||
new \Twig_SimpleFunction('assets_version', array($this, 'getAssetsVersion')),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the public path of an asset.
|
||||
*
|
||||
* 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 bool $absolute Whether to return an absolute URL or a relative one
|
||||
* @param string|bool|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, $absolute = false, $version = null)
|
||||
{
|
||||
$url = $this->container->get('templating.helper.assets')->getUrl($path, $packageName, $version);
|
||||
|
||||
if (!$absolute) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
return $this->ensureUrlIsAbsolute($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the version of the assets in a package.
|
||||
*
|
||||
* @param string $packageName
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getAssetsVersion($packageName = null)
|
||||
{
|
||||
return $this->container->get('templating.helper.assets')->getVersion($packageName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the extension.
|
||||
*
|
||||
* @return string The extension name
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'assets';
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures an URL is absolute, if possible.
|
||||
*
|
||||
* @param string $url The URL that has to be absolute
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*
|
||||
* @return string The absolute URL
|
||||
*/
|
||||
private function ensureUrlIsAbsolute($url)
|
||||
{
|
||||
if (false !== strpos($url, '://') || 0 === strpos($url, '//')) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
if (!$this->context) {
|
||||
throw new \RuntimeException('To generate an absolute URL for an asset, the Symfony Routing component is required.');
|
||||
}
|
||||
|
||||
if ('' === $host = $this->context->getHost()) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$scheme = $this->context->getScheme();
|
||||
$port = '';
|
||||
|
||||
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
|
||||
$port = ':'.$this->context->getHttpPort();
|
||||
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
|
||||
$port = ':'.$this->context->getHttpsPort();
|
||||
}
|
||||
|
||||
return $scheme.'://'.$host.$port.$url;
|
||||
}
|
||||
}
|
@ -87,7 +87,6 @@
|
||||
<service id="twig.extension.assets" class="Symfony\Bridge\Twig\Extension\AssetExtension" public="false">
|
||||
<tag name="twig.extension" />
|
||||
<argument type="service" id="assets.packages" />
|
||||
<argument type="service" id="twig.extension.httpfoundation" />
|
||||
</service>
|
||||
|
||||
<service id="twig.extension.code" class="%twig.extension.code.class%" public="false">
|
||||
|
@ -1,106 +0,0 @@
|
||||
<?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\TwigBundle\Tests\Extension;
|
||||
|
||||
use Symfony\Bundle\TwigBundle\Extension\AssetsExtension;
|
||||
use Symfony\Bundle\TwigBundle\Tests\TestCase;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
class LegacyAssetsExtensionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideGetGetAssetUrlArguments
|
||||
*/
|
||||
public function testGetAssetUrl($path, $packageName, $absolute, $relativeUrl, $expectedUrl, $scheme, $host, $httpPort, $httpsPort)
|
||||
{
|
||||
$helper = $this->createHelperMock($path, $packageName, $relativeUrl);
|
||||
$container = $this->createContainerMock($helper);
|
||||
|
||||
$context = $this->createRequestContextMock($scheme, $host, $httpPort, $httpsPort);
|
||||
|
||||
$extension = new AssetsExtension($container, $context);
|
||||
$this->assertEquals($expectedUrl, $extension->getAssetUrl($path, $packageName, $absolute));
|
||||
}
|
||||
|
||||
public function testGetAssetWithoutHost()
|
||||
{
|
||||
$path = '/path/to/asset';
|
||||
$packageName = null;
|
||||
$relativeUrl = '/bundle-name/path/to/asset';
|
||||
|
||||
$helper = $this->createHelperMock($path, $packageName, $relativeUrl);
|
||||
$container = $this->createContainerMock($helper);
|
||||
|
||||
$context = $this->createRequestContextMock('http', '', 80, 443);
|
||||
|
||||
$extension = new AssetsExtension($container, $context);
|
||||
$this->assertEquals($relativeUrl, $extension->getAssetUrl($path, $packageName, true));
|
||||
}
|
||||
|
||||
public function provideGetGetAssetUrlArguments()
|
||||
{
|
||||
return array(
|
||||
array('/path/to/asset', 'package-name', false, '/bundle-name/path/to/asset', '/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null),
|
||||
array('/path/to/asset', 'package-name', false, 'http://subdomain.symfony.com/bundle-name/path/to/asset', 'http://subdomain.symfony.com/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null),
|
||||
array('/path/to/asset', null, false, '/bundle-name/path/to/asset', '/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null),
|
||||
array('/path/to/asset', 'package-name', true, '/bundle-name/path/to/asset', 'http://symfony.com/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null),
|
||||
array('/path/to/asset', 'package-name', true, 'http://subdomain.symfony.com/bundle-name/path/to/asset', 'http://subdomain.symfony.com/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null),
|
||||
array('/path/to/asset', null, true, '/bundle-name/path/to/asset', 'https://symfony.com:92/bundle-name/path/to/asset', 'https', 'symfony.com', null, 92),
|
||||
array('/path/to/asset', null, true, '/bundle-name/path/to/asset', 'http://symfony.com:660/bundle-name/path/to/asset', 'http', 'symfony.com', 660, null),
|
||||
);
|
||||
}
|
||||
|
||||
private function createRequestContextMock($scheme, $host, $httpPort, $httpsPort)
|
||||
{
|
||||
$context = $this->getMockBuilder('Symfony\Component\Routing\RequestContext')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$context->expects($this->any())
|
||||
->method('getScheme')
|
||||
->will($this->returnValue($scheme));
|
||||
$context->expects($this->any())
|
||||
->method('getHost')
|
||||
->will($this->returnValue($host));
|
||||
$context->expects($this->any())
|
||||
->method('getHttpPort')
|
||||
->will($this->returnValue($httpPort));
|
||||
$context->expects($this->any())
|
||||
->method('getHttpsPort')
|
||||
->will($this->returnValue($httpsPort));
|
||||
|
||||
return $context;
|
||||
}
|
||||
|
||||
private function createContainerMock($helper)
|
||||
{
|
||||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
||||
$container->expects($this->any())
|
||||
->method('get')
|
||||
->with('templating.helper.assets')
|
||||
->will($this->returnValue($helper));
|
||||
|
||||
return $container;
|
||||
}
|
||||
|
||||
private function createHelperMock($path, $packageName, $returnValue)
|
||||
{
|
||||
$helper = $this->getMockBuilder('Symfony\Component\Templating\Helper\CoreAssetsHelper')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$helper->expects($this->any())
|
||||
->method('getUrl')
|
||||
->with($path, $packageName)
|
||||
->will($this->returnValue($returnValue));
|
||||
|
||||
return $helper;
|
||||
}
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
<?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\Component\Templating\Asset;
|
||||
|
||||
trigger_error('The Symfony\Component\Templating\Asset\Package is deprecated since version 2.7 and will be removed in 3.0. Use the Asset component instead.', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* The basic package will add a version to asset URLs.
|
||||
*
|
||||
* @author Kris Wallsmith <kris@symfony.com>
|
||||
*
|
||||
* @deprecated since 2.7, will be removed in 3.0. Use the Asset component instead.
|
||||
*/
|
||||
class Package implements PackageInterface
|
||||
{
|
||||
private $version;
|
||||
private $format;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $version The package version
|
||||
* @param string $format The format used to apply the version
|
||||
*/
|
||||
public function __construct($version = null, $format = '')
|
||||
{
|
||||
$this->version = $version;
|
||||
$this->format = $format ?: '%s?%s';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getUrl($path, $version = null)
|
||||
{
|
||||
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
return $this->applyVersion($path, $version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies version to the supplied path.
|
||||
*
|
||||
* @param string $path A path
|
||||
* @param string|bool|null $version A specific version
|
||||
*
|
||||
* @return string The versionized path
|
||||
*/
|
||||
protected function applyVersion($path, $version = null)
|
||||
{
|
||||
$version = null !== $version ? $version : $this->version;
|
||||
if (null === $version || false === $version) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
$versionized = sprintf($this->format, ltrim($path, '/'), $version);
|
||||
|
||||
if ($path && '/' == $path[0]) {
|
||||
$versionized = '/'.$versionized;
|
||||
}
|
||||
|
||||
return $versionized;
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
<?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\Component\Templating\Asset;
|
||||
|
||||
trigger_error('The Symfony\Component\Templating\Asset\PackageInterface is deprecated since version 2.7 and will be removed in 3.0. Use the Asset component instead.', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* Asset package interface.
|
||||
*
|
||||
* @author Kris Wallsmith <kris@symfony.com>
|
||||
*
|
||||
* @deprecated since 2.7, will be removed in 3.0. Use the Asset component instead.
|
||||
*/
|
||||
interface PackageInterface
|
||||
{
|
||||
/**
|
||||
* Returns the asset package version.
|
||||
*
|
||||
* @return string The version string
|
||||
*/
|
||||
public function getVersion();
|
||||
|
||||
/**
|
||||
* Returns an absolute or root-relative public path.
|
||||
*
|
||||
* @param string $path A path
|
||||
* @param string|bool|null $version A specific version for the path
|
||||
*
|
||||
* @return string The public path
|
||||
*/
|
||||
public function getUrl($path, $version = null);
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
<?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\Component\Templating\Asset;
|
||||
|
||||
trigger_error('The Symfony\Component\Templating\Asset\PathPackage is deprecated since version 2.7 and will be removed in 3.0. Use the Asset component instead.', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* The path packages adds a version and a base path to asset URLs.
|
||||
*
|
||||
* @author Kris Wallsmith <kris@symfony.com>
|
||||
*
|
||||
* @deprecated since 2.7, will be removed in 3.0. Use the Asset component instead.
|
||||
*/
|
||||
class PathPackage extends Package
|
||||
{
|
||||
private $basePath;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $basePath The base path to be prepended to relative paths
|
||||
* @param string $version The package version
|
||||
* @param string $format The format used to apply the version
|
||||
*/
|
||||
public function __construct($basePath = null, $version = null, $format = null)
|
||||
{
|
||||
parent::__construct($version, $format);
|
||||
|
||||
if (!$basePath) {
|
||||
$this->basePath = '/';
|
||||
} else {
|
||||
if ('/' != $basePath[0]) {
|
||||
$basePath = '/'.$basePath;
|
||||
}
|
||||
|
||||
$this->basePath = rtrim($basePath, '/').'/';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getUrl($path, $version = null)
|
||||
{
|
||||
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
$url = $this->applyVersion($path, $version);
|
||||
|
||||
// apply the base path
|
||||
if ('/' !== substr($url, 0, 1)) {
|
||||
$url = $this->basePath.$url;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the base path.
|
||||
*
|
||||
* @return string The base path
|
||||
*/
|
||||
public function getBasePath()
|
||||
{
|
||||
return $this->basePath;
|
||||
}
|
||||
}
|
@ -1,86 +0,0 @@
|
||||
<?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\Component\Templating\Asset;
|
||||
|
||||
trigger_error('The Symfony\Component\Templating\Asset\UrlPackage is deprecated since version 2.7 and will be removed in 3.0. Use the Asset component instead.', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* The URL packages adds a version and a base URL to asset URLs.
|
||||
*
|
||||
* @author Kris Wallsmith <kris@symfony.com>
|
||||
*
|
||||
* @deprecated since 2.7, will be removed in 3.0. Use the Asset component instead.
|
||||
*/
|
||||
class UrlPackage extends Package
|
||||
{
|
||||
private $baseUrls;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string|array $baseUrls Base asset URLs
|
||||
* @param string $version The package version
|
||||
* @param string $format The format used to apply the version
|
||||
*/
|
||||
public function __construct($baseUrls = array(), $version = null, $format = null)
|
||||
{
|
||||
parent::__construct($version, $format);
|
||||
|
||||
if (!is_array($baseUrls)) {
|
||||
$baseUrls = (array) $baseUrls;
|
||||
}
|
||||
|
||||
$this->baseUrls = array();
|
||||
foreach ($baseUrls as $baseUrl) {
|
||||
$this->baseUrls[] = rtrim($baseUrl, '/');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getUrl($path, $version = null)
|
||||
{
|
||||
if (false !== strpos($path, '://') || 0 === strpos($path, '//')) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
$url = $this->applyVersion($path, $version);
|
||||
|
||||
if ($url && '/' != $url[0]) {
|
||||
$url = '/'.$url;
|
||||
}
|
||||
|
||||
return $this->getBaseUrl($path).$url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the base URL for a path.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return string The base URL
|
||||
*/
|
||||
public function getBaseUrl($path)
|
||||
{
|
||||
switch ($count = count($this->baseUrls)) {
|
||||
case 0:
|
||||
return '';
|
||||
|
||||
case 1:
|
||||
return $this->baseUrls[0];
|
||||
|
||||
default:
|
||||
return $this->baseUrls[fmod(hexdec(substr(hash('sha256', $path), 0, 10)), $count)];
|
||||
}
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
<?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\Component\Templating\Helper;
|
||||
|
||||
trigger_error('The Symfony\Component\Templating\Helper\AssetsHelper is deprecated since version 2.7 and will be removed in 3.0. Use the Asset component instead.', E_USER_DEPRECATED);
|
||||
|
||||
use Symfony\Component\Templating\Asset\PathPackage;
|
||||
use Symfony\Component\Templating\Asset\UrlPackage;
|
||||
|
||||
/**
|
||||
* AssetsHelper helps manage asset URLs.
|
||||
*
|
||||
* Usage:
|
||||
*
|
||||
* <code>
|
||||
* <img src="<?php echo $view['assets']->getUrl('foo.png') ?>" />
|
||||
* </code>
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Kris Wallsmith <kris@symfony.com>
|
||||
*
|
||||
* @deprecated since 2.7, will be removed in 3.0. Use the Asset component instead.
|
||||
*/
|
||||
class AssetsHelper extends CoreAssetsHelper
|
||||
{
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $basePath The base path
|
||||
* @param string|array $baseUrls Base asset URLs
|
||||
* @param string $version The asset version
|
||||
* @param string $format The version format
|
||||
* @param array $namedPackages Additional packages
|
||||
*/
|
||||
public function __construct($basePath = null, $baseUrls = array(), $version = null, $format = null, $namedPackages = array())
|
||||
{
|
||||
if ($baseUrls) {
|
||||
$defaultPackage = new UrlPackage($baseUrls, $version, $format);
|
||||
} else {
|
||||
$defaultPackage = new PathPackage($basePath, $version, $format);
|
||||
}
|
||||
|
||||
parent::__construct($defaultPackage, $namedPackages);
|
||||
}
|
||||
}
|
@ -1,132 +0,0 @@
|
||||
<?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\Component\Templating\Helper;
|
||||
|
||||
trigger_error('The Symfony\Component\Templating\Helper\CoreAssetsHelper is deprecated since version 2.7 and will be removed in 3.0. Use the Asset component instead.', E_USER_DEPRECATED);
|
||||
|
||||
use Symfony\Component\Templating\Asset\PackageInterface;
|
||||
|
||||
/**
|
||||
* CoreAssetsHelper helps manage asset URLs.
|
||||
*
|
||||
* Usage:
|
||||
*
|
||||
* <code>
|
||||
* <img src="<?php echo $view['assets']->getUrl('foo.png') ?>" />
|
||||
* </code>
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Kris Wallsmith <kris@symfony.com>
|
||||
*
|
||||
* @deprecated since 2.7, will be removed in 3.0. Use the Asset component instead.
|
||||
*/
|
||||
class CoreAssetsHelper extends Helper implements PackageInterface
|
||||
{
|
||||
protected $defaultPackage;
|
||||
protected $namedPackages = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param PackageInterface $defaultPackage The default package
|
||||
* @param array $namedPackages Additional packages indexed by name
|
||||
*/
|
||||
public function __construct(PackageInterface $defaultPackage, array $namedPackages = array())
|
||||
{
|
||||
$this->defaultPackage = $defaultPackage;
|
||||
|
||||
foreach ($namedPackages as $name => $package) {
|
||||
$this->addPackage($name, $package);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default package.
|
||||
*
|
||||
* @param PackageInterface $defaultPackage The default package
|
||||
*/
|
||||
public function setDefaultPackage(PackageInterface $defaultPackage)
|
||||
{
|
||||
$this->defaultPackage = $defaultPackage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an asset package to the helper.
|
||||
*
|
||||
* @param string $name The package name
|
||||
* @param PackageInterface $package The package
|
||||
*/
|
||||
public function addPackage($name, PackageInterface $package)
|
||||
{
|
||||
$this->namedPackages[$name] = $package;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an asset package.
|
||||
*
|
||||
* @param string $name The name of the package or null for the default package
|
||||
*
|
||||
* @return PackageInterface An asset package
|
||||
*
|
||||
* @throws \InvalidArgumentException If there is no package by that name
|
||||
*/
|
||||
public function getPackage($name = null)
|
||||
{
|
||||
if (null === $name) {
|
||||
return $this->defaultPackage;
|
||||
}
|
||||
|
||||
if (!isset($this->namedPackages[$name])) {
|
||||
throw new \InvalidArgumentException(sprintf('There is no "%s" asset package.', $name));
|
||||
}
|
||||
|
||||
return $this->namedPackages[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the version to add to public URL.
|
||||
*
|
||||
* @param string $packageName A package name
|
||||
*
|
||||
* @return string The current version
|
||||
*/
|
||||
public function getVersion($packageName = null)
|
||||
{
|
||||
return $this->getPackage($packageName)->getVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the public path.
|
||||
*
|
||||
* 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|bool|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, $version = null)
|
||||
{
|
||||
return $this->getPackage($packageName)->getUrl($path, $version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the canonical name of this helper.
|
||||
*
|
||||
* @return string The canonical name
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'assets';
|
||||
}
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
<?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\Component\Templating\Tests\Helper;
|
||||
|
||||
use Symfony\Component\Templating\Helper\AssetsHelper;
|
||||
|
||||
class LegacyAssetsHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
public function testGetVersion()
|
||||
{
|
||||
$helper = new AssetsHelper(null, array(), 'foo');
|
||||
$this->assertEquals('foo', $helper->getVersion(), '->getVersion() returns the version');
|
||||
}
|
||||
|
||||
public function testGetUrl()
|
||||
{
|
||||
$helper = new AssetsHelper();
|
||||
$this->assertEquals('http://example.com/foo.js', $helper->getUrl('http://example.com/foo.js'), '->getUrl() does nothing if an absolute URL is given');
|
||||
|
||||
$helper = new AssetsHelper();
|
||||
$this->assertEquals('/foo.js', $helper->getUrl('foo.js'), '->getUrl() appends a / on relative paths');
|
||||
$this->assertEquals('/foo.js', $helper->getUrl('/foo.js'), '->getUrl() does nothing on absolute paths');
|
||||
|
||||
$helper = new AssetsHelper('/foo');
|
||||
$this->assertEquals('/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() appends the basePath on relative paths');
|
||||
$this->assertEquals('/foo.js', $helper->getUrl('/foo.js'), '->getUrl() does not append the basePath on absolute paths');
|
||||
|
||||
$helper = new AssetsHelper(null, 'http://assets.example.com/');
|
||||
$this->assertEquals('http://assets.example.com/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL');
|
||||
$this->assertEquals('http://assets.example.com/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL');
|
||||
|
||||
$helper = new AssetsHelper(null, 'http://www.example.com/foo');
|
||||
$this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL with a path');
|
||||
$this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL with a path');
|
||||
|
||||
$helper = new AssetsHelper('/foo', 'http://www.example.com/');
|
||||
$this->assertEquals('http://www.example.com/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
|
||||
$this->assertEquals('http://www.example.com/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL but not the base path on absolute paths');
|
||||
|
||||
$helper = new AssetsHelper('/bar', 'http://www.example.com/foo');
|
||||
$this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('foo.js'), '->getUrl() prepends the base URL and the base path if defined');
|
||||
$this->assertEquals('http://www.example.com/foo/foo.js', $helper->getUrl('/foo.js'), '->getUrl() prepends the base URL but not the base path on absolute paths');
|
||||
|
||||
$helper = new AssetsHelper('/bar', 'http://www.example.com/foo', 'abcd');
|
||||
$this->assertEquals('http://www.example.com/foo/foo.js?abcd', $helper->getUrl('foo.js'), '->getUrl() appends the version if defined');
|
||||
|
||||
$helper = new AssetsHelper();
|
||||
$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');
|
||||
$this->assertEquals('//bar.com/asset', $helper->getUrl('//bar.com/asset'));
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
<?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\Component\Templating\Tests\Helper;
|
||||
|
||||
use Symfony\Component\Templating\Helper\CoreAssetsHelper;
|
||||
|
||||
class LegacyCoreAssetsHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $package;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
|
||||
|
||||
$this->package = $this->getMock('Symfony\Component\Templating\Asset\PackageInterface');
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->package = null;
|
||||
}
|
||||
|
||||
public function testAddGetPackage()
|
||||
{
|
||||
$helper = new CoreAssetsHelper($this->package);
|
||||
|
||||
$helper->addPackage('foo', $this->package);
|
||||
|
||||
$this->assertSame($this->package, $helper->getPackage('foo'));
|
||||
}
|
||||
|
||||
public function testGetNonexistingPackage()
|
||||
{
|
||||
$helper = new CoreAssetsHelper($this->package);
|
||||
|
||||
$this->setExpectedException('\InvalidArgumentException');
|
||||
|
||||
$helper->getPackage('foo');
|
||||
}
|
||||
|
||||
public function testGetHelperName()
|
||||
{
|
||||
$helper = new CoreAssetsHelper($this->package);
|
||||
|
||||
$this->assertEquals('assets', $helper->getName());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user