diff --git a/SubModules/ServiceInitializer/Middlewares/DebuggingMiddleware.php b/SubModules/ServiceInitializer/Middlewares/DebuggingMiddleware.php deleted file mode 100644 index df38d8d..0000000 --- a/SubModules/ServiceInitializer/Middlewares/DebuggingMiddleware.php +++ /dev/null @@ -1,226 +0,0 @@ -registered) { - $this->requestFloat = $request->getServerParams()['REQUEST_TIME_FLOAT']??null; - $this->registered = true; - $this->registerBenchmarkDebugBar(); - } - return $request; - } - - protected function registerBenchmarkDebugBar(): void - { - // do not run if cli - if (Consolidation::isCli()) { - return; - } - $container = $this->getContainer(); - $config = ContainerHelper::use(Config::class, $container); - $manager = ContainerHelper::use(ManagerInterface::class, $container); - if (!$config || !$manager) { - return; - } - if (!$config instanceof Config - || !($config = $config->get('environment')) instanceof Config - ) { - return; - } - if ($config->get('showPerformance') === true) { - $manager->attach( - 'response.final', - [$this, 'printPerformance'], - priority: PHP_INT_MAX - 5 - ); - } - - if ($config->get('profiling') !== true - || $config->get('debugBar') !== true - ) { - return; - } - - $this->darkMode = $config->get('debugBarDarkMode') === true; - $manager->attach( - 'response.final', - [$this, 'renderDebugBar'], - priority: PHP_INT_MAX - 100 - ); - } - - private function renderDebugBar($response) : mixed - { - $this->getManager()?->detach( - 'response.final', - [$this, 'renderDebugBar'] - ); - - if (!$response instanceof ResponseInterface) { - return $response; - } - - // if profiler disabled, stop here! - $profiler = ContainerHelper::use(ProfilerInterface::class, $this->getContainer()); - $waterfall = ContainerHelper::use(Waterfall::class, $this->getContainer()); - if (!$profiler?->isEnable() || ! $waterfall || !DataType::isHtmlContentType($response)) { - return $response; - } - - // DO STOP BENCHMARK - $benchmark = ($profiler->getGroup('response') - ??$profiler->getGroup('manager')) - ?->get('response.final'); - $benchmark?->stop([ - 'duration' => $benchmark->convertMicrotime(microtime(true)) - $benchmark->getStartTime(), - 'stopped' => true - ]); - $benchmark = $profiler - ->getGroup('httpKernel') - ?->get('httpKernel.dispatch'); - $benchmark?->stop([ - 'duration' => $benchmark->convertMicrotime(microtime(true)) - $benchmark->getStartTime() - ]); - $benchmark?->setMetadataRecord('stopped', true); - // END BENCHMARK - $startTime = ( - $this->requestFloat - ??$_SERVER['REQUEST_TIME_FLOAT'] - ??$profiler->getStartTime() - ); - // get origin performance - $performanceOrigin = microtime(true) - $startTime; - $memoryOrigin = Consolidation::sizeFormat(memory_get_usage(), 3); - - // start - $body = (string) $response->getBody(); - $streamFactory = $this->getStreamFactory(); - $darkMode = $this->darkMode; - $found = false; - if (str_contains($body, '')) { - $found = true; - $body = preg_replace( - '~~', - $waterfall->render(darkMode: $darkMode), - $body, - 1 - ); - } else { - $regexes = [ - '~(<(body)[^>]*>.*)(\s*\s*(?:<\!\-\-.*)?)$~ism', - '~(<(head)[^>]*>.*)(\s*)~ism', - '~(<(html)[^>]*>.*)(\s*(?:<\!\-\-.*)?)~ism', - ]; - foreach ($regexes as $regex) { - if (!preg_match($regex, $body)) { - continue; - } - $body = preg_replace_callback( - $regex, - static function ($match) use ($waterfall, $darkMode) { - return $match[1] . "\n" . $waterfall->render(darkMode: $darkMode) . "\n" . $match[3]; - }, - $body - ); - $found = true; - break; - } - } - - if (!$found) { - $body .= "\n".$waterfall->render(darkMode: $darkMode)."\n"; - } - // stop - $waterfall = null; - unset($waterfall); - - // doing clear - $profiler->clear(); - $performanceEnd = microtime(true) - $startTime; - return $response->withBody( - $streamFactory->createStream( - $body - .sprintf( - '%s', - "\n", - $profiler->convertMicrotime($performanceOrigin), - $memoryOrigin, - $profiler->convertMicrotime($performanceEnd), - Consolidation::sizeFormat(memory_get_usage(), 3), - ) - ) - ); - } - - private function printPerformance(ResponseInterface $response): ResponseInterface - { - $this->getManager()?->detach( - 'response.final', - [$this, 'printPerformance'] - ); - if (!DataType::isHtmlContentType($response) - || ! $response->getBody()->isWritable() - ) { - return $response; - } - try { - $response->getBody()->seek($response->getBody()->getSize()); - } catch (Throwable) { - // pass - } - $startTime = ( - $this->requestFloat - ??$_SERVER['REQUEST_TIME_FLOAT'] - ??$_SERVER['REQUEST_TIME'] - ); - $str = sprintf( - '%s', - "\n", - sprintf( - '(time: %s ms / peak memory usage: %s / memory usage: %s)', - round( - (microtime(true) * 1000 - $startTime * 1000), - 4 - ), - Consolidation::sizeFormat(memory_get_peak_usage(), 3), - Consolidation::sizeFormat(memory_get_usage(), 3) - ) - ); - $response->getBody()->write($str); - return $response; - } -} diff --git a/SubModules/ServiceInitializer/ServiceInitializer.php b/SubModules/ServiceInitializer/ServiceInitializer.php index cdc5de9..708b761 100644 --- a/SubModules/ServiceInitializer/ServiceInitializer.php +++ b/SubModules/ServiceInitializer/ServiceInitializer.php @@ -6,7 +6,6 @@ use ArrayAccess\TrayDigita\App\Modules\Core\Abstracts\CoreSubmoduleAbstract; use ArrayAccess\TrayDigita\App\Modules\Core\Entities\CacheItem; use ArrayAccess\TrayDigita\App\Modules\Core\Entities\LogItem; -use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\ServiceInitializer\Middlewares\DebuggingMiddleware; use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\ServiceInitializer\Middlewares\ErrorMiddleware; use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\ServiceInitializer\Middlewares\InitMiddleware; use ArrayAccess\TrayDigita\Collection\Config; @@ -16,6 +15,7 @@ use ArrayAccess\TrayDigita\Util\Filter\Consolidation; use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper; use Psr\Http\Server\MiddlewareInterface; +use Throwable; use function is_array; use function is_string; @@ -29,7 +29,6 @@ final class ServiceInitializer extends CoreSubmoduleAbstract * @var array> */ protected array $middlewares = [ - DebuggingMiddleware::class, ErrorMiddleware::class, InitMiddleware::class ]; @@ -104,8 +103,11 @@ private function doRegisterMiddlewares(): void return; } foreach ($this->middlewares as $middleware) { - $middleware = ContainerHelper::resolveCallable($middleware, $this->getContainer()); - $kernel->addMiddleware($middleware); + try { + $middleware = ContainerHelper::resolveCallable($middleware, $this->getContainer()); + $kernel->addMiddleware($middleware); + } catch (Throwable) { + } } }