Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] Do not ignore the choice groups for caching #29695

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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));
}

/**
Expand Down