#109716Cannot DI PageRepository in blank projects because constructor calls init which demands DB and other framework context
Description
As the title says:
If any class in a project based on v14 tries to inject TYPO3\CMS\Core\Domain\Repository\PageRepository in a constructor, DI composition fails at that class because TYPO3\CMS\Core\Domain\Repository\PageRepository calls it’s own “init()” function from the constructor and “init()” demands a configured DB.
The specific error is 1459422492 “The requested database connection named “Default” has not been configured.”
Caused because “init()” does a bunch of things that require configuration and context. The error above is simply the one that happens first and blocks the rest.
This makes it impossible to run `composer install` in a project that has not yet been configured with DB credentials as PageRepository instances cannot be created by DI.
Goes without saying, classes that can be required as constructor arguments in DI should not call any methods that require a fully configured framework to run.
Suggested fix is to initialize on-demand within class methods that require this extra context and avoid calling such methods in the constructor.
Discouraged fix is to prevent PageRepository from being used as DI constructor dependency (which is a valid use case).
Priority set to “must have” since DI of PageRepository is a completely valid use case which is prevented by this call-init-in-constructor approach.
Steps to reproduce:
- Create a blank project but don’t composer install it.
- In that project, require TYPO3v14.
- In that project, add at least one class which requires PageRepository as constructor argument and is registered for DI processing.
- Then run `composer install`
What happens:
- Install process breaks at script `assets:publish` with error above, resulting in a chicken/egg problem (can’t install without already being installed).
What is expected:
- Install process is able to finish without errors.
Reviews 5
- main 94222[TASK] Make PageRepository readonly
- 14.3 94218[BUGFIX] Avoid PageRepository->__construct()->init() DB call
- main 94210[BUGFIX] Avoid PageRepository->__construct()->init() DB call
- 14.3 93906[BUGFIX] Make asset:publish during `composer install` more resilient
- main 93878[BUGFIX] Make asset:publish during `composer install` more resilient