[TASK] Clean up MetaTagManager API
[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
30 public function __construct()
31 {
32 $this->registry['generic'] = [
33 'module' => GenericMetaTagManager::class
34 ];
35 }
36
37 /**
38 * Add a MetaTagManager to the registry
39 *
40 * @param string $name
41 * @param string $className
42 * @param array $before
43 * @param array $after
44 */
45 public function registerManager(string $name, string $className, array $before = ['generic'], array $after = [])
46 {
47 if (!count($before)) {
48 $before[] = 'generic';
49 }
50
51 $this->registry[$name] = [
52 'module' => $className,
53 'before' => $before,
54 'after' => $after
55 ];
56 }
57
58 /**
59 * Get the MetaTagManager for a specific property
60 *
61 * @param string $property
62 * @return MetaTagManagerInterface
63 */
64 public function getManagerForProperty(string $property): MetaTagManagerInterface
65 {
66 $property = strtolower($property);
67 foreach ($this->getAllManagers() as $manager) {
68 if ($manager->canHandleProperty($property)) {
69 return $manager;
70 }
71 }
72
73 // Just a fallback because the GenericMetaTagManager is also registered in the list of MetaTagManagers
74 return GeneralUtility::makeInstance(GenericMetaTagManager::class);
75 }
76
77 /**
78 * Get an array of all registered MetaTagManagers
79 *
80 * @return MetaTagManagerInterface[]
81 */
82 public function getAllManagers(): array
83 {
84 $orderedManagers = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies(
85 $this->registry
86 );
87
88 $managers = [];
89 foreach ($orderedManagers as $managerConfiguration) {
90 if (class_exists($managerConfiguration['module'])) {
91 $managers[] = GeneralUtility::makeInstance($managerConfiguration['module']);
92 }
93 }
94
95 return $managers;
96 }
97
98 /**
99 * Remove all registered MetaTagManagers
100 */
101 public function removeAllManagers()
102 {
103 unset($this->registry);
104 }
105 }