Skip to content

Commit

Permalink
Merge branch 'master' into mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz authored Oct 14, 2023
2 parents 49f28e9 + 8d79cc4 commit e1f3bda
Show file tree
Hide file tree
Showing 61 changed files with 1,490 additions and 197 deletions.
3 changes: 3 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"symbol-whitelist": [
"Yiisoft\\Definitions\\ArrayDefinition",
"Yiisoft\\Definitions\\CallableDefinition",
"Yiisoft\\Definitions\\ValueDefinition",
"Yiisoft\\Middleware\\Dispatcher\\Event\\BeforeMiddleware",
"Yiisoft\\Yii\\Console\\Event\\ApplicationShutdown",
"Yiisoft\\Yii\\Console\\Event\\ApplicationStartup",
Expand Down
18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@
"psr/http-message": "^1.0",
"psr/log": "^2.0|^3.0",
"symfony/console": "^5.4|^6.0",
"symfony/var-dumper": "^5.4",
"yiisoft/aliases": "^3.0",
"yiisoft/arrays": "^2.0|^3.0",
"yiisoft/config": "^1.3",
"yiisoft/di": "^1.0",
"yiisoft/files": "^2.0",
"yiisoft/json": "^1.0",
"yiisoft/mutex": "^1.1",
"yiisoft/profiler": "^3.0",
"yiisoft/proxy": "^1.0.1",
"yiisoft/strings": "^2.0",
"yiisoft/var-dumper": "^1.0"
"yiisoft/strings": "^2.2",
"yiisoft/var-dumper": "^1.7"
},
"require-dev": {
"ext-curl": "*",
"yiisoft/middleware-dispatcher": "^3.0|^4.0|^5.0",
"yiisoft/yii-console": "^2.0",
"yiisoft/yii-http": "^1.0",
"maglnet/composer-require-checker": "^4.2",
"nyholm/psr7": "^1.3",
"phpunit/phpunit": "^9.4",
Expand All @@ -54,7 +53,10 @@
"vimeo/psalm": "^4.30|^5.7",
"yiisoft/error-handler": "^3.0",
"yiisoft/event-dispatcher": "^1.0",
"yiisoft/log": "^2.0"
"yiisoft/log": "^2.0",
"yiisoft/middleware-dispatcher": "^3.0|^4.0|^5.0",
"yiisoft/yii-console": "^2.0",
"yiisoft/yii-http": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -76,6 +78,7 @@
},
"config-plugin": {
"params": "params.php",
"bootstrap": "bootstrap.php",
"di": "di.php",
"di-console": "di-console.php",
"di-web": "di-web.php",
Expand All @@ -94,7 +97,8 @@
"sort-packages": true,
"allow-plugins": {
"infection/extension-installer": true,
"composer/package-versions-deprecated": true
"composer/package-versions-deprecated": true,
"yiisoft/config": false
}
},
"scripts": {
Expand Down
29 changes: 29 additions & 0 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Yiisoft\VarDumper\VarDumper;
use Yiisoft\Yii\Debug\Collector\VarDumperCollector;
use Yiisoft\Yii\Debug\Collector\VarDumperHandlerInterfaceProxy;

/**
* @var $params array
*/

return [
static function ($container) use ($params) {
if (!($params['yiisoft/yii-debug']['enabled'] ?? false)) {
return;
}
if (!$container->has(VarDumperCollector::class)) {
return;
}

VarDumper::setDefaultHandler(
new VarDumperHandlerInterfaceProxy(
VarDumper::getDefaultHandler(),
$container->get(VarDumperCollector::class),
),
);
},
];
8 changes: 7 additions & 1 deletion config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@
},
FilesystemStreamCollector::class => [
'__construct()' => [
'ignoredPathPatterns' => [],
'ignoredPathPatterns' => [
/**
* Examples:
* - templates/
* - src/Directory/To/Ignore
*/
],
'ignoredClasses' => [
ClosureExporter::class,
UseStatementParser::class,
Expand Down
12 changes: 10 additions & 2 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
use Yiisoft\Yii\Debug\Collector\ServiceCollector;
use Yiisoft\Yii\Debug\Collector\Stream\FilesystemStreamCollector;
use Yiisoft\Yii\Debug\Collector\Stream\HttpStreamCollector;
use Yiisoft\Yii\Debug\Collector\TimelineCollector;
use Yiisoft\Yii\Debug\Collector\VarDumperCollector;
use Yiisoft\Yii\Debug\Collector\Web\MiddlewareCollector;
use Yiisoft\Yii\Debug\Collector\Web\RequestCollector;
use Yiisoft\Yii\Debug\Collector\Web\WebAppInfoCollector;
use Yiisoft\Yii\Debug\Command\ResetCommand;
use Yiisoft\Yii\Debug\Command\DebugContainerCommand;
use Yiisoft\Yii\Debug\Command\DebugEventsCommand;
use Yiisoft\Yii\Debug\Command\DebugResetCommand;

/**
* @var $params array
Expand All @@ -41,6 +45,8 @@
FilesystemStreamCollector::class,
HttpStreamCollector::class,
ExceptionCollector::class,
VarDumperCollector::class,
TimelineCollector::class,
],
'collectors.web' => [
WebAppInfoCollector::class,
Expand Down Expand Up @@ -87,7 +93,9 @@
],
'yiisoft/yii-console' => [
'commands' => [
'debug/reset' => ResetCommand::class,
DebugResetCommand::COMMAND_NAME => DebugResetCommand::class,
DebugContainerCommand::COMMAND_NAME => DebugContainerCommand::class,
DebugEventsCommand::COMMAND_NAME => DebugEventsCommand::class,
],
],
];
7 changes: 7 additions & 0 deletions src/Collector/Console/CommandCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput;
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
use Yiisoft\Yii\Debug\Collector\TimelineCollector;

final class CommandCollector implements SummaryCollectorInterface
{
Expand All @@ -24,6 +25,10 @@ final class CommandCollector implements SummaryCollectorInterface
private const UNDEFINED_EXIT_CODE = -1;
private array $commands = [];

public function __construct(private TimelineCollector $timelineCollector)
{
}

public function getCollected(): array
{
return $this->commands;
Expand All @@ -35,6 +40,8 @@ public function collect(ConsoleEvent|ConsoleErrorEvent|ConsoleTerminateEvent $ev
return;
}

$this->timelineCollector->collect($this, spl_object_id($event));

$command = $event->getCommand();

if ($event instanceof ConsoleErrorEvent) {
Expand Down
9 changes: 8 additions & 1 deletion src/Collector/Console/ConsoleAppInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Yiisoft\Yii\Console\Event\ApplicationStartup;
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
use Yiisoft\Yii\Debug\Collector\TimelineCollector;

final class ConsoleAppInfoCollector implements SummaryCollectorInterface
{
Expand All @@ -21,6 +22,10 @@ final class ConsoleAppInfoCollector implements SummaryCollectorInterface
private float $requestProcessingTimeStarted = 0;
private float $requestProcessingTimeStopped = 0;

public function __construct(private TimelineCollector $timelineCollector)
{
}

public function getCollected(): array
{
if (!$this->isActive()) {
Expand All @@ -38,7 +43,7 @@ public function getCollected(): array

public function collect(object $event): void
{
if (!is_object($event) || !$this->isActive()) {
if (!$this->isActive()) {
return;
}

Expand All @@ -58,6 +63,8 @@ public function collect(object $event): void
} elseif ($event instanceof ApplicationShutdown) {
$this->applicationProcessingTimeStopped = microtime(true);
}

$this->timelineCollector->collect($this, spl_object_id($event));
}

public function getSummary(): array
Expand Down
12 changes: 6 additions & 6 deletions src/Collector/EventCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
use Yiisoft\Yii\Console\Event\ApplicationStartup as ConsoleApplicationStartup;
use Yiisoft\Yii\Http\Event\ApplicationStartup as HttpApplicationStartup;

class EventCollector implements SummaryCollectorInterface
final class EventCollector implements SummaryCollectorInterface
{
use CollectorTrait;

private array $events = [];

public function __construct(private TimelineCollector $timelineCollector)
{
}

public function getCollected(): array
{
if (!$this->isActive()) {
Expand All @@ -32,18 +36,14 @@ public function collect(object $event, string $line): void
return;
}

$this->collectEvent($event, $line);
}

private function collectEvent(object $event, $line): void
{
$this->events[] = [
'name' => $event::class,
'event' => $event,
'file' => (new ReflectionClass($event))->getFileName(),
'line' => $line,
'time' => microtime(true),
];
$this->timelineCollector->collect($this, spl_object_id($event), $event::class);
}

public function getSummary(): array
Expand Down
11 changes: 11 additions & 0 deletions src/Collector/ExceptionCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ final class ExceptionCollector implements SummaryCollectorInterface

private ?Throwable $exception = null;

public function __construct(private TimelineCollector $timelineCollector)
{
}

public function getCollected(): array
{
if (!$this->isActive()) {
return [];
}
if ($this->exception === null) {
return [];
}
Expand All @@ -36,10 +43,14 @@ public function collect(ApplicationError $error): void
}

$this->exception = $error->getThrowable();
$this->timelineCollector->collect($this, $error::class);
}

public function getSummary(): array
{
if (!$this->isActive()) {
return [];
}
return [
'exception' => $this->exception === null ? [] : [
'class' => $this->exception::class,
Expand Down
55 changes: 33 additions & 22 deletions src/Collector/HttpClientCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,11 @@ final class HttpClientCollector implements SummaryCollectorInterface
*/
private array $requests = [];

public function getCollected(): array
public function __construct(private TimelineCollector $timelineCollector)
{
return array_merge(...array_values($this->requests));
}

public function getSummary(): array
{
return [
'http' => [
'count' => array_sum(array_map(static fn (array $requests) => count($requests), $this->requests)),
'totalTime' => array_sum(
array_merge(
...array_map(
static fn (array $entry) => array_column($entry, 'totalTime'),
array_values($this->requests)
)
)
),
],
];
}

public function collect(RequestInterface $request, float|string $startTime, string $line, ?string $uniqueId): void
public function collect(RequestInterface $request, float $startTime, string $line, ?string $uniqueId): void
{
if (!$this->isActive()) {
return;
Expand All @@ -66,9 +48,10 @@ public function collect(RequestInterface $request, float|string $startTime, stri
'headers' => $request->getHeaders(),
'line' => $line,
];
$this->timelineCollector->collect($this, $uniqueId);
}

public function collectTotalTime(?ResponseInterface $response, float|string $startTime, ?string $uniqueId): void
public function collectTotalTime(?ResponseInterface $response, float $endTime, ?string $uniqueId): void
{
if (!$this->isActive()) {
return;
Expand All @@ -83,7 +66,35 @@ public function collectTotalTime(?ResponseInterface $response, float|string $sta
$entry['responseStatus'] = $response->getStatusCode();
Message::rewindBody($response);
}
$entry['endTime'] = $startTime;
$entry['endTime'] = $endTime;
$entry['totalTime'] = $entry['endTime'] - $entry['startTime'];
}

public function getCollected(): array
{
if (!$this->isActive()) {
return [];
}
return array_merge(...array_values($this->requests));
}

public function getSummary(): array
{
if (!$this->isActive()) {
return [];
}
return [
'http' => [
'count' => array_sum(array_map(static fn (array $requests) => count($requests), $this->requests)),
'totalTime' => array_sum(
array_merge(
...array_map(
static fn (array $entry) => array_column($entry, 'totalTime'),
array_values($this->requests)
)
)
),
],
];
}
}
5 changes: 5 additions & 0 deletions src/Collector/LogCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class LogCollector implements SummaryCollectorInterface

private array $messages = [];

public function __construct(private TimelineCollector $timelineCollector, )
{
}

public function getCollected(): array
{
if (!$this->isActive()) {
Expand All @@ -31,6 +35,7 @@ public function collect(string $level, $message, array $context, string $line):
'context' => $context,
'line' => $line,
];
$this->timelineCollector->collect($this, count($this->messages));
}

private function reset(): void
Expand Down
5 changes: 5 additions & 0 deletions src/Collector/ServiceCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ final class ServiceCollector implements SummaryCollectorInterface

private array $items = [];

public function __construct(private TimelineCollector $timelineCollector)
{
}

public function getCollected(): array
{
if (!$this->isActive()) {
Expand Down Expand Up @@ -44,6 +48,7 @@ public function collect(
'timeStart' => $timeStart,
'timeEnd' => $timeEnd,
];
$this->timelineCollector->collect($this, count($this->items));
}

public function getSummary(): array
Expand Down
Loading

0 comments on commit e1f3bda

Please sign in to comment.