Skip to content

Commit

Permalink
feature #23437 [TwigBridge] deprecate TwigRenderer (Tobion)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[TwigBridge] deprecate TwigRenderer

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #22783 (comment)
| License       | MIT
| Doc PR        |

<!--
- Bug fixes must be submitted against the lowest branch where they apply
  (lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the 3.4,
  legacy code removals go to the master branch.
- Please fill in this template according to the PR you're about to submit.
- Replace this comment by a description of what your PR is solving.
-->

Commits
-------

6ea71cb [TwigBridge] deprecate TwigRenderer
  • Loading branch information
fabpot committed Jul 17, 2017
2 parents aa27974 + 6ea71cb commit dd6fc4b
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 31 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.4.0
-----

* deprecated `Symfony\Bridge\Twig\Form\TwigRenderer`

3.3.0
-----

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Twig/Extension/FormExtension.php
Expand Up @@ -84,7 +84,7 @@ public function getFunctions()
new TwigFunction('form', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('form_start', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('form_end', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
new TwigFunction('csrf_token', array('Symfony\Bridge\Twig\Form\TwigRenderer', 'renderCsrfToken')),
new TwigFunction('csrf_token', array('Symfony\Component\Form\FormRenderer', 'renderCsrfToken')),
);
}

Expand All @@ -94,7 +94,7 @@ public function getFunctions()
public function getFilters()
{
return array(
new TwigFilter('humanize', array('Symfony\Bridge\Twig\Form\TwigRenderer', 'humanize')),
new TwigFilter('humanize', array('Symfony\Component\Form\FormRenderer', 'humanize')),
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Bridge/Twig/Form/TwigRenderer.php
Expand Up @@ -11,12 +11,16 @@

namespace Symfony\Bridge\Twig\Form;

@trigger_error(sprintf('The %s class is deprecated since version 3.4 and will be removed in 4.0. Use %s instead.', TwigRenderer::class, FormRenderer::class), E_USER_DEPRECATED);

use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Twig\Environment;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @deprecated since version 3.4, to be removed in 4.0. Use Symfony\Component\Form\FormRenderer instead.
*/
class TwigRenderer extends FormRenderer implements TwigRendererInterface
{
Expand Down
14 changes: 13 additions & 1 deletion src/Symfony/Bridge/Twig/Node/FormThemeNode.php
Expand Up @@ -11,7 +11,10 @@

namespace Symfony\Bridge\Twig\Node;

use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Component\Form\FormRenderer;
use Twig\Compiler;
use Twig\Error\RuntimeError;
use Twig\Node\Node;

/**
Expand All @@ -26,9 +29,18 @@ public function __construct(Node $form, Node $resources, $lineno, $tag = null)

public function compile(Compiler $compiler)
{
try {
$compiler->getEnvironment()->getRuntime(FormRenderer::class);
$renderer = FormRenderer::class;
} catch (RuntimeError $e) {
$renderer = TwigRenderer::class;
}

$compiler
->addDebugInfo($this)
->write('$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->setTheme(')
->write('$this->env->getRuntime(')
->string($renderer)
->raw(')->setTheme(')
->subcompile($this->getNode('form'))
->raw(', ')
->subcompile($this->getNode('resources'))
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Node/RenderBlockNode.php
Expand Up @@ -28,7 +28,7 @@ public function compile(Compiler $compiler)
{
$compiler->addDebugInfo($this);
$arguments = iterator_to_array($this->getNode('arguments'));
$compiler->write('$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->renderBlock(');
$compiler->write('$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->renderBlock(');

if (isset($arguments[0])) {
$compiler->subcompile($arguments[0]);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php
Expand Up @@ -24,7 +24,7 @@ class SearchAndRenderBlockNode extends FunctionExpression
public function compile(Compiler $compiler)
{
$compiler->addDebugInfo($this);
$compiler->raw('$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(');
$compiler->raw('$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(');

preg_match('/_([^_]+)$/', $this->getAttribute('name'), $matches);

Expand Down
Expand Up @@ -12,11 +12,11 @@
namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Tests\AbstractBootstrap3HorizontalLayoutTest;
use Twig\Environment;
Expand All @@ -29,6 +29,9 @@ class FormExtensionBootstrap3HorizontalLayoutTest extends AbstractBootstrap3Hori
'choice_attr',
);

/**
* @var FormRenderer
*/
private $renderer;

protected function setUp()
Expand All @@ -48,7 +51,7 @@ protected function setUp()
'bootstrap_3_horizontal_layout.html.twig',
'custom_widgets.html.twig',
), $environment);
$this->renderer = new TwigRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
$this->renderer = new FormRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
$this->registerTwigRuntimeLoader($environment, $this->renderer);
}

Expand Down
Expand Up @@ -12,11 +12,11 @@
namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Tests\AbstractBootstrap3LayoutTest;
use Twig\Environment;
Expand All @@ -25,6 +25,9 @@ class FormExtensionBootstrap3LayoutTest extends AbstractBootstrap3LayoutTest
{
use RuntimeLoaderProvider;

/**
* @var FormRenderer
*/
private $renderer;

protected function setUp()
Expand All @@ -44,7 +47,7 @@ protected function setUp()
'bootstrap_3_layout.html.twig',
'custom_widgets.html.twig',
), $environment);
$this->renderer = new TwigRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
$this->renderer = new FormRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
$this->registerTwigRuntimeLoader($environment, $this->renderer);
}

Expand Down
Expand Up @@ -12,12 +12,12 @@
namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
use Twig\Environment;
Expand All @@ -26,6 +26,9 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
{
use RuntimeLoaderProvider;

/**
* @var FormRenderer
*/
private $renderer;

protected function setUp()
Expand All @@ -48,7 +51,7 @@ protected function setUp()
'form_div_layout.html.twig',
'custom_widgets.html.twig',
), $environment);
$this->renderer = new TwigRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
$this->renderer = new FormRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
$this->registerTwigRuntimeLoader($environment, $this->renderer);
}

Expand Down
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
Expand All @@ -25,6 +25,9 @@ class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
{
use RuntimeLoaderProvider;

/**
* @var FormRenderer
*/
private $renderer;

protected function setUp()
Expand All @@ -45,7 +48,7 @@ protected function setUp()
'form_table_layout.html.twig',
'custom_widgets.html.twig',
), $environment);
$this->renderer = new TwigRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
$this->renderer = new FormRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
$this->registerTwigRuntimeLoader($environment, $this->renderer);
}

Expand Down
Expand Up @@ -11,16 +11,16 @@

namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Component\Form\FormRenderer;
use Twig\Environment;

trait RuntimeLoaderProvider
{
protected function registerTwigRuntimeLoader(Environment $environment, TwigRenderer $renderer)
protected function registerTwigRuntimeLoader(Environment $environment, FormRenderer $renderer)
{
$loader = $this->getMockBuilder('Twig\RuntimeLoader\RuntimeLoaderInterface')->getMock();
$loader->expects($this->any())->method('load')->will($this->returnValueMap(array(
array('Symfony\Bridge\Twig\Form\TwigRenderer', $renderer),
array('Symfony\Component\Form\FormRenderer', $renderer),
)));
$environment->addRuntimeLoader($loader);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php
Expand Up @@ -52,7 +52,7 @@ public function testCompile()

$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand All @@ -64,7 +64,7 @@ public function testCompile()

$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->setTheme(%s, "tpl1");',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->setTheme(%s, "tpl1");',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand Down
20 changes: 10 additions & 10 deletions src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php
Expand Up @@ -35,7 +35,7 @@ public function testCompileWidget()

$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'widget\')',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'widget\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand All @@ -58,7 +58,7 @@ public function testCompileWidgetWithVariables()

$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand All @@ -78,7 +78,7 @@ public function testCompileLabelWithLabel()

$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand All @@ -100,7 +100,7 @@ public function testCompileLabelWithNullLabel()
// Otherwise the default label is overwritten with null.
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\')',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand All @@ -122,7 +122,7 @@ public function testCompileLabelWithEmptyStringLabel()
// Otherwise the default label is overwritten with null.
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\')',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand All @@ -141,7 +141,7 @@ public function testCompileLabelWithDefaultLabel()

$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\')',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand All @@ -168,7 +168,7 @@ public function testCompileLabelWithAttributes()
// https://github.com/symfony/symfony/issues/5029
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand All @@ -194,7 +194,7 @@ public function testCompileLabelWithLabelAndAttributes()

$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand Down Expand Up @@ -225,7 +225,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
// https://github.com/symfony/symfony/issues/5029
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand Down Expand Up @@ -262,7 +262,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
// https://github.com/symfony/symfony/issues/5029
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in attributes") + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in attributes") + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/Resources/config/form.xml
Expand Up @@ -18,7 +18,7 @@
<argument type="service" id="twig" />
</service>

<service id="twig.form.renderer" class="Symfony\Bridge\Twig\Form\TwigRenderer" public="true">
<service id="twig.form.renderer" class="Symfony\Component\Form\FormRenderer" public="true">
<argument type="service" id="twig.form.engine" />
<argument type="service" id="security.csrf.token_manager" on-invalid="null" />
<tag name="twig.runtime" />
Expand Down
Expand Up @@ -266,9 +266,9 @@ public function testRuntimeLoader()

$loader = $container->getDefinition('twig.runtime_loader');
$args = $container->getDefinition((string) $loader->getArgument(0))->getArgument(0);
$this->assertArrayHasKey('Symfony\Bridge\Twig\Form\TwigRenderer', $args);
$this->assertArrayHasKey('Symfony\Component\Form\FormRenderer', $args);
$this->assertArrayHasKey('FooClass', $args);
$this->assertEquals('twig.form.renderer', $args['Symfony\Bridge\Twig\Form\TwigRenderer']->getValues()[0]);
$this->assertEquals('twig.form.renderer', $args['Symfony\Component\Form\FormRenderer']->getValues()[0]);
$this->assertEquals('foo', $args['FooClass']->getValues()[0]);
}

Expand Down

0 comments on commit dd6fc4b

Please sign in to comment.