[FEATURE] Provide core cache frontends via dependency injection
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Documentation / Changelog / master / Feature-89054-ProvideCoreCacheFrontendsViaDependencyInjection.rst
1 .. include:: ../../Includes.txt
2
3 =======================================================================
4 Feature: #89054 - Provide core cache frontends via dependency injection
5 =======================================================================
6
7 See :issue:`89054`
8
9 Description
10 ===========
11
12 With version 10 dependency injection was introduced to TYPO3. To work with
13 the cache, currently only the :php:`CacheManager` is available as a service within
14 the dependency injection container. To foster the „Inversion of Control“ pattern,
15 the instances of :php:`\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface` should
16 be injected to the objects rather than using the :php:`CacheManager`.
17
18 Classes should be adapted to avoid :php:`CacheManager` whenever possible.
19
20 The TYPO3 core provides all core caches as dependency injection services.
21 The name of the service follows the scheme :php:`cache.[CONFIGURATION NAME]`.
22 E.g. the core cache frontend will have the service id :php:`cache.core`.
23
24 Third party extensions are encouraged to do the same and provide a :php:`cache.my_cache`
25 service in :file:`Configuration/Services.yaml` for cache configuration they define
26 in :file:`ext_localconf.php`.
27
28 Usage
29 =====
30
31 Given a class needs the "my_cache" cache Frontend, then the code before version 10.1
32 looked like the following example:
33
34 .. code-block:: php
35
36 class MyClass
37 {
38 /**
39 * @var TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
40 */
41 private $cache;
42
43 public function __construct()
44 {
45 $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
46 $this->cache = $cacheManager->getCache('my_cache');
47 }
48 }
49
50 The instance of :php:`FrontendCacheInterface` was retrieved by creating an instance
51 of :php:`CacheManager` and then by calling the :php:`getCache` method.
52
53 To inject the cache directly, the class needs to be changed as follows. The instance
54 of :php:`FrontendCacheInterface` will be passed as an argument to the constructor.
55
56 .. code-block:: php
57
58 class MyClass
59 {
60 /**
61 * @var TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
62 */
63 private $cache;
64
65 public function __construct(FrontendInterface $cache)
66 {
67 $this->cache = $cache;
68 }
69 }
70
71 Since the auto-wiring feature of the dependency injection container cannot detect,
72 which cache configuration should be used for the :php:`$cache` argument, the container
73 service configuration needs to be extended as well:
74
75 .. code-block:: yaml
76
77 services:
78 cache.my_cache:
79 class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
80 factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
81 arguments: ['my_cache']
82
83 MyClass:
84 arguments:
85 $cache: '@cache.my_cache'
86
87 .. index:: PHP-API, ext:core