Review main Merged

64386[BUGFIX] Consistently fetch SiteConfiguration from DI

Helmut Hummel CR +2 V +2 Easy +19 −26 updated 05/11/20, 18:37

Commit message

SiteConfiguration is available from the DI container (both in failsafe and
symfony DI), therefore no constructor arguments MUST be passed to
GeneralUtility::makeInstance when fetching the SiteConfiguration singleton
instance, or a new instance will be created.

There are however multiple places where this object is still created
by passing constructor arguments to GeneralUtility::makeInstance
(this bypasses proxying to the container).
This leads to the situation that two different objects are created and
retrieved, depending on how it is fetched.

This is now fixed by changing each remaining retrieval via makeInstance
to NOT provide constructor arguments, which in turn results in a call to
the container to fetch the SiteConfiguration object.

Additionally the ServiceProvider introduced in #89892 is changed to allow
providing an alternative implementation using XCLASS. Although XCLASSES
are generally discouraged in favor of Services.yaml overwrites, there is
no other way to provide an alternative for this class, as it is registered
in an internal service provider.

Review

  • Code review CR +2 Done
    • Helmut Hummel +2
    • Benjamin Franzke +1
    • Daniel Goerz +1
  • Verified V +2 Done
    • Helmut Hummel +2
    • Benjamin Franzke +1
    • TYPO3com +1 CI

Issues 1

Files 9

Review log 1417

  1. Helmut Hummel uploaded patch set 1 05/01/20, 17:55

  2. TYPO3com voted on patch set 1 V -1 05/01/20, 17:55 Completed build in 21s on Fri, 1 May, 05:55 PM Test Summary: No tests found Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-3151

  3. Georg Ringer commented on patch set 1 05/01/20, 19:33

    I guess you are meaning 10.4? we didn't branch yet, so master is fine
  4. Benjamin Franzke wrote 05/02/20, 04:56

    Topic set to di
  5. Benjamin Franzke commented on patch set 1 05/02/20, 06:58

    Very good catch, I was so sure I already replaced all those invocations, way before #89892… Why? SiteConfiguration was also configured in Services.yaml (including the constructor argument and as public service) and I faced the same problem with this before (the SiteConfiguration Services.yaml config was already included in the initial symfony DI patch). Anyway, maybe that patch was only in my stash, this change will definitely be needed.
  6. Benjamin Franzke uploaded patch set 2 05/02/20, 07:06

  7. Benjamin Franzke commented on patch set 2 05/02/20, 07:07

    Reply

    Done
  8. TYPO3com voted on patch set 2 V +1 05/02/20, 07:17 Completed build in 10m 53s on Sat, 2 May, 07:17 AM Test Summary: 73663 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-3159

  9. Benjamin Franzke uploaded patch set 3 05/02/20, 08:56

  10. TYPO3com voted on patch set 3 V +1 05/02/20, 09:09 Completed build in 13m 18s on Sat, 2 May, 09:09 AM Test Summary: 73663 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-3162

  11. Helmut Hummel commented on patch set 3 05/02/20, 11:46

    > Patch Set 1: > > Why? SiteConfiguration was also configured in Services.yaml (including the constructor argument and as public service) Ah, right. When I was debugging this, I was looking for that, but somehow did not see it. And thanks for the cleanup!
  12. Alexander Schnitzler commented on patch set 3 05/02/20, 11:47

    SiteConfigurationController.php:331

    Question regarding our general approach: makeInstance internally uses the container for Singletons. I always understood that we have that as a safety net for those who are not familiar with DI yet. Shouldn't the core use `GeneralUtility::getContainer()->get()` instead here to be a good example for users to show the difference between singletons and prototypes?

    Services.yaml:51

    Can you please shed some light on this change please?
  13. Benjamin Franzke commented on patch set 3 05/04/20, 07:09

    SiteConfigurationController.php:331 Reply

    No, GeneralUtility::getContainer() is marked @internal as it is explicitly not suggested to retrieve the container via GeneralUtility. The suggestion is always to use injection of services. If lazy loading is needed (e.g. for dispatchers), then injection of ContainerInterface is fine – GeneralUtility::getContainer() was really meant as internal helper for some codepaths (in core) that are currently not fixable otherwise (without breaking BC) – I'm thinking about some __wakeup Methods here. For my personal taste we already got too many GeneralUtility::getContainer() calls in core. I don't want people to migrate to GeneralUtility::getContainer()->get() while injection would be the right thing. Its better to stick with makeInstance then and migrate to proper injection (once possible) directly. Therefore, we do also rather use makeInstance in the core (if injection is not possible). So, why using GeneralUtility::makeInstance here? I think in Core, especially in bugfixes, we're not always able to simply refactor to dependency injection, as one needs to be *very* clear about the consequences, especially early instatiation of services, vs late (ondemand) instantiation. Regarding the safety net: Yes, makeInstance uses the container – for all clases/services that are marked as available in the container. Therefore it doesn't decide whether a service is a singleton or not, but rather looks at given constructor arguments being zero. If there are more than zero parameters be therefore have to bypass the container Actually, this case (SiteConfiguration) was (IIRC) the only one where non-optional constructor arguments where passed to singleton class before we introduced dependency injection.

    Services.yaml:51 Reply

    SiteDatabaseEditRow didn't really use dependency injection yet. It had an optional constructor argument but retrieved the dependency via makeInstance if it wasn't passed. Now, as it wasn't public in the container, it was not registered in the container. This provider is instanciates lazily and therefore needs to be available in the container. This meant the dependency was never passed. I've therefore marked it public to be able to simplify the constructor of SiteDatabaseEditRow. In contrast to the SiteConfigurationController I considered it save to switch to dependency injection instead of makeInstance here.
  14. Alexander Schnitzler commented on patch set 3 05/04/20, 07:15

    SiteConfigurationController.php:331 Reply

    > The suggestion is always to use injection of services. Sure. I think I have to differentiate here. My understanding was not to use the container in general but in those case where we don't have a DI-ready environment. Then, and only then, I was under the impression that we rather use getContainer rather then makeInstance. Anyways, thanks for clearing that up! :)

    Services.yaml:51 Reply

    Ack
  15. Daniel Goerz voted on patch set 3 CR +1 05/07/20, 07:36

  16. Helmut Hummel voted on patch set 3 V +2 CR +2 05/11/20, 18:37

  17. Helmut Hummel merged patch set 4 05/11/20, 18:37