Skip to content

Commit ab302dc

Browse files
committed
[FrameworkBundle] add support for prioritizing form type extension tags
1 parent 2bc54e0 commit ab302dc

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/FormPass.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

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

14+
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1617

@@ -22,6 +23,8 @@
2223
*/
2324
class FormPass implements CompilerPassInterface
2425
{
26+
use PriorityTaggedServiceTrait;
27+
2528
public function process(ContainerBuilder $container)
2629
{
2730
if (!$container->hasDefinition('form.extension')) {
@@ -47,12 +50,15 @@ public function process(ContainerBuilder $container)
4750

4851
$typeExtensions = array();
4952

50-
foreach ($container->findTaggedServiceIds('form.type_extension') as $serviceId => $tag) {
53+
foreach ($this->findAndSortTaggedServices('form.type_extension', $container) as $reference) {
54+
$serviceId = (string) $reference;
5155
$serviceDefinition = $container->getDefinition($serviceId);
5256
if (!$serviceDefinition->isPublic()) {
5357
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form type extensions are lazy-loaded.', $serviceId));
5458
}
5559

60+
$tag = $container->getDefinition($serviceId)->getTag('form.type_extension');
61+
5662
if (isset($tag[0]['extended_type'])) {
5763
$extendedType = $tag[0]['extended_type'];
5864
} else {

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/FormPassTest.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ public function testAddTaggedTypes()
5959
), $extDefinition->getArgument(1));
6060
}
6161

62-
public function testAddTaggedTypeExtensions()
62+
/**
63+
* @dataProvider addTaggedTypeExtensionsDataProvider
64+
*
65+
* @param array $extensions
66+
* @param array $expectedRegisteredExtensions
67+
*/
68+
public function testAddTaggedTypeExtensions(array $extensions, array $expectedRegisteredExtensions)
6369
{
6470
$container = new ContainerBuilder();
6571
$container->addCompilerPass(new FormPass());
@@ -72,26 +78,49 @@ public function testAddTaggedTypeExtensions()
7278
));
7379

7480
$container->setDefinition('form.extension', $extDefinition);
75-
$container->register('my.type_extension1', 'stdClass')
76-
->addTag('form.type_extension', array('extended_type' => 'type1'));
77-
$container->register('my.type_extension2', 'stdClass')
78-
->addTag('form.type_extension', array('extended_type' => 'type1'));
79-
$container->register('my.type_extension3', 'stdClass')
80-
->addTag('form.type_extension', array('extended_type' => 'type2'));
81+
82+
foreach ($extensions as $serviceId => $tag) {
83+
$container->register($serviceId, 'stdClass')->addTag('form.type_extension', $tag);
84+
}
8185

8286
$container->compile();
8387

8488
$extDefinition = $container->getDefinition('form.extension');
89+
$this->assertSame($expectedRegisteredExtensions, $extDefinition->getArgument(2));
90+
}
8591

86-
$this->assertSame(array(
87-
'type1' => array(
88-
'my.type_extension1',
89-
'my.type_extension2',
92+
/**
93+
* @return array
94+
*/
95+
public function addTaggedTypeExtensionsDataProvider()
96+
{
97+
return array(
98+
array(
99+
array(
100+
'my.type_extension1' => array('extended_type' => 'type1'),
101+
'my.type_extension2' => array('extended_type' => 'type1'),
102+
'my.type_extension3' => array('extended_type' => 'type2'),
103+
),
104+
array(
105+
'type1' => array('my.type_extension1', 'my.type_extension2'),
106+
'type2' => array('my.type_extension3'),
107+
),
90108
),
91-
'type2' => array(
92-
'my.type_extension3',
109+
array(
110+
array(
111+
'my.type_extension1' => array('extended_type' => 'type1', 'priority' => 1),
112+
'my.type_extension2' => array('extended_type' => 'type1', 'priority' => 2),
113+
'my.type_extension3' => array('extended_type' => 'type1', 'priority' => -1),
114+
'my.type_extension4' => array('extended_type' => 'type2', 'priority' => 2),
115+
'my.type_extension5' => array('extended_type' => 'type2', 'priority' => 1),
116+
'my.type_extension6' => array('extended_type' => 'type2', 'priority' => 1),
117+
),
118+
array(
119+
'type1' => array('my.type_extension2', 'my.type_extension1', 'my.type_extension3'),
120+
'type2' => array('my.type_extension4', 'my.type_extension5', 'my.type_extension6'),
121+
),
93122
),
94-
), $extDefinition->getArgument(2));
123+
);
95124
}
96125

97126
/**

0 commit comments

Comments
 (0)