Passed
Push — master ( 6c7aff...ce6d49 )
by
unknown
46:49 queued 20:16
created

GlobalVariableProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 41
dl 0
loc 78
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 10 2
A getConfiguration() 0 26 5
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\Lowlevel\ConfigurationModuleProvider;
19
20
use TYPO3\CMS\Core\Utility\ArrayUtility;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Lowlevel\Controller\ConfigurationController;
23
24
class GlobalVariableProvider extends AbstractProvider
25
{
26
    /**
27
     * Blind configurations which should not be visible to mortal admins
28
     *
29
     * @var array
30
     */
31
    protected array $blindedConfigurationOptions = [
32
        'TYPO3_CONF_VARS' => [
33
            'DB' => [
34
                'database' => '******',
35
                'host' => '******',
36
                'password' => '******',
37
                'port' => '******',
38
                'socket' => '******',
39
                'username' => '******',
40
                'Connections' => [
41
                    'Default' => [
42
                        'dbname' => '******',
43
                        'host' => '******',
44
                        'password' => '******',
45
                        'port' => '******',
46
                        'user' => '******',
47
                        'unix_socket' => '******',
48
                    ],
49
                ],
50
            ],
51
            'SYS' => [
52
                'encryptionKey' => '******'
53
            ],
54
        ],
55
    ];
56
57
    /**
58
     * The $GLOBALS key to be processed
59
     *
60
     * @var string
61
     */
62
    protected string $globalVariableKey;
63
64
    public function __invoke(array $attributes): self
65
    {
66
        parent::__invoke($attributes);
67
68
        if (!($attributes['globalVariableKey'] ?? false)) {
69
            throw new \RuntimeException('Attribute \'globalVariableKey\' must be set to use ' . __CLASS__, 1606478088);
70
        }
71
72
        $this->globalVariableKey = $attributes['globalVariableKey'];
73
        return $this;
74
    }
75
76
    public function getConfiguration(): array
77
    {
78
        $configurationArray = $GLOBALS[$this->globalVariableKey] ?? [];
79
        $blindedConfigurationOptions = $this->blindedConfigurationOptions;
80
81
        // Hook for Processing blindedConfigurationOptions
82
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][ConfigurationController::class]['modifyBlindedConfigurationOptions'] ?? [] as $classReference) {
83
            $processingObject = GeneralUtility::makeInstance($classReference);
84
            $blindedConfigurationOptions = $processingObject->modifyBlindedConfigurationOptions($blindedConfigurationOptions, $this);
85
        }
86
87
        if (isset($blindedConfigurationOptions[$this->globalVariableKey])) {
88
            // Prepare blinding for all database connection types
89
            foreach (array_keys($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']) as $connectionName) {
90
                if ($connectionName !== 'Default') {
91
                    $blindedConfigurationOptions['TYPO3_CONF_VARS']['DB']['Connections'][$connectionName] =
92
                        $blindedConfigurationOptions['TYPO3_CONF_VARS']['DB']['Connections']['Default'];
93
                }
94
            }
95
            ArrayUtility::mergeRecursiveWithOverrule(
96
                $configurationArray,
97
                ArrayUtility::intersectRecursive($blindedConfigurationOptions[$this->globalVariableKey], $configurationArray)
98
            );
99
        }
100
        ArrayUtility::naturalKeySortRecursive($configurationArray);
101
        return $configurationArray;
102
    }
103
}
104