[FEATURE] Add symfony dependency injection for core and extbase 55/58255/71
authorBenjamin Franzke <bfr@qbus.de>
Thu, 11 Jul 2019 08:13:04 +0000 (10:13 +0200)
committerBenjamin Franzke <bfr@qbus.de>
Thu, 11 Jul 2019 09:35:11 +0000 (11:35 +0200)
commit0cf8053e8499247d1d9b63c671b63ab0787b2e54
tree65a19992b2c611fd59e36a8fc90a72109f547178
parent41fee66d91394f09725e4ea5607290c1931a91e1
[FEATURE] Add symfony dependency injection for core and extbase

This feature is a combined approach using two containers/concepts:

1) symfony/dependency-injection (now exposed as official TYPO3 API):
   Supports: autoconfiguration, autowiring using Service.{yaml,php}
   files

2) service providers (fork of the experimental interfaces in
   container-interop/service-provider, sometimes called PSR-11+)
   Supports: factory-style configuration using plain php code
   provided for internal use and possible future public use.

1) Symfony dependency injection is provided to be used by extensions
and throughout the TYPO3 core for (auto)wiring of services (classes).
Extensions can control symfony's dependency injection use a file
located in Configuration/Services.yaml, if they need more flexibility
they may also use Configuration/Services.php which can
use symfony's ContainerConfigurator or ContainerBuilder.

2) Service providers are used (within TYPO3 core) to provide
dependency injection for services used in the install tool which require
failsafe boot. This is based on container-interop/service-provider.
container-interop/service-provider is supposed to become a PSR but is
still marked experimental, therefore this commit adds an implementation
to TYPO3 provided for internal use only – it is not public API.
We do still include it (as @interal fork) right now, to adapt to this
upcoming PSR from the very beginning, in order to actually push the
development of this proposal forward.

Service providers in failsafe mode are consumed by a simplistic,
non-compiled container.
Symfony's container is too slow when running uncached (as the compilation,
recursive directory lookups and autowiring needs to be performed on every
invocation). We do not want caching for recovery maintenance tasks
which would not be able to recover from scenarios with broken caches.

Services that are configured through service providers, are both available
in the non compiled install tool container and in the symfony container.
They do *not* need to be defined twice. The two "worlds" are bridged
using a ServiceProviderCompilerPass which creates symfony DI definitions
from service-provider factories. The code is a derivative of the code from
the (outdated) symfony bundle
thecodingmachine/service-provider-bridge-bundle.

Features
--------
 * inject* methods are now supported for all classes (not just Extbase)
   It is suggested to use constructor based injection, this feature
   is available to provide maximum compatibility to the old Extbase DI.
 * Autoconfiguration based on interface implementation.
   (current) autoconfigured interface are:
     SingletonInterface,  MiddlewareInterface, RequestHandlerInterface,
     LoggerAwareInterface, ControllerInterface, ViewHelperInterface
     (we expect more to follow in subsequent commits)

Included adaptions
------------------
In order to demonstrate and to be able to verify the features of this
patch, some classes are adapted to use or support dependency injection:
 * RedirectHandler/RedirectService autowired/autoconfigured
 * Application classes and their dependencies are manually wired in
   service providers. This is required as the application runs in fail-
   safe mode when there is no configuration available (first install)
 * MiddlewareStack is composed in service providers and injected
   as a RequestHandler into the Http\Application classes (in order to
   prevent injecting the Container, which would be an anti pattern).
 * Middleware configuration is treated as service (like a registry),
   and retrieval is provided through service providers (the service
   providers default to the existing middleware configuration format)
 * The Middleware dispatcher now first tries to retrieve classes from
   the PSR-11 container, falling back to makeInstance if not available
 * Dependency Injection using symfony for all core Extbase Controllers
 * A Services.yaml is added for each core extension which contains
   classes that are used in frontend/backend context
 * A LateBoot service is added for install tool

DI vs ext_localconf – loading order, legacy makeInstance support
----------------------------------------------------------------
Dependency Injection containers solve most of the tasks that
ext_localconf.php and GeneralUtility::makeInstance solve for Singletons:
Service configuration and instance management.

(Symfony) DI could therefore be used to replace both in the long run.
Both are doing similar tasks when talking about Singletons:
They create and configure services that are stored and shared in a
central memory storage (container for DI, GU::$singletonInstances for
our legacy TYPO3 case). That means they both actually conflict right
now – in terms of: Who is responsible for creating a class?

We've made a decision to connect these both: GeneralUtility::makeInstance
will offload instantiation to the Container if the class is a) available
and b) no runtime constructor arguments have been passed to makeInstance
(which symfony DI doesn't support).

The DI container itself does provides backwards compatibility to XCLASSES
and class aliases use a new internal helper method makeInstanceForDi.

ext_localconf's main design flaw is the mixture of initialization
and configuration composition without assurance that all configuration
is available before a configuration-dependent class is instantiated.
Proper DI first reads all configuration and then performs all service
injections on demand and pre calculated using a dependency tree – and
therefore always in proper sequence.

What we *don't* want (+ reasons)
-------------------------------
 * Symfony Bundles
   pro: provide a defined way for container configuration)
   con: Requires the Symfony HTTP kernel which is not compatible with PSR7
 * Usage of Symfony\DependencyInjection\ExtensionInterface
   con: It is not useful as it cannot add new compiler passes
 * Handling of prototypes that need dependency injection *and* a way to
   to get custom constructor arguments passed (individual per class)
   While symfony DI can handle prototypes (called 'shared: false'), it
   does not support passing constructor arguments like the Extbase object
   manager does. We'll advocate to using a factory pattern for those
   prototypes instead.
 * Circular dependencies: This is not supported by symfony DI and isn't
   possible with service providers either

Future changes (left out for brevity)
-------------------------------------

 * (cli) Build tool to warmup DI cache/state during deployment
 * Adaptions to (all) core dispatchers to prefer a PSR-11
   container over GeneralUtility::makeInstance

Dependency changes
------------------
composer require symfony/dependency-injection:^4.1 symfony/config:^4.1

Releases: master
Resolves: #88689
Resolves: #84112
Change-Id: I8efd817066b528a5953c56fdd027cb94b2bb8eb3
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/58255
Tested-by: Tobi Kretschmann <tobi@tobishome.de>
Tested-by: Jörg Bösche <typo3@joergboesche.de>
Tested-by: Alexander Schnitzler <review.typo3.org@alexanderschnitzler.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Benjamin Franzke <bfr@qbus.de>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Alexander Schnitzler <review.typo3.org@alexanderschnitzler.de>
Reviewed-by: Benjamin Franzke <bfr@qbus.de>
119 files changed:
composer.json
composer.lock
typo3/sysext/about/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/adminpanel/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/backend/Classes/Http/Application.php
typo3/sysext/backend/Classes/Http/RequestHandler.php
typo3/sysext/backend/Classes/ServiceProvider.php [new file with mode: 0644]
typo3/sysext/backend/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/backend/composer.json
typo3/sysext/belog/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/beuser/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/core/Classes/Core/Bootstrap.php
typo3/sysext/core/Classes/DependencyInjection/AutowireInjectMethodsPass.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/ContainerBuilder.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/ContainerException.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/EnvVarProcessor.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/FailsafeContainer.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/LoggerAwarePass.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/NotFoundException.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/PublicServicePass.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/ServiceProviderCompilationPass.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/ServiceProviderInterface.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/ServiceProviderRegistry.php [new file with mode: 0644]
typo3/sysext/core/Classes/DependencyInjection/SingletonPass.php [new file with mode: 0644]
typo3/sysext/core/Classes/Http/AbstractApplication.php
typo3/sysext/core/Classes/Http/MiddlewareDispatcher.php
typo3/sysext/core/Classes/Http/MiddlewareStackResolver.php
typo3/sysext/core/Classes/Package/AbstractServiceProvider.php [new file with mode: 0644]
typo3/sysext/core/Classes/Package/Package.php
typo3/sysext/core/Classes/Package/PseudoServiceProvider.php [new file with mode: 0644]
typo3/sysext/core/Classes/ServiceProvider.php [new file with mode: 0644]
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Configuration/Services.php [new file with mode: 0644]
typo3/sysext/core/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/core/Documentation/Changelog/master/Feature-84112-SymfonyDependencyInjectionForCoreAndExtbase.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/DependencyInjection/FailsafeContainerTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/DependencyInjection/Fixtures/TestRegistryServiceProvider.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/DependencyInjection/Fixtures/TestServiceProvider.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/DependencyInjection/Fixtures/TestServiceProviderFactoryOverride.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/DependencyInjection/Fixtures/TestServiceProviderOverride.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/DependencyInjection/Fixtures/TestServiceProviderOverride2.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/DependencyInjection/Fixtures/TestStatefulServiceProvider.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/DependencyInjection/ServiceProviderCompilationPassTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/DependencyInjection/ServiceProviderRegistryTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Http/MiddlewareDispatcherTest.php
typo3/sysext/core/Tests/Unit/Http/MiddlewareStackResolverTest.php
typo3/sysext/core/Tests/Unit/Package/AbstractServiceProviderTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Package/Mocks/Package1ServiceProviderMock.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Package/Mocks/Package2ServiceProviderMock.php [new file with mode: 0644]
typo3/sysext/core/composer.json
typo3/sysext/extbase/Classes/Core/Bootstrap.php
typo3/sysext/extbase/Classes/Mvc/Dispatcher.php
typo3/sysext/extbase/Classes/Object/Container/Container.php
typo3/sysext/extbase/Classes/Object/ObjectManager.php
typo3/sysext/extbase/Classes/ServiceProvider.php [new file with mode: 0644]
typo3/sysext/extbase/Classes/SignalSlot/Dispatcher.php
typo3/sysext/extbase/Configuration/Services.php [new file with mode: 0644]
typo3/sysext/extbase/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/extbase/Tests/Unit/Object/Container/ContainerTest.php
typo3/sysext/extbase/Tests/Unit/Persistence/Generic/Mapper/DataMapperTest.php
typo3/sysext/extbase/Tests/Unit/Persistence/Generic/PersistenceManagerTest.php
typo3/sysext/extbase/Tests/Unit/Persistence/Generic/SessionTest.php
typo3/sysext/extbase/Tests/Unit/SignalSlot/DispatcherTest.php
typo3/sysext/extbase/composer.json
typo3/sysext/extbase/ext_localconf.php
typo3/sysext/extensionmanager/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/felogin/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/filelist/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/fluid/Configuration/Services.php [new file with mode: 0644]
typo3/sysext/fluid/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/fluid/composer.json
typo3/sysext/fluid_styled_content/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/form/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
typo3/sysext/frontend/Classes/Http/Application.php
typo3/sysext/frontend/Classes/Middleware/EidHandler.php
typo3/sysext/frontend/Classes/ServiceProvider.php [new file with mode: 0644]
typo3/sysext/frontend/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/frontend/composer.json
typo3/sysext/impexp/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/indexed_search/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/info/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/install/Classes/Command/UpgradeWizardListCommand.php
typo3/sysext/install/Classes/Command/UpgradeWizardRunCommand.php
typo3/sysext/install/Classes/Controller/AbstractController.php
typo3/sysext/install/Classes/Controller/InstallerController.php
typo3/sysext/install/Classes/Controller/MaintenanceController.php
typo3/sysext/install/Classes/Controller/UpgradeController.php
typo3/sysext/install/Classes/Http/Application.php
typo3/sysext/install/Classes/Middleware/Maintenance.php
typo3/sysext/install/Classes/Service/ClearCacheService.php
typo3/sysext/install/Classes/Service/LateBootService.php [new file with mode: 0644]
typo3/sysext/install/Classes/Service/LoadTcaService.php
typo3/sysext/install/Classes/ServiceProvider.php [new file with mode: 0644]
typo3/sysext/install/Tests/Unit/Controller/UpgradeControllerTest.php
typo3/sysext/install/composer.json
typo3/sysext/linkvalidator/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/lowlevel/Classes/Controller/ConfigurationController.php
typo3/sysext/lowlevel/Classes/ServiceProvider.php [new file with mode: 0644]
typo3/sysext/lowlevel/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/lowlevel/composer.json
typo3/sysext/opendocs/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/recordlist/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/recycler/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/redirects/Classes/Http/Middleware/RedirectHandler.php
typo3/sysext/redirects/Classes/Service/RedirectService.php
typo3/sysext/redirects/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/redirects/Tests/Unit/Service/RedirectServiceTest.php
typo3/sysext/reports/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/rte_ckeditor/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/scheduler/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/seo/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/setup/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/sys_note/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/t3editor/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/tstemplate/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/viewpage/Configuration/Services.yaml [new file with mode: 0644]
typo3/sysext/workspaces/Configuration/Services.yaml [new file with mode: 0644]