Skip to content

Commit 1353a44

Browse files
authored
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
1 parent a0a3f4e commit 1353a44

File tree

6 files changed

+71
-15
lines changed

6 files changed

+71
-15
lines changed

composer-require-checker.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
"Yiisoft\\Db\\Connection\\ConnectionInterface",
2727
"Yiisoft\\Db\\Query\\Query",
2828
"Yiisoft\\Yii\\Debug\\Collector\\RequestCollector",
29+
"Yiisoft\\Yii\\Http\\Application",
2930
"posix_getgrgid",
30-
"posix_getpwuid"
31+
"posix_getpwuid",
32+
"opcache_get_status",
33+
"opcache_get_configuration"
3134
]
3235
}

config/routes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Yiisoft\Yii\Debug\Api\Inspector\Controller\ComposerController;
1616
use Yiisoft\Yii\Debug\Api\Inspector\Controller\GitController;
1717
use Yiisoft\Yii\Debug\Api\Inspector\Controller\InspectController;
18+
use Yiisoft\Yii\Debug\Api\Inspector\Controller\OpcacheController;
1819
use Yiisoft\Yii\Middleware\CorsAllowAll;
1920
use Yiisoft\Yii\Middleware\IpFilter;
2021

@@ -171,5 +172,12 @@ static function (ResponseFactoryInterface $responseFactory, ValidatorInterface $
171172
->action([CacheController::class, 'clear'])
172173
->name('/clear'),
173174
),
175+
Group::create('/opcache')
176+
->namePrefix('opcache')
177+
->routes(
178+
Route::get('[/]')
179+
->action([OpcacheController::class, 'index'])
180+
->name('/index'),
181+
),
174182
),
175183
];

src/Debug/Http/HttpApplicationWrapper.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,25 @@ public function __construct(
1717
) {
1818
}
1919

20+
/**
21+
* @psalm-suppress UndefinedClass
22+
*/
2023
public function wrap(Application $application): void
2124
{
2225
$middlewareDispatcher = $this->middlewareDispatcher;
2326
$middlewareDefinitions = $this->middlewareDefinitions;
2427

25-
$closure = Closure::bind(static function (Application $application) use (
26-
$middlewareDispatcher,
27-
$middlewareDefinitions,
28-
) {
29-
$application->dispatcher = $middlewareDispatcher->withMiddlewares([
28+
$closure = Closure::bind(
29+
/**
30+
* @psalm-suppress UndefinedClass
31+
*/
32+
static fn (Application $application) => $application->dispatcher = $middlewareDispatcher->withMiddlewares([
3033
...$middlewareDefinitions,
3134
['class' => MiddlewareDispatcherMiddleware::class, '$middlewareDispatcher' => $application->dispatcher],
32-
]);
33-
}, null, $application);
35+
]),
36+
null,
37+
$application
38+
);
3439

3540
$closure($application);
3641
}

src/Debug/Provider/DebugApiProvider.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ public function getDefinitions(): array
2121
return [];
2222
}
2323

24+
/**
25+
* @psalm-suppress UndefinedClass
26+
*/
2427
public function getExtensions(): array
2528
{
26-
return [
29+
$extensions = [
2730
RouteCollectorInterface::class => static function (
2831
ContainerInterface $container,
2932
RouteCollectorInterface $routeCollector
@@ -36,12 +39,18 @@ public function getExtensions(): array
3639

3740
return $routeCollector;
3841
},
39-
Application::class => static function (ContainerInterface $container, Application $application) {
42+
];
43+
if (class_exists(Application::class)) {
44+
$extensions[Application::class] = static function (
45+
ContainerInterface $container,
46+
Application $application
47+
): Application {
4048
$applicationWrapper = $container->get(HttpApplicationWrapper::class);
4149
$applicationWrapper->wrap($application);
4250

4351
return $application;
44-
},
45-
];
52+
};
53+
}
54+
return $extensions;
4655
}
4756
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Yii\Debug\Api\Inspector\Controller;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Yiisoft\DataResponse\DataResponseFactoryInterface;
9+
use Yiisoft\Http\Status;
10+
11+
final class OpcacheController
12+
{
13+
public function __construct(
14+
private DataResponseFactoryInterface $responseFactory,
15+
) {
16+
}
17+
18+
public function index(): ResponseInterface
19+
{
20+
if (!\function_exists('opcache_get_status') || ($status = \opcache_get_status(true)) === false) {
21+
return $this->responseFactory->createResponse([
22+
'message' => 'OPcache is not installed or configured',
23+
], Status::UNPROCESSABLE_ENTITY);
24+
}
25+
26+
return $this->responseFactory->createResponse([
27+
'status' => $status,
28+
'configuration' => \opcache_get_configuration(),
29+
]);
30+
}
31+
}

src/ServerSentEventsStream.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public function detach(): void
2727
$this->eof = true;
2828
}
2929

30-
public function getSize()
30+
public function getSize(): int
3131
{
32-
return null;
32+
return 0;
3333
}
3434

3535
public function tell(): int
@@ -62,7 +62,7 @@ public function isWritable(): bool
6262
return false;
6363
}
6464

65-
public function write($string): void
65+
public function write($string): int
6666
{
6767
throw new \RuntimeException('Stream is not writable');
6868
}

0 commit comments

Comments
 (0)