Completed
Push — master ( 5275e7...db9ddb )
by
unknown
15:03 queued 13s
created

PageTitleProviderManager::getPageTitleCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Core\PageTitle;
19
20
use Psr\Log\LoggerAwareInterface;
21
use Psr\Log\LoggerAwareTrait;
22
use TYPO3\CMS\Core\Service\DependencyOrderingService;
23
use TYPO3\CMS\Core\SingletonInterface;
24
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27
/**
28
 * This class will take care of the different providers and returns the title with the highest priority
29
 */
30
class PageTitleProviderManager implements SingletonInterface, LoggerAwareInterface
31
{
32
    use LoggerAwareTrait;
33
34
    /**
35
     * @var array
36
     */
37
    private $pageTitleCache = [];
38
39
    /**
40
     * @return string
41
     */
42
    public function getTitle(): string
43
    {
44
        $pageTitle = '';
45
46
        $titleProviders = $this->getPageTitleProviderConfiguration();
47
        $titleProviders = $this->setProviderOrder($titleProviders);
48
49
        $orderedTitleProviders = GeneralUtility::makeInstance(DependencyOrderingService::class)
50
            ->orderByDependencies($titleProviders);
51
52
        $this->logger->debug(
53
            'Page title providers ordered',
54
            ['orderedTitleProviders' => $orderedTitleProviders]
55
        );
56
57
        foreach ($orderedTitleProviders as $provider => $configuration) {
58
            if (class_exists($configuration['provider']) && is_subclass_of($configuration['provider'], PageTitleProviderInterface::class)) {
59
                /** @var PageTitleProviderInterface $titleProviderObject */
60
                $titleProviderObject = GeneralUtility::makeInstance($configuration['provider']);
61
                if (($pageTitle = $titleProviderObject->getTitle())
62
                    || ($pageTitle = $this->pageTitleCache[$configuration['provider']] ?? '') !== ''
63
                ) {
64
                    $this->logger->debug(
65
                        'Page title provider ' . $configuration['provider'] . ' used',
66
                        ['title' => $pageTitle, 'providerUsed' => $configuration['provider']]
67
                    );
68
                    $this->pageTitleCache[$configuration['provider']] = $pageTitle;
69
                    break;
70
                }
71
                $this->logger->debug(
72
                    'Page title provider ' . $configuration['provider'] . ' skipped',
73
                    ['name' => $provider, 'skippedProvider' => $configuration['provider']]
74
                );
75
            }
76
        }
77
78
        return $pageTitle;
79
    }
80
81
    /**
82
     * @return array
83
     * @internal
84
     */
85
    public function getPageTitleCache(): array
86
    {
87
        return $this->pageTitleCache;
88
    }
89
90
    /**
91
     * @param array $pageTitleCache
92
     * @internal
93
     */
94
    public function setPageTitleCache(array $pageTitleCache): void
95
    {
96
        $this->pageTitleCache = $pageTitleCache;
97
    }
98
99
    /**
100
     * Get the TypoScript configuration for pageTitleProviders
101
     * @return array
102
     */
103
    private function getPageTitleProviderConfiguration(): array
104
    {
105
        $typoscriptService = GeneralUtility::makeInstance(TypoScriptService::class);
106
        $config = $typoscriptService->convertTypoScriptArrayToPlainArray(
107
            $GLOBALS['TSFE']->config['config'] ?? []
108
        );
109
110
        return $config['pageTitleProviders'] ?? [];
111
    }
112
113
    /**
114
     * @param array $orderInformation
115
     * @return string[]
116
     * @throws \UnexpectedValueException
117
     */
118
    protected function setProviderOrder(array $orderInformation): array
119
    {
120
        foreach ($orderInformation as $provider => &$configuration) {
121
            if (isset($configuration['before'])) {
122
                if (is_string($configuration['before'])) {
123
                    $configuration['before'] = GeneralUtility::trimExplode(',', $configuration['before'], true);
124
                } elseif (!is_array($configuration['before'])) {
125
                    throw new \UnexpectedValueException(
126
                        'The specified "before" order configuration for provider "' . $provider . '" is invalid.',
127
                        1535803185
128
                    );
129
                }
130
            }
131
            if (isset($configuration['after'])) {
132
                if (is_string($configuration['after'])) {
133
                    $configuration['after'] = GeneralUtility::trimExplode(',', $configuration['after'], true);
134
                } elseif (!is_array($configuration['after'])) {
135
                    throw new \UnexpectedValueException(
136
                        'The specified "after" order configuration for provider "' . $provider . '" is invalid.',
137
                        1535803186
138
                    );
139
                }
140
            }
141
        }
142
        return $orderInformation;
143
    }
144
}
145