Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle] Determine templating.engine.php scope as late as po…
…ssible
- Loading branch information
Showing
5 changed files
with
189 additions
and
15 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TemplatingAssetHelperPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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; | ||
|
||
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? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...ndle/FrameworkBundle/Tests/DependencyInjection/Compiler/TemplatingAssetHelperPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?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 TemplatingAssetHelperPassTest 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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters