[BUGFIX] Remove caches for page title and meta tag
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / MetaTag / MetaTagManagerRegistry.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\MetaTag;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\Service\DependencyOrderingService;
20 use TYPO3\CMS\Core\SingletonInterface;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Holds all available meta tag managers
25 */
26 class MetaTagManagerRegistry implements SingletonInterface
27 {
28 protected $registry = [];
29 private $instances = [];
30 private $managers;
31
32 public function __construct()
33 {
34 $this->registry['generic'] = [
35 'module' => GenericMetaTagManager::class
36 ];
37 }
38
39 /**
40 * Add a MetaTagManager to the registry
41 *
42 * @param string $name
43 * @param string $className
44 * @param array $before
45 * @param array $after
46 */
47 public function registerManager(string $name, string $className, array $before = ['generic'], array $after = [])
48 {
49 if (!count($before)) {
50 $before[] = 'generic';
51 }
52
53 $this->registry[$name] = [
54 'module' => $className,
55 'before' => $before,
56 'after' => $after
57 ];
58 $this->managers = null;
59 }
60
61 /**
62 * Get the MetaTagManager for a specific property
63 *
64 * @param string $property
65 * @return MetaTagManagerInterface
66 */
67 public function getManagerForProperty(string $property): MetaTagManagerInterface
68 {
69 $property = strtolower($property);
70 foreach ($this->getAllManagers() as $manager) {
71 if ($manager->canHandleProperty($property)) {
72 return $manager;
73 }
74 }
75
76 // Just a fallback because the GenericMetaTagManager is also registered in the list of MetaTagManagers
77 return GeneralUtility::makeInstance(GenericMetaTagManager::class);
78 }
79
80 /**
81 * Get an array of all registered MetaTagManagers
82 *
83 * @return MetaTagManagerInterface[]
84 */
85 public function getAllManagers(): array
86 {
87 if ($this->managers !== null) {
88 return $this->managers;
89 }
90
91 $orderedManagers = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies(
92 $this->registry
93 );
94
95 $this->managers = [];
96 foreach ($orderedManagers as $manager => $managerConfiguration) {
97 $module = $managerConfiguration['module'];
98 if (class_exists($module)) {
99 $this->instances[$module] = $this->instances[$module] ?? GeneralUtility::makeInstance($module);
100 $this->managers[$manager] = $this->instances[$module];
101 }
102 }
103
104 return $this->managers;
105 }
106
107 /**
108 * Remove all registered MetaTagManagers
109 */
110 public function removeAllManagers()
111 {
112 $this->registry = [];
113 $this->managers = null;
114 }
115 }