From 1353a44acc90c4a5e6d783f072e21bc451bd81aa Mon Sep 17 00:00:00 2001 From: Dmitriy Derepko Date: Sat, 6 Jul 2024 21:32:56 +0300 Subject: [PATCH] Add opcache inspector controller (#147) * Add opcache page * Check if opcache is enabled * Skip optional dependency * Fix return value * Fix return value * Skip optional dependency * Check if class exists * Suppress psalm * Fix psalm --- composer-require-checker.json | 5 ++- config/routes.php | 8 +++++ src/Debug/Http/HttpApplicationWrapper.php | 19 +++++++----- src/Debug/Provider/DebugApiProvider.php | 17 +++++++--- .../Controller/OpcacheController.php | 31 +++++++++++++++++++ src/ServerSentEventsStream.php | 6 ++-- 6 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 src/Inspector/Controller/OpcacheController.php diff --git a/composer-require-checker.json b/composer-require-checker.json index 0528b49..95cc108 100644 --- a/composer-require-checker.json +++ b/composer-require-checker.json @@ -26,7 +26,10 @@ "Yiisoft\\Db\\Connection\\ConnectionInterface", "Yiisoft\\Db\\Query\\Query", "Yiisoft\\Yii\\Debug\\Collector\\RequestCollector", + "Yiisoft\\Yii\\Http\\Application", "posix_getgrgid", - "posix_getpwuid" + "posix_getpwuid", + "opcache_get_status", + "opcache_get_configuration" ] } 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/Debug/Http/HttpApplicationWrapper.php b/src/Debug/Http/HttpApplicationWrapper.php index 93bd7ae..3392001 100644 --- a/src/Debug/Http/HttpApplicationWrapper.php +++ b/src/Debug/Http/HttpApplicationWrapper.php @@ -17,20 +17,25 @@ public function __construct( ) { } + /** + * @psalm-suppress UndefinedClass + */ 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 7de2b6a..ec3a5ec 100644 --- a/src/Debug/Provider/DebugApiProvider.php +++ b/src/Debug/Provider/DebugApiProvider.php @@ -21,9 +21,12 @@ public function getDefinitions(): array return []; } + /** + * @psalm-suppress UndefinedClass + */ public function getExtensions(): array { - return [ + $extensions = [ RouteCollectorInterface::class => static function ( ContainerInterface $container, RouteCollectorInterface $routeCollector @@ -36,12 +39,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 + ): Application { $applicationWrapper = $container->get(HttpApplicationWrapper::class); $applicationWrapper->wrap($application); return $application; - }, - ]; + }; + } + return $extensions; } } diff --git a/src/Inspector/Controller/OpcacheController.php b/src/Inspector/Controller/OpcacheController.php new file mode 100644 index 0000000..c2c2732 --- /dev/null +++ b/src/Inspector/Controller/OpcacheController.php @@ -0,0 +1,31 @@ +responseFactory->createResponse([ + 'message' => 'OPcache is not installed or configured', + ], Status::UNPROCESSABLE_ENTITY); + } + + return $this->responseFactory->createResponse([ + 'status' => $status, + 'configuration' => \opcache_get_configuration(), + ]); + } +} diff --git a/src/ServerSentEventsStream.php b/src/ServerSentEventsStream.php index 5cecdce..8956b11 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 @@ -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'); }