‪TYPO3CMS  ‪main
TimeTrackerInitialization.php
Go to the documentation of this file.
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 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\MiddlewareInterface;
23 use Psr\Http\Server\RequestHandlerInterface;
27 
33 final class ‪TimeTrackerInitialization implements MiddlewareInterface
34 {
36 
37  public function ‪__construct(protected readonly ‪TimeTracker $timeTracker) {}
38 
42  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
43  {
44  $timeTrackingEnabled = $this->‪isBackendUserCookieSet($request);
45  $this->timeTracker->setEnabled($timeTrackingEnabled);
46  $this->timeTracker->start(microtime(true));
47  $this->timeTracker->push('');
48 
49  $response = $handler->handle($request);
50 
51  // Finish time tracking
52  $this->timeTracker->pull();
53  $this->timeTracker->finish();
54 
55  if ($this->‪isDebugModeEnabled()) {
56  return $response->withHeader('X-TYPO3-Parsetime', $this->timeTracker->getParseTime() . 'ms');
57  }
58  return $response;
59  }
60 
67  #[AsEventListener('typo3-frontend/timetracker-init-middleware')]
69  {
70  $typoScriptConfig = $event->‪getFrontendTypoScript()->getConfigArray();
71  if (!empty($typoScriptConfig['debug'] ?? false)) {
72  $this->isDebugEnabledInTypoScriptConfig = true;
73  }
74  }
75 
76  protected function ‪isBackendUserCookieSet(ServerRequestInterface $request): bool
77  {
78  $configuredCookieName = trim(‪$GLOBALS['TYPO3_CONF_VARS']['BE']['cookieName']) ?: 'be_typo_user';
79  return !empty($request->getCookieParams()[$configuredCookieName]);
80  }
81 
82  protected function ‪isDebugModeEnabled(): bool
83  {
84  if ($this->isDebugEnabledInTypoScriptConfig) {
85  return true;
86  }
87  return !empty(‪$GLOBALS['TYPO3_CONF_VARS']['FE']['debug']);
88  }
89 }
‪TYPO3\CMS\Frontend\Middleware\TimeTrackerInitialization\isBackendUserCookieSet
‪isBackendUserCookieSet(ServerRequestInterface $request)
Definition: TimeTrackerInitialization.php:76
‪TYPO3\CMS\Frontend\Middleware\TimeTrackerInitialization
Definition: TimeTrackerInitialization.php:34
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\Frontend\Middleware\TimeTrackerInitialization\isDebugModeEnabled
‪isDebugModeEnabled()
Definition: TimeTrackerInitialization.php:82
‪TYPO3\CMS\Frontend\Event\AfterTypoScriptDeterminedEvent\getFrontendTypoScript
‪getFrontendTypoScript()
Definition: AfterTypoScriptDeterminedEvent.php:46
‪TYPO3\CMS\Frontend\Middleware\TimeTrackerInitialization\__construct
‪__construct(protected readonly TimeTracker $timeTracker)
Definition: TimeTrackerInitialization.php:37
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:18
‪TYPO3\CMS\Frontend\Middleware\TimeTrackerInitialization\process
‪process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: TimeTrackerInitialization.php:42
‪TYPO3\CMS\Frontend\Middleware\TimeTrackerInitialization\typoScriptDeterminedListener
‪typoScriptDeterminedListener(AfterTypoScriptDeterminedEvent $event)
Definition: TimeTrackerInitialization.php:68
‪TYPO3\CMS\Frontend\Event\AfterTypoScriptDeterminedEvent
Definition: AfterTypoScriptDeterminedEvent.php:41
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Middleware\TimeTrackerInitialization\$isDebugEnabledInTypoScriptConfig
‪bool $isDebugEnabledInTypoScriptConfig
Definition: TimeTrackerInitialization.php:35
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:34