diff --git a/src/Symfony/Bridge/Twig/Extension/ContainerAwareRuntimeLoader.php b/src/Symfony/Bridge/Twig/Extension/ContainerAwareRuntimeLoader.php new file mode 100644 index 0000000000000..4885f93444a93 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Extension/ContainerAwareRuntimeLoader.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Extension; + +use Symfony\Component\DependencyInjection\ContainerInterface; + +/** + * Loads Twig extension runtimes. + * + * @author Fabien Potencier + */ +class ContainerAwareRuntimeLoader implements \Twig_RuntimeLoaderInterface +{ + private $container; + + public function __construct(ContainerInterface $container) + { + $this->container = $container; + } + + /** + * {@inheritdoc} + */ + public function load($name) + { + $id = 'twig.extension.runtime.'.$name; + + if ($this->container->has($id)) { + return $this->container->get($id); + } + } +} diff --git a/src/Symfony/Bridge/Twig/Extension/FormExtension.php b/src/Symfony/Bridge/Twig/Extension/FormExtension.php index 395884aab5f8a..378ba01171be3 100644 --- a/src/Symfony/Bridge/Twig/Extension/FormExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/FormExtension.php @@ -12,7 +12,6 @@ namespace Symfony\Bridge\Twig\Extension; use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser; -use Symfony\Bridge\Twig\Form\TwigRendererInterface; use Symfony\Component\Form\ChoiceList\View\ChoiceView; /** @@ -21,29 +20,8 @@ * @author Fabien Potencier * @author Bernhard Schussek */ -class FormExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface +class FormExtension extends \Twig_Extension { - /** - * This property is public so that it can be accessed directly from compiled - * templates without having to call a getter, which slightly decreases performance. - * - * @var TwigRendererInterface - */ - public $renderer; - - public function __construct(TwigRendererInterface $renderer) - { - $this->renderer = $renderer; - } - - /** - * {@inheritdoc} - */ - public function initRuntime(\Twig_Environment $environment) - { - $this->renderer->setEnvironment($environment); - } - /** * {@inheritdoc} */ @@ -93,58 +71,6 @@ public function getTests() ); } - /** - * {@inheritdoc} - */ - public function renderCsrfToken($tokenId) - { - return $this->renderer->renderCsrfToken($tokenId); - } - - /** - * Makes a technical name human readable. - * - * @param string $text The text to humanize - * - * @return string The humanized text - */ - public function humanize($text) - { - return $this->renderer->humanize($text); - } - - /** - * Returns whether a choice is selected for a given form value. - * - * Unfortunately Twig does not support an efficient way to execute the - * "is_selected" closure passed to the template by ChoiceType. It is faster - * to implement the logic here (around 65ms for a specific form). - * - * Directly implementing the logic here is also faster than doing so in - * ChoiceView (around 30ms). - * - * The worst option tested so far is to implement the logic in ChoiceView - * and access the ChoiceView method directly in the template. Doing so is - * around 220ms slower than doing the method call here in the filter. Twig - * seems to be much more efficient at executing filters than at executing - * methods of an object. - * - * @param ChoiceView $choice The choice to check - * @param string|array $selectedValue The selected value to compare - * - * @return bool Whether the choice is selected - * - * @see ChoiceView::isSelected() - */ - public function isSelectedChoice(ChoiceView $choice, $selectedValue) - { - if (is_array($selectedValue)) { - return in_array($choice->value, $selectedValue, true); - } - - return $choice->value === $selectedValue; - } - /** * {@inheritdoc} */ diff --git a/src/Symfony/Bridge/Twig/Form/TwigRenderer.php b/src/Symfony/Bridge/Twig/Form/TwigRenderer.php index 2f526100f58c3..10987b182a4da 100644 --- a/src/Symfony/Bridge/Twig/Form/TwigRenderer.php +++ b/src/Symfony/Bridge/Twig/Form/TwigRenderer.php @@ -12,30 +12,53 @@ namespace Symfony\Bridge\Twig\Form; use Symfony\Component\Form\FormRenderer; -use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; +use Symfony\Component\Form\ChoiceList\View\ChoiceView; /** * @author Bernhard Schussek + * + * @internal */ class TwigRenderer extends FormRenderer implements TwigRendererInterface { /** - * @var TwigRendererEngineInterface + * {@inheritdoc} + * + * @deprecated Deprecated in 2.8, to be removed in 3.0. */ - private $engine; - - public function __construct(TwigRendererEngineInterface $engine, CsrfTokenManagerInterface $csrfTokenManager = null) + public function setEnvironment(\Twig_Environment $environment) { - parent::__construct($engine, $csrfTokenManager); - - $this->engine = $engine; } /** - * {@inheritdoc} + * Returns whether a choice is selected for a given form value. + * + * Unfortunately Twig does not support an efficient way to execute the + * "is_selected" closure passed to the template by ChoiceType. It is faster + * to implement the logic here (around 65ms for a specific form). + * + * Directly implementing the logic here is also faster than doing so in + * ChoiceView (around 30ms). + * + * The worst option tested so far is to implement the logic in ChoiceView + * and access the ChoiceView method directly in the template. Doing so is + * around 220ms slower than doing the method call here in the filter. Twig + * seems to be much more efficient at executing filters than at executing + * methods of an object. + * + * @param ChoiceView $choice The choice to check. + * @param string|array $selectedValue The selected value to compare. + * + * @return bool Whether the choice is selected. + * + * @see ChoiceView::isSelected() */ - public function setEnvironment(\Twig_Environment $environment) + public function isSelectedChoice(ChoiceView $choice, $selectedValue) { - $this->engine->setEnvironment($environment); + if (is_array($selectedValue)) { + return in_array($choice->value, $selectedValue, true); + } + + return $choice->value === $selectedValue; } } diff --git a/src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php b/src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php index 5248d5fd370ea..d97f722ce86e7 100644 --- a/src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php +++ b/src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php @@ -16,6 +16,8 @@ /** * @author Bernhard Schussek + * + * @internal */ class TwigRendererEngine extends AbstractRendererEngine implements TwigRendererEngineInterface { @@ -29,12 +31,20 @@ class TwigRendererEngine extends AbstractRendererEngine implements TwigRendererE */ private $template; + public function __construct(array $defaultThemes = array(), \Twig_Environment $environment) + { + parent::__construct($defaultThemes); + + $this->environment = $environment; + } + /** * {@inheritdoc} + * + * @deprecated Deprecated in 2.8, to be removed in 3.0. */ public function setEnvironment(\Twig_Environment $environment) { - $this->environment = $environment; } /** diff --git a/src/Symfony/Bridge/Twig/Form/TwigRendererEngineInterface.php b/src/Symfony/Bridge/Twig/Form/TwigRendererEngineInterface.php index ef764a248f339..b65da2fc6a1f3 100644 --- a/src/Symfony/Bridge/Twig/Form/TwigRendererEngineInterface.php +++ b/src/Symfony/Bridge/Twig/Form/TwigRendererEngineInterface.php @@ -15,6 +15,10 @@ /** * @author Bernhard Schussek + * + * @deprecated Deprecated in 2.8, to be removed in 3.0. + * + * @internal */ interface TwigRendererEngineInterface extends FormRendererEngineInterface { diff --git a/src/Symfony/Bridge/Twig/Form/TwigRendererInterface.php b/src/Symfony/Bridge/Twig/Form/TwigRendererInterface.php index 4682f520008ac..53adcc3d089bc 100644 --- a/src/Symfony/Bridge/Twig/Form/TwigRendererInterface.php +++ b/src/Symfony/Bridge/Twig/Form/TwigRendererInterface.php @@ -15,6 +15,10 @@ /** * @author Bernhard Schussek + * + * @deprecated Deprecated in 2.8, to be removed in 3.0. + * + * @internal */ interface TwigRendererInterface extends FormRendererInterface { diff --git a/src/Symfony/Bridge/Twig/Node/FormThemeNode.php b/src/Symfony/Bridge/Twig/Node/FormThemeNode.php index 91793771393d8..4373d99f55893 100644 --- a/src/Symfony/Bridge/Twig/Node/FormThemeNode.php +++ b/src/Symfony/Bridge/Twig/Node/FormThemeNode.php @@ -30,7 +30,7 @@ public function compile(\Twig_Compiler $compiler) { $compiler ->addDebugInfo($this) - ->write('$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->setTheme(') + ->write('$this->env->getRuntime(\'Symfony\Bridge\Twig\Extension\FormExtension\')->setTheme(') ->subcompile($this->getNode('form')) ->raw(', ') ->subcompile($this->getNode('resources')) diff --git a/src/Symfony/Bridge/Twig/Node/RenderBlockNode.php b/src/Symfony/Bridge/Twig/Node/RenderBlockNode.php index 33865e2f1cb78..0630cb9aa9e48 100644 --- a/src/Symfony/Bridge/Twig/Node/RenderBlockNode.php +++ b/src/Symfony/Bridge/Twig/Node/RenderBlockNode.php @@ -25,7 +25,7 @@ public function compile(\Twig_Compiler $compiler) { $compiler->addDebugInfo($this); $arguments = iterator_to_array($this->getNode('arguments')); - $compiler->write('$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->renderBlock('); + $compiler->write('$this->env->getRuntime(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderBlock('); if (isset($arguments[0])) { $compiler->subcompile($arguments[0]); diff --git a/src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php b/src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php index 8c42bcd3c2650..8fb140be1744b 100644 --- a/src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php +++ b/src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php @@ -19,7 +19,7 @@ class SearchAndRenderBlockNode extends \Twig_Node_Expression_Function public function compile(\Twig_Compiler $compiler) { $compiler->addDebugInfo($this); - $compiler->raw('$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock('); + $compiler->raw('$this->env->getRuntime(\'Symfony\Bridge\Twig\Extension\FormExtension\')->searchAndRenderBlock('); preg_match('/_([^_]+)$/', $this->getAttribute('name'), $matches); diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3HorizontalLayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3HorizontalLayoutTest.php index 833829fd97178..3c4f6ea864788 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3HorizontalLayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3HorizontalLayoutTest.php @@ -22,37 +22,37 @@ class FormExtensionBootstrap3HorizontalLayoutTest extends AbstractBootstrap3HorizontalLayoutTest { - /** - * @var FormExtension - */ - protected $extension; - protected $testableFeatures = array( 'choice_attr', ); + private $environment; + protected function setUp() { parent::setUp(); - $rendererEngine = new TwigRendererEngine(array( - 'bootstrap_3_horizontal_layout.html.twig', - 'custom_widgets.html.twig', - )); - $renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')); - - $this->extension = new FormExtension($renderer); - $loader = new StubFilesystemLoader(array( __DIR__.'/../../Resources/views/Form', __DIR__.'/Fixtures/templates/form', )); - $environment = new \Twig_Environment($loader, array('strict_variables' => true)); - $environment->addExtension(new TranslationExtension(new StubTranslator())); - $environment->addExtension($this->extension); + $this->environment = new \Twig_Environment($loader, array('strict_variables' => true)); + $this->environment->addExtension(new TranslationExtension(new StubTranslator())); + $this->environment->addExtension(new FormExtension()); + + $rendererEngine = new TwigRendererEngine(array( + 'bootstrap_3_horizontal_layout.html.twig', + 'custom_widgets.html.twig', + ), $this->environment); + $renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')); - $this->extension->initRuntime($environment); + $loader = $this->getMock('Twig_RuntimeLoaderInterface'); + $loader->expects($this->any())->method('load')->will($this->returnValueMap(array( + array('form', $renderer), + array('translator', null), + ))); + $this->environment->registerRuntimeLoader($loader); } protected function tearDown() @@ -64,7 +64,7 @@ protected function tearDown() protected function renderForm(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form', $vars); } protected function renderLabel(FormView $view, $label = null, array $vars = array()) @@ -73,41 +73,41 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra $vars += array('label' => $label); } - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'label', $vars); } protected function renderErrors(FormView $view) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors'); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'errors'); } protected function renderWidget(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'widget', $vars); } protected function renderRow(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'row', $vars); } protected function renderRest(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'rest', $vars); } protected function renderStart(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_start', $vars); } protected function renderEnd(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_end', $vars); } protected function setTheme(FormView $view, array $themes) { - $this->extension->renderer->setTheme($view, $themes); + $this->environment->getRuntime('form')->setTheme($view, $themes); } } diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php index 45cddd4050389..459c9297e4657 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php @@ -22,40 +22,38 @@ class FormExtensionBootstrap3LayoutTest extends AbstractBootstrap3LayoutTest { - /** - * @var FormExtension - */ - protected $extension; + protected $testableFeatures = array( + 'choice_attr', + ); + + private $environment; protected function setUp() { parent::setUp(); - $rendererEngine = new TwigRendererEngine(array( - 'bootstrap_3_layout.html.twig', - 'custom_widgets.html.twig', - )); - $renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')); - - $this->extension = new FormExtension($renderer); - $loader = new StubFilesystemLoader(array( __DIR__.'/../../Resources/views/Form', __DIR__.'/Fixtures/templates/form', )); - $environment = new \Twig_Environment($loader, array('strict_variables' => true)); - $environment->addExtension(new TranslationExtension(new StubTranslator())); - $environment->addExtension($this->extension); + $this->environment = new \Twig_Environment($loader, array('strict_variables' => true)); + $this->environment->addExtension(new TranslationExtension(new StubTranslator())); + $this->environment->addExtension(new FormExtension()); - $this->extension->initRuntime($environment); - } + $rendererEngine = new TwigRendererEngine(array( + 'bootstrap_3_layout.html.twig', + 'custom_widgets.html.twig', + ), $this->environment); + $renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')); - protected function tearDown() - { - parent::tearDown(); + $loader = $this->getMock('Twig_RuntimeLoaderInterface'); + $loader->expects($this->any())->method('load')->will($this->returnValueMap(array( + array('form', $renderer), + array('translator', null), + ))); - $this->extension = null; + $this->environment->registerRuntimeLoader($loader); } public function testStartTagHasNoActionAttributeWhenActionIsEmpty() @@ -84,7 +82,7 @@ public function testStartTagHasActionAttributeWhenActionIsZero() protected function renderForm(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form', $vars); } protected function renderLabel(FormView $view, $label = null, array $vars = array()) @@ -93,41 +91,41 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra $vars += array('label' => $label); } - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'label', $vars); } protected function renderErrors(FormView $view) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors'); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'errors'); } protected function renderWidget(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'widget', $vars); } protected function renderRow(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'row', $vars); } protected function renderRest(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'rest', $vars); } protected function renderStart(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_start', $vars); } protected function renderEnd(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_end', $vars); } protected function setTheme(FormView $view, array $themes) { - $this->extension->renderer->setTheme($view, $themes); + $this->environment->getRuntime('form')->setTheme($view, $themes); } } diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php index 1f5161897af81..f58462b2202c4 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php @@ -23,43 +23,41 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest { - /** - * @var FormExtension - */ - protected $extension; + protected $testableFeatures = array( + 'choice_attr', + ); + + private $environment; protected function setUp() { parent::setUp(); - $rendererEngine = new TwigRendererEngine(array( - 'form_div_layout.html.twig', - 'custom_widgets.html.twig', - )); - $renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')); - - $this->extension = new FormExtension($renderer); - $loader = new StubFilesystemLoader(array( __DIR__.'/../../Resources/views/Form', __DIR__.'/Fixtures/templates/form', )); - $environment = new \Twig_Environment($loader, array('strict_variables' => true)); - $environment->addExtension(new TranslationExtension(new StubTranslator())); - $environment->addGlobal('global', ''); + $this->environment = new \Twig_Environment($loader, array('strict_variables' => true)); + $this->environment->addExtension(new TranslationExtension(new StubTranslator())); + $this->environment->addGlobal('global', ''); // the value can be any template that exists - $environment->addGlobal('dynamic_template_name', 'child_label'); - $environment->addExtension($this->extension); + $this->environment->addGlobal('dynamic_template_name', 'child_label'); + $this->environment->addExtension(new FormExtension($renderer)); - $this->extension->initRuntime($environment); - } + $rendererEngine = new TwigRendererEngine(array( + 'form_div_layout.html.twig', + 'custom_widgets.html.twig', + ), $this->environment); + $renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')); - protected function tearDown() - { - parent::tearDown(); + $loader = $this->getMock('Twig_RuntimeLoaderInterface'); + $loader->expects($this->any())->method('load')->will($this->returnValueMap(array( + array('form', $renderer), + array('translator', null), + ))); - $this->extension = null; + $this->environment->registerRuntimeLoader($loader); } public function testThemeBlockInheritanceUsingUse() @@ -99,7 +97,7 @@ public function testThemeBlockInheritanceUsingDynamicExtend() ->createView() ; - $renderer = $this->extension->renderer; + $renderer = $this->environment->getRuntime('form'); $renderer->setTheme($view, array('page_dynamic_extends.html.twig')); $renderer->searchAndRenderBlock($view, 'row'); } @@ -127,7 +125,7 @@ public function testIsChoiceSelected($expected, $choice, $value) { $choice = new ChoiceView($choice, $choice, $choice.' label'); - $this->assertSame($expected, $this->extension->isSelectedChoice($choice, $value)); + $this->assertSame($expected, $this->environment->getRuntime('form')->isSelectedChoice($choice, $value)); } public function testStartTagHasNoActionAttributeWhenActionIsEmpty() @@ -156,7 +154,7 @@ public function testStartTagHasActionAttributeWhenActionIsZero() protected function renderForm(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form', $vars); } protected function renderLabel(FormView $view, $label = null, array $vars = array()) @@ -165,42 +163,42 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra $vars += array('label' => $label); } - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'label', $vars); } protected function renderErrors(FormView $view) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors'); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'errors'); } protected function renderWidget(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'widget', $vars); } protected function renderRow(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'row', $vars); } protected function renderRest(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'rest', $vars); } protected function renderStart(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_start', $vars); } protected function renderEnd(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_end', $vars); } protected function setTheme(FormView $view, array $themes) { - $this->extension->renderer->setTheme($view, $themes); + $this->environment->getRuntime('form')->setTheme($view, $themes); } public static function themeBlockInheritanceProvider() diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php index 38a3ba3436aa1..d59008c70bdfc 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php @@ -22,41 +22,39 @@ class FormExtensionTableLayoutTest extends AbstractTableLayoutTest { - /** - * @var FormExtension - */ - protected $extension; + protected $testableFeatures = array( + 'choice_attr', + ); + + private $environment; protected function setUp() { parent::setUp(); - $rendererEngine = new TwigRendererEngine(array( - 'form_table_layout.html.twig', - 'custom_widgets.html.twig', - )); - $renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')); - - $this->extension = new FormExtension($renderer); - $loader = new StubFilesystemLoader(array( __DIR__.'/../../Resources/views/Form', __DIR__.'/Fixtures/templates/form', )); - $environment = new \Twig_Environment($loader, array('strict_variables' => true)); - $environment->addExtension(new TranslationExtension(new StubTranslator())); - $environment->addGlobal('global', ''); - $environment->addExtension($this->extension); + $this->environment = new \Twig_Environment($loader, array('strict_variables' => true)); + $this->environment->addExtension(new TranslationExtension(new StubTranslator())); + $this->environment->addGlobal('global', ''); + $this->environment->addExtension(new FormExtension($renderer)); - $this->extension->initRuntime($environment); - } + $rendererEngine = new TwigRendererEngine(array( + 'form_table_layout.html.twig', + 'custom_widgets.html.twig', + ), $this->environment); + $renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')); - protected function tearDown() - { - parent::tearDown(); + $loader = $this->getMock('Twig_RuntimeLoaderInterface'); + $loader->expects($this->any())->method('load')->will($this->returnValueMap(array( + array('form', $renderer), + array('translator', null), + ))); - $this->extension = null; + $this->environment->registerRuntimeLoader($loader); } public function testStartTagHasNoActionAttributeWhenActionIsEmpty() @@ -85,7 +83,7 @@ public function testStartTagHasActionAttributeWhenActionIsZero() protected function renderForm(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form', $vars); } protected function renderLabel(FormView $view, $label = null, array $vars = array()) @@ -94,41 +92,41 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra $vars += array('label' => $label); } - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'label', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'label', $vars); } protected function renderErrors(FormView $view) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'errors'); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'errors'); } protected function renderWidget(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'widget', $vars); } protected function renderRow(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'row', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'row', $vars); } protected function renderRest(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars); + return (string) $this->environment->getRuntime('form')->searchAndRenderBlock($view, 'rest', $vars); } protected function renderStart(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_start', $vars); } protected function renderEnd(FormView $view, array $vars = array()) { - return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars); + return (string) $this->environment->getRuntime('form')->renderBlock($view, 'form_end', $vars); } protected function setTheme(FormView $view, array $themes) { - $this->extension->renderer->setTheme($view, $themes); + $this->environment->getRuntime('form')->setTheme($view, $themes); } } diff --git a/src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php b/src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php index 52d79eb6129f8..746e6838a63dc 100644 --- a/src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php @@ -45,7 +45,7 @@ public function testCompile() $this->assertEquals( sprintf( - '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));', + '$this->env->getRuntime(\'Symfony\Bridge\Twig\Extension\FormExtension\')->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));', $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) @@ -57,7 +57,7 @@ public function testCompile() $this->assertEquals( sprintf( - '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->setTheme(%s, "tpl1");', + '$this->env->getRuntime(\'Symfony\Bridge\Twig\Extension\FormExtension\')->setTheme(%s, "tpl1");', $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) diff --git a/src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php b/src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php index 599f2c8c34b00..175389835f11f 100644 --- a/src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php @@ -27,7 +27,7 @@ public function testCompileWidget() $this->assertEquals( sprintf( - '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'widget\')', + '$this->env->getRuntime(\'Symfony\Bridge\Twig\Extension\FormExtension\')->searchAndRenderBlock(%s, \'widget\')', $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) @@ -50,7 +50,15 @@ public function testCompileWidgetWithVariables() $this->assertEquals( sprintf( +<<<<<<< HEAD +<<<<<<< HEAD '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))', +======= + '$this->env->getRuntime(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))', +>>>>>>> Bumped Twig version to 1.24 and get rid of initRuntime +======= + '$this->env->getRuntime(\'form\')->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))', +>>>>>>> removed the form runtime class as it's not needed $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) @@ -70,7 +78,15 @@ public function testCompileLabelWithLabel() $this->assertEquals( sprintf( +<<<<<<< HEAD +<<<<<<< HEAD '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))', +======= + '$this->env->getRuntime(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))', +>>>>>>> Bumped Twig version to 1.24 and get rid of initRuntime +======= + '$this->env->getRuntime(\'form\')->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))', +>>>>>>> removed the form runtime class as it's not needed $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) @@ -92,7 +108,15 @@ public function testCompileLabelWithNullLabel() // Otherwise the default label is overwritten with null. $this->assertEquals( sprintf( +<<<<<<< HEAD +<<<<<<< HEAD '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\')', +======= + '$this->env->getRuntime(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')', +>>>>>>> Bumped Twig version to 1.24 and get rid of initRuntime +======= + '$this->env->getRuntime(\'form\')->searchAndRenderBlock(%s, \'label\')', +>>>>>>> removed the form runtime class as it's not needed $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) @@ -114,7 +138,15 @@ public function testCompileLabelWithEmptyStringLabel() // Otherwise the default label is overwritten with null. $this->assertEquals( sprintf( +<<<<<<< HEAD +<<<<<<< HEAD '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\')', +======= + '$this->env->getRuntime(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')', +>>>>>>> Bumped Twig version to 1.24 and get rid of initRuntime +======= + '$this->env->getRuntime(\'form\')->searchAndRenderBlock(%s, \'label\')', +>>>>>>> removed the form runtime class as it's not needed $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) @@ -133,7 +165,15 @@ public function testCompileLabelWithDefaultLabel() $this->assertEquals( sprintf( +<<<<<<< HEAD +<<<<<<< HEAD '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\')', +======= + '$this->env->getRuntime(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')', +>>>>>>> Bumped Twig version to 1.24 and get rid of initRuntime +======= + '$this->env->getRuntime(\'form\')->searchAndRenderBlock(%s, \'label\')', +>>>>>>> removed the form runtime class as it's not needed $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) @@ -160,7 +200,15 @@ public function testCompileLabelWithAttributes() // https://github.com/symfony/symfony/issues/5029 $this->assertEquals( sprintf( +<<<<<<< HEAD +<<<<<<< HEAD '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))', +======= + '$this->env->getRuntime(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))', +>>>>>>> Bumped Twig version to 1.24 and get rid of initRuntime +======= + '$this->env->getRuntime(\'form\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))', +>>>>>>> removed the form runtime class as it's not needed $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) @@ -186,7 +234,15 @@ public function testCompileLabelWithLabelAndAttributes() $this->assertEquals( sprintf( +<<<<<<< HEAD +<<<<<<< HEAD '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))', +======= + '$this->env->getRuntime(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))', +>>>>>>> Bumped Twig version to 1.24 and get rid of initRuntime +======= + '$this->env->getRuntime(\'form\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))', +>>>>>>> removed the form runtime class as it's not needed $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) @@ -217,7 +273,15 @@ public function testCompileLabelWithLabelThatEvaluatesToNull() // https://github.com/symfony/symfony/issues/5029 $this->assertEquals( sprintf( +<<<<<<< HEAD +<<<<<<< HEAD '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))', +======= + '$this->env->getRuntime(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))', +>>>>>>> Bumped Twig version to 1.24 and get rid of initRuntime +======= + '$this->env->getRuntime(\'form\')->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))', +>>>>>>> removed the form runtime class as it's not needed $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) @@ -254,7 +318,15 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes() // https://github.com/symfony/symfony/issues/5029 $this->assertEquals( sprintf( +<<<<<<< HEAD +<<<<<<< HEAD '$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in attributes") + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))', +======= + '$this->env->getRuntime(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in attributes") + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))', +>>>>>>> Bumped Twig version to 1.24 and get rid of initRuntime +======= + '$this->env->getRuntime(\'form\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in attributes") + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))', +>>>>>>> removed the form runtime class as it's not needed $this->getVariableGetter('form') ), trim($compiler->compile($node)->getSource()) diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index d7b3ebfb390ae..a756159ed7d65 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -12,9 +12,16 @@ app + + + + + + + %kernel.environment% %kernel.debug% @@ -116,13 +123,14 @@ - - + + %twig.form.resources% +