From dc3a680cd3afb935082920d7206413547e53da32 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sat, 21 Jul 2012 19:44:09 +0200 Subject: [PATCH] [Form] Improved FormRenderer API to reduce the size of the function call stack during rendering --- UPGRADE-2.1.md | 31 ++-- composer.json | 2 +- .../Bridge/Twig/Extension/FormExtension.php | 12 +- .../Bridge/Twig/Form/TwigRendererEngine.php | 28 ++-- .../Twig/Node/SearchAndRenderBlockNode.php | 56 ++++++++ .../Extension/FormExtensionDivLayoutTest.php | 16 ++- .../FormExtensionTableLayoutTest.php | 16 ++- src/Symfony/Bridge/Twig/composer.json | 2 +- .../Resources/views/Form/attributes.html.php | 2 +- .../views/Form/checkbox_widget.html.php | 2 +- .../views/Form/choice_options.html.php | 2 +- .../views/Form/choice_widget.html.php | 4 +- .../Form/choice_widget_collapsed.html.php | 6 +- .../Form/choice_widget_expanded.html.php | 2 +- .../views/Form/choice_widget_options.html.php | 2 +- .../views/Form/container_attributes.html.php | 2 +- .../Resources/views/Form/date_widget.html.php | 4 +- .../views/Form/datetime_widget.html.php | 4 +- .../views/Form/email_widget.html.php | 2 +- .../views/Form/field_enctype.html.php | 2 +- .../views/Form/field_errors.html.php | 2 +- .../Resources/views/Form/field_label.html.php | 2 +- .../Resources/views/Form/field_rest.html.php | 2 +- .../Resources/views/Form/field_row.html.php | 2 +- .../Resources/views/Form/field_rows.html.php | 2 +- .../views/Form/field_widget.html.php | 2 +- .../Resources/views/Form/form_widget.html.php | 4 +- .../views/Form/form_widget_compound.html.php | 4 +- .../views/Form/form_widget_simple.html.php | 2 +- .../views/Form/hidden_widget.html.php | 2 +- .../views/Form/integer_widget.html.php | 2 +- .../views/Form/money_widget.html.php | 2 +- .../views/Form/number_widget.html.php | 2 +- .../views/Form/password_widget.html.php | 2 +- .../views/Form/percent_widget.html.php | 2 +- .../views/Form/radio_widget.html.php | 2 +- .../views/Form/repeated_row.html.php | 2 +- .../views/Form/search_widget.html.php | 2 +- .../views/Form/textarea_widget.html.php | 2 +- .../Resources/views/Form/time_widget.html.php | 4 +- .../Resources/views/Form/url_widget.html.php | 2 +- .../FormTable/form_widget_compound.html.php | 4 +- .../Templating/Helper/FormHelper.php | 51 +++---- .../Parent/form_widget_simple.html.php | 2 +- .../Component/Form/AbstractRendererEngine.php | 86 +++++------ src/Symfony/Component/Form/CHANGELOG.md | 2 +- .../Templating/TemplatingRendererEngine.php | 36 ++--- src/Symfony/Component/Form/FormRenderer.php | 136 ++++++------------ .../Form/FormRendererEngineInterface.php | 74 +++++----- .../Component/Form/FormRendererInterface.php | 100 +++---------- 50 files changed, 343 insertions(+), 393 deletions(-) create mode 100644 src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 8804281f8b..1b56d129f0 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -559,6 +559,22 @@ {% endblock %} ``` + * The method `renderBlock()` of the helper for the PHP Templating component was + renamed to `block()`. Its first argument is now expected to be a `FormView` + instance. + + Before: + + ``` + renderBlock('widget_attributes') ?> + ``` + + After: + + ``` + block($form, 'widget_attributes') ?> + ``` + #### Other BC Breaks * The order of the first two arguments of the methods `createNamed` and @@ -1012,21 +1028,6 @@ $registry->addType($registry->resolveType(new MyFormType())); ``` - * The method `renderBlock()` of the helper for the PHP Templating component was - deprecated and will be removed in Symfony 2.3. You should use `block()` instead. - - Before: - - ``` - renderBlock('widget_attributes') ?> - ``` - - After: - - ``` - block('widget_attributes') ?> - ``` - * The following methods in class `FormView` were deprecated and will be removed in Symfony 2.3: diff --git a/composer.json b/composer.json index a514890e31..4c062ea9b7 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "require": { "php": ">=5.3.3", "doctrine/common": ">2.2,<2.4-dev", - "twig/twig": ">=1.8,<2.0-dev" + "twig/twig": ">=1.9.1,<2.0-dev" }, "replace": { "symfony/doctrine-bridge": "self.version", diff --git a/src/Symfony/Bridge/Twig/Extension/FormExtension.php b/src/Symfony/Bridge/Twig/Extension/FormExtension.php index 0a5f356350..13ce859e3c 100644 --- a/src/Symfony/Bridge/Twig/Extension/FormExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/FormExtension.php @@ -62,12 +62,12 @@ class FormExtension extends \Twig_Extension public function getFunctions() { return array( - 'form_enctype' => new \Twig_Function_Method($this, 'renderer->renderEnctype', array('is_safe' => array('html'))), - 'form_widget' => new \Twig_Function_Method($this, 'renderer->renderWidget', array('is_safe' => array('html'))), - 'form_errors' => new \Twig_Function_Method($this, 'renderer->renderErrors', array('is_safe' => array('html'))), - 'form_label' => new \Twig_Function_Method($this, 'renderer->renderLabel', array('is_safe' => array('html'))), - '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'))), + 'form_enctype' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))), + 'form_widget' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))), + 'form_errors' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))), + 'form_label' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))), + 'form_row' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))), + 'form_rest' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))), 'csrf_token' => new \Twig_Function_Method($this, 'renderer->renderCsrfToken'), ); } diff --git a/src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php b/src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php index df907cb971..05beb31565 100644 --- a/src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php +++ b/src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php @@ -40,7 +40,7 @@ class TwigRendererEngine extends AbstractRendererEngine implements TwigRendererE /** * {@inheritdoc} */ - public function renderBlock(FormView $view, $resource, $block, array $variables = array()) + public function renderBlock(FormView $view, $resource, $blockName, array $variables = array()) { $cacheKey = $view->vars[self::CACHE_KEY_VAR]; @@ -55,7 +55,7 @@ class TwigRendererEngine extends AbstractRendererEngine implements TwigRendererE // We do not call renderBlock here to avoid too many nested level calls // (XDebug limits the level to 100 by default) - $this->template->displayBlock($block, $context, $this->resources[$cacheKey]); + $this->template->displayBlock($blockName, $context, $this->resources[$cacheKey]); return ob_get_clean(); } @@ -70,13 +70,13 @@ class TwigRendererEngine extends AbstractRendererEngine implements TwigRendererE * * @see getResourceForBlock() * - * @param string $cacheKey The cache key of the form view. - * @param FormView $view The form view for finding the applying themes. - * @param string $block The name of the block to load. + * @param string $cacheKey The cache key of the form view. + * @param FormView $view The form view for finding the applying themes. + * @param string $blockName The name of the block to load. * * @return Boolean True if the resource could be loaded, false otherwise. */ - protected function loadResourceForBlock($cacheKey, FormView $view, $block) + protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName) { // The caller guarantees that $this->resources[$cacheKey][$block] is // not set, but it doesn't have to check whether $this->resources[$cacheKey] @@ -86,7 +86,7 @@ class TwigRendererEngine extends AbstractRendererEngine implements TwigRendererE // As said in the previous, the caller guarantees that // $this->resources[$cacheKey][$block] is not set. Since the themes are // already loaded, it can only be a non-existing block. - $this->resources[$cacheKey][$block] = false; + $this->resources[$cacheKey][$blockName] = false; return false; } @@ -117,25 +117,25 @@ class TwigRendererEngine extends AbstractRendererEngine implements TwigRendererE $parentCacheKey = $view->parent->vars[self::CACHE_KEY_VAR]; if (!isset($this->resources[$parentCacheKey])) { - $this->loadResourceForBlock($parentCacheKey, $view->parent, $block); + $this->loadResourceForBlockName($parentCacheKey, $view->parent, $blockName); } // EAGER CACHE POPULATION (see doc comment) - foreach ($this->resources[$parentCacheKey] as $blockName => $resource) { - if (!isset($this->resources[$cacheKey][$blockName])) { - $this->resources[$cacheKey][$blockName] = $resource; + foreach ($this->resources[$parentCacheKey] as $nestedBlockName => $resource) { + if (!isset($this->resources[$cacheKey][$nestedBlockName])) { + $this->resources[$cacheKey][$nestedBlockName] = $resource; } } } // Even though we loaded the themes, it can happen that none of them // contains the searched block - if (!isset($this->resources[$cacheKey][$block])) { + if (!isset($this->resources[$cacheKey][$blockName])) { // Cache that we didn't find anything to speed up further accesses - $this->resources[$cacheKey][$block] = false; + $this->resources[$cacheKey][$blockName] = false; } - return false !== $this->resources[$cacheKey][$block]; + return false !== $this->resources[$cacheKey][$blockName]; } /** diff --git a/src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php b/src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php new file mode 100644 index 0000000000..8eec7e8c11 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Node; + +/** + * @author Bernhard Schussek + */ +class SearchAndRenderBlockNode extends \Twig_Node_Expression_Function +{ + public function compile(\Twig_Compiler $compiler) + { + $compiler->addDebugInfo($this); + $compiler->raw('$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock('); + + preg_match('/_([^_]+)$/', $this->getAttribute('name'), $matches); + + $label = null; + $arguments = iterator_to_array($this->getNode('arguments')); + $blockNameSuffix = $matches[1]; + + if (isset($arguments[0])) { + $compiler->subcompile($arguments[0]); + $compiler->raw(', \'' . $blockNameSuffix . '\''); + + if (isset($arguments[1])) { + $compiler->raw(', '); + + // The "label" function allows one extra argument here, the label + if ('label' === $blockNameSuffix) { + if (isset($arguments[2])) { + $compiler->subcompile($arguments[2]); + $compiler->raw(' + '); + } + + // Add the label to the variable array + $compiler->raw('array(\'label\' => '); + $compiler->subcompile($arguments[1]); + $compiler->raw(')'); + } else { + $compiler->subcompile($arguments[1]); + } + } + } + + $compiler->raw(")"); + } +} diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php index 65f613abe8..043d8c9afd 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php @@ -141,32 +141,36 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest protected function renderEnctype(FormView $view) { - return (string) $this->extension->renderer->renderEnctype($view); + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'enctype'); } protected function renderLabel(FormView $view, $label = null, array $vars = array()) { - return (string) $this->extension->renderer->renderLabel($view, $label, $vars); + if ($label !== null) { + $vars += array('label' => $label); + } + + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars); } protected function renderErrors(FormView $view) { - return (string) $this->extension->renderer->renderErrors($view); + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors'); } protected function renderWidget(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderWidget($view, $vars); + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars); } protected function renderRow(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderRow($view, $vars); + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars); } protected function renderRest(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderRest($view, $vars); + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars); } protected function setTheme(FormView $view, array $themes) diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php index effca091ad..da0d846e63 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php @@ -77,32 +77,36 @@ class FormExtensionTableLayoutTest extends AbstractTableLayoutTest protected function renderEnctype(FormView $view) { - return (string) $this->extension->renderer->renderEnctype($view); + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'enctype'); } protected function renderLabel(FormView $view, $label = null, array $vars = array()) { - return (string) $this->extension->renderer->renderLabel($view, $label, $vars); + if ($label !== null) { + $vars += array('label' => $label); + } + + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars); } protected function renderErrors(FormView $view) { - return (string) $this->extension->renderer->renderErrors($view); + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors'); } protected function renderWidget(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderWidget($view, $vars); + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars); } protected function renderRow(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderRow($view, $vars); + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars); } protected function renderRest(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderRest($view, $vars); + return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars); } protected function setTheme(FormView $view, array $themes) diff --git a/src/Symfony/Bridge/Twig/composer.json b/src/Symfony/Bridge/Twig/composer.json index 5635912a56..c86cb1bd67 100644 --- a/src/Symfony/Bridge/Twig/composer.json +++ b/src/Symfony/Bridge/Twig/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": ">=5.3.3", - "twig/twig": ">=1.8,<2.0-dev" + "twig/twig": ">=1.9.1,<2.0-dev" }, "require-dev": { "symfony/form": "2.1.*", diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/attributes.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/attributes.html.php index 621cf9c871..eb421be817 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/attributes.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/attributes.html.php @@ -1 +1 @@ -block('widget_attributes') ?> +block($form, 'widget_attributes') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/checkbox_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/checkbox_widget.html.php index 2efc537192..d3cf747353 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/checkbox_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/checkbox_widget.html.php @@ -1,5 +1,5 @@ block('widget_attributes') ?> + block($form, 'widget_attributes') ?> value="escape($value) ?>" checked="checked" /> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_options.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_options.html.php index 9beb0ad9c6..211ae73f1c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_options.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_options.html.php @@ -1 +1 @@ -block('choice_widget_options') ?> +block($form, 'choice_widget_options') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget.html.php index 66e23ef6f8..13593a96f1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget.html.php @@ -1,5 +1,5 @@ -block('choice_widget_expanded') ?> +block($form, 'choice_widget_expanded') ?> -block('choice_widget_collapsed') ?> +block($form, 'choice_widget_collapsed') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_collapsed.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_collapsed.html.php index 0bc61c4525..251d4184eb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_collapsed.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_collapsed.html.php @@ -1,13 +1,13 @@ diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_expanded.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_expanded.html.php index b4b9a0f6d3..64ae7dbc95 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_expanded.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_expanded.html.php @@ -1,4 +1,4 @@ -
block('widget_container_attributes') ?>> +
block($form, 'widget_container_attributes') ?>> widget($child) ?> label($child) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_options.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_options.html.php index 39c97f091f..a7a9311d51 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_options.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_options.html.php @@ -3,7 +3,7 @@ $choice): ?> - block('choice_widget_options', array('choices' => $choice)) ?> + block($form, 'choice_widget_options', array('choices' => $choice)) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/container_attributes.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/container_attributes.html.php index 588028ba74..302bbfcd47 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/container_attributes.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/container_attributes.html.php @@ -1 +1 @@ -block('widget_container_attributes') ?> +block($form, 'widget_container_attributes') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/date_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/date_widget.html.php index 7e087bee06..22e51d2735 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/date_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/date_widget.html.php @@ -1,7 +1,7 @@ - block('form_widget_simple'); ?> + block($form, 'form_widget_simple'); ?> -
block('widget_container_attributes') ?>> +
block($form, 'widget_container_attributes') ?>> widget($form['year']), $view['form']->widget($form['month']), diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/datetime_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/datetime_widget.html.php index 3685c08bfc..aef2a32512 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/datetime_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/datetime_widget.html.php @@ -1,7 +1,7 @@ - block('form_widget_simple'); ?> + block($form, 'form_widget_simple'); ?> -
block('widget_container_attributes') ?>> +
block($form, 'widget_container_attributes') ?>> widget($form['date']).' '.$view['form']->widget($form['time']) ?>
diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/email_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/email_widget.html.php index 782de8c421..0b30c5bb7a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/email_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/email_widget.html.php @@ -1 +1 @@ -block('form_widget_simple', array('type' => isset($type) ? $type : 'email')) ?> +block($form, 'form_widget_simple', array('type' => isset($type) ? $type : 'email')) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_enctype.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_enctype.html.php index 1f56ba0e7a..2e107a7b26 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_enctype.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_enctype.html.php @@ -1 +1 @@ -block('form_enctype') ?> +block($form, 'form_enctype') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_errors.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_errors.html.php index 4542230327..ffed7cf43c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_errors.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_errors.html.php @@ -1 +1 @@ -block('form_errors') ?> +block($form, 'form_errors') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_label.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_label.html.php index bd9fd27b4f..0b59dfb301 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_label.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_label.html.php @@ -1 +1 @@ -block('form_label') ?> +block($form, 'form_label') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_rest.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_rest.html.php index 8341168aca..ee1bc31333 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_rest.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_rest.html.php @@ -1 +1 @@ -block('form_rest') ?> +block($form, 'form_rest') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_row.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_row.html.php index 10c495266d..971d8ac5a9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_row.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_row.html.php @@ -1 +1 @@ -block('form_row') ?> +block($form, 'form_row') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_rows.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_rows.html.php index 62b69ab411..d4af23d712 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_rows.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_rows.html.php @@ -1 +1 @@ -block('form_rows') ?> +block($form, 'form_rows') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_widget.html.php index 627555cff6..91e5ef1e1c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_widget.html.php @@ -1 +1 @@ -block('form_widget_simple') ?> +block($form, 'form_widget_simple') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget.html.php index 4695c1cc59..c5af39a5b6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget.html.php @@ -1,5 +1,5 @@ -block('form_widget_compound')?> +block($form, 'form_widget_compound')?> -block('form_widget_simple')?> +block($form, 'form_widget_simple')?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_compound.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_compound.html.php index 34a65bd542..45a821032e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_compound.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_compound.html.php @@ -1,4 +1,4 @@ -
block('widget_container_attributes') ?>> +
block($form, 'widget_container_attributes') ?>> parent && $errors): ?> @@ -6,6 +6,6 @@ - block('form_rows') ?> + block($form, 'form_rows') ?> rest($form) ?>
diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_simple.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_simple.html.php index 203f7a4266..11c22832a7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_simple.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_simple.html.php @@ -1,5 +1,5 @@ value="escape($value) ?>" - block('widget_attributes') ?> + block($form, 'widget_attributes') ?> /> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/hidden_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/hidden_widget.html.php index 120ebd1c5f..68eec540e2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/hidden_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/hidden_widget.html.php @@ -1 +1 @@ -block('form_widget_simple', array('type' => isset($type) ? $type : "hidden")) ?> +block($form, 'form_widget_simple', array('type' => isset($type) ? $type : "hidden")) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/integer_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/integer_widget.html.php index aa34a0f42d..3775a71771 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/integer_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/integer_widget.html.php @@ -1 +1 @@ -block('form_widget_simple', array('type' => isset($type) ? $type : "number")) ?> +block($form, 'form_widget_simple', array('type' => isset($type) ? $type : "number")) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/money_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/money_widget.html.php index f829637b99..644d284915 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/money_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/money_widget.html.php @@ -1 +1 @@ -block('form_widget_simple'), $money_pattern) ?> +block($form, 'form_widget_simple'), $money_pattern) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/number_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/number_widget.html.php index 6fa4aa4220..854952f33f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/number_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/number_widget.html.php @@ -1 +1 @@ -block('form_widget_simple', array('type' => isset($type) ? $type : "text")) ?> +block($form, 'form_widget_simple', array('type' => isset($type) ? $type : "text")) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/password_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/password_widget.html.php index 463b39e55d..845fcb25c3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/password_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/password_widget.html.php @@ -1 +1 @@ -block('form_widget_simple', array('type' => isset($type) ? $type : "password")) ?> +block($form, 'form_widget_simple', array('type' => isset($type) ? $type : "password")) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/percent_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/percent_widget.html.php index 36bc2a11d5..35b9b0457f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/percent_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/percent_widget.html.php @@ -1 +1 @@ -block('form_widget_simple', array('type' => isset($type) ? $type : "text")) ?> % +block($form, 'form_widget_simple', array('type' => isset($type) ? $type : "text")) ?> % diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/radio_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/radio_widget.html.php index 024ca8c278..ddc8c529df 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/radio_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/radio_widget.html.php @@ -1,5 +1,5 @@ block('widget_attributes') ?> + block($form, 'widget_attributes') ?> value="escape($value) ?>" checked="checked" /> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/repeated_row.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/repeated_row.html.php index 62b69ab411..d4af23d712 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/repeated_row.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/repeated_row.html.php @@ -1 +1 @@ -block('form_rows') ?> +block($form, 'form_rows') ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/search_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/search_widget.html.php index 04cd5a571e..d91267fedc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/search_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/search_widget.html.php @@ -1 +1 @@ -block('form_widget_simple', array('type' => isset($type) ? $type : "search")) ?> +block($form, 'form_widget_simple', array('type' => isset($type) ? $type : "search")) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/textarea_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/textarea_widget.html.php index 5973706b3d..c989ce575d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/textarea_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/textarea_widget.html.php @@ -1 +1 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/time_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/time_widget.html.php index 9483561d0f..591bbf797a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/time_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/time_widget.html.php @@ -1,7 +1,7 @@ - block('form_widget_simple'); ?> + block($form, 'form_widget_simple'); ?> -
block('widget_container_attributes') ?>> +
block($form, 'widget_container_attributes') ?>> block('form_widget_simple', array('type' => isset($type) ? $type : "url")) ?> +block($form, 'form_widget_simple', array('type' => isset($type) ? $type : "url")) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/FormTable/form_widget_compound.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/FormTable/form_widget_compound.html.php index 86808af324..a4878e57db 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/FormTable/form_widget_compound.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/FormTable/form_widget_compound.html.php @@ -1,7 +1,7 @@ -block('widget_container_attributes') ?>> +
block($form, 'widget_container_attributes') ?>> parent): ?> errors($form) ?> - block('form_rows') ?> + block($form, 'form_rows') ?> rest($form) ?>
diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php index c7297fc365..b8c6a0256a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php @@ -75,7 +75,7 @@ class FormHelper extends Helper */ public function enctype(FormView $view) { - return $this->renderer->renderEnctype($view); + return $this->renderer->searchAndRenderBlock($view, 'enctype'); } /** @@ -92,40 +92,44 @@ class FormHelper extends Helper * widget(array('separator' => '+++++)) ?> * * @param FormView $view The view for which to render the widget - * @param array $variables Additional variables passed to the template + * @param array $variables Additional variables passed to the template * * @return string The HTML markup */ public function widget(FormView $view, array $variables = array()) { - return $this->renderer->renderWidget($view, $variables); + return $this->renderer->searchAndRenderBlock($view, 'widget', $variables); } /** * Renders the entire form field "row". * * @param FormView $view The view for which to render the row - * @param array $variables Additional variables passed to the template + * @param array $variables Additional variables passed to the template * * @return string The HTML markup */ public function row(FormView $view, array $variables = array()) { - return $this->renderer->renderRow($view, $variables); + return $this->renderer->searchAndRenderBlock($view, 'row', $variables); } /** * Renders the label of the given view. * * @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 + * @param string $label The label + * @param array $variables Additional variables passed to the template * * @return string The HTML markup */ public function label(FormView $view, $label = null, array $variables = array()) { - return $this->renderer->renderLabel($view, $label, $variables); + if ($label !== null) { + $variables += array('label' => $label); + } + + return $this->renderer->searchAndRenderBlock($view, 'label', $variables); } /** @@ -137,49 +141,34 @@ class FormHelper extends Helper */ public function errors(FormView $view) { - return $this->renderer->renderErrors($view); + return $this->renderer->searchAndRenderBlock($view, 'errors'); } /** * Renders views which have not already been rendered. * * @param FormView $view The parent view - * @param array $variables An array of variables + * @param array $variables An array of variables * * @return string The HTML markup */ public function rest(FormView $view, array $variables = array()) { - return $this->renderer->renderRest($view, $variables); - } - - /** - * Alias of {@link block()} - * - * @param string $block The name of the block to render. - * @param array $variables The variable to pass to the template. - * - * @return string The HTML markup - * - * @deprecated Deprecated since version 2.1, to be removed in 2.3. Use - * {@link block()} instead. - */ - public function renderBlock($block, array $variables = array()) - { - return $this->renderer->renderBlock($block, $variables); + return $this->renderer->searchAndRenderBlock($view, 'rest', $variables); } /** * Renders a block of the template. * - * @param string $block The name of the block to render. - * @param array $variables The variable to pass to the template. + * @param FormView $view The view for determining the used themes. + * @param string $blockName The name of the block to render. + * @param array $variables The variable to pass to the template. * * @return string The HTML markup */ - public function block($block, array $variables = array()) + public function block(FormView $view, $blockName, array $variables = array()) { - return $this->renderer->renderBlock($block, $variables); + return $this->renderer->renderBlock($view, $blockName, $variables); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Parent/form_widget_simple.html.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Parent/form_widget_simple.html.php index 6506f9140e..3c6c158a53 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Parent/form_widget_simple.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Parent/form_widget_simple.html.php @@ -1,2 +1,2 @@ -renderBlock('widget_attributes') ?> value="" rel="theme" /> +block($form, 'widget_attributes') ?> value="" rel="theme" /> diff --git a/src/Symfony/Component/Form/AbstractRendererEngine.php b/src/Symfony/Component/Form/AbstractRendererEngine.php index 371f4a0f1f..5d7416d2a8 100644 --- a/src/Symfony/Component/Form/AbstractRendererEngine.php +++ b/src/Symfony/Component/Form/AbstractRendererEngine.php @@ -74,52 +74,52 @@ abstract class AbstractRendererEngine implements FormRendererEngineInterface /** * {@inheritdoc} */ - public function getResourceForBlock(FormView $view, $block) + public function getResourceForBlockName(FormView $view, $blockName) { $cacheKey = $view->vars[self::CACHE_KEY_VAR]; - if (!isset($this->resources[$cacheKey][$block])) { - $this->loadResourceForBlock($cacheKey, $view, $block); + if (!isset($this->resources[$cacheKey][$blockName])) { + $this->loadResourceForBlockName($cacheKey, $view, $blockName); } - return $this->resources[$cacheKey][$block]; + return $this->resources[$cacheKey][$blockName]; } /** * {@inheritdoc} */ - public function getResourceForBlockHierarchy(FormView $view, array $blockHierarchy, $hierarchyLevel) + public function getResourceForBlockNameHierarchy(FormView $view, array $blockNameHierarchy, $hierarchyLevel) { $cacheKey = $view->vars[self::CACHE_KEY_VAR]; - $block = $blockHierarchy[$hierarchyLevel]; + $blockName = $blockNameHierarchy[$hierarchyLevel]; - if (!isset($this->resources[$cacheKey][$block])) { - $this->loadResourceForBlockHierarchy($cacheKey, $view, $blockHierarchy, $hierarchyLevel); + if (!isset($this->resources[$cacheKey][$blockName])) { + $this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $hierarchyLevel); } - return $this->resources[$cacheKey][$block]; + return $this->resources[$cacheKey][$blockName]; } /** * {@inheritdoc} */ - public function getResourceHierarchyLevel(FormView $view, array $blockHierarchy, $hierarchyLevel) + public function getResourceHierarchyLevel(FormView $view, array $blockNameHierarchy, $hierarchyLevel) { $cacheKey = $view->vars[self::CACHE_KEY_VAR]; - $block = $blockHierarchy[$hierarchyLevel]; + $blockName = $blockNameHierarchy[$hierarchyLevel]; - if (!isset($this->resources[$cacheKey][$block])) { - $this->loadResourceForBlockHierarchy($cacheKey, $view, $blockHierarchy, $hierarchyLevel); + if (!isset($this->resources[$cacheKey][$blockName])) { + $this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $hierarchyLevel); } // If $block was previously rendered loaded with loadTemplateForBlock(), the template // is cached but the hierarchy level is not. In this case, we know that the block // exists at this very hierarchy level, so we can just set it. - if (!isset($this->resourceHierarchyLevels[$cacheKey][$block])) { - $this->resourceHierarchyLevels[$cacheKey][$block] = $hierarchyLevel; + if (!isset($this->resourceHierarchyLevels[$cacheKey][$blockName])) { + $this->resourceHierarchyLevels[$cacheKey][$blockName] = $hierarchyLevel; } - return $this->resourceHierarchyLevels[$cacheKey][$block]; + return $this->resourceHierarchyLevels[$cacheKey][$blockName]; } /** @@ -127,79 +127,79 @@ abstract class AbstractRendererEngine implements FormRendererEngineInterface * * @see getResourceForBlock() * - * @param string $cacheKey The cache key of the form view. - * @param FormView $view The form view for finding the applying themes. - * @param string $block The name of the block to load. + * @param string $cacheKey The cache key of the form view. + * @param FormView $view The form view for finding the applying themes. + * @param string $blockName The name of the block to load. * * @return Boolean True if the resource could be loaded, false otherwise. */ - abstract protected function loadResourceForBlock($cacheKey, FormView $view, $block); + abstract protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName); /** * Loads the cache with the resource for a specific level of a block hierarchy. * * @see getResourceForBlockHierarchy() * - * @param string $cacheKey The cache key used for storing the - * resource. - * @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. - * @param integer $hierarchyLevel The level in the block hierarchy that - * should be loaded. + * @param string $cacheKey The cache key used for storing the + * resource. + * @param FormView $view The form view for finding the applying + * themes. + * @param array $blockNameHierarchy The block hierarchy, with the most + * specific block name at the end. + * @param integer $hierarchyLevel The level in the block hierarchy that + * should be loaded. * * @return Boolean True if the resource could be loaded, false otherwise. */ - private function loadResourceForBlockHierarchy($cacheKey, FormView $view, array $blockHierarchy, $hierarchyLevel) + private function loadResourceForBlockNameHierarchy($cacheKey, FormView $view, array $blockNameHierarchy, $hierarchyLevel) { - $block = $blockHierarchy[$hierarchyLevel]; + $blockName = $blockNameHierarchy[$hierarchyLevel]; // Try to find a template for that block - if ($this->loadResourceForBlock($cacheKey, $view, $block)) { + if ($this->loadResourceForBlockName($cacheKey, $view, $blockName)) { // If loadTemplateForBlock() returns true, it was able to populate the // cache. The only missing thing is to set the hierarchy level at which // the template was found. - $this->resourceHierarchyLevels[$cacheKey][$block] = $hierarchyLevel; + $this->resourceHierarchyLevels[$cacheKey][$blockName] = $hierarchyLevel; return true; } if ($hierarchyLevel > 0) { $parentLevel = $hierarchyLevel - 1; - $parentBlock = $blockHierarchy[$parentLevel]; + $parentBlockName = $blockNameHierarchy[$parentLevel]; // The next two if statements contain slightly duplicated code. This is by intention // and tries to avoid execution of unnecessary checks in order to increase performance. - if (isset($this->resources[$cacheKey][$parentBlock])) { + if (isset($this->resources[$cacheKey][$parentBlockName])) { // It may happen that the parent block is already loaded, but its level is not. // In this case, the parent block must have been loaded by loadResourceForBlock(), // which does not check the hierarchy of the block. Subsequently the block must have // been found directly on the parent level. - if (!isset($this->resourceHierarchyLevels[$cacheKey][$parentBlock])) { - $this->resourceHierarchyLevels[$cacheKey][$parentBlock] = $parentLevel; + if (!isset($this->resourceHierarchyLevels[$cacheKey][$parentBlockName])) { + $this->resourceHierarchyLevels[$cacheKey][$parentBlockName] = $parentLevel; } // Cache the shortcuts for further accesses - $this->resources[$cacheKey][$block] = $this->resources[$cacheKey][$parentBlock]; - $this->resourceHierarchyLevels[$cacheKey][$block] = $this->resourceHierarchyLevels[$cacheKey][$parentBlock]; + $this->resources[$cacheKey][$blockName] = $this->resources[$cacheKey][$parentBlockName]; + $this->resourceHierarchyLevels[$cacheKey][$blockName] = $this->resourceHierarchyLevels[$cacheKey][$parentBlockName]; return true; } - if ($this->loadResourceForBlockHierarchy($cacheKey, $view, $blockHierarchy, $parentLevel)) { + if ($this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $parentLevel)) { // Cache the shortcuts for further accesses - $this->resources[$cacheKey][$block] = $this->resources[$cacheKey][$parentBlock]; - $this->resourceHierarchyLevels[$cacheKey][$block] = $this->resourceHierarchyLevels[$cacheKey][$parentBlock]; + $this->resources[$cacheKey][$blockName] = $this->resources[$cacheKey][$parentBlockName]; + $this->resourceHierarchyLevels[$cacheKey][$blockName] = $this->resourceHierarchyLevels[$cacheKey][$parentBlockName]; return true; } } // Cache the result for further accesses - $this->resources[$cacheKey][$block] = false; - $this->resourceHierarchyLevels[$cacheKey][$block] = false; + $this->resources[$cacheKey][$blockName] = false; + $this->resourceHierarchyLevels[$cacheKey][$blockName] = false; return false; } diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 2229f587f1..60d4a64e77 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -175,5 +175,5 @@ CHANGELOG * `toArrayKeys` * `isChoiceGroup` * `isChoiceSelected` - * added method `block` to FormHelper and deprecated `renderBlock` instead + * [BC BREAK] renamed method `renderBlock` in FormHelper to `block` and changed its signature * made FormView properties public and deprecated their accessor methods diff --git a/src/Symfony/Component/Form/Extension/Templating/TemplatingRendererEngine.php b/src/Symfony/Component/Form/Extension/Templating/TemplatingRendererEngine.php index 3ea3765f7b..a6a303ad10 100644 --- a/src/Symfony/Component/Form/Extension/Templating/TemplatingRendererEngine.php +++ b/src/Symfony/Component/Form/Extension/Templating/TemplatingRendererEngine.php @@ -35,7 +35,7 @@ class TemplatingRendererEngine extends AbstractRendererEngine /** * {@inheritdoc} */ - public function renderBlock(FormView $view, $resource, $block, array $variables = array()) + public function renderBlock(FormView $view, $resource, $blockName, array $variables = array()) { return trim($this->engine->render($resource, $variables)); } @@ -48,13 +48,13 @@ class TemplatingRendererEngine extends AbstractRendererEngine * * @see getResourceForBlock() * - * @param string $cacheKey The cache key of the form view. - * @param FormView $view The form view for finding the applying themes. - * @param string $block The name of the block to load. + * @param string $cacheKey The cache key of the form view. + * @param FormView $view The form view for finding the applying themes. + * @param string $blockName The name of the block to load. * * @return Boolean True if the resource could be loaded, false otherwise. */ - protected function loadResourceForBlock($cacheKey, FormView $view, $block) + protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName) { // Recursively try to find the block in the themes assigned to $view, // then of its parent form, then of the parent form of the parent and so on. @@ -64,7 +64,7 @@ class TemplatingRendererEngine extends AbstractRendererEngine // Check each theme whether it contains the searched block if (isset($this->themes[$cacheKey])) { for ($i = count($this->themes[$cacheKey]) - 1; $i >= 0; --$i) { - if ($this->loadResourceFromTheme($cacheKey, $block, $this->themes[$cacheKey][$i])) { + if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->themes[$cacheKey][$i])) { return true; } } @@ -73,7 +73,7 @@ class TemplatingRendererEngine extends AbstractRendererEngine // Check the default themes once we reach the root form without success if (!$view->parent) { for ($i = count($this->defaultThemes) - 1; $i >= 0; --$i) { - if ($this->loadResourceFromTheme($cacheKey, $block, $this->defaultThemes[$i])) { + if ($this->loadResourceFromTheme($cacheKey, $blockName, $this->defaultThemes[$i])) { return true; } } @@ -84,21 +84,21 @@ class TemplatingRendererEngine extends AbstractRendererEngine if ($view->parent) { $parentCacheKey = $view->parent->vars[self::CACHE_KEY_VAR]; - if (!isset($this->resources[$parentCacheKey][$block])) { - $this->loadResourceForBlock($parentCacheKey, $view->parent, $block); + if (!isset($this->resources[$parentCacheKey][$blockName])) { + $this->loadResourceForBlockName($parentCacheKey, $view->parent, $blockName); } // If a template exists in the parent themes, cache that template // for the current theme as well to speed up further accesses - if ($this->resources[$parentCacheKey][$block]) { - $this->resources[$cacheKey][$block] = $this->resources[$parentCacheKey][$block]; + if ($this->resources[$parentCacheKey][$blockName]) { + $this->resources[$cacheKey][$blockName] = $this->resources[$parentCacheKey][$blockName]; return true; } } // Cache that we didn't find anything to speed up further accesses - $this->resources[$cacheKey][$block] = false; + $this->resources[$cacheKey][$blockName] = false; return false; } @@ -106,16 +106,16 @@ class TemplatingRendererEngine extends AbstractRendererEngine /** * Tries to load the resource for a block from a theme. * - * @param string $cacheKey The cache key for storing the resource. - * @param string $block The name of the block to load a resource for. - * @param mixed $theme The theme to load the block from. + * @param string $cacheKey The cache key for storing the resource. + * @param string $blockName The name of the block to load a resource for. + * @param mixed $theme The theme to load the block from. * * @return Boolean True if the resource could be loaded, false otherwise. */ - protected function loadResourceFromTheme($cacheKey, $block, $theme) + protected function loadResourceFromTheme($cacheKey, $blockName, $theme) { - if ($this->engine->exists($templateName = $theme . ':' . $block . '.html.php')) { - $this->resources[$cacheKey][$block] = $templateName; + if ($this->engine->exists($templateName = $theme . ':' . $blockName . '.html.php')) { + $this->resources[$cacheKey][$blockName] = $templateName; return true; } diff --git a/src/Symfony/Component/Form/FormRenderer.php b/src/Symfony/Component/Form/FormRenderer.php index 49d7877c90..4d5538f106 100644 --- a/src/Symfony/Component/Form/FormRenderer.php +++ b/src/Symfony/Component/Form/FormRenderer.php @@ -35,7 +35,7 @@ class FormRenderer implements FormRendererInterface /** * @var array */ - private $blockHierarchyMap = array(); + private $blockNameHierarchyMap = array(); /** * @var array @@ -50,7 +50,7 @@ class FormRenderer implements FormRendererInterface /** * @var array */ - private $stack = array(); + private $variableStack = array(); public function __construct(FormRendererEngineInterface $engine, CsrfProviderInterface $csrfProvider = null) { @@ -74,58 +74,6 @@ class FormRenderer implements FormRendererInterface $this->engine->setTheme($view, $themes); } - /** - * {@inheritdoc} - */ - public function renderEnctype(FormView $view) - { - return $this->renderSection($view, 'enctype'); - } - - /** - * {@inheritdoc} - */ - public function renderRow(FormView $view, array $variables = array()) - { - return $this->renderSection($view, 'row', $variables); - } - - /** - * {@inheritdoc} - */ - public function renderRest(FormView $view, array $variables = array()) - { - return $this->renderSection($view, 'rest', $variables); - } - - /** - * {@inheritdoc} - */ - public function renderWidget(FormView $view, array $variables = array()) - { - return $this->renderSection($view, 'widget', $variables); - } - - /** - * {@inheritdoc} - */ - public function renderErrors(FormView $view) - { - return $this->renderSection($view, 'errors'); - } - - /** - * {@inheritdoc} - */ - public function renderLabel(FormView $view, $label = null, array $variables = array()) - { - if ($label !== null) { - $variables += array('label' => $label); - } - - return $this->renderSection($view, 'label', $variables); - } - /** * {@inheritdoc} */ @@ -141,18 +89,18 @@ class FormRenderer implements FormRendererInterface /** * {@inheritdoc} */ - public function renderBlock($block, array $variables = array()) + public function renderBlock(FormView $view, $blockName, array $variables = array()) { - if (0 == count($this->stack)) { + if (0 == count($this->variableStack)) { throw new FormException('This method should only be called while rendering a form element.'); } - list($view, $scopeVariables) = end($this->stack); + $scopeVariables = end($this->variableStack); - $resource = $this->engine->getResourceForBlock($view, $block); + $resource = $this->engine->getResourceForBlockName($view, $blockName); if (!$resource) { - throw new FormException(sprintf('No block "%s" found while rendering the form.', $block)); + throw new FormException(sprintf('No block "%s" found while rendering the form.', $blockName)); } // Merge the passed with the existing attributes @@ -169,38 +117,30 @@ class FormRenderer implements FormRendererInterface // cannot be overwritten $variables = array_replace($scopeVariables, $variables); - return $this->engine->renderBlock($view, $resource, $block, $variables); + $this->variableStack[] = $variables; + + // Do the rendering + $html = $this->engine->renderBlock($view, $resource, $blockName, $variables); + + // Clear the stack + array_pop($this->variableStack); + + return $html; } /** * {@inheritdoc} */ - public function humanize($text) + public function searchAndRenderBlock(FormView $view, $blockNameSuffix, array $variables = array()) { - return ucfirst(trim(strtolower(preg_replace('/[_\s]+/', ' ', $text)))); - } - - /** - * Renders the given section of a form view. - * - * @param FormView $view The form view. - * @param string $section The name of the section to render. - * @param array $variables The variables to pass to the template. - * - * @return string The HTML markup. - * - * @throws Exception\FormException If no fitting template was found. - */ - protected function renderSection(FormView $view, $section, array $variables = array()) - { - $renderOnlyOnce = in_array($section, array('row', 'widget')); + $renderOnlyOnce = in_array($blockNameSuffix, array('row', 'widget')); if ($renderOnlyOnce && $view->isRendered()) { return ''; } // The cache key for storing the variables and types - $mapKey = $uniqueBlockName = $view->vars['full_block_name'] . '_' . $section; + $mapKey = $uniqueBlockName = $view->vars['full_block_name'] . '_' . $blockNameSuffix; // In templates, we have to deal with two kinds of block hierarchies: // @@ -229,16 +169,16 @@ class FormRenderer implements FormRendererInterface // widget() function again to render the block for the parent type. // // The second kind is implemented in the following blocks. - if (!isset($this->blockHierarchyMap[$mapKey])) { + if (!isset($this->blockNameHierarchyMap[$mapKey])) { // INITIAL CALL // Calculate the hierarchy of template blocks and start on // the bottom level of the hierarchy (= "__
" block) - $blockHierarchy = array(); + $blockNameHierarchy = array(); foreach ($view->vars['types'] as $type) { - $blockHierarchy[] = $type . '_' . $section; + $blockNameHierarchy[] = $type . '_' . $blockNameSuffix; } - $blockHierarchy[] = $uniqueBlockName; - $hierarchyLevel = count($blockHierarchy) - 1; + $blockNameHierarchy[] = $uniqueBlockName; + $hierarchyLevel = count($blockNameHierarchy) - 1; // The default variable scope contains all view variables, merged with // the variables passed explicitly to the helper @@ -247,7 +187,7 @@ class FormRenderer implements FormRendererInterface // RECURSIVE CALL // If a block recursively calls renderSection() again, resume rendering // using the parent type in the hierarchy. - $blockHierarchy = $this->blockHierarchyMap[$mapKey]; + $blockNameHierarchy = $this->blockNameHierarchyMap[$mapKey]; $hierarchyLevel = $this->hierarchyLevelMap[$mapKey] - 1; // Reuse the current scope and merge it with the explicitly passed variables @@ -255,22 +195,22 @@ class FormRenderer implements FormRendererInterface } // Load the resource where this block can be found - $resource = $this->engine->getResourceForBlockHierarchy($view, $blockHierarchy, $hierarchyLevel); + $resource = $this->engine->getResourceForBlockNameHierarchy($view, $blockNameHierarchy, $hierarchyLevel); // Update the current hierarchy level to the one at which the resource was // found. For example, if looking for "choice_widget", but only a resource // is found for its parent "form_widget", then the level is updated here // to the parent level. - $hierarchyLevel = $this->engine->getResourceHierarchyLevel($view, $blockHierarchy, $hierarchyLevel); + $hierarchyLevel = $this->engine->getResourceHierarchyLevel($view, $blockNameHierarchy, $hierarchyLevel); // The actually existing block name in $resource - $block = $blockHierarchy[$hierarchyLevel]; + $blockName = $blockNameHierarchy[$hierarchyLevel]; // Escape if no resource exists for this block if (!$resource) { throw new FormException(sprintf( 'Unable to render the form as none of the following blocks exist: "%s".', - implode('", "', array_reverse($blockHierarchy)) + implode('", "', array_reverse($blockNameHierarchy)) )); } @@ -295,7 +235,7 @@ class FormRenderer implements FormRendererInterface // We need to store these values in maps (associative arrays) because within a // call to widget() another call to widget() can be made, but for a different view // object. These nested calls should not override each other. - $this->blockHierarchyMap[$mapKey] = $blockHierarchy; + $this->blockNameHierarchyMap[$mapKey] = $blockNameHierarchy; $this->hierarchyLevelMap[$mapKey] = $hierarchyLevel; $this->variableMap[$mapKey] = $variables; @@ -305,16 +245,16 @@ class FormRenderer implements FormRendererInterface // // A stack is sufficient for this purpose, because renderBlock() always accesses // the immediate next outer scope, which is always stored at the end of the stack. - $this->stack[] = array($view, $variables); + $this->variableStack[] = $variables; // Do the rendering - $html = $this->engine->renderBlock($view, $resource, $block, $variables); + $html = $this->engine->renderBlock($view, $resource, $blockName, $variables); // Clear the stack - array_pop($this->stack); + array_pop($this->variableStack); // Clear the maps - unset($this->blockHierarchyMap[$mapKey]); + unset($this->blockNameHierarchyMap[$mapKey]); unset($this->hierarchyLevelMap[$mapKey]); unset($this->variableMap[$mapKey]); @@ -324,4 +264,12 @@ class FormRenderer implements FormRendererInterface return $html; } + + /** + * {@inheritdoc} + */ + public function humanize($text) + { + return ucfirst(trim(strtolower(preg_replace('/[_\s]+/', ' ', $text)))); + } } diff --git a/src/Symfony/Component/Form/FormRendererEngineInterface.php b/src/Symfony/Component/Form/FormRendererEngineInterface.php index a91704afa6..e06824ec95 100644 --- a/src/Symfony/Component/Form/FormRendererEngineInterface.php +++ b/src/Symfony/Component/Form/FormRendererEngineInterface.php @@ -22,8 +22,8 @@ interface FormRendererEngineInterface * Sets the theme(s) to be used for rendering a view and its children. * * @param FormView $view The view to assign the theme(s) to. - * @param mixed $themes The theme(s). The type of these themes - * is open to the implementation. + * @param mixed $themes The theme(s). The type of these themes + * is open to the implementation. */ public function setTheme(FormView $view, $themes); @@ -36,15 +36,15 @@ interface FormRendererEngineInterface * The type of the resource is decided by the implementation. The resource * is later passed to {@link renderBlock()} by the rendering algorithm. * - * @param FormView $view The view for determining the used themes. - * First the themes attached directly to the - * view with {@link setTheme()} are considered, - * then the ones of its parent etc. - * @param string $block The name of the block to render. + * @param FormView $view The view for determining the used themes. + * First the themes attached directly to the + * view with {@link setTheme()} are considered, + * then the ones of its parent etc. + * @param string $blockName The name of the block to render. * * @return mixed The renderer resource or false, if none was found. */ - public function getResourceForBlock(FormView $view, $block); + public function getResourceForBlockName(FormView $view, $blockName); /** * Returns the resource for a block hierarchy. @@ -70,21 +70,23 @@ interface FormRendererEngineInterface * The type of the resource is decided by the implementation. The resource * is later passed to {@link renderBlock()} by the rendering algorithm. * - * @param FormView $view The view for determining the used - * themes. First the themes attached - * directly to the view with - * {@link setTheme()} are considered, - * then the ones of its parent etc. - * @param array $blockHierarchy The block name hierarchy, with - * the root block at the beginning. - * @param integer $hierarchyLevel The level in the hierarchy at - * which to start looking. Level 0 - * indicates the root block, i.e. - * the first element of $blockHierarchy. + * @param FormView $view The view for determining the + * used themes. First the themes + * attached directly to the view + * with {@link setTheme()} are + * considered, then the ones of + * its parent etc. + * @param array $blockNameHierarchy The block name hierarchy, with + * the root block at the beginning. + * @param integer $hierarchyLevel The level in the hierarchy at + * which to start looking. Level 0 + * indicates the root block, i.e. + * the first element of + * $blockNameHierarchy. * * @return mixed The renderer resource or false, if none was found. */ - public function getResourceForBlockHierarchy(FormView $view, array $blockHierarchy, $hierarchyLevel); + public function getResourceForBlockNameHierarchy(FormView $view, array $blockNameHierarchy, $hierarchyLevel); /** * Returns the hierarchy level at which a resource can be found. @@ -112,21 +114,23 @@ interface FormRendererEngineInterface * The type of the resource is decided by the implementation. The resource * is later passed to {@link renderBlock()} by the rendering algorithm. * - * @param FormView $view The view for determining the used - * themes. First the themes attached - * directly to the view with - * {@link setTheme()} are considered, - * then the ones of its parent etc. - * @param array $blockHierarchy The block name hierarchy, with - * the root block at the beginning. - * @param integer $hierarchyLevel The level in the hierarchy at - * which to start looking. Level 0 - * indicates the root block, i.e. - * the first element of $blockHierarchy. + * @param FormView $view The view for determining the + * used themes. First the themes + * attached directly to the view + * with {@link setTheme()} are + * considered, then the ones of + * its parent etc. + * @param array $blockNameHierarchy The block name hierarchy, with + * the root block at the beginning. + * @param integer $hierarchyLevel The level in the hierarchy at + * which to start looking. Level 0 + * indicates the root block, i.e. + * the first element of + * $blockNameHierarchy. * * @return integer|Boolean The hierarchy level or false, if no resource was found. */ - public function getResourceHierarchyLevel(FormView $view, array $blockHierarchy, $hierarchyLevel); + public function getResourceHierarchyLevel(FormView $view, array $blockNameHierarchy, $hierarchyLevel); /** * Renders a block in the given renderer resource. @@ -135,12 +139,12 @@ interface FormRendererEngineInterface * or {@link getResourceForBlockHierarchy()}. The type of the resource is * decided by the implementation. * - * @param FormView $view The view to render. + * @param FormView $view The view to render. * @param mixed $resource The renderer resource. - * @param string $block The name of the block to render. + * @param string $blockName The name of the block to render. * @param array $variables The variables to pass to the template. * * @return string The HTML markup. */ - public function renderBlock(FormView $view, $resource, $block, array $variables = array()); + public function renderBlock(FormView $view, $resource, $blockName, array $variables = array()); } diff --git a/src/Symfony/Component/Form/FormRendererInterface.php b/src/Symfony/Component/Form/FormRendererInterface.php index 56936a6dd2..21e1a16e01 100644 --- a/src/Symfony/Component/Form/FormRendererInterface.php +++ b/src/Symfony/Component/Form/FormRendererInterface.php @@ -36,90 +36,34 @@ interface FormRendererInterface */ public function setTheme(FormView $view, $themes); - /** - * Renders the HTML enctype in the form tag, if necessary. - * - * Example usage templates: - * - *
renderEnctype($form) ?>> - * - * @param FormView $view The view for which to render the encoding type - * - * @return string The HTML markup - */ - public function renderEnctype(FormView $view); - - /** - * Renders the entire row for a form field. - * - * A row typically contains the label, errors and widget of a field. - * - * @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 renderRow(FormView $view, array $variables = array()); - - /** - * Renders views which have not already been rendered. - * - * @param FormView $view The parent view - * @param array $variables An array of variables - * - * @return string The HTML markup - */ - public function renderRest(FormView $view, array $variables = array()); - - /** - * Renders the HTML for a given view. - * - * Example usage: - * - * renderWidget($form) ?> - * - * You can pass options during the call: - * - * renderWidget($form, array('attr' => array('class' => 'foo'))) ?> - * - * renderWidget($form, array('separator' => '+++++)) ?> - * - * @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 renderWidget(FormView $view, array $variables = array()); - - /** - * Renders the errors of the given view. - * - * @param FormView $view The view to render the errors for - * - * @return string The HTML markup - */ - public function renderErrors(FormView $view); - - /** - * Renders the label of the given view. - * - * @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 renderLabel(FormView $view, $label = null, array $variables = array()); - /** * Renders a named block of the form theme. * - * @param string $block The name of the block. - * @param array $variables The variables to pass to the template. + * @param FormView $view The view for which to render the block. + * @param string $blockName The name of the block. + * @param array $variables The variables to pass to the template. * * @return string The HTML markup */ - public function renderBlock($block, array $variables = array()); + public function renderBlock(FormView $view, $blockName, array $variables = array()); + + /** + * Searches and renders a block for a given name suffix. + * + * The block is searched by combining the block names stored in the + * form view with the given suffix. If a block name is found, that + * block is rendered. + * + * If this method is called recursively, the block search is continued + * where a block was found before. + * + * @param FormView $view The view for which to render the block. + * @param string $blockNameSuffix The suffix of the block name. + * @param array $variables The variables to pass to the template. + * + * @return string The HTML markup + */ + public function searchAndRenderBlock(FormView $view, $blockNameSuffix, array $variables = array()); /** * Renders a CSRF token.