[BUGFIX] Remove caches for page title and meta tag
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / MetaTag / GenericMetaTagManager.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 /**
20 * Handles typical meta tags (non-grouped). Use AbstractMetaTagManager
21 * to create you own MetaTags, this class is final by design
22 */
23 final class GenericMetaTagManager implements MetaTagManagerInterface
24 {
25 /**
26 * The separator to define subproperties like og:image:width
27 *
28 * @var string
29 */
30 protected $subPropertySeparator = ':';
31
32 /**
33 * Array of properties that are set by the manager
34 *
35 * @var array
36 */
37 protected $properties = [];
38
39 /**
40 * Add a property (including subProperties)
41 *
42 * @param string $property
43 * @param string $content
44 * @param array $subProperties
45 * @param bool $replace
46 * @param string $type
47 */
48 public function addProperty(string $property, string $content, array $subProperties = [], bool $replace = false, string $type = 'name')
49 {
50 $property = strtolower($property);
51 $type = strtolower($type) ?: 'name';
52
53 if ($replace) {
54 $this->removeProperty($property, $type);
55 }
56
57 $this->properties[$property][$type][] = [
58 'content' => $content,
59 'subProperties' => $subProperties
60 ];
61 }
62
63 /**
64 * Get the data of a specific property
65 *
66 * @param string $property
67 * @param string $type
68 * @return array
69 */
70 public function getProperty(string $property, string $type = 'name'): array
71 {
72 $property = strtolower($property);
73 $type = strtolower($type) ?: 'name';
74
75 if (!empty($this->properties[$property][$type])) {
76 return $this->properties[$property][$type];
77 }
78 return [];
79 }
80
81 /**
82 * Returns an array with all properties that can be handled by this manager
83 * @return array
84 */
85 public function getAllHandledProperties(): array
86 {
87 return [];
88 }
89
90 /**
91 * Render all registered properties of this manager
92 *
93 * @return string
94 */
95 public function renderAllProperties(): string
96 {
97 $metatags = [];
98 foreach (array_keys($this->properties) as $property) {
99 $metatags[] = $this->renderProperty($property);
100 }
101
102 return implode(PHP_EOL, $metatags);
103 }
104
105 /**
106 * Render a specific property including subproperties of that property
107 *
108 * @param string $property
109 * @return string
110 */
111 public function renderProperty(string $property): string
112 {
113 $property = strtolower($property);
114
115 $metaTags = [];
116 foreach ((array)$this->properties[$property] as $type => $propertyItems) {
117 foreach ($propertyItems as $propertyItem) {
118 $metaTags[] = '<meta ' .
119 htmlspecialchars($type) . '="' . htmlspecialchars($property) . '" ' .
120 'content="' . htmlspecialchars($propertyItem['content']) . '" />';
121
122 if (!count($propertyItem['subProperties'])) {
123 continue;
124 }
125 foreach ($propertyItem['subProperties'] as $subProperty => $value) {
126 $metaTags[] = '<meta ' .
127 htmlspecialchars($type) . '="' . htmlspecialchars($property . $this->subPropertySeparator . $subProperty) . '" ' .
128 'content="' . htmlspecialchars((string)$value) . '" />';
129 }
130 }
131 }
132
133 return implode(PHP_EOL, $metaTags);
134 }
135
136 /**
137 * Remove one property from the MetaTagManager
138 * If there are multiple occurrences of a property, they all will be removed
139 *
140 * @param string $property
141 * @param string $type
142 */
143 public function removeProperty(string $property, string $type = '')
144 {
145 if (!empty($type)) {
146 unset($this->properties[$property][$type]);
147 } else {
148 unset($this->properties[$property]);
149 }
150 }
151
152 /**
153 * Unset all properties
154 */
155 public function removeAllProperties()
156 {
157 $this->properties = [];
158 }
159
160 /**
161 * @param string $property
162 * @return bool
163 */
164 public function canHandleProperty(string $property): bool
165 {
166 return true;
167 }
168 }