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

Commit aeef2bc

Browse filesBrowse files
committed
feature #11961 [FrameworkBundle] Determine templating.engine.php scope as late as possible (lyrixx)
This PR was merged into the 2.6-dev branch. Discussion ---------- [FrameworkBundle] Determine templating.engine.php scope as late as possible | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11653 | License | MIT | Doc PR | - Commits ------- 169dadd [FrameworkBundle] Determine templating.engine.php scope as late as possible
2 parents 1a55995 + 169dadd commit aeef2bc
Copy full SHA for aeef2bc

File tree

5 files changed

+189
-15
lines changed
Filter options

5 files changed

+189
-15
lines changed
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
class TemplatingAssetHelperPass implements CompilerPassInterface
20+
{
21+
public function process(ContainerBuilder $container)
22+
{
23+
if (!$container->hasDefinition('templating.helper.assets')) {
24+
return;
25+
}
26+
27+
$assetsHelperDefinition = $container->getDefinition('templating.helper.assets');
28+
$args = $assetsHelperDefinition->getArguments();
29+
30+
if ('request' === $this->getPackageScope($container, $args[0])) {
31+
$assetsHelperDefinition->setScope('request');
32+
33+
return;
34+
}
35+
36+
if (!array_key_exists(1, $args)) {
37+
return;
38+
}
39+
40+
if (!is_array($args[1])) {
41+
return;
42+
}
43+
44+
foreach ($args[1] as $arg) {
45+
if ('request' === $this->getPackageScope($container, $arg)) {
46+
$assetsHelperDefinition->setScope('request');
47+
48+
break;
49+
}
50+
}
51+
}
52+
53+
private function getPackageScope(ContainerBuilder $container, $package)
54+
{
55+
if ($package instanceof Reference) {
56+
return $container->findDefinition((string) $package)->getScope();
57+
}
58+
59+
if ($package instanceof Definition) {
60+
return $package->getScope();
61+
}
62+
63+
// Someone did some voodoo with a compiler pass. So we ignore this
64+
// 'package'. Can we be sure, it's a package anyway?
65+
}
66+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
-13Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -484,19 +484,6 @@ private function registerTemplatingConfiguration(array $config, $ide, ContainerB
484484
$namedPackages,
485485
));
486486

487-
// Apply request scope to assets helper if one or more packages are request-scoped
488-
$requireRequestScope = array_reduce(
489-
$namedPackages,
490-
function ($v, Reference $ref) use ($container) {
491-
return $v || 'request' === $container->getDefinition($ref)->getScope();
492-
},
493-
'request' === $defaultPackage->getScope()
494-
);
495-
496-
if ($requireRequestScope) {
497-
$container->getDefinition('templating.helper.assets')->setScope('request');
498-
}
499-
500487
if (!empty($config['loaders'])) {
501488
$loaders = array_map(function ($loader) { return new Reference($loader); }, $config['loaders']);
502489

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
1717
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
1818
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
19+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingAssetHelperPass;
1920
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
2021
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
2122
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass;
@@ -70,6 +71,7 @@ public function build(ContainerBuilder $container)
7071
// but as late as possible to get resolved parameters
7172
$container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
7273
$container->addCompilerPass(new TemplatingPass());
74+
$container->addCompilerPass(new TemplatingAssetHelperPass());
7375
$container->addCompilerPass(new AddConstraintValidatorsPass());
7476
$container->addCompilerPass(new AddValidatorInitializersPass());
7577
$container->addCompilerPass(new AddConsoleCommandPass());
+121Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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\DependencyInjection\Compiler;
13+
14+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingAssetHelperPass;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
class TemplatingAssetHelperPassTest extends \PHPUnit_Framework_TestCase
20+
{
21+
public function getScopesTests()
22+
{
23+
return array(
24+
array('container'),
25+
array('request'),
26+
);
27+
}
28+
29+
/** @dataProvider getScopesTests */
30+
public function testFindLowestScopeInDefaultPackageWithReference($scope)
31+
{
32+
$container = new ContainerBuilder();
33+
34+
$defaultPackage = new Definition('stdClass');
35+
$defaultPackage->setScope($scope);
36+
$container->setDefinition('default_package', $defaultPackage);
37+
38+
$definition = new Definition('stdClass', array(new Reference('default_package')));
39+
$container->setDefinition('templating.helper.assets', $definition);
40+
41+
$profilerPass = new TemplatingAssetHelperPass();
42+
$profilerPass->process($container);
43+
44+
$this->assertSame($scope, $definition->getScope());
45+
}
46+
47+
/** @dataProvider getScopesTests */
48+
public function testFindLowestScopeInDefaultPackageWithDefinition($scope)
49+
{
50+
$container = new ContainerBuilder();
51+
52+
$defaultPackage = new Definition('stdClass');
53+
$defaultPackage->setScope($scope);
54+
55+
$definition = new Definition('stdClass', array($defaultPackage));
56+
$container->setDefinition('templating.helper.assets', $definition);
57+
58+
$profilerPass = new TemplatingAssetHelperPass();
59+
$profilerPass->process($container);
60+
61+
$this->assertSame($scope, $definition->getScope());
62+
}
63+
64+
/** @dataProvider getScopesTests */
65+
public function testFindLowestScopeInNamedPackageWithReference($scope)
66+
{
67+
$container = new ContainerBuilder();
68+
69+
$defaultPackage = new Definition('stdClass');
70+
$container->setDefinition('default_package', $defaultPackage);
71+
72+
$aPackage = new Definition('stdClass');
73+
$container->setDefinition('a_package', $aPackage);
74+
75+
$bPackage = new Definition('stdClass');
76+
$bPackage->setScope($scope);
77+
$container->setDefinition('b_package', $bPackage);
78+
79+
$cPackage = new Definition('stdClass');
80+
$container->setDefinition('c_package', $cPackage);
81+
82+
$definition = new Definition('stdClass', array(new Reference('default_package'), array(
83+
new Reference('a_package'),
84+
new Reference('b_package'),
85+
new Reference('c_package'),
86+
)));
87+
$container->setDefinition('templating.helper.assets', $definition);
88+
89+
$profilerPass = new TemplatingAssetHelperPass();
90+
$profilerPass->process($container);
91+
92+
$this->assertSame($scope, $definition->getScope());
93+
}
94+
95+
/** @dataProvider getScopesTests */
96+
public function testFindLowestScopeInNamedPackageWithDefinition($scope)
97+
{
98+
$container = new ContainerBuilder();
99+
100+
$defaultPackage = new Definition('stdClass');
101+
102+
$aPackage = new Definition('stdClass');
103+
104+
$bPackage = new Definition('stdClass');
105+
$bPackage->setScope($scope);
106+
107+
$cPackage = new Definition('stdClass');
108+
109+
$definition = new Definition('stdClass', array($defaultPackage, array(
110+
$aPackage,
111+
$bPackage,
112+
$cPackage,
113+
)));
114+
$container->setDefinition('templating.helper.assets', $definition);
115+
116+
$profilerPass = new TemplatingAssetHelperPass();
117+
$profilerPass->process($container);
118+
119+
$this->assertSame($scope, $definition->getScope());
120+
}
121+
}

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ public function testTemplating()
186186

187187
$this->assertTrue($container->hasDefinition('templating.name_parser'), '->registerTemplatingConfiguration() loads templating.xml');
188188

189-
$this->assertEquals('request', $container->getDefinition('templating.helper.assets')->getScope(), '->registerTemplatingConfiguration() sets request scope on assets helper if one or more packages are request-scoped');
190-
191189
// default package should have one HTTP base URL and path package SSL URL
192190
$this->assertTrue($container->hasDefinition('templating.asset.default_package.http'));
193191
$package = $container->getDefinition('templating.asset.default_package.http');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.