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

Skip to content

Commit 7570bbb

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

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public function process(ContainerBuilder $container)
4545

4646
$definition->replaceArgument(1, $types);
4747

48-
$typeExtensions = array();
48+
/** @var \SplPriorityQueue[] $typeExtensionQueues */
49+
$typeExtensionQueues = array();
4950

5051
foreach ($container->findTaggedServiceIds('form.type_extension') as $serviceId => $tag) {
5152
$serviceDefinition = $container->getDefinition($serviceId);
@@ -59,10 +60,16 @@ public function process(ContainerBuilder $container)
5960
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));
6061
}
6162

62-
$typeExtensions[$extendedType][] = $serviceId;
63+
if (!isset($typeExtensionQueues[$extendedType])) {
64+
$typeExtensionQueues[$extendedType] = new \SplPriorityQueue();
65+
}
66+
67+
$typeExtensionQueues[$extendedType]->insert($serviceId, isset($tag[0]['priority']) ? $tag[0]['priority'] : 0);
6368
}
6469

65-
$definition->replaceArgument(2, $typeExtensions);
70+
$definition->replaceArgument(2, array_map(function (\SplPriorityQueue $queue) {
71+
return iterator_to_array($queue, false);
72+
}, $typeExtensionQueues));
6673

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

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_extension6', 'my.type_extension5'),
121+
),
93122
),
94-
), $extDefinition->getArgument(2));
123+
);
95124
}
96125

97126
/**

0 commit comments

Comments
 (0)