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

Skip to content

Commit 3d0bda3

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

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,21 @@ public function process(ContainerBuilder $container)
5959
throw new \InvalidArgumentException(sprintf('Tagged form type extension must have the extended type configured using the extended_type/extended-type attribute, none was configured for the "%s" service.', $serviceId));
6060
}
6161

62-
$typeExtensions[$extendedType][] = $serviceId;
62+
$typeExtensions[$extendedType][] = array($serviceId, isset($tag[0]['priority']) ? $tag[0]['priority'] : 0);
6363
}
6464

65-
$definition->replaceArgument(2, $typeExtensions);
65+
$sortedTypeExtensions = array();
66+
foreach ($typeExtensions as $extendedType => $extensionsWithPriority) {
67+
usort($extensionsWithPriority, function (array $a, array $b) {
68+
return $a[1] > $b[1] ? -1 : 1;
69+
});
70+
71+
$sortedTypeExtensions[$extendedType] = array_map(function (array $extensionWithPriority) {
72+
return $extensionWithPriority[0];
73+
}, $extensionsWithPriority);
74+
}
75+
76+
$definition->replaceArgument(2, $sortedTypeExtensions);
6677

6778
// Find all services annotated with "form.type_guesser"
6879
$guessers = array_keys($container->findTaggedServiceIds('form.type_guesser'));

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

Lines changed: 41 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,47 @@ 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' => 'type2', 'priority' => 2),
114+
'my.type_extension4' => array('extended_type' => 'type2', 'priority' => 1),
115+
),
116+
array(
117+
'type1' => array('my.type_extension2', 'my.type_extension1'),
118+
'type2' => array('my.type_extension3', 'my.type_extension4'),
119+
),
93120
),
94-
), $extDefinition->getArgument(2));
121+
);
95122
}
96123

97124
/**

0 commit comments

Comments
 (0)