From 1231526cf85d28ff01cd5e1f2c2b025ad1910144 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Wed, 26 Jun 2024 10:01:25 +0300 Subject: [PATCH 1/9] Add opcache page --- config/routes.php | 8 ++++++ .../Controller/OpcacheController.php | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/Inspector/Controller/OpcacheController.php diff --git a/config/routes.php b/config/routes.php index 95323cf..68cc336 100644 --- a/config/routes.php +++ b/config/routes.php @@ -15,6 +15,7 @@ use Yiisoft\Yii\Debug\Api\Inspector\Controller\ComposerController; use Yiisoft\Yii\Debug\Api\Inspector\Controller\GitController; use Yiisoft\Yii\Debug\Api\Inspector\Controller\InspectController; +use Yiisoft\Yii\Debug\Api\Inspector\Controller\OpcacheController; use Yiisoft\Yii\Middleware\CorsAllowAll; use Yiisoft\Yii\Middleware\IpFilter; @@ -171,5 +172,12 @@ static function (ResponseFactoryInterface $responseFactory, ValidatorInterface $ ->action([CacheController::class, 'clear']) ->name('/clear'), ), + Group::create('/opcache') + ->namePrefix('opcache') + ->routes( + Route::get('[/]') + ->action([OpcacheController::class, 'index']) + ->name('/index'), + ), ), ]; diff --git a/src/Inspector/Controller/OpcacheController.php b/src/Inspector/Controller/OpcacheController.php new file mode 100644 index 0000000..dbfa68a --- /dev/null +++ b/src/Inspector/Controller/OpcacheController.php @@ -0,0 +1,26 @@ +responseFactory->createResponse([ + 'status' => opcache_get_status(true), + 'configuration' => opcache_get_configuration(), + ]); + } +} From 426f0b0c3da5ae09aebe48b9e7cc69902ef97e7d Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 6 Jul 2024 21:01:51 +0300 Subject: [PATCH 2/9] Check if opcache is enabled --- src/Inspector/Controller/OpcacheController.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Inspector/Controller/OpcacheController.php b/src/Inspector/Controller/OpcacheController.php index dbfa68a..c2c2732 100644 --- a/src/Inspector/Controller/OpcacheController.php +++ b/src/Inspector/Controller/OpcacheController.php @@ -6,8 +6,7 @@ use Psr\Http\Message\ResponseInterface; use Yiisoft\DataResponse\DataResponseFactoryInterface; - -use function Safe\opcache_get_status; +use Yiisoft\Http\Status; final class OpcacheController { @@ -18,9 +17,15 @@ public function __construct( public function index(): ResponseInterface { + if (!\function_exists('opcache_get_status') || ($status = \opcache_get_status(true)) === false) { + return $this->responseFactory->createResponse([ + 'message' => 'OPcache is not installed or configured', + ], Status::UNPROCESSABLE_ENTITY); + } + return $this->responseFactory->createResponse([ - 'status' => opcache_get_status(true), - 'configuration' => opcache_get_configuration(), + 'status' => $status, + 'configuration' => \opcache_get_configuration(), ]); } } From 132bc1699ce9a8d22b039a8a31cc78f1ff071601 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 6 Jul 2024 21:06:39 +0300 Subject: [PATCH 3/9] Skip optional dependency --- composer-require-checker.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer-require-checker.json b/composer-require-checker.json index 0528b49..cbb5833 100644 --- a/composer-require-checker.json +++ b/composer-require-checker.json @@ -27,6 +27,8 @@ "Yiisoft\\Db\\Query\\Query", "Yiisoft\\Yii\\Debug\\Collector\\RequestCollector", "posix_getgrgid", - "posix_getpwuid" + "posix_getpwuid", + "opcache_get_status", + "opcache_get_configuration" ] } From 4691616a17da131effaa513fc66b231dae3c0378 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 6 Jul 2024 21:08:02 +0300 Subject: [PATCH 4/9] Fix return value --- src/ServerSentEventsStream.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ServerSentEventsStream.php b/src/ServerSentEventsStream.php index 5cecdce..9a2aaf3 100644 --- a/src/ServerSentEventsStream.php +++ b/src/ServerSentEventsStream.php @@ -27,9 +27,9 @@ public function detach(): void $this->eof = true; } - public function getSize() + public function getSize(): int { - return null; + return 0; } public function tell(): int From 13fb6d699ce569cfa9f08388f95ed24367be8604 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 6 Jul 2024 21:08:22 +0300 Subject: [PATCH 5/9] Fix return value --- src/ServerSentEventsStream.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ServerSentEventsStream.php b/src/ServerSentEventsStream.php index 9a2aaf3..8956b11 100644 --- a/src/ServerSentEventsStream.php +++ b/src/ServerSentEventsStream.php @@ -62,7 +62,7 @@ public function isWritable(): bool return false; } - public function write($string): void + public function write($string): int { throw new \RuntimeException('Stream is not writable'); } From 73fb5f62644e4769519c3189dbe47652c86e95e2 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 6 Jul 2024 21:09:14 +0300 Subject: [PATCH 6/9] Skip optional dependency --- composer-require-checker.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer-require-checker.json b/composer-require-checker.json index cbb5833..95cc108 100644 --- a/composer-require-checker.json +++ b/composer-require-checker.json @@ -26,6 +26,7 @@ "Yiisoft\\Db\\Connection\\ConnectionInterface", "Yiisoft\\Db\\Query\\Query", "Yiisoft\\Yii\\Debug\\Collector\\RequestCollector", + "Yiisoft\\Yii\\Http\\Application", "posix_getgrgid", "posix_getpwuid", "opcache_get_status", From e55726a999f28e83f15bcd42bda8f024f97e6907 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 6 Jul 2024 21:14:07 +0300 Subject: [PATCH 7/9] Check if class exists --- src/Debug/Provider/DebugApiProvider.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Debug/Provider/DebugApiProvider.php b/src/Debug/Provider/DebugApiProvider.php index 7de2b6a..232b230 100644 --- a/src/Debug/Provider/DebugApiProvider.php +++ b/src/Debug/Provider/DebugApiProvider.php @@ -23,7 +23,7 @@ public function getDefinitions(): array public function getExtensions(): array { - return [ + $extensions = [ RouteCollectorInterface::class => static function ( ContainerInterface $container, RouteCollectorInterface $routeCollector @@ -36,12 +36,18 @@ public function getExtensions(): array return $routeCollector; }, - Application::class => static function (ContainerInterface $container, Application $application) { + ]; + if (class_exists(Application::class)) { + $extensions[Application::class] = static function ( + ContainerInterface $container, + Application $application + ) { $applicationWrapper = $container->get(HttpApplicationWrapper::class); $applicationWrapper->wrap($application); return $application; - }, - ]; + }; + } + return $extensions; } } From 24faa51f67f2680d15189890340f788b3f0b96ee Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 6 Jul 2024 21:16:42 +0300 Subject: [PATCH 8/9] Suppress psalm --- src/Debug/Http/HttpApplicationWrapper.php | 3 +++ src/Debug/Provider/DebugApiProvider.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/Debug/Http/HttpApplicationWrapper.php b/src/Debug/Http/HttpApplicationWrapper.php index 93bd7ae..c2fada8 100644 --- a/src/Debug/Http/HttpApplicationWrapper.php +++ b/src/Debug/Http/HttpApplicationWrapper.php @@ -17,6 +17,9 @@ public function __construct( ) { } + /** + * @psalm-suppress UndefinedClass + */ public function wrap(Application $application): void { $middlewareDispatcher = $this->middlewareDispatcher; diff --git a/src/Debug/Provider/DebugApiProvider.php b/src/Debug/Provider/DebugApiProvider.php index 232b230..7c7f1dd 100644 --- a/src/Debug/Provider/DebugApiProvider.php +++ b/src/Debug/Provider/DebugApiProvider.php @@ -37,6 +37,9 @@ public function getExtensions(): array return $routeCollector; }, ]; + /** + * @psalm-suppress UndefinedClass + */ if (class_exists(Application::class)) { $extensions[Application::class] = static function ( ContainerInterface $container, From a8055902e888be205f0cabfd32e5cd49f83b5d60 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 6 Jul 2024 21:29:25 +0300 Subject: [PATCH 9/9] Fix psalm --- src/Debug/Http/HttpApplicationWrapper.php | 16 +++++++++------- src/Debug/Provider/DebugApiProvider.php | 8 ++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Debug/Http/HttpApplicationWrapper.php b/src/Debug/Http/HttpApplicationWrapper.php index c2fada8..3392001 100644 --- a/src/Debug/Http/HttpApplicationWrapper.php +++ b/src/Debug/Http/HttpApplicationWrapper.php @@ -25,15 +25,17 @@ public function wrap(Application $application): void $middlewareDispatcher = $this->middlewareDispatcher; $middlewareDefinitions = $this->middlewareDefinitions; - $closure = Closure::bind(static function (Application $application) use ( - $middlewareDispatcher, - $middlewareDefinitions, - ) { - $application->dispatcher = $middlewareDispatcher->withMiddlewares([ + $closure = Closure::bind( + /** + * @psalm-suppress UndefinedClass + */ + static fn (Application $application) => $application->dispatcher = $middlewareDispatcher->withMiddlewares([ ...$middlewareDefinitions, ['class' => MiddlewareDispatcherMiddleware::class, '$middlewareDispatcher' => $application->dispatcher], - ]); - }, null, $application); + ]), + null, + $application + ); $closure($application); } diff --git a/src/Debug/Provider/DebugApiProvider.php b/src/Debug/Provider/DebugApiProvider.php index 7c7f1dd..ec3a5ec 100644 --- a/src/Debug/Provider/DebugApiProvider.php +++ b/src/Debug/Provider/DebugApiProvider.php @@ -21,6 +21,9 @@ public function getDefinitions(): array return []; } + /** + * @psalm-suppress UndefinedClass + */ public function getExtensions(): array { $extensions = [ @@ -37,14 +40,11 @@ public function getExtensions(): array return $routeCollector; }, ]; - /** - * @psalm-suppress UndefinedClass - */ if (class_exists(Application::class)) { $extensions[Application::class] = static function ( ContainerInterface $container, Application $application - ) { + ): Application { $applicationWrapper = $container->get(HttpApplicationWrapper::class); $applicationWrapper->wrap($application);