Skip to content

Commit f2691d5

Browse files
committed
bug symfony#20769 [Bridge\Twig] Trigger deprecation when using FormExtension::$renderer (nicolas-grekas)
This PR was merged into the 3.2 branch. Discussion ---------- [Bridge\Twig] Trigger deprecation when using FormExtension::$renderer | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | yes (instead of a BC break) | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - As spotted in symfony#20710 and sonata-project/SonataAdminBundle#4216. Note that this simple implementation is fine because neither the class nor its parent have any private/protected properties. Commits ------- 6f1c59c [Bridge\Twig] Trigger deprecation when using FormExtension::$renderer
2 parents 16cea37 + 6f1c59c commit f2691d5

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
1515
use Symfony\Bridge\Twig\Form\TwigRendererInterface;
16+
use Symfony\Component\DependencyInjection\ContainerInterface;
1617
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
1718

1819
/**
@@ -23,12 +24,17 @@
2324
*/
2425
class FormExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
2526
{
27+
/**
28+
* @deprecated since version 3.2, to be removed in 4.0 alongside with magic methods below
29+
*/
2630
private $renderer;
2731

28-
public function __construct(TwigRendererInterface $renderer = null)
32+
public function __construct($renderer = null)
2933
{
30-
if (null !== $this->renderer) {
34+
if ($this->renderer instanceof TwigRendererInterface) {
3135
@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);
36+
} elseif (null !== $renderer && !(is_array($renderer) && isset($renderer[0], $renderer[1]) && $renderer[0] instanceof ContainerInterface)) {
37+
throw new \InvalidArgumentException(sprintf('Passing any arguments the constructor of %s is reserved for internal use.', __CLASS__));
3238
}
3339
$this->renderer = $renderer;
3440
}
@@ -40,8 +46,10 @@ public function __construct(TwigRendererInterface $renderer = null)
4046
*/
4147
public function initRuntime(\Twig_Environment $environment)
4248
{
43-
if (null !== $this->renderer) {
49+
if ($this->renderer instanceof TwigRendererInterface) {
4450
$this->renderer->setEnvironment($environment);
51+
} elseif (null !== $this->renderer) {
52+
$this->renderer[2] = $environment;
4553
}
4654
}
4755

@@ -94,6 +102,62 @@ public function getTests()
94102
);
95103
}
96104

105+
/**
106+
* @internal
107+
*/
108+
public function __get($name)
109+
{
110+
if ('renderer' === $name) {
111+
@trigger_error(sprintf('Using the "%s::$renderer" property is deprecated since version 3.2 as it will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
112+
113+
if (is_array($this->renderer)) {
114+
$renderer = $this->renderer[0]->get($this->renderer[1]);
115+
if (isset($this->renderer[2])) {
116+
$renderer->setEnvironment($this->renderer[2]);
117+
}
118+
$this->renderer = $renderer;
119+
}
120+
}
121+
122+
return $this->$name;
123+
}
124+
125+
/**
126+
* @internal
127+
*/
128+
public function __set($name, $value)
129+
{
130+
if ('renderer' === $name) {
131+
@trigger_error(sprintf('Using the "%s::$renderer" property is deprecated since version 3.2 as it will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
132+
}
133+
134+
$this->$name = $value;
135+
}
136+
137+
/**
138+
* @internal
139+
*/
140+
public function __isset($name)
141+
{
142+
if ('renderer' === $name) {
143+
@trigger_error(sprintf('Using the "%s::$renderer" property is deprecated since version 3.2 as it will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
144+
}
145+
146+
return isset($this->$name);
147+
}
148+
149+
/**
150+
* @internal
151+
*/
152+
public function __unset($name)
153+
{
154+
if ('renderer' === $name) {
155+
@trigger_error(sprintf('Using the "%s::$renderer" property is deprecated since version 3.2 as it will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
156+
}
157+
158+
unset($this->$name);
159+
}
160+
97161
/**
98162
* {@inheritdoc}
99163
*/

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@
118118
<argument type="service" id="router.request_context" on-invalid="ignore" />
119119
</service>
120120

121-
<service id="twig.extension.form" class="Symfony\Bridge\Twig\Extension\FormExtension" public="false" />
121+
<service id="twig.extension.form" class="Symfony\Bridge\Twig\Extension\FormExtension" public="false">
122+
<argument type="collection">
123+
<argument type="service" id="service_container" />
124+
<argument>twig.form.renderer</argument>
125+
</argument>
126+
</service>
122127

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

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=5.5.9",
20-
"symfony/twig-bridge": "~3.2",
20+
"symfony/twig-bridge": "^3.2.1",
2121
"symfony/http-foundation": "~2.8|~3.0",
2222
"symfony/http-kernel": "~2.8|~3.0",
2323
"twig/twig": "~1.28|~2.0"

0 commit comments

Comments
 (0)