/
ConfigurationService.php
136 lines (122 loc) · 4.35 KB
/
ConfigurationService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Adminpanel\Service;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Adminpanel\ModuleApi\ConfigurableInterface;
use TYPO3\CMS\Adminpanel\ModuleApi\OnSubmitActorInterface;
use TYPO3\CMS\Adminpanel\ModuleApi\SubmoduleProviderInterface;
use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\SingletonInterface;
/**
* Admin Panel Service Class for Configuration Handling
*
* Scope: User TSConfig + Backend User UC
*/
class ConfigurationService implements SingletonInterface
{
/**
* @var array
*/
protected $mainConfiguration;
public function __construct()
{
$this->mainConfiguration = $this->getBackendUser()->getTSConfig()['admPanel.'] ?? [];
}
/**
* Get MainConfiguration (User TSConfig admPanel)
*
* @return array
*/
public function getMainConfiguration(): array
{
return $this->mainConfiguration;
}
/**
* Helper method to return configuration options
* Checks User TSConfig overrides and current backend user session
*
* @param string $identifier
* @param string $option
* @return string
*/
public function getConfigurationOption(string $identifier, string $option): string
{
if ($identifier === '' || $option === '') {
throw new \InvalidArgumentException('Identifier and option may not be empty', 1532861423);
}
if (isset($this->mainConfiguration['override.'][$identifier . '.'][$option])) {
$returnValue = $this->mainConfiguration['override.'][$identifier . '.'][$option];
} else {
$returnValue = $this->getBackendUser()->uc['AdminPanel'][$identifier . '_' . $option] ?? '';
}
return (string)$returnValue;
}
/**
* Save admin panel configuration to backend user UC
* triggers onSubmit method of modules to enable each module
* to enhance the save action
*
* @param \TYPO3\CMS\Adminpanel\ModuleApi\ModuleInterface[] $modules
* @param ServerRequestInterface $request
*/
public function saveConfiguration(array $modules, ServerRequestInterface $request): void
{
$configurationToSave = $request->getParsedBody()['TSFE_ADMIN_PANEL'] ?? [];
$beUser = $this->getBackendUser();
$this->triggerOnSubmitActors($modules, $request, $configurationToSave);
$existingConfiguration = $beUser->uc['AdminPanel'] ?? [];
$existingConfiguration = is_array($existingConfiguration) ? $existingConfiguration : [];
// Settings
$beUser->uc['AdminPanel'] = array_merge($existingConfiguration, $configurationToSave);
unset($beUser->uc['AdminPanel']['action']);
// Saving
$beUser->writeUC();
}
/**
* Returns the current BE user.
*
* @return BackendUserAuthentication|FrontendBackendUserAuthentication
*/
protected function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
/**
* @param array $modules
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param array $configurationToSave
*/
protected function triggerOnSubmitActors(
array $modules,
ServerRequestInterface $request,
$configurationToSave
): void {
foreach ($modules as $module) {
if (
$module instanceof OnSubmitActorInterface
&& (
($module instanceof ConfigurableInterface && $module->isEnabled())
|| !($module instanceof ConfigurableInterface)
)
) {
$module->onSubmit($configurationToSave, $request);
}
if ($module instanceof SubmoduleProviderInterface) {
$this->triggerOnSubmitActors($module->getSubModules(), $request, $configurationToSave);
}
}
}
}