Skip to content

Commit adf1d5c

Browse files
authored
Improve collectors, adjust name of keys (#114)
1 parent 93b043c commit adf1d5c

13 files changed

+126
-34
lines changed

CHANGELOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22

33
## 1.0.0 under development
44

5-
- Bug: Fixed `beforeAction($action)` annotations to avoid errors (pgaultier)
65
- Initial release.

src/Collector/ConsoleAppInfoCollector.php

+25-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Yiisoft\Yii\Debug\Collector;
66

7+
use JetBrains\PhpStorm\ArrayShape;
8+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
9+
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
710
use Yiisoft\Yii\Console\Event\ApplicationShutdown;
811
use Yiisoft\Yii\Console\Event\ApplicationStartup;
912

@@ -16,12 +19,23 @@ final class ConsoleAppInfoCollector implements CollectorInterface, IndexCollecto
1619
private float $requestProcessingTimeStarted = 0;
1720
private float $requestProcessingTimeStopped = 0;
1821

22+
#[ArrayShape([
23+
'applicationProcessingTime' => 'float|int',
24+
'applicationPreload' => 'float|int',
25+
'requestProcessingTime' => 'float|int',
26+
'applicationEmit' => 'float|int',
27+
'memoryPeakUsage' => 'int',
28+
'memoryUsage' => 'int',
29+
])]
1930
public function getCollected(): array
2031
{
2132
return [
22-
'application_processing_time' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted,
23-
'memory_peak_usage' => memory_get_peak_usage(),
24-
'memory_usage' => memory_get_usage(),
33+
'applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted,
34+
'applicationPreload' => $this->requestProcessingTimeStarted - $this->applicationProcessingTimeStarted,
35+
'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
36+
'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped,
37+
'memoryPeakUsage' => memory_get_peak_usage(),
38+
'memoryUsage' => memory_get_usage(),
2539
];
2640
}
2741

@@ -33,17 +47,23 @@ public function collect(object $event): void
3347

3448
if ($event instanceof ApplicationStartup) {
3549
$this->applicationProcessingTimeStarted = microtime(true);
50+
} elseif ($event instanceof ConsoleCommandEvent) {
51+
$this->requestProcessingTimeStarted = microtime(true);
52+
} elseif ($event instanceof ConsoleTerminateEvent) {
53+
$this->requestProcessingTimeStopped = microtime(true);
3654
} elseif ($event instanceof ApplicationShutdown) {
3755
$this->applicationProcessingTimeStopped = microtime(true);
3856
}
3957
}
4058

59+
#[ArrayShape(['phpVersion' => 'string', 'time' => 'float|int', 'memory' => 'int', 'timestamp' => 'float|int'])]
4160
public function getIndexData(): array
4261
{
4362
return [
44-
'time' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted,
63+
'phpVersion' => PHP_VERSION,
64+
'time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
4565
'memory' => memory_get_peak_usage(),
46-
'timestamp' => $this->applicationProcessingTimeStarted,
66+
'timestamp' => $this->requestProcessingTimeStarted,
4767
];
4868
}
4969

src/Collector/EventCollector.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
namespace Yiisoft\Yii\Debug\Collector;
66

7+
use JetBrains\PhpStorm\ArrayShape;
78
use Yiisoft\Yii\Console\Event\ApplicationStartup as ConsoleApplicationStartup;
89
use Yiisoft\Yii\Http\Event\ApplicationStartup as HttpApplicationStartup;
910

1011
use function get_class;
1112

12-
final class EventCollector implements EventCollectorInterface
13+
final class EventCollector implements EventCollectorInterface, IndexCollectorInterface
1314
{
1415
use CollectorTrait;
1516

@@ -22,7 +23,11 @@ public function getCollected(): array
2223

2324
public function collect(object $event): void
2425
{
25-
if (!$event instanceof HttpApplicationStartup && !$event instanceof ConsoleApplicationStartup && !$this->isActive()) {
26+
if (
27+
!$event instanceof HttpApplicationStartup
28+
&& !$event instanceof ConsoleApplicationStartup
29+
&& !$this->isActive()
30+
) {
2631
return;
2732
}
2833

@@ -38,6 +43,14 @@ private function collectEvent(object $event): void
3843
];
3944
}
4045

46+
#[ArrayShape(['totalEvents' => 'int'])]
47+
public function getIndexData(): array
48+
{
49+
return [
50+
'totalEvents' => count($this->events),
51+
];
52+
}
53+
4154
private function reset(): void
4255
{
4356
$this->events = [];

src/Collector/LogCollector.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Yiisoft\Yii\Debug\Collector;
66

7-
final class LogCollector implements LogCollectorInterface
7+
use JetBrains\PhpStorm\ArrayShape;
8+
9+
final class LogCollector implements LogCollectorInterface, IndexCollectorInterface
810
{
911
use CollectorTrait;
1012

@@ -33,4 +35,12 @@ private function reset(): void
3335
{
3436
$this->messages = [];
3537
}
38+
39+
#[ArrayShape(['totalLogs' => 'int'])]
40+
public function getIndexData(): array
41+
{
42+
return [
43+
'totalLogs' => count($this->messages),
44+
];
45+
}
3646
}

src/Collector/MiddlewareCollector.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55
namespace Yiisoft\Yii\Debug\Collector;
66

7+
use JetBrains\PhpStorm\ArrayShape;
78
use Yiisoft\Middleware\Dispatcher\Event\AfterMiddleware;
89
use Yiisoft\Middleware\Dispatcher\Event\BeforeMiddleware;
910

10-
final class MiddlewareCollector implements CollectorInterface
11+
final class MiddlewareCollector implements CollectorInterface, IndexCollectorInterface
1112
{
1213
use CollectorTrait;
1314

1415
private array $beforeStack = [];
1516
private array $afterStack = [];
1617

18+
#[ArrayShape(['beforeStack' => 'array', 'afterStack' => 'array'])]
1719
public function getCollected(): array
1820
{
1921
return [
@@ -51,4 +53,12 @@ private function reset(): void
5153
$this->beforeStack = [];
5254
$this->afterStack = [];
5355
}
56+
57+
#[ArrayShape(['totalMiddlewares' => 'int'])]
58+
public function getIndexData(): array
59+
{
60+
return [
61+
'totalMiddlewares' => count($this->beforeStack),
62+
];
63+
}
5464
}

src/Collector/RequestCollector.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Yii\Debug\Collector;
66

7+
use JetBrains\PhpStorm\ArrayShape;
78
use Yiisoft\Yii\Http\Event\AfterRequest;
89
use Yiisoft\Yii\Http\Event\BeforeRequest;
910

@@ -19,14 +20,21 @@ final class RequestCollector implements CollectorInterface, IndexCollectorInterf
1920
private ?string $userIp = null;
2021
private int $responseStatusCode = 200;
2122

23+
#[ArrayShape([
24+
'requestUrl' => 'string',
25+
'requestMethod' => 'string',
26+
'requestIsAjax' => 'bool',
27+
'userIp' => 'null|string',
28+
'responseStatusCode' => 'int',
29+
])]
2230
public function getCollected(): array
2331
{
2432
return [
25-
'request_url' => $this->requestUrl,
26-
'request_method' => $this->requestMethod,
27-
'request_is_ajax' => $this->requestIsAjax,
28-
'user_ip' => $this->userIp,
29-
'response_status_code' => $this->responseStatusCode,
33+
'requestUrl' => $this->requestUrl,
34+
'requestMethod' => $this->requestMethod,
35+
'requestIsAjax' => $this->requestIsAjax,
36+
'userIp' => $this->userIp,
37+
'responseStatusCode' => $this->responseStatusCode,
3038
];
3139
}
3240

@@ -49,6 +57,13 @@ public function collect(object $event): void
4957
}
5058
}
5159

60+
#[ArrayShape([
61+
'requestUrl' => 'string',
62+
'requestMethod' => 'string',
63+
'requestIsAjax' => 'bool',
64+
'userIp' => 'null|string',
65+
'responseStatusCode' => 'int',
66+
])]
5267
public function getIndexData(): array
5368
{
5469
return [

src/Collector/RouterCollector.php

+5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Yiisoft\Yii\Debug\Collector;
66

7+
use JetBrains\PhpStorm\ArrayShape;
78
use Psr\Container\ContainerInterface;
9+
use Yiisoft\Router\CurrentRoute;
810
use Yiisoft\Router\RouteCollectionInterface;
911

1012
final class RouterCollector implements RouterCollectorInterface, IndexCollectorInterface
@@ -41,10 +43,13 @@ public function getCollected(): array
4143
];
4244
}
4345

46+
#[ArrayShape(['routeMatchTime' => 'float|int', 'matchedRoute' => 'string'])]
4447
public function getIndexData(): array
4548
{
49+
$currentRoute = $this->container->has(CurrentRoute::class) ? $this->container->get(CurrentRoute::class) : null;
4650
return [
4751
'routeMatchTime' => $this->matchTime,
52+
'matchedRoute' => $currentRoute?->getName(),
4853
];
4954
}
5055
}

src/Collector/ServiceCollector.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Yiisoft\Yii\Debug\Collector;
66

7-
final class ServiceCollector implements ServiceCollectorInterface
7+
use JetBrains\PhpStorm\ArrayShape;
8+
9+
final class ServiceCollector implements ServiceCollectorInterface, IndexCollectorInterface
810
{
911
use CollectorTrait;
1012

@@ -43,6 +45,14 @@ public function collect(
4345
];
4446
}
4547

48+
#[ArrayShape(['totalServices' => 'int'])]
49+
public function getIndexData(): array
50+
{
51+
return [
52+
'totalServices' => count($this->items),
53+
];
54+
}
55+
4656
private function reset(): void
4757
{
4858
$this->items = [];

src/Collector/WebAppInfoCollector.php

+21-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Yiisoft\Yii\Debug\Collector;
66

7+
use JetBrains\PhpStorm\ArrayShape;
8+
use Yiisoft\Yii\Console\Event\ApplicationStartup;
79
use Yiisoft\Yii\Http\Event\AfterEmit;
810
use Yiisoft\Yii\Http\Event\AfterRequest;
911
use Yiisoft\Yii\Http\Event\BeforeRequest;
@@ -19,15 +21,23 @@ final class WebAppInfoCollector implements CollectorInterface, IndexCollectorInt
1921
private float $requestProcessingTimeStarted = 0;
2022
private float $requestProcessingTimeStopped = 0;
2123

24+
#[ArrayShape([
25+
'applicationProcessingTime' => 'float|int',
26+
'applicationPreload' => 'float|int',
27+
'requestProcessingTime' => 'float|int',
28+
'applicationEmit' => 'float|int',
29+
'memoryPeakUsage' => 'int',
30+
'memoryUsage' => 'int',
31+
])]
2232
public function getCollected(): array
2333
{
2434
return [
25-
'application_processing_time' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted,
26-
'application_preload' => $this->requestProcessingTimeStarted - $this->applicationProcessingTimeStarted,
27-
'request_processing_time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
28-
'application_emit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped,
29-
'memory_peak_usage' => memory_get_peak_usage(),
30-
'memory_usage' => memory_get_usage(),
35+
'applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted,
36+
'applicationPreload' => $this->requestProcessingTimeStarted - $this->applicationProcessingTimeStarted,
37+
'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
38+
'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped,
39+
'memoryPeakUsage' => memory_get_peak_usage(),
40+
'memoryUsage' => memory_get_usage(),
3141
];
3242
}
3343

@@ -37,22 +47,22 @@ public function collect(object $event): void
3747
return;
3848
}
3949

40-
if ($event instanceof BeforeRequest) {
50+
if ($event instanceof ApplicationStartup) {
51+
$this->applicationProcessingTimeStarted = microtime(true);
52+
} elseif ($event instanceof BeforeRequest) {
4153
$this->requestProcessingTimeStarted = microtime(true);
42-
$this->applicationProcessingTimeStarted = $event->getRequest()->getAttribute(
43-
'applicationStartTime',
44-
$this->requestProcessingTimeStarted
45-
);
4654
} elseif ($event instanceof AfterRequest) {
4755
$this->requestProcessingTimeStopped = microtime(true);
4856
} elseif ($event instanceof AfterEmit) {
4957
$this->applicationProcessingTimeStopped = microtime(true);
5058
}
5159
}
5260

61+
#[ArrayShape(['phpVersion' => 'string', 'time' => 'float|int', 'memory' => 'int', 'timestamp' => 'float|int'])]
5362
public function getIndexData(): array
5463
{
5564
return [
65+
'phpVersion' => PHP_VERSION,
5666
'time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
5767
'memory' => memory_get_peak_usage(),
5868
'timestamp' => $this->requestProcessingTimeStarted,

src/Event/ProxyMethodCallEvent.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class ProxyMethodCallEvent
1414

1515
public ?array $arguments;
1616

17-
public $result;
17+
public mixed $result;
1818

1919
public string $status;
2020

@@ -29,7 +29,7 @@ public function __construct(
2929
string $class,
3030
string $method,
3131
?array $arguments,
32-
$result,
32+
mixed $result,
3333
string $status,
3434
?object $error,
3535
float $timeStart,

tests/Collector/ConsoleAppInfoCollectorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ protected function checkCollectedData(CollectorInterface $collector): void
3232
parent::checkCollectedData($collector);
3333
$data = $collector->getCollected();
3434

35-
$this->assertGreaterThan(0.122, $data['application_processing_time']);
35+
$this->assertGreaterThan(0.122, $data['applicationProcessingTime']);
3636
}
3737
}

tests/Collector/RequestCollectorTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ protected function checkCollectedData(CollectorInterface $collector): void
4040
parent::checkCollectedData($collector);
4141
$data = $collector->getCollected();
4242

43-
$this->assertEquals('http://test.site/url', $data['request_url']);
44-
$this->assertEquals('GET', $data['request_method']);
45-
$this->assertEquals(200, $data['response_status_code']);
43+
$this->assertEquals('http://test.site/url', $data['requestUrl']);
44+
$this->assertEquals('GET', $data['requestMethod']);
45+
$this->assertEquals(200, $data['responseStatusCode']);
4646
}
4747
}

tests/Collector/WebAppInfoCollectorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ protected function checkCollectedData(CollectorInterface $collector): void
3636
parent::checkCollectedData($collector);
3737
$data = $collector->getCollected();
3838

39-
$this->assertGreaterThan(0.122, $data['request_processing_time']);
39+
$this->assertGreaterThan(0.122, $data['requestProcessingTime']);
4040
}
4141
}

0 commit comments

Comments
 (0)