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