Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 512742b

Browse files
committed
[SecurityBundle] simplified code
1 parent 677df7b commit 512742b

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Compiler/AddSecurityVotersPass.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler;
1313

14-
use Symfony\Component\DependencyInjection\Reference;
1514
use Symfony\Component\DependencyInjection\ContainerBuilder;
1615
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1717
use Symfony\Component\DependencyInjection\Exception\LogicException;
1818

1919
/**
@@ -23,6 +23,8 @@
2323
*/
2424
class AddSecurityVotersPass implements CompilerPassInterface
2525
{
26+
use PriorityTaggedServiceTrait;
27+
2628
/**
2729
* {@inheritdoc}
2830
*/
@@ -32,15 +34,7 @@ public function process(ContainerBuilder $container)
3234
return;
3335
}
3436

35-
$voters = array();
36-
foreach ($container->findTaggedServiceIds('security.voter') as $id => $attributes) {
37-
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
38-
$voters[$priority][] = new Reference($id);
39-
}
40-
41-
krsort($voters);
42-
$voters = call_user_func_array('array_merge', $voters);
43-
37+
$voters = $this->findAndSortTaggedServices('security.voter', $container);
4438
if (!$voters) {
4539
throw new LogicException('No security voters found. You need to tag at least one with "security.voter"');
4640
}

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Compiler/AddSecurityVotersPassTest.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@
1818

1919
class AddSecurityVotersPassTest extends TestCase
2020
{
21+
/**
22+
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
23+
*/
24+
public function testNoVoters()
25+
{
26+
$container = new ContainerBuilder();
27+
$container
28+
->register('security.access.decision_manager', 'Symfony\Component\Security\Core\Authorization\AccessDecisionManager')
29+
->addArgument(array())
30+
;
31+
32+
$compilerPass = new AddSecurityVotersPass();
33+
$compilerPass->process($container);
34+
}
35+
2136
public function testThatSecurityVotersAreProcessedInPriorityOrder()
2237
{
2338
$container = new ContainerBuilder();
@@ -45,15 +60,9 @@ public function testThatSecurityVotersAreProcessedInPriorityOrder()
4560
$compilerPass->process($container);
4661

4762
$calls = $container->getDefinition('security.access.decision_manager')->getMethodCalls();
48-
49-
$this->assertEquals(
50-
array(
51-
new Reference('highest_prio_service'),
52-
new Reference('lowest_prio_service'),
53-
new Reference('no_prio_service'),
54-
new Reference('zero_prio_service'),
55-
),
56-
$calls[0][1][0]
57-
);
63+
$refs = $calls[0][1][0];
64+
$this->assertEquals(new Reference('highest_prio_service'), $refs[0]);
65+
$this->assertEquals(new Reference('lowest_prio_service'), $refs[1]);
66+
$this->assertCount(4, $refs);
5867
}
5968
}

0 commit comments

Comments
 (0)