From 9007911a8594ff6c77e82c877667017b2d797094 Mon Sep 17 00:00:00 2001 From: Valentin Date: Thu, 27 Dec 2018 02:35:06 +0300 Subject: [PATCH] Do not ignore the choice groups for caching --- .../Factory/CachingFactoryDecorator.php | 31 +------------------ .../Factory/CachingFactoryDecoratorTest.php | 17 ++++++---- 2 files changed, 12 insertions(+), 36 deletions(-) diff --git a/src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php b/src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php index dba025bc6487..ece33ad65c5c 100644 --- a/src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php +++ b/src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php @@ -62,30 +62,6 @@ public static function generateHash($value, $namespace = '') return hash('sha256', $namespace.':'.serialize($value)); } - /** - * Flattens an array into the given output variable. - * - * @param array $array The array to flatten - * @param array $output The flattened output - * - * @internal - */ - private static function flatten(array $array, &$output) - { - if (null === $output) { - $output = array(); - } - - foreach ($array as $key => $value) { - if (\is_array($value)) { - self::flatten($value, $output); - continue; - } - - $output[$key] = $value; - } - } - public function __construct(ChoiceListFactoryInterface $decoratedFactory) { $this->decoratedFactory = $decoratedFactory; @@ -113,12 +89,7 @@ public function createListFromChoices($choices, $value = null) // The value is not validated on purpose. The decorated factory may // decide which values to accept and which not. - // We ignore the choice groups for caching. If two choice lists are - // requested with the same choices, but a different grouping, the same - // choice list is returned. - self::flatten($choices, $flatChoices); - - $hash = self::generateHash(array($flatChoices, $value), 'fromChoices'); + $hash = self::generateHash(array($choices, $value), 'fromChoices'); if (!isset($this->lists[$hash])) { $this->lists[$hash] = $this->decoratedFactory->createListFromChoices($choices, $value); diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php index dd6c968bd84b..c0d34c1bfd51 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/CachingFactoryDecoratorTest.php @@ -64,19 +64,24 @@ public function testCreateFromChoicesComparesTraversableChoicesAsArray() $this->assertSame($list, $this->factory->createListFromChoices($choices2)); } - public function testCreateFromChoicesFlattensChoices() + public function testCreateFromChoicesGroupedChoices() { $choices1 = array('key' => array('A' => 'a')); $choices2 = array('A' => 'a'); - $list = new \stdClass(); + $list1 = new \stdClass(); + $list2 = new \stdClass(); - $this->decoratedFactory->expects($this->once()) + $this->decoratedFactory->expects($this->at(0)) ->method('createListFromChoices') ->with($choices1) - ->will($this->returnValue($list)); + ->will($this->returnValue($list1)); + $this->decoratedFactory->expects($this->at(1)) + ->method('createListFromChoices') + ->with($choices2) + ->will($this->returnValue($list2)); - $this->assertSame($list, $this->factory->createListFromChoices($choices1)); - $this->assertSame($list, $this->factory->createListFromChoices($choices2)); + $this->assertSame($list1, $this->factory->createListFromChoices($choices1)); + $this->assertSame($list2, $this->factory->createListFromChoices($choices2)); } /**