This is a simple patch to allow for ContainerAware controllers.

That is, a controller (nee page callback) may at times be an object that implements the ContainerAwareInterface. That is, it wants to have the dependency injection container passed into it wholesale. Normally you don't want to do that, but controllers are supposed to be small glue-code things so it's easier than setting up the exact dependencies for each separate object.

With this patch, any controller that has that interface will automagically have the container injected into it when it's created. This is the same logic that Symfony fullstack uses.

This issue is forked off of #1606794: Implement new routing system, because it's small and self-contained enough to stand on its own.

Patch in a moment as soon as I have a nid. :-)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Crell’s picture

Status: Active » Needs review
Issue tags: +WSCCI, +symfony, +Dependency Injection (DI)
FileSize
3.25 KB

And patch.

sun’s picture

Can we copy the issue summary into the class' phpDoc, please? :)

We're generally missing explanations for the new abstract concepts and builders, and more importantly reasonings for why they exist and why they're useful, at which point they're used/invoked, and how they relate to other classes, and so on. That would be a huge help for people who're looking at some of the code/files to understand what's going on.

Symfony itself improved a lot in its latest version with regard to that, but overall, doesn't set a good example. (Other frameworks are providing much more elaborate documentation in code to explain the usage and concepts.)

effulgentsia’s picture

Once sun is happy with the docs, this looks RTBC to me.

Crell’s picture

Well I don't want to do a direct c&p since I assume we'll want to do other things in the subclass later besides just this. But yeah, I can try to beef up the inline comments a bit. Would that be sufficient? (Down where ContainerAwareInterface is actually checked.)

I think sometime after code freeze I'm going to finally make good on my thread to organize a bunch of Drupalers and let them loose on the Symfony code base's docblocks. :-) It really does need some Drupal treatment, I agree.

sun’s picture

oink - let's not overcomplicate it :) I was basically just asking to get from zero to something ;)

Let me try (not sure whether this is correct):
---

/**
Enhanced resolver to allow for ContainerAware controllers.

By implementing the ContainerAwareInterface, a controller may denote that it wants to have the dependency injection container passed into it wholesale.

Normally this is considered bad practice, but most request controllers in Drupal are supposed to contain minimal glue-code only in order to handle a callback, so injecting the entire container is easier than setting up the exact dependencies for each separate object.

@see Symfony\...\ContainerAwareInterface
@see ...
 */
class ControllerResolver extends BaseControllerResolver {
Crell’s picture

Revised version with additional docs. I didn't do a copy paste because a class docblock is not the place to wax about good controller practices, but it does say what's going on and why. I think this file is now more comment than code. Poor fabpot would probably cry. :-)

fgm’s picture

In that version, there is no longer a warning about this not being a recommended practice, as in the original text. Which means passing the container wholesale is likely to become quite frequent when newcomers discover the class without a best practices warning, since it is so convenient (oooh, I can have the whole global configuration if I use this controller, let's do this !)

Lars Toomre’s picture

Small nit... I think Drupal coding style calls for NULL (not null).

Crell’s picture

Bah, inherited from Symfony when I copy/pasted the code. Fixed.

fgm: I don't believe a class docblock is the right place to get into the nuance of when a controller should be ContainerAware and when a controller should itself be a service. That's a complicated issue; Symfony full stack supports both. (Service controllers would be a later patch to this class, probably.) Several leading Symfonians disagree on it. (Fabpot strongly favors ContainerAware controllers; lsmith strongly favors service controllers.)

Basically, a docblock on a class virtually no module developer will ever see is not the place to try and capture and codify that nuance. :-)

sun’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
2.09 KB
3.52 KB

Thanks!

I started a code review, but then I realized that I could just as well fix the issues myself. I usually prefer to just review, so the problems don't get repeated in other patches... hopefully the interdiff will suffice.

+++ b/core/lib/Drupal/Core/ControllerResolver.php
@@ -0,0 +1,63 @@
+ * @file Definition of Drupal\Core\ControllerResolver;

For the @file directive, the summary should appear on the next line. Should also end in a period.

+++ b/core/lib/Drupal/Core/ControllerResolver.php
@@ -0,0 +1,63 @@
+   * Constructors a new ControllerResolver.

"Constructs"

...

Status: Reviewed & tested by the community » Needs work

The last submitted patch, kernel.containeraware.11.patch, failed testing.

Crell’s picture

Status: Needs work » Needs review
FileSize
3.48 KB

I don't know what you did, but here it is again with the changes from #11. :-)

sun’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
3.52 KB

The only changes are docs changes, as visible in the interdiff in #11.

I suspect we have another random test failure in Drupal\locale\Tests\LocaleFileImportStatus...

catch’s picture

Status: Reviewed & tested by the community » Fixed

OK this looks reasonable to me, and thanks for the docs improvements. Committed/pushed to 8.x.

tim.plunkett’s picture

This broke the ability to use a method as a page callback: #1781560: Regression: Methods can no longer be used in page callbacks

Crell’s picture

Status: Fixed » Needs work

Sigh. All that back and forth about code comments, and NONE of us realized that we forgot a return statement. :-(

Tim indicated in IRC that we should still have tests after fixing it. If so, it should be a unit test on Drupal\Core\ContainerResolver, not a functional test. In practice, nothing should be breaking here that was supposed to work yet, until after the routing patch gets in.

tim.plunkett’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests
FileSize
2.66 KB
2.22 KB

Here is the functional test and test+fix. They can be replaced with unit tests, but should not be removed.

This should set the issue back to "needs work".

Status: Needs review » Needs work

The last submitted patch, method_page_callback-PASS.patch, failed testing.

fgm’s picture

(sorry: answered another thread and ended on that one, please ignore)

Crell’s picture

Status: Needs work » Needs review
FileSize
2.61 KB

Sigh. With the correct test.

Anonymous’s picture

Status: Needs review » Reviewed & tested by the community
tim.plunkett’s picture

Reuploading #21 with and without the tests, just to be sure the test works. But, RTBC because it makes sense.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

Automatically closed -- issue fixed for 2 weeks with no activity.