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

Skip to content

Commit a3db5f0

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

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* The `Controller::getUser()` method has been deprecated and will be removed in
88
Symfony 4.0; typehint the security user object in the action instead.
9+
* Added possibility to prioritize form type extensions with `'priority'` attribute on tags `form.type_extension`
910

1011
3.1.0
1112
-----

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

Lines changed: 6 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,14 @@ 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 = $serviceDefinition->getTag('form.type_extension');
5661
if (isset($tag[0]['extended_type'])) {
5762
$extendedType = $tag[0]['extended_type'];
5863
} else {

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

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

62-
public function testAddTaggedTypeExtensions()
62+
/**
63+
* @dataProvider addTaggedTypeExtensionsDataProvider
64+
*/
65+
public function testAddTaggedTypeExtensions(array $extensions, array $expectedRegisteredExtensions)
6366
{
6467
$container = new ContainerBuilder();
6568
$container->addCompilerPass(new FormPass());
@@ -72,26 +75,49 @@ public function testAddTaggedTypeExtensions()
7275
));
7376

7477
$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'));
78+
79+
foreach ($extensions as $serviceId => $tag) {
80+
$container->register($serviceId, 'stdClass')->addTag('form.type_extension', $tag);
81+
}
8182

8283
$container->compile();
8384

8485
$extDefinition = $container->getDefinition('form.extension');
86+
$this->assertSame($expectedRegisteredExtensions, $extDefinition->getArgument(2));
87+
}
8588

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

97123
/**

0 commit comments

Comments
 (0)