Completed
Branch master (d17104)
by Christian
21:20
created

TypoScriptWaterfall::getIdentifier()   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
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Adminpanel\Modules\TsDebug;
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 Psr\Http\Message\ServerRequestInterface;
20
use TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule;
21
use TYPO3\CMS\Adminpanel\ModuleApi\ContentProviderInterface;
22
use TYPO3\CMS\Adminpanel\ModuleApi\InitializableInterface;
23
use TYPO3\CMS\Adminpanel\ModuleApi\ModuleData;
24
use TYPO3\CMS\Adminpanel\ModuleApi\ModuleSettingsProviderInterface;
25
use TYPO3\CMS\Adminpanel\Service\ConfigurationService;
26
use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
27
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
28
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Fluid\View\StandaloneView;
31
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
32
33
/**
34
 * Class TypoScriptWaterfall
35
 *
36
 * @internal
37
 */
38
class TypoScriptWaterfall extends AbstractSubModule implements InitializableInterface, ContentProviderInterface, ModuleSettingsProviderInterface
39
{
40
    /**
41
     * @var ConfigurationService
42
     */
43
    protected $configurationService;
44
45
    public function __construct()
46
    {
47
        $this->configurationService = GeneralUtility::makeInstance(ConfigurationService::class);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function getIdentifier(): string
54
    {
55
        return 'typoscript-waterfall';
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function getLabel(): string
62
    {
63
        return $this->getLanguageService()->sL(
64
            'LLL:EXT:adminpanel/Resources/Private/Language/locallang_tsdebug.xlf:sub.waterfall.label'
65
        );
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function initializeModule(ServerRequestInterface $request): void
72
    {
73
        $typoScriptFrontend = $this->getTypoScriptFrontendController();
74
        $typoScriptFrontend->forceTemplateParsing = $this->getConfigurationOption(
75
            'forceTemplateParsing'
76
        );
77
        if ($typoScriptFrontend->forceTemplateParsing) {
78
            $typoScriptFrontend->set_no_cache('Admin Panel: Force template parsing', true);
79
        }
80
        $this->getTimeTracker()->LR = (bool)$this->getConfigurationOption('LR');
81
    }
82
83
    /**
84
     * Creates the content for the "tsdebug" section ("module") of the Admin Panel
85
     *
86
     * @param ModuleData $data
87
     * @return string HTML
88
     */
89
    public function getContent(ModuleData $data): string
90
    {
91
        $view = GeneralUtility::makeInstance(StandaloneView::class);
92
        $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/TsDebug/TypoScript.html';
93
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
94
        $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
95
96
        $view->assignMultiple(
97
            [
98
                'tree' => (int)$this->getConfigurationOption('tree'),
99
                'display' => [
100
                    'times' => (int)$this->getConfigurationOption('displayTimes'),
101
                    'messages' => (int)$this->getConfigurationOption('displayMessages'),
102
                    'content' => (int)$this->getConfigurationOption('displayContent'),
103
                ],
104
                'trackContentRendering' => (int)$this->getConfigurationOption('LR'),
105
                'forceTemplateParsing' => (int)$this->getConfigurationOption('forceTemplateParsing'),
106
                'typoScriptLog' => $this->renderTypoScriptLog(),
107
            ]
108
        );
109
110
        return $view->render();
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function getSettings(): string
117
    {
118
        $view = GeneralUtility::makeInstance(StandaloneView::class);
119
        $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/TsDebug/TypoScriptSettings.html';
120
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
121
        $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
122
123
        $view->assignMultiple(
124
            [
125
                'tree' => (int)$this->getConfigurationOption('tree'),
126
                'display' => [
127
                    'times' => (int)$this->getConfigurationOption('displayTimes'),
128
                    'messages' => (int)$this->getConfigurationOption('displayMessages'),
129
                    'content' => (int)$this->getConfigurationOption('displayContent'),
130
                ],
131
                'trackContentRendering' => (int)$this->getConfigurationOption('LR'),
132
                'forceTemplateParsing' => (int)$this->getConfigurationOption('forceTemplateParsing')
133
            ]
134
        );
135
136
        return $view->render();
137
    }
138
139
    /**
140
     * Returns the current BE user.
141
     *
142
     * @return BackendUserAuthentication|FrontendBackendUserAuthentication
143
     */
144
    protected function getBackendUser(): BackendUserAuthentication
145
    {
146
        return $GLOBALS['BE_USER'];
147
    }
148
149
    /**
150
     * @param string $option
151
     * @return bool
152
     */
153
    protected function getConfigurationOption(string $option): bool
154
    {
155
        return (bool)$this->configurationService->getConfigurationOption('tsdebug', $option);
156
    }
157
158
    /**
159
     * Renders the TypoScript log as string
160
     *
161
     * @return string
162
     */
163
    protected function renderTypoScriptLog(): string
164
    {
165
        $timeTracker = $this->getTimeTracker();
166
        $timeTracker->printConf['flag_tree'] = $this->getConfigurationOption('tree');
167
        $timeTracker->printConf['allTime'] = $this->getConfigurationOption(
168
            'displayTimes'
169
        );
170
        $timeTracker->printConf['flag_messages'] = $this->getConfigurationOption(
171
            'displayMessages'
172
        );
173
        $timeTracker->printConf['flag_content'] = $this->getConfigurationOption(
174
            'displayContent'
175
        );
176
        return $timeTracker->printTSlog();
177
    }
178
179
    /**
180
     * @return TimeTracker
181
     */
182
    protected function getTimeTracker(): TimeTracker
183
    {
184
        return GeneralUtility::makeInstance(TimeTracker::class);
185
    }
186
187
    /**
188
     * @return TypoScriptFrontendController
189
     */
190
    protected function getTypoScriptFrontendController(): TypoScriptFrontendController
191
    {
192
        return $GLOBALS['TSFE'];
193
    }
194
}
195