-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBridge] fix BC for FormExtension if renderer is FormRenderer #24535
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
Conversation
@@ -54,6 +56,8 @@ public function initRuntime(Environment $environment) | |||
{ | |||
if ($this->renderer instanceof TwigRendererInterface) { | |||
$this->renderer->setEnvironment($environment); | |||
} elseif ($this->renderer instanceof FormRendererInterface && $this->renderer->getEngine() instanceof TwigRendererEngineInterface) { | |||
$this->renderer->getEngine()->setEnvironment($environment); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should do nothing in this case instead (it already has the environment injected properly)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok. Done 😉
$renderer->setEnvironment($this->renderer[2]); | ||
if ($renderer instanceof TwigRendererInterface) { | ||
$renderer->setEnvironment($this->renderer[2]); | ||
} elseif ($renderer instanceof FormRendererInterface && $renderer->getEngine() instanceof TwigRendererEngineInterface) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this case should never happen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can happen if you call ->initRuntime
before accessing ->renderer
on 3.4?
$twig = $this->get('twig');
$twig
->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')
->initRuntime($twig);
$twig
->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')
->renderer;
Results in:
Attempted to call an undefined method named "setEnvironment" of class "Symfony\Component\Form\FormRenderer".
This works fine on 3.3.
Can't we create a test case that would fail without these changes? |
use Symfony\Component\Form\FormRendererInterface; | ||
use Twig\Environment; | ||
|
||
class FormExtensionTest extends TestCase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should I mark this test somehow as deprecated or similar? There is no need to merge it into master for example as there is nothing to test anymore really
I don't quite understand this test fail:
I'm mocking |
Looks like you need to add your tests to the |
@xabbuh thanks looks better now. One test fail left: https://travis-ci.org/symfony/symfony/jobs/287174522 |
You need to add |
thanks again @xabbuh 👍 Can be reviewed then |
@@ -54,7 +54,7 @@ public function initRuntime(Environment $environment) | |||
{ | |||
if ($this->renderer instanceof TwigRendererInterface) { | |||
$this->renderer->setEnvironment($environment); | |||
} elseif (null !== $this->renderer) { | |||
} elseif (is_array($this->renderer)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was not needed to fix the bug, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is needed:
Testing Symfony\Bridge\Twig\Tests\Extension\FormExtensionTest
.....E 6 / 6 (100%)
Time: 132 ms, Memory: 4.00MB
There was 1 error:
1) Symfony\Bridge\Twig\Tests\Extension\FormExtensionTest::testAccessRendererAndInitRuntime with data set #2 (array(Mock_ContainerInterface_3ca94d5e Object (...), 'service_id'), Mock_FormRendererInterface_d5edefef Object (...))
Error: Cannot use object of type Mock_FormRendererInterface_d5edefef as array
/var/www/symfony/src/Symfony/Bridge/Twig/Extension/FormExtension.php:58
/var/www/symfony/src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTest.php:43
ERRORS!
Tests: 6, Assertions: 15, Errors: 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which is the problem reported here: sonata-project/SonataAdminBundle#4652
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For anyone else wondering: The array stored in $this->renderer
is replaced by the FormRenderInterface
instance that is fetched from the container in __get()
: https://github.com/dmaicher/symfony/blob/6813a9f1f1251412bf6161dd732e65e853680736/src/Symfony/Bridge/Twig/Extension/FormExtension.php#L124
@@ -22,6 +22,7 @@ | |||
"require-dev": { | |||
"fig/link-util": "^1.0", | |||
"symfony/asset": "~2.8|~3.0|~4.0", | |||
"symfony/dependency-injection": "~2.8|~3.0|~4.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this targets 3.4, should this be ~3.4|~4.0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just need the ContainerInterface
for the tests. So I guess 2.8
is also fine? Not sure what to do here tbh 😊
Thank you @dmaicher. |
…enderer (dmaicher) This PR was squashed before being merged into the 3.4 branch (closes #24535). Discussion ---------- [TwigBridge] fix BC for FormExtension if renderer is FormRenderer | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24533 | License | MIT | Doc PR | - This fixes some issues within FormExtension since the renderer is now a `FormRenderer`. Commits ------- 4a2f608 [TwigBridge] fix BC for FormExtension if renderer is FormRenderer
Just noticed: Shouldn't we have merged this into the |
@xabbuh this bug did not exist on 3.3 though as the rendering service there was an instance of |
This PR was merged into the 4.0-dev branch. Discussion ---------- [TwigBridge] fix merge of 3.4 into master | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes/no | Fixed tickets | - | License | MIT | Doc PR | - Within #24535 I added `FormExtensionTest` which is completely useless now on master 😊 So this PR just removes the test after it has been merged from 3.4 into master. Commits ------- 5c36609 [TwigBridge] fix merge of 3.4 into master
@dmaicher could you please check sonata-project/SonataAdminBundle#4758, is it somehow related to this one? |
@nicolas-grekas seems not related as my fix was for 3.4 but the problem there actually happens on 3.3 |
But I have the feeling 3.4 broke BC by switching from This PR only fixes some symptoms of that BC break imho. |
This fixes some issues within FormExtension since the renderer is now a
FormRenderer
.