[FEATURE] Provide core cache frontends via dependency injection 88/61588/6
authorMarkus Poerschke <markus@poerschke.nrw>
Sun, 1 Sep 2019 18:48:30 +0000 (20:48 +0200)
committerSusanne Moog <look@susi.dev>
Wed, 11 Sep 2019 12:59:43 +0000 (14:59 +0200)
Configure services for each service instance. The service names of the
cache frontends will follow this name pattern:
"cache.[NAME OF CONFIGURATION]".

E.g. the l10n cache frontend will be added as a service "cache.l10n".
(One exception has been made for the workspaces_cache, which is names
cache.workspaces)

Resolves: #89054
Releases: master
Change-Id: I5e328503ee0399f20ea37d766b8a80cd6d9930fc
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61588
Tested-by: Benjamin Franzke <bfr@qbus.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog <look@susi.dev>
Reviewed-by: Benjamin Franzke <bfr@qbus.de>
Reviewed-by: Markus Poerschke <markus@poerschke.nrw>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Susanne Moog <look@susi.dev>
typo3/sysext/adminpanel/Configuration/Services.yaml
typo3/sysext/core/Configuration/Services.yaml
typo3/sysext/core/Documentation/Changelog/master/Feature-89054-ProvideCoreCacheFrontendsViaDependencyInjection.rst [new file with mode: 0644]
typo3/sysext/extbase/Configuration/Services.yaml
typo3/sysext/fluid/Configuration/Services.yaml
typo3/sysext/workspaces/Configuration/Services.yaml

index 15ca606..d09783a 100644 (file)
@@ -6,3 +6,8 @@ services:
 
   TYPO3\CMS\Adminpanel\:
     resource: '../Classes/*'
+
+  cache.adminpanel_requestcache:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['adminpanel_requestcache']
index a6f5619..415f203 100644 (file)
@@ -35,3 +35,40 @@ services:
       - { name: event.listener,
           identifier: 'legacy-slot',
           event: TYPO3\CMS\Core\Mail\Event\AfterMailerInitializationEvent }
+
+  # Core caches, cache.core and cache.assets are injected as early
+  # entries in TYPO3\CMS\Core\Core\Bootstrap and therefore omitted here
+  cache.hash:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['hash']
+
+  cache.pages:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['pages']
+
+  cache.pagesection:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['pagesection']
+
+  cache.runtime:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['runtime']
+
+  cache.rootline:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['rootline']
+
+  cache.imagesizes:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['imagesizes']
+
+  cache.l10n:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['l10n']
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-89054-ProvideCoreCacheFrontendsViaDependencyInjection.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-89054-ProvideCoreCacheFrontendsViaDependencyInjection.rst
new file mode 100644 (file)
index 0000000..8210709
--- /dev/null
@@ -0,0 +1,87 @@
+.. include:: ../../Includes.txt
+
+=======================================================================
+Feature: #89054 - Provide core cache frontends via dependency injection
+=======================================================================
+
+See :issue:`89054`
+
+Description
+===========
+
+With version 10 dependency injection was introduced to TYPO3. To work with
+the cache, currently only the :php:`CacheManager` is available as a service within
+the dependency injection container. To foster the „Inversion of Control“ pattern,
+the instances of :php:`\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface` should
+be injected to the objects rather than using the :php:`CacheManager`.
+
+Classes should be adapted to avoid :php:`CacheManager` whenever possible.
+
+The TYPO3 core provides all core caches as dependency injection services.
+The name of the service follows the scheme :php:`cache.[CONFIGURATION NAME]`.
+E.g. the core cache frontend will have the service id :php:`cache.core`.
+
+Third party extensions are encouraged to do the same and provide a :php:`cache.my_cache`
+service in :file:`Configuration/Services.yaml` for cache configuration they define
+in :file:`ext_localconf.php`.
+
+Usage
+=====
+
+Given a class needs the "my_cache" cache Frontend, then the code before version 10.1
+looked like the following example:
+
+.. code-block:: php
+
+   class MyClass
+   {
+       /**
+        * @var TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+        */
+       private $cache;
+
+       public function __construct()
+       {
+           $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
+           $this->cache = $cacheManager->getCache('my_cache');
+       }
+   }
+
+The instance of :php:`FrontendCacheInterface` was retrieved by creating an instance
+of :php:`CacheManager` and then by calling the :php:`getCache` method.
+
+To inject the cache directly, the class needs to be changed as follows. The instance
+of :php:`FrontendCacheInterface` will be passed as an argument to the constructor.
+
+.. code-block:: php
+
+   class MyClass
+   {
+       /**
+        * @var TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+        */
+       private $cache;
+
+       public function __construct(FrontendInterface $cache)
+       {
+           $this->cache = $cache;
+       }
+   }
+
+Since the auto-wiring feature of the dependency injection container cannot detect,
+which cache configuration should be used for the :php:`$cache` argument, the container
+service configuration needs to be extended as well:
+
+.. code-block:: yaml
+
+    services:
+      cache.my_cache:
+        class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+        factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+        arguments: ['my_cache']
+
+      MyClass:
+        arguments:
+          $cache: '@cache.my_cache'
+
+.. index:: PHP-API, ext:core
index 366498c..691376f 100644 (file)
@@ -34,3 +34,8 @@ services:
   TYPO3\CMS\Extbase\Persistence\Generic\Query: ~
   TYPO3\CMS\Extbase\Persistence\Generic\QueryResult: ~
   TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings: ~
+
+  cache.extbase:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['extbase']
index f8b1272..c822b70 100644 (file)
@@ -12,3 +12,8 @@ services:
   TYPO3\CMS\Fluid\View\TemplateView:
     arguments:
       $context: null
+
+  cache.fluid_template:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['fluid_template']
index fa6a502..a2a64f2 100644 (file)
@@ -6,3 +6,8 @@ services:
 
   TYPO3\CMS\Workspaces\:
     resource: '../Classes/*'
+
+  cache.workspaces:
+    class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
+    factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
+    arguments: ['workspaces_cache']