Skip to content

Commit

Permalink
[Form] The properties of FormView are now accessed directly in order …
Browse files Browse the repository at this point in the history
…to increase performance (PHP +200ms, Twig +150ms)
  • Loading branch information
webmozart committed Jul 21, 2012
1 parent 9f157a1 commit d072f35
Show file tree
Hide file tree
Showing 55 changed files with 438 additions and 650 deletions.
10 changes: 5 additions & 5 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Expand Up @@ -124,7 +124,7 @@ public function testSetDataToUninitializedEntityWithNonRequired()
'property' => 'name'
));

$this->assertEquals(array(1 => new ChoiceView('1', 'Foo'), 2 => new ChoiceView('2', 'Bar')), $field->createView()->getVar('choices'));
$this->assertEquals(array(1 => new ChoiceView('1', 'Foo'), 2 => new ChoiceView('2', 'Bar')), $field->createView()->vars['choices']);
}

public function testSetDataToUninitializedEntityWithNonRequiredToString()
Expand All @@ -140,7 +140,7 @@ public function testSetDataToUninitializedEntityWithNonRequiredToString()
'required' => false,
));

$this->assertEquals(array(1 => new ChoiceView('1', 'Foo'), 2 => new ChoiceView('2', 'Bar')), $field->createView()->getVar('choices'));
$this->assertEquals(array(1 => new ChoiceView('1', 'Foo'), 2 => new ChoiceView('2', 'Bar')), $field->createView()->vars['choices']);
}

public function testSetDataToUninitializedEntityWithNonRequiredQueryBuilder()
Expand All @@ -159,7 +159,7 @@ public function testSetDataToUninitializedEntityWithNonRequiredQueryBuilder()
'query_builder' => $qb
));

$this->assertEquals(array(1 => new ChoiceView('1', 'Foo'), 2 => new ChoiceView('2', 'Bar')), $field->createView()->getVar('choices'));
$this->assertEquals(array(1 => new ChoiceView('1', 'Foo'), 2 => new ChoiceView('2', 'Bar')), $field->createView()->vars['choices']);
}

/**
Expand Down Expand Up @@ -503,7 +503,7 @@ public function testOverrideChoices()

$field->bind('2');

$this->assertEquals(array(1 => new ChoiceView('1', 'Foo'), 2 => new ChoiceView('2', 'Bar')), $field->createView()->getVar('choices'));
$this->assertEquals(array(1 => new ChoiceView('1', 'Foo'), 2 => new ChoiceView('2', 'Bar')), $field->createView()->vars['choices']);
$this->assertTrue($field->isSynchronized());
$this->assertSame($entity2, $field->getData());
$this->assertSame('2', $field->getClientData());
Expand Down Expand Up @@ -533,7 +533,7 @@ public function testGroupByChoices()
'Group1' => array(1 => new ChoiceView('1', 'Foo'), 2 => new ChoiceView('2', 'Bar')),
'Group2' => array(3 => new ChoiceView('3', 'Baz')),
'4' => new ChoiceView('4', 'Boo!')
), $field->createView()->getVar('choices'));
), $field->createView()->vars['choices']);
}

public function testDisallowChoicesThatAreNotIncluded_choicesSingleIdentifier()
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Extension/FormExtension.php
Expand Up @@ -13,7 +13,7 @@

use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
use Symfony\Bridge\Twig\Form\TwigRendererInterface;
use Symfony\Component\Form\FormViewInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;

/**
Expand Down
18 changes: 9 additions & 9 deletions src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Bridge\Twig\Form;

use Symfony\Component\Form\AbstractRendererEngine;
use Symfony\Component\Form\FormViewInterface;
use Symfony\Component\Form\FormView;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -40,9 +40,9 @@ public function setEnvironment(\Twig_Environment $environment)
/**
* {@inheritdoc}
*/
public function renderBlock(FormViewInterface $view, $resource, $block, array $variables = array())
public function renderBlock(FormView $view, $resource, $block, array $variables = array())
{
$cacheKey = $view->getVar(self::CACHE_KEY_VAR);
$cacheKey = $view->vars[self::CACHE_KEY_VAR];

$context = $this->environment->mergeGlobals($variables);

Expand Down Expand Up @@ -71,12 +71,12 @@ public function renderBlock(FormViewInterface $view, $resource, $block, array $v
* @see getResourceForBlock()
*
* @param string $cacheKey The cache key of the form view.
* @param FormViewInterface $view The form view for finding the applying themes.
* @param FormView $view The form view for finding the applying themes.
* @param string $block The name of the block to load.
*
* @return Boolean True if the resource could be loaded, false otherwise.
*/
protected function loadResourceForBlock($cacheKey, FormViewInterface $view, $block)
protected function loadResourceForBlock($cacheKey, FormView $view, $block)
{
// The caller guarantees that $this->resources[$cacheKey][$block] is
// not set, but it doesn't have to check whether $this->resources[$cacheKey]
Expand Down Expand Up @@ -105,19 +105,19 @@ protected function loadResourceForBlock($cacheKey, FormViewInterface $view, $blo
}

// Check the default themes once we reach the root view without success
if (!$view->hasParent()) {
if (!$view->parent) {
for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) {
$this->loadResourcesFromTheme($cacheKey, $this->defaultThemes[$i]);
// CONTINUE LOADING (see doc comment)
}
}

// Proceed with the themes of the parent view
if ($view->hasParent()) {
$parentCacheKey = $view->getParent()->getVar(self::CACHE_KEY_VAR);
if ($view->parent) {
$parentCacheKey = $view->parent->vars[self::CACHE_KEY_VAR];

if (!isset($this->resources[$parentCacheKey])) {
$this->loadResourceForBlock($parentCacheKey, $view->getParent(), $block);
$this->loadResourceForBlock($parentCacheKey, $view->parent, $block);
}

// EAGER CACHE POPULATION (see doc comment)
Expand Down
@@ -1 +1 @@
<?php if ($form->getVar('multipart')): ?>enctype="multipart/form-data"<?php endif ?>
<?php if ($form->vars['multipart']): ?>enctype="multipart/form-data"<?php endif ?>
@@ -1,5 +1,5 @@
<div <?php echo $view['form']->block('widget_container_attributes') ?>>
<?php if (!$form->hasParent() && $errors): ?>
<?php if (!$form->parent && $errors): ?>
<tr>
<td colspan="2">
<?php echo $view['form']->errors($form) ?>
Expand Down
@@ -1,5 +1,5 @@
<table <?php echo $view['form']->block('widget_container_attributes') ?>>
<?php if (!$form->hasParent()): ?>
<?php if (!$form->parent): ?>
<?php echo $view['form']->errors($form) ?>
<?php endif ?>
<?php echo $view['form']->block('form_rows') ?>
Expand Down
32 changes: 16 additions & 16 deletions src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\Templating\Helper\Helper;
use Symfony\Component\Form\FormRendererInterface;
use Symfony\Component\Form\FormViewInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
Expand Down Expand Up @@ -54,7 +54,7 @@ public function isChoiceGroup($label)
return $this->renderer->isChoiceGroup($label);
}

public function isChoiceSelected(FormViewInterface $view, ChoiceView $choice)
public function isChoiceSelected(FormView $view, ChoiceView $choice)
{
return $this->renderer->isChoiceSelected($view, $choice);
}
Expand All @@ -64,10 +64,10 @@ public function isChoiceSelected(FormViewInterface $view, ChoiceView $choice)
*
* The theme format is "<Bundle>:<Controller>".
*
* @param FormViewInterface $view A FormViewInterface instance
* @param FormView $view A FormView instance
* @param string|array $themes A theme or an array of theme
*/
public function setTheme(FormViewInterface $view, $themes)
public function setTheme(FormView $view, $themes)
{
$this->renderer->setTheme($view, $themes);
}
Expand All @@ -79,11 +79,11 @@ public function setTheme(FormViewInterface $view, $themes)
*
* <form action="..." method="post" <?php echo $view['form']->enctype() ?>>
*
* @param FormViewInterface $view The view for which to render the encoding type
* @param FormView $view The view for which to render the encoding type
*
* @return string The HTML markup
*/
public function enctype(FormViewInterface $view)
public function enctype(FormView $view)
{
return $this->renderer->renderEnctype($view);
}
Expand All @@ -101,64 +101,64 @@ public function enctype(FormViewInterface $view)
*
* <?php echo view['form']->widget(array('separator' => '+++++)) ?>
*
* @param FormViewInterface $view The view for which to render the widget
* @param FormView $view The view for which to render the widget
* @param array $variables Additional variables passed to the template
*
* @return string The HTML markup
*/
public function widget(FormViewInterface $view, array $variables = array())
public function widget(FormView $view, array $variables = array())
{
return $this->renderer->renderWidget($view, $variables);
}

/**
* Renders the entire form field "row".
*
* @param FormViewInterface $view The view for which to render the row
* @param FormView $view The view for which to render the row
* @param array $variables Additional variables passed to the template
*
* @return string The HTML markup
*/
public function row(FormViewInterface $view, array $variables = array())
public function row(FormView $view, array $variables = array())
{
return $this->renderer->renderRow($view, $variables);
}

/**
* Renders the label of the given view.
*
* @param FormViewInterface $view The view for which to render the label
* @param FormView $view The view for which to render the label
* @param string $label The label
* @param array $variables Additional variables passed to the template
*
* @return string The HTML markup
*/
public function label(FormViewInterface $view, $label = null, array $variables = array())
public function label(FormView $view, $label = null, array $variables = array())
{
return $this->renderer->renderLabel($view, $label, $variables);
}

/**
* Renders the errors of the given view.
*
* @param FormViewInterface $view The view to render the errors for
* @param FormView $view The view to render the errors for
*
* @return string The HTML markup
*/
public function errors(FormViewInterface $view)
public function errors(FormView $view)
{
return $this->renderer->renderErrors($view);
}

/**
* Renders views which have not already been rendered.
*
* @param FormViewInterface $view The parent view
* @param FormView $view The parent view
* @param array $variables An array of variables
*
* @return string The HTML markup
*/
public function rest(FormViewInterface $view, array $variables = array())
public function rest(FormView $view, array $variables = array())
{
return $this->renderer->renderRest($view, $variables);
}
Expand Down
26 changes: 13 additions & 13 deletions src/Symfony/Component/Form/AbstractRendererEngine.php
Expand Up @@ -19,7 +19,7 @@
abstract class AbstractRendererEngine implements FormRendererEngineInterface
{
/**
* The variable in {@link FormViewInterface} used as cache key.
* The variable in {@link FormView} used as cache key.
*/
const CACHE_KEY_VAR = 'full_block_name';

Expand Down Expand Up @@ -57,9 +57,9 @@ public function __construct(array $defaultThemes = array())
/**
* {@inheritdoc}
*/
public function setTheme(FormViewInterface $view, $themes)
public function setTheme(FormView $view, $themes)
{
$cacheKey = $view->getVar(self::CACHE_KEY_VAR);
$cacheKey = $view->vars[self::CACHE_KEY_VAR];

// Do not cast, as casting turns objects into arrays of properties
$this->themes[$cacheKey] = is_array($themes) ? $themes : array($themes);
Expand All @@ -74,9 +74,9 @@ public function setTheme(FormViewInterface $view, $themes)
/**
* {@inheritdoc}
*/
public function getResourceForBlock(FormViewInterface $view, $block)
public function getResourceForBlock(FormView $view, $block)
{
$cacheKey = $view->getVar(self::CACHE_KEY_VAR);
$cacheKey = $view->vars[self::CACHE_KEY_VAR];

if (!isset($this->resources[$cacheKey][$block])) {
$this->loadResourceForBlock($cacheKey, $view, $block);
Expand All @@ -88,9 +88,9 @@ public function getResourceForBlock(FormViewInterface $view, $block)
/**
* {@inheritdoc}
*/
public function getResourceForBlockHierarchy(FormViewInterface $view, array $blockHierarchy, $hierarchyLevel)
public function getResourceForBlockHierarchy(FormView $view, array $blockHierarchy, $hierarchyLevel)
{
$cacheKey = $view->getVar(self::CACHE_KEY_VAR);
$cacheKey = $view->vars[self::CACHE_KEY_VAR];
$block = $blockHierarchy[$hierarchyLevel];

if (!isset($this->resources[$cacheKey][$block])) {
Expand All @@ -103,9 +103,9 @@ public function getResourceForBlockHierarchy(FormViewInterface $view, array $blo
/**
* {@inheritdoc}
*/
public function getResourceHierarchyLevel(FormViewInterface $view, array $blockHierarchy, $hierarchyLevel)
public function getResourceHierarchyLevel(FormView $view, array $blockHierarchy, $hierarchyLevel)
{
$cacheKey = $view->getVar(self::CACHE_KEY_VAR);
$cacheKey = $view->vars[self::CACHE_KEY_VAR];
$block = $blockHierarchy[$hierarchyLevel];

if (!isset($this->resources[$cacheKey][$block])) {
Expand All @@ -128,12 +128,12 @@ public function getResourceHierarchyLevel(FormViewInterface $view, array $blockH
* @see getResourceForBlock()
*
* @param string $cacheKey The cache key of the form view.
* @param FormViewInterface $view The form view for finding the applying themes.
* @param FormView $view The form view for finding the applying themes.
* @param string $block The name of the block to load.
*
* @return Boolean True if the resource could be loaded, false otherwise.
*/
abstract protected function loadResourceForBlock($cacheKey, FormViewInterface $view, $block);
abstract protected function loadResourceForBlock($cacheKey, FormView $view, $block);

/**
* Loads the cache with the resource for a specific level of a block hierarchy.
Expand All @@ -142,7 +142,7 @@ abstract protected function loadResourceForBlock($cacheKey, FormViewInterface $v
*
* @param string $cacheKey The cache key used for storing the
* resource.
* @param FormViewInterface $view The form view for finding the applying
* @param FormView $view The form view for finding the applying
* themes.
* @param array $blockHierarchy The block hierarchy, with the most
* specific block name at the end.
Expand All @@ -151,7 +151,7 @@ abstract protected function loadResourceForBlock($cacheKey, FormViewInterface $v
*
* @return Boolean True if the resource could be loaded, false otherwise.
*/
private function loadResourceForBlockHierarchy($cacheKey, FormViewInterface $view, array $blockHierarchy, $hierarchyLevel)
private function loadResourceForBlockHierarchy($cacheKey, FormView $view, array $blockHierarchy, $hierarchyLevel)
{
$block = $blockHierarchy[$hierarchyLevel];

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/AbstractType.php
Expand Up @@ -36,14 +36,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
/**
* {@inheritdoc}
*/
public function buildView(FormViewInterface $view, FormInterface $form, array $options)
public function buildView(FormView $view, FormInterface $form, array $options)
{
}

/**
* {@inheritdoc}
*/
public function finishView(FormViewInterface $view, FormInterface $form, array $options)
public function finishView(FormView $view, FormInterface $form, array $options)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/AbstractTypeExtension.php
Expand Up @@ -28,14 +28,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
/**
* {@inheritdoc}
*/
public function buildView(FormViewInterface $view, FormInterface $form, array $options)
public function buildView(FormView $view, FormInterface $form, array $options)
{
}

/**
* {@inheritdoc}
*/
public function finishView(FormViewInterface $view, FormInterface $form, array $options)
public function finishView(FormView $view, FormInterface $form, array $options)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -113,9 +113,9 @@ CHANGELOG
* deprecated the methods `getDefaultOptions` and `getAllowedOptionValues`
in FormTypeInterface and FormTypeExtensionInterface
* options passed during construction can now be accessed from FormConfigInterface
* added FormBuilderInterface, FormViewInterface and FormConfigEditorInterface
* added FormBuilderInterface, FormView and FormConfigEditorInterface
* [BC BREAK] the methods in FormTypeInterface and FormTypeExtensionInterface now
receive FormBuilderInterface and FormViewInterface instead of FormBuilder and
receive FormBuilderInterface and FormView instead of FormBuilder and
FormView
* [BC BREAK] the method `buildViewBottomUp` was renamed to `finishView` in
FormTypeInterface and FormTypeExtensionInterface
Expand Down

3 comments on commit d072f35

@stof
Copy link
Member

@stof stof commented on d072f35 Dec 19, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Drak rendering a big form

@stof
Copy link
Member

@stof stof commented on d072f35 Dec 19, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I said 1 big form. Remember that it is a hierarchical structure, so rendering it will involve many Form and FormView instances

@webmozart
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.