Passed
Push — master ( 55c9b4...872e0c )
by
unknown
12:42
created

SiteProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Frontend\DataProcessing;
19
20
use TYPO3\CMS\Core\Site\Entity\Site;
21
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
23
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
24
25
/**
26
 * Fetch the site object containing all information about the current site
27
 *
28
 * Example TypoScript configuration:
29
 *
30
 * 10 = TYPO3\CMS\Frontend\DataProcessing\SiteProcessor
31
 * 10 {
32
 *   as = site
33
 * }
34
 *
35
 * where "as" names the variable containing the site object
36
 */
37
class SiteProcessor implements DataProcessorInterface
38
{
39
    protected ?TypoScriptFrontendController $tsfe;
40
41
    public function __construct(TypoScriptFrontendController $tsfe = null)
42
    {
43
        $this->tsfe = $tsfe ?? $GLOBALS['TSFE'] ?? null;
44
    }
45
46
    /**
47
     * @param ContentObjectRenderer $cObj The data of the content element or page
48
     * @param array $contentObjectConfiguration The configuration of Content Object
49
     * @param array $processorConfiguration The configuration of this processor
50
     * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
51
     * @return array the processed data as key/value store
52
     */
53
    public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData): array
54
    {
55
        $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'site');
56
        $processedData[$targetVariableName] = $this->getCurrentSite();
57
        return $processedData;
58
    }
59
60
    /**
61
     * Returns the currently configured "site" if a site is configured (= resolved) in the current request.
62
     *
63
     * @return Site|null
64
     */
65
    protected function getCurrentSite(): ?Site
66
    {
67
        if ($this->tsfe instanceof TypoScriptFrontendController) {
68
            return $this->tsfe->getSite();
69
        }
70
        return null;
71
    }
72
}
73