Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Twig extensions refatoring to decouple definitions from implementations #20093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixed circular reference in Twig Form integration
  • Loading branch information
fabpot committed Oct 1, 2016
commit b515702fc90d759fba502b7e474f7462b4a9b578
6 changes: 6 additions & 0 deletions 6 src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

3.2.0
-----

* Deprecated the possibility to inject the Form Twig Renderer into the form
extension. Inject it on TwigRendererEngine instead.

2.7.0
-----

Expand Down
13 changes: 11 additions & 2 deletions 13 src/Symfony/Bridge/Twig/Extension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@
*/
class FormExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
{
public function __construct(TwigRendererInterface $renderer)
private $renderer;

public function __construct(TwigRendererInterface $renderer = null)
{
if (null !== $this->renderer) {
@trigger_error(sprintf('Passing a Twig Form Renderer to the "%s" constructor is deprecated since version 3.2 and won\'t be possible in 4.0. Pass the Twig_Environment to the TwigRendererEngine constructor instead.', static::class), E_USER_DEPRECATED);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

could use static::class instead of get_class($this).

Copy link
Member Author

Choose a reason for hiding this comment

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

done

$this->renderer = $renderer;
}

/**
* {@inheritdoc}
*
* To be removed in 4.0
*/
public function initRuntime(\Twig_Environment $environment)
{
$this->renderer->setEnvironment($environment);
if (null !== $this->renderer) {
$this->renderer->setEnvironment($environment);
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions 10 src/Symfony/Bridge/Twig/Form/TwigRendererEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class TwigRendererEngine extends AbstractRendererEngine implements TwigRendererE
*/
private $template;

public function __construct(array $defaultThemes = array(), \Twig_Environment $environment = null)
Copy link
Member

Choose a reason for hiding this comment

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

Should we trigger a deprecation when the environment is not injected in the constructor ? It would make sense to remove the setter in 4.0 IMO. The environment is a required dependency, and replacing it after we started using the renderer may break things due to the caching of the block resolution

Copy link
Member Author

Choose a reason for hiding this comment

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

done

{
if (null === $environment) {
@trigger_error(sprintf('Not passing a Twig Environment as the second argument for "%s" constructor is deprecated since version 3.2 and won\'t be possible in 4.0.', static::class), E_USER_DEPRECATED);
}

parent::__construct($defaultThemes);
$this->environment = $environment;
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 2 additions & 0 deletions 2 src/Symfony/Bridge/Twig/Form/TwigRendererEngineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @deprecated Deprecated since version 3.2, to be removed in 4.0.
*/
interface TwigRendererEngineInterface extends FormRendererEngineInterface
{
Expand Down
2 changes: 2 additions & 0 deletions 2 src/Symfony/Bridge/Twig/Form/TwigRendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @deprecated Deprecated since version 3.2, to be removed in 4.0.
*/
interface TwigRendererInterface extends FormRendererInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,20 @@ protected function setUp()
{
parent::setUp();

$rendererEngine = new TwigRendererEngine(array(
'bootstrap_3_horizontal_layout.html.twig',
'custom_widgets.html.twig',
));
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
$extension = new FormExtension($this->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($extension);
$extension->initRuntime($environment);
$environment->addExtension(new FormExtension());

$rendererEngine = new TwigRendererEngine(array(
'bootstrap_3_horizontal_layout.html.twig',
'custom_widgets.html.twig',
), $environment);
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
$this->registerTwigRuntimeLoader($environment, $this->renderer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,20 @@ protected function setUp()
{
parent::setUp();

$rendererEngine = new TwigRendererEngine(array(
'bootstrap_3_layout.html.twig',
'custom_widgets.html.twig',
));
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
$extension = new FormExtension($this->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($extension);
$extension->initRuntime($environment);
$environment->addExtension(new FormExtension());

$rendererEngine = new TwigRendererEngine(array(
'bootstrap_3_layout.html.twig',
'custom_widgets.html.twig',
), $environment);
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
$this->registerTwigRuntimeLoader($environment, $this->renderer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ protected function setUp()
{
parent::setUp();

$rendererEngine = new TwigRendererEngine(array(
'form_div_layout.html.twig',
'custom_widgets.html.twig',
));
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
$extension = new FormExtension($this->renderer);

$loader = new StubFilesystemLoader(array(
__DIR__.'/../../Resources/views/Form',
__DIR__.'/Fixtures/templates/form',
Expand All @@ -48,8 +41,13 @@ protected function setUp()
$environment->addGlobal('global', '');
// the value can be any template that exists
$environment->addGlobal('dynamic_template_name', 'child_label');
$environment->addExtension($extension);
$extension->initRuntime($environment);
$environment->addExtension(new FormExtension());

$rendererEngine = new TwigRendererEngine(array(
'form_div_layout.html.twig',
'custom_widgets.html.twig',
), $environment);
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
$this->registerTwigRuntimeLoader($environment, $this->renderer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ protected function setUp()
{
parent::setUp();

$rendererEngine = new TwigRendererEngine(array(
'form_table_layout.html.twig',
'custom_widgets.html.twig',
));
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
$extension = new FormExtension($this->renderer);

$loader = new StubFilesystemLoader(array(
__DIR__.'/../../Resources/views/Form',
__DIR__.'/Fixtures/templates/form',
Expand All @@ -45,8 +38,13 @@ protected function setUp()
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addGlobal('global', '');
$environment->addExtension($extension);
$extension->initRuntime($environment);
$environment->addExtension(new FormExtension());

$rendererEngine = new TwigRendererEngine(array(
'form_table_layout.html.twig',
'custom_widgets.html.twig',
), $environment);
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'));
$this->registerTwigRuntimeLoader($environment, $this->renderer);
}

Expand Down
5 changes: 2 additions & 3 deletions 5 src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,13 @@
<argument type="service" id="router.request_context" on-invalid="ignore" />
</service>

<service id="twig.extension.form" class="Symfony\Bridge\Twig\Extension\FormExtension" public="false">
<argument type="service" id="twig.form.renderer" />
</service>
<service id="twig.extension.form" class="Symfony\Bridge\Twig\Extension\FormExtension" public="false" />

<service id="twig.extension.debug" class="Twig_Extension_Debug" public="false" />

<service id="twig.form.engine" class="Symfony\Bridge\Twig\Form\TwigRendererEngine" public="false">
<argument>%twig.form.resources%</argument>
<argument type="service" id="twig" />
</service>

<service id="twig.form.renderer" class="Symfony\Bridge\Twig\Form\TwigRenderer">
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.