8000 Merge branch '2.8' into 3.0 · dunglas/symfony@1d71189 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d71189

Browse files
committed
Merge branch '2.8' into 3.0
* 2.8: [Form] fix FQCN in tests added by symfony#17798 bug symfony#17798 [Form] allow `choice_label` option to be `false` [DependencyInjection] Simplified code in AutowirePass
2 parents 248b4cf + feed607 commit 1d71189

File tree

6 files changed

+438
-40
lines changed

6 files changed

+438
-40
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -148,45 +148,17 @@ private function populateAvailableType($id, Definition $definition)
148148
$this->types[$type] = $id;
149149
}
150150

151-
// Cannot use reflection if the class isn't set
152-
if (!$definition->getClass()) {
151+
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
153152
return;
154153
}
155154

156-
if ($reflectionClass = $this->getReflectionClass($id, $definition)) {
157-
$this->extractInterfaces($id, $reflectionClass);
158-
$this->extractAncestors($id, $reflectionClass);
159-
}
160-
}
161-
162-
/**
163-
* Extracts the list of all interfaces implemented by a class.
164-
*
165-
* @param string $id
166-
* @param \ReflectionClass $reflectionClass
167-
*/
168-
private function extractInterfaces($id, \ReflectionClass $reflectionClass)
169-
{
170-
foreach ($reflectionClass->getInterfaces() as $interfaceName => $reflectionInterface) {
171-
$this->set($interfaceName, $id);
172-
173-
$this->extractInterfaces($id, $reflectionInterface);
155+
foreach ($reflectionClass->getInterfaces() as $reflectionInterface) {
156+
$this->set($reflectionInterface->name, $id);
174157
}
175-
}
176-
177-
/**
178-
* Extracts all inherited types of a class.
179-
*
180-
* @param string $id
181-
* @param \ReflectionClass $reflectionClass
182-
*/
183-
private function extractAncestors($id, \ReflectionClass $reflectionClass)
184-
{
185-
$this->set($reflectionClass->name, $id);
186158

187-
if ($reflectionParentClass = $reflectionClass->getParentClass()) {
188-
$this->extractAncestors($id, $reflectionParentClass);
189-
}
159+
do {
160+
$this->set($reflectionClass->name, $id);
161+
} while ($reflectionClass = $reflectionClass->getParentClass());
190162
}
191163

192164
/**
@@ -256,6 +228,7 @@ private function getReflectionClass($id, Definition $definition)
256228
return $this->reflectionClasses[$id];
257229
}
258230

231+
// Cannot use reflection if the class isn't set
259232
if (!$class = $definition->getClass()) {
260233
return;
261234
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public function testProcessAutowireInterface()
6161
$pass = new AutowirePass();
6262
$pass->process($container);
6363

64-
$this->assertCount(2, $container->getDefinition('g')->getArguments());
64+
$this->assertCount(3, $container->getDefinition('g')->getArguments());
6565
$this->assertEquals('f', (string) $container->getDefinition('g')->getArgument(0));
6666
$this->assertEquals('f', (string) $container->getDefinition('g')->getArgument(1));
67+
$this->assertEquals('f', (string) $container->getDefinition('g')->getArgument(2));
6768
}
6869

6970
public function testCompleteExistingDefinition()
@@ -317,13 +318,21 @@ interface EInterface extends DInterface
317318
{
318319
}
319320

320-
class F implements EInterface
321+
interface IInterface
322+
{
323+
}
324+
325+
class I implements IInterface
326+
{
327+
}
328+
329+
class F extends I implements EInterface
321330
{
322331
}
323332

324333
class G
325334
{
326-
public function __construct(DInterface $d, EInterface $e)
335+
public function __construct(DInterface $d, EInterface $e, IInterface $i)
327336
{
328337
}
329338
}

src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,21 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
120120
$key = $keys[$value];
121121
$nextIndex = is_int($index) ? $index++ : call_user_func($index, $choice, $key, $value);
122122

123+
// BC normalize label to accept a false value
124+
if (null === $label) {
125+
// If the labels are null, use the original choice key by default
126+
$label = (string) $key;
127+
} elseif (false !== $label) {
128+
// If "choice_label" is set to false and "expanded" is true, the value false
129+
// should be passed on to the "label" option of the checkboxes/radio buttons
130+
$dynamicLabel = call_user_func($label, $choice, $key, $value);
131+
$label = false === $dynamicLabel ? false : (string) $dynamicLabel;
132+
}
133+
123134
$view = new ChoiceView(
124135
$choice,
125136
$value,
126-
// If the labels are null, use the original choice key by default
127-
null === $label ? (string) $key : (string) call_user_func($label, $choice, $key, $value),
137+
$label,
128138
// The attributes may be a callable or a mapping from choice indices
129139
// to nested arrays
130140
is_callable($attr) ? call_user_func($attr, $choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())

src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public function configureOptions(OptionsResolver $resolver)
341341
$resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable'));
342342
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
343343
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'));
344-
$resolver->setAllowedTypes('choice_label', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
344+
$resolver->setAllowedTypes('choice_label', array('null', 'bool', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
345345
$resolver->setAllowedTypes('choice_name', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
346346
$resolver->setAllowedTypes('choice_value', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
347347
$resolver->setAllowedTypes('choice_attr', array('null', 'array', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));

src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,129 @@ public function testSingleChoiceExpanded()
693693
);
694694
}
695695

696+
public function testSingleChoiceExpandedWithLabelsAsFalse()
697+
{
698+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
699+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
700+
'choices_as_values' => true,
701+
'choice_label' => false,
702+
'multiple' => false,
703+
'expanded' => true,
704+
));
705+
706+
$this->assertWidgetMatchesXpath($form->createView(), array(),
707+
'/div
708+
[
709+
./div
710+
[@class="radio"]
711+
[
712+
./label
713+
[
714+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
715+
]
716+
]
717+
/following-sibling::div
718+
[@class="radio"]
719+
[
720+
./label
721+
[
722+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
723+
]
724+
]
725+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
726+
]
727+
'
728+
);
729+
}
730+
731+
public function testSingleChoiceExpandedWithLabelsSetByCallable()
732+
{
733+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
734+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'),
735+
'choices_as_values' => true,
736+
'choice_label' => function ($choice, $label, $value) {
737+
if ('&b' === $choice) {
738+
return false;
739+
}
740+
741+
return 'label.'.$value;
742+
},
743+
'multiple' => false,
744+
'expanded' => true,
745+
));
746+
747+
$this->assertWidgetMatchesXpath($form->createView(), array(),
748+
'/div
749+
[
750+
./div
751+
[@class="radio"]
752+
[
753+
./label
754+
[.=" [trans]label.&a[/trans]"]
755+
[
756+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
757+
]
758+
]
759+
/following-sibling::div
760+
[@class="radio"]
761+
[
762+
./label
763+
[
764+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
765+
]
766+
]
767+
/following-sibling::div
768+
[@class="radio"]
769+
[
770+
./label
771+
[.=" [trans]label.&c[/trans]"]
772+
[
773+
./input[@type="radio"][@name="name"][@id="name_2"][@value="&c"][not(@checked)]
774+
]
775+
]
776+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
777+
]
778+
'
779+
);
780+
}
781+
782+
public function testSingleChoiceExpandedWithLabelsSetFalseByCallable()
783+
{
784+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
785+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
786+
'choices_as_values' => true,
787+
'choice_label' => function () {
788+
return false;
789+
},
790+
'multiple' => false,
791+
'expanded' => true,
792+
));
793+
794+
$this->assertWidgetMatchesXpath($form->createView(), array(),
795+
'/div
796+
[
797+
./div
798+
[@class="radio"]
799+
[
800+
./label
801+
[
802+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
803+
]
804+
]
805+
/following-sibling::div
806+
[@class="radio"]
807+
[
808+
./label
809+
[
810+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
811+
]
812+
]
813+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
814+
]
815+
'
816+
);
817+
}
818+
696819
public function testSingleChoiceExpandedWithoutTranslation()
697820
{
698821
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
@@ -938,6 +1061,129 @@ public function testMultipleChoiceExpanded()
9381061
);
9391062
}
9401063

1064+
public function testMultipleChoiceExpandedWithLabelsAsFalse()
1065+
{
1066+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a'), array(
1067+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
1068+
'choices_as_values' => true,
1069+
'choice_label' => false,
1070+
'multiple' => true,
1071+
'expanded' => true,
1072+
));
1073+
1074+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1075+
'/div
1076+
[
1077+
./div
1078+
[@class="checkbox"]
1079+
[
1080+
./label
1081+
[
1082+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1083+
]
1084+
]
1085+
/following-sibling::div
1086+
[@class="checkbox"]
1087+
[
1088+
./label
1089+
[
1090+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1091+
]
1092+
]
1093+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1094+
]
1095+
'
1096+
);
1097+
}
1098+
1099+
public function testMultipleChoiceExpandedWithLabelsSetByCallable()
1100+
{
1101+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a'), array(
1102+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'),
1103+
'choices_as_values' => true,
1104+
'choice_label' => function ($choice, $label, $value) {
1105+
if ('&b' === $choice) {
1106+
return false;
1107+
}
1108+
1109+
return 'label.'.$value;
1110+
},
1111+
'multiple' => true,
1112+
'expanded' => true,
1113+
));
1114+
1115+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1116+
'/div
1117+
[
1118+
./div
1119+
[@class="checkbox"]
1120+
[
1121+
./label
1122+
[.=" [trans]label.&a[/trans]"]
1123+
[
1124+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1125+
]
1126+
]
1127+
/following-sibling::div
1128+
[@class="checkbox"]
1129+
[
1130+
./label
1131+
[
1132+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1133+
]
1134+
]
1135+
/following-sibling::div
1136+
[@class="checkbox"]
1137+
[
1138+
./label
1139+
[.=" [trans]label.&c[/trans]"]
1140+
[
1141+
./input[@type="checkbox"][@name="name[]"][@id="name_2"][@value="&c"][not(@checked)]
1142+
]
1143+
]
1144+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1145+
]
1146+
'
1147+
);
1148+
}
1149+
1150+
public function testMultipleChoiceExpandedWithLabelsSetFalseByCallable()
1151+
{
1152+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a'), array(
1153+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
1154+
'choices_as_values' => true,
1155+
'choice_label' => function () {
1156+
return false;
1157+
},
1158+
'multiple' => true,
1159+
'expanded' => true,
1160+
));
1161+
1162+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1163+
'/div
1164+
[
1165+
./div
1166+
[@class="checkbox"]
1167+
[
1168+
./label
1169+
[
1170+
./input[@type="checkbox"][@name="name[]"][@id="name_0"][@value="&a"][@checked]
1171+
]
1172+
]
1173+
/following-sibling::div
1174+
[@class="checkbox"]
1175+
[
1176+
./label
1177+
[
1178+
./input[@type="checkbox"][@name="name[]"][@id="name_1"][@value="&b"][not(@checked)]
1179+
]
1180+
]
1181+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
1182+
]
1183+
'
1184+
);
1185+
}
1186+
9411187
public function testMultipleChoiceExpandedWithoutTranslation()
9421188
{
9431189
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a', '&c'), array(

0 commit comments

Comments
 (0)
0