Problem/Motivation

In #3397522: Fork Symfony's ContainerAwareTrait and ContainerAwareInterface into core we are trying to reduce the use of ContainerAwareTrait as Symfony has deprecated it.

LoggerChannelFactory retrieves the request stack and current user services from the container. There seems to be no reason for this, they could just be injected in the constructor in the normal way.

Steps to reproduce

Proposed resolution

Inject the services directly.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#31 3416354-nr-bot.txt90 bytesneeds-review-queue-bot

Issue fork drupal-3416354

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

longwave created an issue. See original summary.

longwave’s picture

Status: Active » Needs review
spokje’s picture

Status: Needs review » Reviewed & tested by the community

Code changes are minimal and make sense, so does the reasoning in the IS, green tests => RTBC

longwave’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

Let's write a change record in case someone is overriding this for some reason and adding their own constructor. However as it's possible to actually inject NULL (in the case of the installer) I'm not sure if or how we can provide BC.

longwave’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record

Added BC, because we should be able to inject services if the container is available. As per deprecation policy we can link the message to the issue, not sure this is worth it's own CR, so linked it here.

spokje’s picture

Status: Needs review » Needs work

Well....

The new BC triggers deprecation messages on basically every test using a container...

longwave’s picture

Status: Needs work » Needs review

Yeah, that didn't work, there is a container available but it's not the right one. So a slightly uglier approach - if we explicitly pass NULL then that means we are in the installer, otherwise trigger the deprecation and BC layer.

https://3v4l.org/pcU1u implies this should be OK.

spokje’s picture

Status: Needs review » Needs work

Besides adding some NULL, NULL to new LoggerChannelFactory() in \Drupal\Tests\media\Unit\ProviderRepositoryTest and \Drupal\Tests\Core\Logger\LoggerChannelFactoryTest, I think you're onto a winner.

longwave’s picture

Status: Needs work » Needs review

Fixed LoggerChannelFactoryTest as suggested.

Refactored ProviderRepositoryTest to inject a mock LoggerChannelFactory instead of a real object.

spokje’s picture

Status: Needs review » Needs work

PHPCS hates you...;)
#TheStruggleIsReal

Refactored ProviderRepositoryTest to inject a mock LoggerChannelFactory instead of a real object.

Is this because we want to mock as much as possible in unit tests?

And in LoggerChannelFactoryTest we can't mock LoggerChannelFactory since it's the SUT?

longwave’s picture

Status: Needs work » Needs review

Fixed PHPCS.

Re #12, yes, in unit tests it is better to mock things that you aren't specifically testing. Here, we don't actually care what LoggerChannelFactory does internally, except that it needs to provide a LoggerChannel for the ProviderRepository to use - so we inject a mock that only does that. But in LoggerChannelFactoryTest, we actually want to test the functionality of LoggerChannelFactory itself, so we want a real object in order to prove that it behaves correctly.

Which made me think that I could also improve LoggerChannelFactoryTest: we might as well inject mock versions of the correct arguments, so I made that change.

spokje’s picture

Status: Needs review » Reviewed & tested by the community

Nice, like the added mocked arguments.

All my (endless) moaning has been addressed, green tests => RTBC.

longwave’s picture

Also realised we can likely remove the NULL, NULL special case once #2838474: Remove dependency of "file_system" service on "logger" lands.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Merge conflicts on the MR.

longwave’s picture

Title: Inject services into LoggerChannelFactory » [PP-1] Inject services into LoggerChannelFactory
Status: Needs work » Postponed

Let's postpone this on #2838474: Remove dependency of "file_system" service on "logger" as that makes this easier anyway.

andypost made their first commit to this issue’s fork.

andypost’s picture

Title: [PP-1] Inject services into LoggerChannelFactory » Inject services into LoggerChannelFactory
Status: Postponed » Needs review
smustgrave’s picture

Left 1 question on the MR.

longwave’s picture

Status: Needs review » Needs work

Following #3416697: Remove install container definitions of FileSystem and StreamWrapperManager I think we can simplify this.

Unsure of the value of a change record here. The likelihood that someone is overriding the entire logger factory seems low to me. But for completeness it won't take long to write one.

andypost’s picture

andypost’s picture

Status: Needs work » Needs review

Removed the condition, ++ to not add CR as we do all other places

longwave’s picture

Status: Needs review » Needs work

As the request stack and current user are now always available, we can always inject them into the created LoggerChannel.

andypost’s picture

Status: Needs work » Needs review

I bet we should provide BC to allow old container to work for upgrades at least

longwave’s picture

The constructor will ensure the new properties are always set, so $this->container is no longer needed at all?

andypost’s picture

Good point - fixed

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Injection seems fine.

Seems there's still open discussion about when to use a CR vs linking to a ticket. Not sure if that should hold up things so will go ahead and mark.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Question on the MR.

longwave’s picture

Status: Needs work » Needs review

Responded to the feedback.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

longwave’s picture

Status: Needs work » Needs review

Rebased, responded to feedback.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Given there were no changes here, being bold and marking RTBC to get this back with other committers for re-review.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

What's the plan for things like:

I think all of these will be broken after this change. At the very least we need a change record because I think we could allow this under the constructors are not API. But I think we need to consider BC here and what to do.

longwave’s picture

Assigned: Unassigned » longwave

I think we can add BC for all of those cases, it might be a bit awkward but looks doable.

longwave’s picture

Assigned: longwave » Unassigned
Status: Needs work » Needs review

Added BC and deprecations for calling get() without the constructor being called, setContainer() being called, and $this->container being accessed, which I think covers all the cases in #34.

Out of time for now, still needs a change record.

andypost’s picture

Added few comments but the BC is the main concerns

longwave’s picture

@andypost good questions/suggestions, thank you - responded in MR

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Thank you! Then it's ready, follow-up to remove bc can be filled later

  • catch committed 6d74992c on 10.3.x
    Issue #3416354 by longwave, andypost, Spokje, alexpott: Inject services...

  • catch committed 90d2c749 on 11.x
    Issue #3416354 by longwave, andypost, Spokje, alexpott: Inject services...
catch’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs change record

The bc layer is tricky but we get to remove it in the 11.x branch soon, which is good. Committed/pushed to 11.x and cherry-picked to 10.3.x, thanks!

After committing this, I realised it was tagged for a change record and didn't have one. I'm not really convinced we should add change records for constructor deprecations, we're already going above and beyond providing bc for code that supposed to be @internal, but since we know in this case that contrib will be affected, went ahead and added https://www.drupal.org/node/3432835

Status: Fixed » Closed (fixed)

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