Skip to content
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

Fix compatibility with Symfony 3.2 form renderer #4216

Merged
merged 7 commits into from Dec 12, 2016

Conversation

Koc
Copy link
Contributor

@Koc Koc commented Nov 24, 2016

I am targetting this branch, because this is bugfix.

Closes #4152, #4154

Changelog

### Added
- Added helper method `CRUDController::setFormTheme`.
### Fixed
- Fix compatibility with Symfony 3.2 form renderer.

Subject

Proposed sollution in #4154 breaks compatibility with older Symfony versions because twig.form.renderer service is private https://github.com/symfony/symfony/blob/2.8/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml#L156 . Also I've updated code in other places.

@Koc
Copy link
Contributor Author

Koc commented Nov 24, 2016

Also another approach is maybe calling

$this->get('twig')->getRuntime('Symfony\Bridge\Twig\Form\TwigRenderer')->setTheme(...)

@fabpot, @stof can you please suggest how to deal in this case?

@@ -105,7 +105,12 @@ public function listAction()
$formView = $datagrid->getForm()->createView();

// set the theme for the current Admin Form
$this->get('twig')->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')->renderer->setTheme($formView, $this->admin->getFilterTheme());
if ($this->has('twig.form.renderer')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

has does not seem to exist (see Travis)

Copy link
Contributor

Choose a reason for hiding this comment

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

$this->get('twig.form.renderer')->setTheme($formView, $this->admin->getFilterTheme());
} else {
// BC for Symfony < 3.2
$this->get('twig')->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')->renderer->setTheme($formView, $this->admin->getFilterTheme());
Copy link
Contributor

Choose a reason for hiding this comment

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

Please linebreak this

@@ -112,7 +112,13 @@ public function appendFormFieldElementAction(Request $request)

$extension = $this->twig->getExtension('Symfony\Bridge\Twig\Extension\FormExtension');
$extension->initRuntime($this->twig);
$extension->renderer->setTheme($view, $admin->getFormTheme());

if ($this->has('twig.form.renderer')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

HelperController does not extend the standard sf controller, hence the error you get in Travis

Copy link
Contributor

Choose a reason for hiding this comment

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

You can get the container from the pool if you wish (it's a very ugly, but should do the trick).

Copy link
Contributor

Choose a reason for hiding this comment

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

The alternative would be to simply extend the standard controller.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I will inject twig.form.renderer service here inside constructor. Controller as a service helps a lot here.

Copy link
Contributor

@greg0ire greg0ire Nov 24, 2016

Choose a reason for hiding this comment

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

Ok, but you will have do inject it conditionally, since it does not necessarily exist, right? my bad there is always a renderer

Copy link
Contributor Author

@Koc Koc Nov 24, 2016

Choose a reason for hiding this comment

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

It exists since 2.3 (or even earlier but we support >= 2.3) https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml#L102 but it was private service before 3.2. Private services cann't be retrieved at runtime but they can properly injected into another services.

@Koc Koc changed the title Fix compatibility with Symfony 3.2 form renderer [WIP] Fix compatibility with Symfony 3.2 form renderer Nov 24, 2016
@@ -717,4 +717,14 @@ public function getValidatorInterfaces()

return $data;
}

protected function getTwigRenderer()
Copy link
Contributor

Choose a reason for hiding this comment

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

private?

@@ -1364,4 +1363,18 @@ protected function preList(Request $request)

return $this->get('translator')->trans($id, $parameters, $domain, $locale);
}

protected function setFormTheme(FormView $formView)
Copy link
Member

Choose a reason for hiding this comment

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

PHPdoc missing

Copy link
Member

Choose a reason for hiding this comment

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

Would prefer a private method, because we will move the twig stuff to the template completly.

*/
public function __construct(\Twig_Environment $twig, Pool $pool, AdminHelper $helper, $validator)
public function __construct(\Twig_Environment $twig, Pool $pool, AdminHelper $helper, $validator, TwigRenderer $twigRenderer)
Copy link
Member

Choose a reason for hiding this comment

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

This is a BC break. Please make the last parameter optional

Copy link
Contributor

Choose a reason for hiding this comment

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

And use TwigRendererInterface as type hinting, it will be easier to make the build pass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh… too bad.

Copy link
Member

@OskarStark OskarStark left a comment

Choose a reason for hiding this comment

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

We need to stay BC here, this is a BC break!

@@ -1348,6 +1347,23 @@ protected function preList(Request $request)
{
}

protected function setFormTheme(FormView $formView)
Copy link
Member

Choose a reason for hiding this comment

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

  • private please
  • add phpdoc

@@ -497,4 +493,18 @@ private function retrieveFilterFieldDescription(AdminInterface $admin, $field)

return $fieldDescription;
}

protected function setFormTheme(FormView $formView, $theme)
Copy link
Member

Choose a reason for hiding this comment

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

  • private please
  • add phpdoc

@fbourigault
Copy link
Contributor

This is a Symfony BC break. I opened a pull request: symfony/symfony#20710.

@cedricziel
Copy link

Just saw this PR. FYI: I have a similar approach, but use the container to retrieve the renderer, which should save some hassle. cedricziel@23707f9

@Koc
Copy link
Contributor Author

Koc commented Dec 1, 2016

@cedricziel I've tryed this before. The problem that we need pass another dependency to HelperController. In my updated sollution we using only Twig.

@cedricziel
Copy link

@Koc the HelperController in my commit uses twig itself or falls back on the old way. cedricziel@23707f9#diff-bca3ecd7f88753148a6fec5ea1b4623aR446

@greg0ire
Copy link
Contributor

greg0ire commented Dec 2, 2016

@Koc are you stuck? If yes how can we help? Should we ask @cedricziel to make a PR?

@Koc Koc force-pushed the fix-symfony-compatibility branch from 512b4d5 to 6ee4761 Compare December 2, 2016 09:25
@Koc
Copy link
Contributor Author

Koc commented Dec 2, 2016

@greg0ire just need more time to fix tests

@Koc Koc force-pushed the fix-symfony-compatibility branch from 6ee4761 to 513116b Compare December 2, 2016 09:37
core23
core23 previously requested changes Dec 2, 2016
*
* @param FormView $formView
*/
protected function setFormTheme(FormView $formView)
Copy link
Member

Choose a reason for hiding this comment

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

We still need a private method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method can be useful for custom actions that extends from crud controller and used forms. We have a lot setFormTheme calls in our actions and for now they also not works anymore.

Copy link
Member

Choose a reason for hiding this comment

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

That's true, but we will remove the feature to set the form rendering in the controller. This will be done in the template files with the next major.

Copy link
Member

Choose a reason for hiding this comment

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

See #4088

@@ -328,6 +329,20 @@ public function testappendFormFieldElementAction($validatorInterface)

$twig = new \Twig_Environment($this->getMock('\Twig_LoaderInterface'));
$twig->addExtension(new FormExtension($mockRenderer));

if (Kernel::MAJOR_VERSION >= 3 && Kernel::MINOR_VERSION >= 2) {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure, if we could rely on the kernel version.

cc @greg0ire

Copy link
Contributor

@greg0ire greg0ire Dec 2, 2016

Choose a reason for hiding this comment

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

We shouldn't. I'm currently waiting for the tests to pass, and then I think we should do a complete review, but yeah this is not the way to go. Detecting the presence of the public property might be a solution, or anything else that changed since 3.2 if it is too hard (the property does not exist at all times).

Copy link

@cedricziel cedricziel Dec 2, 2016

Choose a reason for hiding this comment

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

Question: Why do you think it might be a bad idea to rely on Kernel to bring the version information? Maybe I'm mistaken, but those fields provide exactly the info we need. I'd rather switch to Kernel::VERSION_ID >= 30200 though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Interesting-thank you for the link.

Copy link
Contributor

Choose a reason for hiding this comment

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

You're very welcome :)

Copy link
Contributor

Choose a reason for hiding this comment

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

@cedricziel the bundle depends on individual packages, not on the fullstack repo (to give flexibility to users), but this allows to have different versions of each package. Kernel constants give you the version of HttpKernel, not of the Twig bridge

Copy link
Contributor

Choose a reason for hiding this comment

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

@wouterj
Copy link
Contributor

wouterj commented Dec 4, 2016

Why not simply doing $this->get('twig.form.renderer')->setTheme(...) ? Anywhy, please fix Symfony 3.2 support soon :)

@nicolas-grekas
Copy link
Contributor

Please test symfony/symfony#20769

@greg0ire greg0ire mentioned this pull request Dec 5, 2016
@greg0ire
Copy link
Contributor

greg0ire commented Dec 9, 2016

Triggering a new build. Let's pray ⛪

@greg0ire
Copy link
Contributor

greg0ire commented Dec 9, 2016

Let's not celebrate prematurely, but it looks good :)

@greg0ire
Copy link
Contributor

greg0ire commented Dec 9, 2016

@Koc please fix the StyleCI build

@greg0ire
Copy link
Contributor

greg0ire commented Dec 9, 2016

Aaaand it's green! Congrats!

@Koc
Copy link
Contributor Author

Koc commented Dec 9, 2016

@greg0ire done

@Koc
Copy link
Contributor Author

Koc commented Dec 9, 2016

Have someone tested it in real life application (not only unit tests)? does it really works?

@greg0ire
Copy link
Contributor

greg0ire commented Dec 9, 2016

@wouterj maybe you can? I don't have such an app lying around.

->method('getRuntime')
->will($this->returnCallback(function ($name) use ($twigRenderer) {
switch ($name) {
case 'Symfony\Bridge\Twig\Form\TwigRenderer':
Copy link
Contributor

Choose a reason for hiding this comment

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

A switch with one case… 😕

@cedricziel
Copy link

Just test-drove it in a (rather minimal) sf 3.2 app. All nice and dandy so far.

@greg0ire greg0ire dismissed stale reviews from core23 and OskarStark December 9, 2016 14:23

requested changes were addressed

@greg0ire
Copy link
Contributor

greg0ire commented Dec 9, 2016

@core23 @OskarStark please give this a final review and merge

@Koc Koc changed the title [WIP] Fix compatibility with Symfony 3.2 form renderer Fix compatibility with Symfony 3.2 form renderer Dec 9, 2016
Copy link
Contributor

@wouterj wouterj left a comment

Choose a reason for hiding this comment

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

Tested this PR in the CMF test jobs and previously failing builds are now fixed. So I can confirm this fixes the bug.

@greg0ire
Copy link
Contributor

greg0ire commented Dec 9, 2016

@core23 @OskarStark this needs to be squash-merged (or at least, some of the commits should be squashed together)

@greg0ire
Copy link
Contributor

@sonata-project/contributors : this is kind of a blocker. I'll merge this afternoon if you don't have anything to say.

@greg0ire greg0ire merged commit d8fc2ed into sonata-project:3.x Dec 12, 2016
@greg0ire
Copy link
Contributor

Squashed with a new commit message

@greg0ire
Copy link
Contributor

Thanks @Koc ! And thanks everyone for testing / reviews !

@greg0ire
Copy link
Contributor

greg0ire commented Dec 13, 2016

3.10.1 has been released with this fix in it!

@axzx
Copy link
Contributor

axzx commented Dec 14, 2016

Do you know, that this code

// set the theme for the current Admin Form
$this->get('twig')->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')
          ->renderer->setTheme($view, $this->admin->getFormTheme());

works fine in Symfony 3.2.1?

@stof
Copy link
Contributor

stof commented Dec 14, 2016

@axzx but it triggers deprecation warnings

mba242 pushed a commit to mba242/SonataAdminBundle that referenced this pull request Dec 17, 2016
…4216)

Our code was relying on an undeclared private property of the form Twig
extension to get a renderer object, in order to set the form theme. On recent
versions of Symfony, the renderer is now obtained from the Twig environment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CRUD Controller tries to access private property Symfony\Bridge\Twig\Extension\FormExtension::$renderer