Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle] added a compiler pass for expression language provi…
…ders
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...undle/FrameworkBundle/DependencyInjection/Compiler/AddExpressionLanguageProvidersPass.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,46 @@ | ||
<?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\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Registers the expression language providers. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class AddExpressionLanguageProvidersPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
// routing | ||
if ($container->hasDefinition('router')) { | ||
$definition = $container->findDefinition('router'); | ||
foreach ($container->findTaggedServiceIds('routing.expression_language_provider') as $id => $attributes) { | ||
$definition->addMethodCall('addExpressionLanguageProvider', array(new Reference($id))); | ||
} | ||
} | ||
|
||
// security | ||
if ($container->hasDefinition('security.access.expression_voter')) { | ||
$definition = $container->findDefinition('security.access.expression_voter'); | ||
foreach ($container->findTaggedServiceIds('security.expression_language_provider') as $id => $attributes) { | ||
$definition->addMethodCall('addExpressionLanguageProvider', array(new Reference($id))); | ||
} | ||
} | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
...eworkBundle/Tests/DependencyInjection/Compiler/AddExpressionLanguageProvidersPassTest.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,62 @@ | ||
<?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\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass; | ||
|
||
class AddExpressionLanguageProvidersPassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testProcessForRouter() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->addCompilerPass(new AddExpressionLanguageProvidersPass()); | ||
|
||
$definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\TestProvider'); | ||
$definition->addTag('routing.expression_language_provider'); | ||
$container->setDefinition('some_routing_provider', $definition); | ||
|
||
$container->register('router', '\stdClass'); | ||
$container->compile(); | ||
|
||
$router = $container->getDefinition('router'); | ||
$calls = $router->getMethodCalls(); | ||
$this->assertCount(1, $calls); | ||
$this->assertEquals('addExpressionLanguageProvider', $calls[0][0]); | ||
$this->assertEquals(new Reference('some_routing_provider'), $calls[0][1][0]); | ||
} | ||
|
||
public function testProcessForSecurity() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->addCompilerPass(new AddExpressionLanguageProvidersPass()); | ||
|
||
$definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\TestProvider'); | ||
$definition->addTag('security.expression_language_provider'); | ||
$container->setDefinition('some_security_provider', $definition); | ||
|
||
$container->register('security.access.expression_voter', '\stdClass'); | ||
$container->compile(); | ||
|
||
$router = $container->getDefinition('security.access.expression_voter'); | ||
$calls = $router->getMethodCalls(); | ||
$this->assertCount(1, $calls); | ||
$this->assertEquals('addExpressionLanguageProvider', $calls[0][0]); | ||
$this->assertEquals(new Reference('some_security_provider'), $calls[0][1][0]); | ||
} | ||
} | ||
|
||
class TestProvider | ||
{ | ||
} |