Review main Work in progress Orphaned

81188[WIP][FEATURE] Use DI to autoconfigure extbase repositories

Alexander Schnitzler CR 0 V 0 Normal +127 −4 updated 04/21/26, 15:15

Commit message

This change introduces a CompilerPass which calculates the
corresponding entity class to a repository and configures
said entity class as constuctor argument value.

That way, entity classes are set from outside the repository
and allow users to change the current logic which forces
developers to put entities and repositories into specifically
named folders inside an extension.

This is important for those who want to structure their code
differently, like e.g. when applying domain driven design
principles.

Goal is to use
`__construct(protected readonly string $entityClass)` in TYPO3 v15.

TODO:
 * Add deprecation

Test this change

Run a command in your clone of the TYPO3 Core. Then try what the change does and vote on Verified in Gerrit. How to review a patch

Check out patch set 9
git fetch https://review.typo3.org/Packages/TYPO3.CMS refs/changes/88/81188/9 && git checkout FETCH_HEAD
Cherry-pick onto main
git fetch https://review.typo3.org/Packages/TYPO3.CMS refs/changes/88/81188/9 && git cherry-pick FETCH_HEAD

Review

  • Code review CR 0 Needs a vote
  • Verified V 0 Needs a vote
    • core-ci +1 CI

Merge conflicts 4

Issues 1

Files 6

Review log 2130

Show 45 earlier messages
  1. Alexander Schnitzler uploaded patch set 1 09/24/23, 14:37

  2. core-ci voted on patch set 1 V -1 09/24/23, 14:44 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/47122

  3. Alexander Schnitzler uploaded patch set 2 V -1 09/25/23, 06:04

  4. Alexander Schnitzler commented on patch set 2 09/25/23, 06:05

    RepositoryPass.php:39

    Remove this
  5. Alexander Schnitzler wrote 09/25/23, 06:05

    Topic set to extbase
  1. core-ci voted on patch set 2 V +1 09/25/23, 06:11 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/47133

  2. Alexander Schnitzler uploaded patch set 3 V +1 09/27/23, 05:58

  3. Alexander Schnitzler uploaded patch set 4 09/27/23, 05:59

  4. core-ci voted on patch set 3 V +1 09/27/23, 06:10 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/47204

  5. core-ci voted on patch set 4 V +1 09/27/23, 06:10 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/47205

  6. Benjamin Franzke uploaded patch set 5 V +1 09/27/23, 09:00

  7. Benjamin Franzke commented on patch set 5 09/27/23, 09:00

    Looks good in general, only some minor docs remarks.

    Feature-102027-UseDIToAutoconfigureExtbaseRepositories.rst:6

    Let's use long form "dependency injection" in Feature titles

    Feature-102027-UseDIToAutoconfigureExtbaseRepositories.rst:23

    I think this section is good for a commit message, but the Feature.rst description doesn't need to describe the details of the internal constructur changes. We should rather describe how to be migrate in a forward/backwards compatible way (though that's probably best added to a separate Deprecation.rst), like: public function __construct(private readonly SomeDependency $someDep, string $entityClass = null) { parent::__construct($entityClass); }

    Feature-102027-UseDIToAutoconfigureExtbaseRepositories.rst:64

    We usually use Services.yaml examples in documentation.

    Feature-102027-UseDIToAutoconfigureExtbaseRepositories.rst:66

    The services autoconfiguration doesn't need to be fulled turned of, setting the argument is enough (with your current Pass implementation, and that is fine/good)

    RepositoryPass.php:39 Reply

    Done

    RepositoryPass.php:39 Reply

    Done

    RepositoryPass.php:42

    Hm, so this is a workaround because we actually never want `TYPO3\CMS\Extbase\Persistence\Repository` to be instantiated/configured at all, right? (it should actually be an abstract class) We can be more explicit in our config and mark this service to not be available via `autoconfigure: false` in `Services.yaml` Even better would be to actually use `abstract class`. I think that would not even be breaking, as `Repository` couldn't be used as distinct service anyway as `objectType` was not writable. What do you think?

    RepositoryPass.php:51

    A check is needed if the class actually accepts an argument named `$entityClass`, otherwise a compile time error appears for existing repositories that use constructor based dependency injection (so do not yet have $entityClass defined): Invalid service "MyVendor\MyExtension\Domain\Repository\MyRepository": method "__construct()" has no argument named "$entityClass". Check your service definition.

    Repository.php:63

    Let's add a proper deprecation right away. I've marked that as TODO in the commit message.
  8. core-ci voted on patch set 5 V -1 09/27/23, 09:11 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/47219

  9. Benjamin Franzke uploaded patch set 6 V -1 09/27/23, 09:25

  10. core-ci voted on patch set 6 V +1 09/27/23, 09:32 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/47220

  11. Alexander Schnitzler commented on patch set 6 09/27/23, 16:21

    Feature-102027-UseDIToAutoconfigureExtbaseRepositories.rst:23 Reply

    Ack

    RepositoryPass.php:42 Reply

    I agree, Repository should be abstract but I wasn't sure if this is considered breaking and I want to avoid a breaking rst and the scanner shizle.

    Repository.php:63 Reply

    Fine for me.
  12. Alexander Schnitzler marked it work in progress 09/27/23, 16:21

  13. Garvin Hicking uploaded patch set 7 V +1 09/30/25, 18:44

  14. Garvin Hicking marked it ready for review 09/30/25, 18:45

  15. core-ci voted on patch set 7 V -1 09/30/25, 18:51 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/85756

  16. Garvin Hicking uploaded patch set 8 V -1 09/30/25, 19:06

  17. core-ci voted on patch set 8 V +1 09/30/25, 19:12 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/85757

  18. Garvin Hicking commented on patch set 8 09/30/25, 19:17

    (rebased, moved ReST file - I think this one could be carried over to the finish line?)

    RepositoryPass.php:42 Reply

    I've declared the repository as 'abstract'. I wouldn't know cases where a direct repository class gets instantiated without getting extended from `Repository`. So I would agree this should not be breaking.

    Repository.php:63 Reply

    Hhhhhm. I'm a bit unsure about this. Do I understand this right, that would require developers to never do a "new DummyRepository()" anywhere? I think this could affect a few people, and the "annoyance" to explicitly configure coupled entity classes to a repository might be impactful. I would like to continue having some Extbase convenience in my DX, and would appreciate an "opt in" to decoupling namespaces here. I think our usual recommendation to follow extbase conventions here is followed by many developers, and I believe in the usual cases, developers will keep their "couple by naming scheme". Using DI is still hard for a lot of extbase developers and integrators I know, despite all the length we've gone through to advocate it. Doing this without a real-life gain in migrations feels unlucky to me.
  19. Garvin Hicking voted on patch set 8 CR +1 09/30/25, 19:17

  20. Stefan Bürk commented on patch set 8 10/01/25, 10:52

    Repository.php:63 Reply

    @garvin@hick.ing `new DummyRepository()` does not work anyway, as all the `inject*` method injection is not executed and missing persistancemenanger and event manager already. It should be at least `GeneralUtility::makeInstance()` getting it from the DI. @git@alexanderschnitzler.de @ben@bnf.dev However, I'm not sure about making the constructor API now with a scalar value, first optional and later (v15) mandantory. That means, that custom repositories extending this base class needs to ensure to have the "same" argument provided and calling the parent with it, and future additions to the constructor is considerable breaking special for non optionale arguments. In case the custom repository overrides the constructor, not having it in place it's okay in v14, but makint it not-nullable in v15 would lead to ``` Typed property Repository::$entityClass must not be accessed before initialization ``` I'm getting the intention and use-case, but maybe we should have that as a setter method keeping the fallback to calculate the matching domain model, or we need to communicate and making the constructor final (not the class) and developers implementing own repository classes based on this (abstract/base) needs to use inject methods then instead of constructor DI. At least we must be sure what we want in the Repository constructor and commenting it that changes are considerable breaking and can only be changed for `*.0` realeases as breaking if not made final. Not into the smyfony configurator thing but maybe that could be an option to avoid making the constructor api ? Another solution staying on a "setter" method (which the pass could set if not defined as call): https://symfony.com/doc/current/service_container/calls.html ``` services: SBUERK\DiTests\Services\ExampleRepository: public: true calls: - withClassName: - 'custom-class-name-value' ``` Arguments needs to be provided as array then (not sure / tested if named argument mapping can be done. The #[Required] attriubute could be added to setter (wither) enforcing that the call needs to be configured (not tested, but can be provided by the compiler pass eventually ?). ``` If autowire is enabled, you can also use attributes; with the previous example it would be: use Symfony\Contracts\Service\Attribute\Required; #[Required] public function withLogger(LoggerInterface $logger): static { $new = clone $this; $new->logger = $logger; return $new; } If you don't want a method with a static return type and a #[Required] attribute to behave as a wither, you can add a @return $this annotation to disable the returns clone feature. ```
  21. Benjamin Franzke uploaded patch set 9 CR +1 V +1 10/01/25, 12:02

  22. Benjamin Franzke commented on patch set 9 10/01/25, 12:02

    Services.yaml:40

    Can be removed as these dependencies are wired via PHP attributes inside the classes, this must be a rebasing mistake.
  23. core-ci voted on patch set 9 V +1 10/01/25, 12:08 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/85812

  24. Stefan Bürk commented on patch set 9 10/01/25, 21:01

    Repository.php:63 Reply Unresolved

  25. Benni Mack marked it work in progress 04/21/26, 15:15