Review main Abandoned

56068[WIP][POC][TASK] Introduce a configurable PSR-11 DI container

Benjamin Franzke CR 0 V 0 Expert +1425 −230 updated 10/02/18, 07:05

Commit message

This patch is a proof of concept and base for PSR-11 dependency
injection container discussion and therefore marked WIP.

General
-------
The container is configured through service providers.
Service providers provide factories which perform dependency injection and
extension of services through plain php code. There is no configuration
required besides registering the service provider in composer.json
per extension.
A "service" can be an instantiated class or simpel types like arrays or
strings (e.g. to store configuration). Both classes and configuration
values can be (class) dependencies and are thus summarized as "services".

The container and the service providers are marked @internal as the
ServiceProviderInterface (provided by container-interop/service-provider)
is still in development. Marking them @internal allows to adapt to changes
and to update to new releases without backward compatibility problems.

Route and Middleware Dispatcher
-------------------------------
Additionally this patch prepares the middleware dispatcher and the backend
route dispatcher to (optionally) retrieve objects from the container.
That demos common usecases of the container (and allows to verify that it
is actually working).

Implemented examples:
 * RedirectHandler (frontend middleware)
 * ConfigurationController and InfoModuleController (backend routes)

ServiceProviderInterface defines two methods: getFactories() and
getExtensions(). The former is used to define how classes, or
configuration values are created.
The latter (getExtensions) provides an API to extend services (that means:
services as created by a recipe as returned by getFactrories()).
Use cases for getExtensions():
 * Configuring a signal slot (by extending the signal slot dispatcher)
 * Adding a locallang namespace
 * Adding a configuration setting

Configuration Reading
---------------------
An AbstractServiceProvider is added that implements conventional support
for extending common core services with extension specific settings.
To demo the usecase this patch adds support for configuring middleware
stacks through the container (which processes the service providers).

The concept would be the same for TCA configuration handling:
The TYPO3 core extension provides a factory (e.g. 'middlewares' or 'tca')
which is initially set to be an empty array and all TYPO3 extensions
enhance this through their service provider getExtensions() method.

In case of TCA the TYPO3 convention is to read from Configuration/TCA. We
would implement that convention as a default getExtensions() entry in
AbstractServiceProvider. All ServiceProviders that inherit from that
abstract class thus provide support for the configuration convention.
We add a LegacyServiceProvider which is used, in case a TYPO3 extension
does not define an own service provider. This is to ensure those
extensions still benefit from the default getExtensions() method(s)
in AbstractServiceProvider.

The logic to initialize configurations like $GLOBALS['TYPO3_CONF_VARS']
and $GLOBALS['TCA'] should be moved into the AbstractServiceProvider in
far future (as that will probably be breaking).

TODO
----
 * document that this container can only be used for fully stateless
   objects (formerly called singletons)
 * decide whether AbstractServiceProvider::new() is a good idea
   for handling LoggerAwareInterface (or find alternatives).
   code looks ugly when writing `return static::new($container, $class)`
   well, `GeneralUtlity::makeInstance($class) is not better.
 * add tests for AbstractServiceProvider::configureMiddlewares and ::new
 * add container tests in Backend\…\RouteDispatcherTest

Depdendency changes
-------------------
composer require container-interop/service-provider:~0.4.0

Review

  • Code review CR 0

    Imported before Forger kept the single votes, the next import adds them.

  • Verified V 0

    Imported before Forger kept the single votes, the next import adds them.

Issues 1

Files 39

Show 19 more files