Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit48d3d15

Browse files
committed
renamed asset_path() to asset() and added a BC layer
1 parent2f9edd9 commit48d3d15

File tree

34 files changed

+613
-180
lines changed

34 files changed

+613
-180
lines changed

‎src/Symfony/Bridge/Twig/CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CHANGELOG
55
-----
66

77
* added an HttpFoundation extension (provides the`absolute_url` and the`relative_path` functions)
8-
* added AssetExtension (provides the`asset_path` function)
8+
* added AssetExtension (provides the`asset` and`asset_version` functions)
99

1010
2.5.0
1111
-----

‎src/Symfony/Bridge/Twig/Extension/AssetExtension.php‎

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespaceSymfony\Bridge\Twig\Extension;
1313

1414
useSymfony\Component\Asset\Packages;
15+
useSymfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
1516

1617
/**
1718
* Twig extension for the Symfony Asset component.
@@ -21,10 +22,16 @@
2122
class AssetExtensionextends \Twig_Extension
2223
{
2324
private$packages;
25+
private$foundationExtension;
2426

25-
publicfunction__construct(Packages$packages)
27+
/**
28+
* Passing an HttpFoundationExtension instance as a second argument must not be relied on
29+
* as it's only there to maintain BC with older Symfony version. It will be removed in 3.0.
30+
*/
31+
publicfunction__construct(Packages$packages,HttpFoundationExtension$foundationExtension =null)
2632
{
2733
$this->packages =$packages;
34+
$this->foundationExtension =$foundationExtension;
2835
}
2936

3037
/**
@@ -33,13 +40,14 @@ public function __construct(Packages $packages)
3340
publicfunctiongetFunctions()
3441
{
3542
returnarray(
36-
new \Twig_SimpleFunction('asset_path',array($this,'getAssetPath')),
43+
new \Twig_SimpleFunction('asset',array($this,'getAssetUrl')),
3744
new \Twig_SimpleFunction('asset_version',array($this,'getAssetVersion')),
45+
new \Twig_SimpleFunction('assets_version',array($this,'getAssetsVersion')),
3846
);
3947
}
4048

4149
/**
42-
* Returns the public path of an asset.
50+
* Returns the publicurl/path of an asset.
4351
*
4452
* If the package used to generate the path is an instance of
4553
* UrlPackage, you will always get a URL and not a path.
@@ -49,8 +57,20 @@ public function getFunctions()
4957
*
5058
* @return string The public path of the asset
5159
*/
52-
publicfunctiongetAssetPath($path,$packageName =null)
60+
publicfunctiongetAssetUrl($path,$packageName =null,$absolute =false,$version =null)
5361
{
62+
// BC layer to be removed in 3.0
63+
if (2 <$count =func_num_args()) {
64+
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);
65+
if (4 ===$count) {
66+
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);
67+
}
68+
69+
$args =func_get_args();
70+
71+
return$this->getLegacyAssetUrl($path,$packageName,$args[2],isset($args[3]) ?$args[3] :null);
72+
}
73+
5474
return$this->packages->getUrl($path,$packageName);
5575
}
5676

@@ -67,6 +87,51 @@ public function getAssetVersion($path, $packageName = null)
6787
return$this->packages->getVersion($path,$packageName);
6888
}
6989

90+
publicfunctiongetAssetsVersion($packageName =null)
91+
{
92+
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);
93+
94+
return$this->packages->getVersion('/',$packageName);
95+
}
96+
97+
privatefunctiongetLegacyAssetUrl($path,$packageName =null,$absolute =false,$version =null)
98+
{
99+
if ($version) {
100+
$package =$this->packages->getPackage($packageName);
101+
102+
$v =new \ReflectionProperty($package,'versionStrategy');
103+
$v->setAccessible(true);
104+
105+
$currentVersionStrategy =$v->getValue($package);
106+
107+
$f =new \ReflectionProperty($currentVersionStrategy,'format');
108+
$f->setAccessible(true);
109+
$format =$f->getValue($currentVersionStrategy);
110+
111+
$v->setValue($package,newStaticVersionStrategy($version,$format));
112+
}
113+
114+
try {
115+
$url =$this->packages->getUrl($path,$packageName);
116+
}catch (\Exception$e) {
117+
if ($version) {
118+
$v->setValue($package,$currentVersionStrategy);
119+
}
120+
121+
throw$e;
122+
}
123+
124+
if ($version) {
125+
$v->setValue($package,$currentVersionStrategy);
126+
}
127+
128+
if ($absolute) {
129+
return$this->foundationExtension->generateAbsoluteUrl($url);
130+
}
131+
132+
return$url;
133+
}
134+
70135
/**
71136
* Returns the name of the extension.
72137
*
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
namespaceSymfony\Bridge\Twig\Tests\Extension;
13+
14+
useSymfony\Bridge\Twig\Extension\AssetExtension;
15+
useSymfony\Component\Asset\Package;
16+
useSymfony\Component\Asset\Packages;
17+
useSymfony\Component\Asset\VersionStrategy\StaticVersionStrategy;
18+
19+
class AssetExtensionTestextends \PHPUnit_Framework_TestCase
20+
{
21+
publicfunctiontestLegacyGetAssetUrl()
22+
{
23+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
24+
25+
$foundationExtension =$this->getMockBuilder('Symfony\Bridge\Twig\Extension\HttpFoundationExtension')->disableOriginalConstructor()->getMock();
26+
$foundationExtension
27+
->expects($this->any())
28+
->method('generateAbsoluteUrl')
29+
->will($this->returnCallback(function ($arg) {return'http://localhost/'.$arg; }))
30+
;
31+
32+
$package =newPackage(newStaticVersionStrategy('22','%s?version=%s'));
33+
$packages =newPackages($package);
34+
$extension =newAssetExtension($packages,$foundationExtension);
35+
36+
$this->assertEquals('me.png?version=42',$extension->getAssetUrl('me.png',null,false,'42'));
37+
$this->assertEquals('http://localhost/me.png?version=22',$extension->getAssetUrl('me.png',null,true));
38+
$this->assertEquals('http://localhost/me.png?version=42',$extension->getAssetUrl('me.png',null,true,'42'));
39+
}
40+
}

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TemplatingAssetHelperPass.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
useSymfony\Component\DependencyInjection\Definition;
1717
useSymfony\Component\DependencyInjection\Reference;
1818

19+
trigger_error('The'.__NAMESPACE__.'\TemplatingAssetHelperPass class is deprecated since version 2.7 and will be removed in 3.0.',E_USER_DEPRECATED);
20+
1921
/**
2022
* @deprecated since 2.7, will be removed in 3.0
2123
*/

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,44 @@ public function getConfigTreeBuilder()
5353
return$v;
5454
})
5555
->end()
56+
->validate()
57+
->ifTrue(function ($v) {returnisset($v['templating']); })
58+
->then(function ($v) {
59+
if ($v['templating']['assets_version']
60+
||count($v['templating']['assets_base_urls']['http'])
61+
||count($v['templating']['assets_base_urls']['ssl'])
62+
||count($v['templating']['packages'])
63+
) {
64+
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);
65+
66+
// convert the old configuration to the new one
67+
if (isset($v['assets'])) {
68+
thrownewLogicException('You cannot use assets settings under "templating.templating" and "assets" configurations in the same project.');
69+
}
70+
71+
$v['assets'] =array(
72+
'version' =>$v['templating']['assets_version'],
73+
'version_format' =>$v['templating']['assets_version_format'],
74+
'base_path' =>'',
75+
'base_urls' =>array_values(array_unique(array_merge($v['templating']['assets_base_urls']['http'],$v['templating']['assets_base_urls']['ssl']))),
76+
'packages' =>array(),
77+
);
78+
79+
foreach ($v['templating']['packages']as$name =>$config) {
80+
$v['assets']['packages'][$name] =array(
81+
'version' => (string)$config['version'],
82+
'version_format' =>$config['version_format'],
83+
'base_path' =>'',
84+
'base_urls' =>array_values(array_unique(array_merge($config['base_urls']['http'],$config['base_urls']['ssl']))),
85+
);
86+
}
87+
}
88+
89+
unset($v['templating']['assets_version'],$v['templating']['assets_version_format'],$v['templating']['assets_base_urls'],$v['templating']['packages']);
90+
91+
return$v;
92+
})
93+
->end()
5694
->children()
5795
->scalarNode('secret')->end()
5896
->scalarNode('http_method_override')

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php‎

Lines changed: 4 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function load(array $configs, ContainerBuilder $container)
4949
$loader->load('web.xml');
5050
$loader->load('services.xml');
5151
$loader->load('fragment_renderer.xml');
52+
$loader->load('assets.xml');
5253

5354
// A translator must always be registered (as support is included by
5455
// default in the Form component). If disabled, an identity translator
@@ -473,23 +474,6 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
473474
$container->setParameter('templating.helper.code.file_link_format',isset($links[$ide]) ?$links[$ide] :$ide);
474475
$container->setParameter('fragment.renderer.hinclude.global_template',$config['hinclude_default_template']);
475476

476-
$loader->load('old_assets.xml');
477-
478-
// create package definitions and add them to the assets helper
479-
$defaultPackage =$this->createTemplatingPackageDefinition($container,$config['assets_base_urls']['http'],$config['assets_base_urls']['ssl'],$config['assets_version'],$config['assets_version_format']);
480-
$container->setDefinition('templating.asset.default_package',$defaultPackage);
481-
$namedPackages =array();
482-
foreach ($config['packages']as$name =>$package) {
483-
$namedPackage =$this->createTemplatingPackageDefinition($container,$package['base_urls']['http'],$package['base_urls']['ssl'],$package['version'],$package['version_format'],$name);
484-
$container->setDefinition('templating.asset.package.'.$name,$namedPackage);
485-
$namedPackages[$name] =newReference('templating.asset.package.'.$name);
486-
}
487-
488-
$container->getDefinition('templating.helper.assets')->setArguments(array(
489-
newReference('templating.asset.default_package'),
490-
$namedPackages,
491-
));
492-
493477
if ($container->getParameter('kernel.debug')) {
494478
$logger =newReference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE);
495479

@@ -569,73 +553,6 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
569553
}
570554
}
571555

572-
/**
573-
* Returns a definition for an asset package.
574-
*/
575-
privatefunctioncreateTemplatingPackageDefinition(ContainerBuilder$container,array$httpUrls,array$sslUrls,$version,$format,$name =null)
576-
{
577-
if (!$httpUrls) {
578-
$package =newDefinitionDecorator('templating.asset.path_package');
579-
$package
580-
->setPublic(false)
581-
->setScope('request')
582-
->replaceArgument(1,$version)
583-
->replaceArgument(2,$format)
584-
;
585-
586-
return$package;
587-
}
588-
589-
if ($httpUrls ==$sslUrls) {
590-
$package =newDefinitionDecorator('templating.asset.url_package');
591-
$package
592-
->setPublic(false)
593-
->replaceArgument(0,$sslUrls)
594-
->replaceArgument(1,$version)
595-
->replaceArgument(2,$format)
596-
;
597-
598-
return$package;
599-
}
600-
601-
$prefix =$name ?'templating.asset.package.'.$name :'templating.asset.default_package';
602-
603-
$httpPackage =newDefinitionDecorator('templating.asset.url_package');
604-
$httpPackage
605-
->replaceArgument(0,$httpUrls)
606-
->replaceArgument(1,$version)
607-
->replaceArgument(2,$format)
608-
;
609-
$container->setDefinition($prefix.'.http',$httpPackage);
610-
611-
if ($sslUrls) {
612-
$sslPackage =newDefinitionDecorator('templating.asset.url_package');
613-
$sslPackage
614-
->replaceArgument(0,$sslUrls)
615-
->replaceArgument(1,$version)
616-
->replaceArgument(2,$format)
617-
;
618-
}else {
619-
$sslPackage =newDefinitionDecorator('templating.asset.path_package');
620-
$sslPackage
621-
->setScope('request')
622-
->replaceArgument(1,$version)
623-
->replaceArgument(2,$format)
624-
;
625-
}
626-
$container->setDefinition($prefix.'.ssl',$sslPackage);
627-
628-
$package =newDefinitionDecorator('templating.asset.request_aware_package');
629-
$package
630-
->setPublic(false)
631-
->setScope('request')
632-
->replaceArgument(1,$prefix.'.http')
633-
->replaceArgument(2,$prefix.'.ssl')
634-
;
635-
636-
return$package;
637-
}
638-
639556
/**
640557
* Loads the assets configuration.
641558
*
@@ -645,9 +562,7 @@ private function createTemplatingPackageDefinition(ContainerBuilder $container,
645562
*/
646563
privatefunctionregisterAssetsConfiguration(array$config,ContainerBuilder$container,XmlFileLoader$loader)
647564
{
648-
$loader->load('assets.xml');
649-
650-
$defaultVersion =$this->createVersion($container,$config['version'],$config['version_format']);
565+
$defaultVersion =$this->createVersion($container,$config['version'],$config['version_format'],'_default');
651566

652567
$defaultPackage =$this->createPackageDefinition($config['base_path'],$config['base_urls'],$defaultVersion);
653568
$container->setDefinition('assets._default_package',$defaultPackage);
@@ -658,7 +573,6 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
658573
$version =$defaultVersion;
659574
}else {
660575
$format =$package['version_format'] ?:$config['version_format'];
661-
662576
$version =$this->createVersion($container,$package['version'],$format,$name);
663577
}
664578

@@ -700,9 +614,9 @@ private function createPackageDefinition($basePath, array $baseUrls, Reference $
700614
;
701615
}
702616

703-
privatefunctioncreateVersion(ContainerBuilder$container,$version,$format,$name =null)
617+
privatefunctioncreateVersion(ContainerBuilder$container,$version,$format,$name)
704618
{
705-
if (!$version) {
619+
if (null ===$version) {
706620
returnnewReference('assets.empty_version_strategy');
707621
}
708622

‎src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
1717
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
1818
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
19-
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingAssetHelperPass;
2019
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
2120
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
2221
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass;
@@ -76,7 +75,6 @@ public function build(ContainerBuilder $container)
7675
// but as late as possible to get resolved parameters
7776
$container->addCompilerPass(newRegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
7877
$container->addCompilerPass(newTemplatingPass());
79-
$container->addCompilerPass(newTemplatingAssetHelperPass());
8078
$container->addCompilerPass(newAddConstraintValidatorsPass());
8179
$container->addCompilerPass(newAddValidatorInitializersPass());
8280
$container->addCompilerPass(newAddConsoleCommandPass());

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
<services>
88

99
<serviceid="assets.packages"class="Symfony\Component\Asset\Packages">
10-
<argument /><!-- default package-->
10+
<argumenttype="service"id="assets.empty_package"/><!-- default package-->
1111
<argumenttype="collection" /><!-- named packages-->
1212
</service>
1313

14+
<serviceid="assets.empty_package"class="Symfony\Component\Asset\Package"public="false">
15+
<argumenttype="service"id="assets.empty_version_strategy" />
16+
</service>
17+
1418
<serviceid="assets.context"class="Symfony\Component\Asset\Context\RequestStackContext">
1519
<argumenttype="service"id="request_stack" />
1620
</service>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp