diff --git a/Classes/Utility/TimingUtility.php b/Classes/Utility/TimingUtility.php index 05814b6..e00367a 100644 --- a/Classes/Utility/TimingUtility.php +++ b/Classes/Utility/TimingUtility.php @@ -22,6 +22,8 @@ final class TimingUtility implements SingletonInterface { public const MAX_SINGLE_HEADER_SIZE = 2 ** 12; + private static ?TimingUtility $instance = null; + private bool $registered = false; /** @var bool */ @@ -41,7 +43,7 @@ public function __construct(private readonly RegisterShutdownFunctionInterface $ public static function getInstance(): TimingUtility { - return GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); + return static::$instance ??= GeneralUtility::makeInstance(TimingUtility::class, new RegisterShutdownFunction(), new ConfigService()); } /** @var StopWatch[] */ diff --git a/Tests/TimingUtilityTest.php b/Tests/TimingUtilityTest.php index dabdcd4..04f5176 100644 --- a/Tests/TimingUtilityTest.php +++ b/Tests/TimingUtilityTest.php @@ -48,6 +48,10 @@ protected function tearDown(): void public function getInstance(): void { self::assertInstanceOf(TimingUtility::class, $firstCall = TimingUtility::getInstance()); + // even if we clear GeneralUtility, the instance should be the same + // we measure so erly in TYPO3s bootstrap, that GeneralUtility singletons are cleared after our first measurement + // but we need to be sure, that the instance is not recreated as that will run the shutdown function again, and that is not good! + GeneralUtility::resetSingletonInstances([]); self::assertSame($firstCall, TimingUtility::getInstance()); } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index bffe951..bd8c450 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -789,7 +789,7 @@ parameters: path: Tests/TimingUtilityTest.php - - message: '#^Method Kanti\\ServerTiming\\Service\\SentryServiceInterface@anonymous/Tests/TimingUtilityTest\.php\:305\:\:sendSentryTrace\(\) never returns null so it can be removed from the return type\.$#' + message: '#^Method Kanti\\ServerTiming\\Service\\SentryServiceInterface@anonymous/Tests/TimingUtilityTest\.php\:[0-9]+\:\:sendSentryTrace\(\) never returns null so it can be removed from the return type\.$#' identifier: return.unusedType count: 1 path: Tests/TimingUtilityTest.php