8000 feature #12174 [TwigBundle] Add loader priority (wizhippo) · symfony/symfony@1882cb1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1882cb1

Browse files
committed
feature #12174 [TwigBundle] Add loader priority (wizhippo)
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #12174). Discussion ---------- [TwigBundle] Add loader priority | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Add the ability to specify a priority to the tag of `twig.loader` services. eg: ``` <service id="twig.loader.filesystem" class="%twig.loader.filesystem.class%" public="false"> <argument type="service" id="templating.locator" /> <argument type="service" id="templating.name_parser" /> <tag name="twig.loader" priority="100"/> </service> ``` Commits ------- 67dffea Add Twig loader priority
2 parents 2544134 + 67dffea commit 1882cb1

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigLoaderPass.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,24 @@ public function process(ContainerBuilder $container)
4040
$container->setAlias('twig.loader', key($loaderIds));
4141
} else {
4242
$chainLoader = $container->getDefinition('twig.loader.chain');
43-
foreach (array_keys($loaderIds) as $id) {
44-
$chainLoader->addMethodCall('addLoader', array(new Reference($id)));
43+
44+
$prioritizedLoaders = array();
45+
46+
foreach ($loaderIds as $id => $tags) {
47+
foreach ($tags as $tag) {
48+
$priority = isset($tag['priority']) ? $tag['priority'] : 0;
49+
$prioritizedLoaders[$priority][] = $id;
50+
}
51+
}
52+
53+
krsort($prioritizedLoaders);
54+
55+
foreach ($prioritizedLoaders as $loaders) {
56+
foreach ($loaders as $loader) {
57+
$chainLoader->addMethodCall('addLoader', array(new Reference($loader)));
58+
}
4559
}
60+
4661
$container->setAlias('twig.loader', 'twig.loader.chain');
4762
}
4863
}

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigLoaderPassTest.php

Lines changed: 42 additions & 0 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function testMapperPassWithOneTaggedLoaders()
4343
{
4444
$serviceIds = array(
4545
'test_loader_1' => array(
46+
array(),
4647
),
4748
);
4849

@@ -65,8 +66,10 @@ public function testMapperPassWithTwoTaggedLoaders()
6566
{
6667
$serviceIds = array(
6768
'test_loader_1' => array(
69+
array(),
6870
),
6971
'test_loader_2' => array(
72+
array(),
7073
),
7174
);
7275

@@ -90,6 +93,45 @@ public function testMapperPassWithTwoTaggedLoaders()
9093
$calls = $this->chainLoader->getMethodCalls();
9194
$this->assertCount(2, $calls);
9295
$this->assertEquals('addLoader', $calls[0][0]);
96+
$this->assertEquals('addLoader', $calls[1][0]);
97+
$this->assertEquals('test_loader_1', (string) $calls[0][1][0]);
98+
$this->assertEquals('test_loader_2', (string) $calls[1][1][0]);
99+
}
100+
101+
public function testMapperPassWithTwoTaggedLoadersWithPriority()
102+
{
103+
$serviceIds = array(
104+
'test_loader_1' => array(
105+
array('priority' => 100),
106+
),
107+
'test_loader_2' => array(
108+
array('priority' => 200),
109+
),
110+
);
111+
112+
$this->builder->expects($this->once())
113+
->method('hasDefinition')
114+
->with('twig')
115+
->will($this->returnValue(true));
116+
$this->builder->expects($this->once())
117+
->method('findTaggedServiceIds')
118+
->with('twig.loader')
119+
->will($this->returnValue($serviceIds));
120+
$this->builder->expects($this->once())
121+
->method('getDefinition')
122+
->with('twig.loader.chain')
123+
->will($this->returnValue($this->chainLoader));
124+
$this->builder->expects($this->once())
125+
->method('setAlias')
126+
->with('twig.loader', 'twig.loader.chain');
127+
128+
$this->pass->process($this->builder);
129+
$calls = $this->chainLoader->getMethodCalls();
130+
$this->assertCount(2, $calls);
131+
$this->assertEquals('addLoader', $calls[0][0]);
132+
$this->assertEquals('addLoader', $calls[1][0]);
133+
$this->assertEquals('test_loader_2', (string) $calls[0][1][0]);
134+
$this->assertEquals('test_loader_1', (string) $calls[1][1][0]);
93135
}
94136

95137
/**

0 commit comments

Comments
 (0)
0