[BUGFIX] Remove caches for page title and meta tag
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / PageTitle / PageTitleProviderManager.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\PageTitle;
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\TypoScript\TypoScriptService;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * This class will take care of the different providers and returns the title with the highest priority
26 */
27 class PageTitleProviderManager implements SingletonInterface
28 {
29 /**
30 * @return string
31 * @throws \TYPO3\CMS\Core\Cache\Exception
32 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException
33 */
34 public function getTitle(): string
35 {
36 $pageTitle = '';
37
38 $titleProviders = $this->getPageTitleProviderConfiguration();
39 $titleProviders = $this->setProviderOrder($titleProviders);
40
41 $orderedTitleProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)
42 ->orderByDependencies($titleProviders);
43
44 foreach ($orderedTitleProviders as $provider => $configuration) {
45 if (class_exists($configuration['provider']) && is_subclass_of($configuration['provider'], PageTitleProviderInterface::class)) {
46 /** @var PageTitleProviderInterface $titleProviderObject */
47 $titleProviderObject = GeneralUtility::makeInstance($configuration['provider']);
48 if ($pageTitle = $titleProviderObject->getTitle()) {
49 break;
50 }
51 }
52 }
53
54 return $pageTitle;
55 }
56
57 /**
58 * Get the TypoScript configuration for pageTitleProviders
59 * @return array
60 */
61 private function getPageTitleProviderConfiguration(): array
62 {
63 $typoscriptService = GeneralUtility::makeInstance(TypoScriptService::class);
64 $config = $typoscriptService->convertTypoScriptArrayToPlainArray(
65 $GLOBALS['TSFE']->config['config'] ?? []
66 );
67
68 return $config['pageTitleProviders'] ?? [];
69 }
70
71 /**
72 * @param array $orderInformation
73 * @return string[]
74 * @throws \UnexpectedValueException
75 */
76 protected function setProviderOrder(array $orderInformation): array
77 {
78 foreach ($orderInformation as $provider => &$configuration) {
79 if (isset($configuration['before'])) {
80 if (is_string($configuration['before'])) {
81 $configuration['before'] = GeneralUtility::trimExplode(',', $configuration['before'], true);
82 } elseif (!is_array($configuration['before'])) {
83 throw new \UnexpectedValueException(
84 'The specified "before" order configuration for provider "' . $provider . '" is invalid.',
85 1535803185
86 );
87 }
88 }
89 if (isset($configuration['after'])) {
90 if (is_string($configuration['after'])) {
91 $configuration['after'] = GeneralUtility::trimExplode(',', $configuration['after'], true);
92 } elseif (!is_array($configuration['after'])) {
93 throw new \UnexpectedValueException(
94 'The specified "after" order configuration for provider "' . $provider . '" is invalid.',
95 1535803186
96 );
97 }
98 }
99 }
100 return $orderInformation;
101 }
102 }