Skip to content

Commit

Permalink
[Form] Tweaked the generation of option tags for performance (PHP +20…
Browse files Browse the repository at this point in the history
…0ms, Twig +50ms)
  • Loading branch information
webmozart committed Jul 21, 2012
1 parent 400c95b commit 8b72766
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 108 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Bridge/Twig/Extension/FormExtension.php
Expand Up @@ -69,8 +69,6 @@ public function getFunctions()
'form_row' => new \Twig_Function_Method($this, 'renderer->renderRow', array('is_safe' => array('html'))),
'form_rest' => new \Twig_Function_Method($this, 'renderer->renderRest', array('is_safe' => array('html'))),
'csrf_token' => new \Twig_Function_Method($this, 'renderer->renderCsrfToken'),
'_form_is_choice_group' => new \Twig_Function_Method($this, 'renderer->isChoiceGroup', array('is_safe' => array('html'))),
'_form_is_choice_selected' => new \Twig_Function_Method($this, 'renderer->isChoiceSelected', array('is_safe' => array('html'))),
);
}

Expand All @@ -80,7 +78,9 @@ public function getFunctions()
public function getFilters()
{
return array(
'humanize' => new \Twig_Filter_Method($this, 'renderer->humanize'),
'humanize' => new \Twig_Filter_Method($this, 'renderer->humanize'),
'is_choice_group' => new \Twig_Filter_Function('is_array', array('is_safe' => array('html'))),
'is_choice_selected' => new \Twig_Filter_Method($this, 'renderer->isChoiceSelected', array('is_safe' => array('html'))),
);
}

Expand Down
Expand Up @@ -86,15 +86,14 @@

{% block choice_widget_options %}
{% spaceless %}
{% for index, choice in options %}
{% if _form_is_choice_group(choice) %}
<optgroup label="{{ index|trans({}, translation_domain) }}">
{% for nested_choice in choice %}
<option value="{{ nested_choice.value }}"{% if _form_is_choice_selected(form, nested_choice) %} selected="selected"{% endif %}>{{ nested_choice.label|trans({}, translation_domain) }}</option>
{% endfor %}
{% for group_label, choice in options %}
{% if choice|is_choice_group %}
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
{% set options = choice %}
{{ block('choice_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if _form_is_choice_selected(form, choice) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
<option value="{{ choice.value }}"{% if choice|is_choice_selected(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
{% endif %}
{% endfor %}
{% endspaceless %}
Expand Down
Expand Up @@ -4,10 +4,10 @@
>
<?php if (null !== $empty_value): ?><option value=""><?php echo $view->escape($view['translator']->trans($empty_value, array(), $translation_domain)) ?></option><?php endif; ?>
<?php if (count($preferred_choices) > 0): ?>
<?php echo $view['form']->block('choice_widget_options', array('options' => $preferred_choices)) ?>
<?php echo $view['form']->block('choice_widget_options', array('choices' => $preferred_choices)) ?>
<?php if (count($choices) > 0 && null !== $separator): ?>
<option disabled="disabled"><?php echo $separator ?></option>
<?php endif ?>
<?php endif ?>
<?php echo $view['form']->block('choice_widget_options', array('options' => $choices)) ?>
<?php echo $view['form']->block('choice_widget_options', array('choices' => $choices)) ?>
</select>
@@ -1,11 +1,9 @@
<?php foreach ($options as $index => $choice): ?>
<?php if ($view['form']->isChoiceGroup($choice)): ?>
<?php foreach ($choices as $index => $choice): ?>
<?php if (is_array($choice)): ?>
<optgroup label="<?php echo $view->escape($view['translator']->trans($index, array(), $translation_domain)) ?>">
<?php foreach ($choice as $nested_choice): ?>
<option value="<?php echo $view->escape($nested_choice->value) ?>"<?php if ($view['form']->isChoiceSelected($form, $nested_choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($nested_choice->label, array(), $translation_domain)) ?></option>
<?php endforeach ?>
<?php echo $view['form']->block('choice_widget_options', array('choices' => $choice)) ?>
</optgroup>
<?php else: ?>
<option value="<?php echo $view->escape($choice->value) ?>"<?php if ($view['form']->isChoiceSelected($form, $choice)): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($choice->label, array(), $translation_domain)) ?></option>
<option value="<?php echo $view->escape($choice->value) ?>"<?php if ($view['form']->isChoiceSelected($choice, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($choice->label, array(), $translation_domain)) ?></option>
<?php endif ?>
<?php endforeach ?>
Expand Up @@ -49,14 +49,9 @@ public function getName()
return 'form';
}

public function isChoiceGroup($label)
public function isChoiceSelected(ChoiceView $choice, $selectedValue)
{
return $this->renderer->isChoiceGroup($label);
}

public function isChoiceSelected(FormView $view, ChoiceView $choice)
{
return $this->renderer->isChoiceSelected($view, $choice);
return $this->renderer->isChoiceSelected($choice, $selectedValue);
}

/**
Expand Down Expand Up @@ -176,7 +171,7 @@ public function rest(FormView $view, array $variables = array())
*/
public function renderBlock($block, array $variables = array())
{
return $this->block($block, $variables);
return $this->renderer->renderBlock($block, $variables);
}

/**
Expand Down
44 changes: 20 additions & 24 deletions src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php
Expand Up @@ -240,27 +240,19 @@ public function getIndicesForValues(array $values)
* view objects.
* @param array $bucketForRemaining The bucket where to store the
* non-preferred view objects.
* @param array $choices The list of choices.
* @param array $labels The labels corresponding to the choices.
* @param array $preferredChoices The preferred choices.
* @param array $choices The list of choices.
* @param array $labels The labels corresponding to the choices.
* @param array $preferredChoices The preferred choices.
*
* @throws UnexpectedTypeException If the structure of the $labels array
* does not match the structure of the
* $choices array.
*/
protected function addChoices(&$bucketForPreferred, &$bucketForRemaining, $choices, $labels, array $preferredChoices)
protected function addChoices(&$bucketForPreferred, &$bucketForRemaining, array $choices, array $labels, array $preferredChoices)
{
if (!is_array($choices) && !$choices instanceof \Traversable) {
throw new UnexpectedTypeException($choices, 'array or \Traversable');
}

// Add choices to the nested buckets
foreach ($choices as $group => $choice) {
if (is_array($choice)) {
if (!is_array($labels)) {
throw new UnexpectedTypeException($labels, 'array');
}

// Don't do the work if the array is empty
if (count($choice) > 0) {
$this->addChoiceGroup(
Expand Down Expand Up @@ -292,11 +284,11 @@ protected function addChoices(&$bucketForPreferred, &$bucketForRemaining, $choic
* view objects.
* @param array $bucketForRemaining The bucket where to store the
* non-preferred view objects.
* @param array $choices The list of choices in the group.
* @param array $labels The labels corresponding to the choices in the group.
* @param array $preferredChoices The preferred choices.
* @param array $choices The list of choices in the group.
* @param array $labels The labels corresponding to the choices in the group.
* @param array $preferredChoices The preferred choices.
*/
protected function addChoiceGroup($group, &$bucketForPreferred, &$bucketForRemaining, $choices, $labels, array $preferredChoices)
protected function addChoiceGroup($group, &$bucketForPreferred, &$bucketForRemaining, array $choices, array $labels, array $preferredChoices)
{
// If this is a choice group, create a new level in the choice
// key hierarchy
Expand All @@ -323,13 +315,15 @@ protected function addChoiceGroup($group, &$bucketForPreferred, &$bucketForRemai
/**
* Adds a new choice.
*
* @param array $bucketForPreferred The bucket where to store the preferred
* view objects.
* @param array $bucketForRemaining The bucket where to store the
* non-preferred view objects.
* @param mixed $choice The choice to add.
* @param string $label The label for the choice.
* @param array $preferredChoices The preferred choices.
* @param array $bucketForPreferred The bucket where to store the preferred
* view objects.
* @param array $bucketForRemaining The bucket where to store the
* non-preferred view objects.
* @param mixed $choice The choice to add.
* @param string $label The label for the choice.
* @param array $preferredChoices The preferred choices.
*
* @throws InvalidConfigurationException If no valid value or index could be created.
*/
protected function addChoice(&$bucketForPreferred, &$bucketForRemaining, $choice, $label, array $preferredChoices)
{
Expand Down Expand Up @@ -366,8 +360,10 @@ protected function addChoice(&$bucketForPreferred, &$bucketForRemaining, $choice
*
* @param mixed $choice The choice to test.
* @param array $preferredChoices An array of preferred choices.
*
* @return Boolean Whether the choice is preferred.
*/
protected function isPreferred($choice, $preferredChoices)
protected function isPreferred($choice, array $preferredChoices)
{
return false !== array_search($choice, $preferredChoices, true);
}
Expand Down
Expand Up @@ -89,7 +89,7 @@ public function getValuesForChoices(array $choices)
*
* @see parent::addChoices
*/
protected function addChoices(&$bucketForPreferred, &$bucketForRemaining, $choices, $labels, array $preferredChoices)
protected function addChoices(&$bucketForPreferred, &$bucketForRemaining, array $choices, array $labels, array $preferredChoices)
{
// Add choices to the nested buckets
foreach ($choices as $choice => $label) {
Expand Down Expand Up @@ -126,8 +126,10 @@ protected function addChoices(&$bucketForPreferred, &$bucketForRemaining, $choic
*
* @param mixed $choice The choice to test.
* @param array $preferredChoices An array of preferred choices.
*
* @return Boolean Whether the choice is preferred.
*/
protected function isPreferred($choice, $preferredChoices)
protected function isPreferred($choice, array $preferredChoices)
{
// Optimize performance over the default implementation
return isset($preferredChoices[$choice]);
Expand Down
17 changes: 4 additions & 13 deletions src/Symfony/Component/Form/FormRenderer.php
Expand Up @@ -175,24 +175,15 @@ public function renderBlock($block, array $variables = array())
/**
* {@inheritdoc}
*/
public function isChoiceGroup($choice)
public function isChoiceSelected(ChoiceView $choice, $selectedValue)
{
return is_array($choice) || $choice instanceof \Traversable;
}

/**
* {@inheritdoc}
*/
public function isChoiceSelected(FormView $view, ChoiceView $choice)
{
$value = $view->vars['value'];
$choiceValue = $choice->value;

if (is_array($value)) {
return false !== array_search($choiceValue, $value, true);
if (is_array($selectedValue)) {
return false !== array_search($choiceValue, $selectedValue, true);
}

return $choiceValue === $value;
return $choiceValue === $selectedValue;
}

/**
Expand Down
15 changes: 3 additions & 12 deletions src/Symfony/Component/Form/FormRendererInterface.php
Expand Up @@ -146,24 +146,15 @@ public function renderBlock($block, array $variables = array());
*/
public function renderCsrfToken($intention);

/**
* Returns whether the given choice is a group.
*
* @param mixed $choice A choice
*
* @return Boolean Whether the choice is a group
*/
public function isChoiceGroup($choice);

/**
* Returns whether the given choice is selected.
*
* @param FormView $view The view of the choice field
* @param ChoiceView $choice The choice to check
* @param ChoiceView $choice The choice to check.
* @param string|array $selectedValue The selected value(s).
*
* @return Boolean Whether the choice is selected
*/
public function isChoiceSelected(FormView $view, ChoiceView $choice);
public function isChoiceSelected(ChoiceView $choice, $selectedValue);

/**
* Makes a technical name human readable.
Expand Down
32 changes: 1 addition & 31 deletions src/Symfony/Component/Form/Tests/FormRendererTest.php
Expand Up @@ -40,34 +40,6 @@ protected function setUp()
$this->renderer = new FormRenderer($this->engine, $this->csrfProvider);
}

public function isChoiceGroupProvider()
{
return array(
array(false, 0),
array(false, '0'),
array(false, '1'),
array(false, 1),
array(false, ''),
array(false, null),
array(false, true),

array(true, array()),
);
}

/**
* @dataProvider isChoiceGroupProvider
*/
public function testIsChoiceGroup($expected, $value)
{
$this->assertSame($expected, $this->renderer->isChoiceGroup($value));
}

public function testIsChoiceGroupPart2()
{
$this->assertTrue($this->renderer->isChoiceGroup(new \SplFixedArray(1)));
}

public function isChoiceSelectedProvider()
{
// The commented cases should not be necessary anymore, because the
Expand Down Expand Up @@ -96,10 +68,8 @@ public function isChoiceSelectedProvider()
*/
public function testIsChoiceSelected($expected, $choice, $value)
{
$view = new FormView();
$view->vars['value'] = $value;
$choice = new ChoiceView($choice, $choice . ' label');

$this->assertSame($expected, $this->renderer->isChoiceSelected($view, $choice));
$this->assertSame($expected, $this->renderer->isChoiceSelected($choice, $value));
}
}

0 comments on commit 8b72766

Please sign in to comment.