Skip to content

Commit

Permalink
Add var dumper collector
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Aug 20, 2023
1 parent e16d64d commit 3f90d3f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
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),
),
);
},
];
2 changes: 2 additions & 0 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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\VarDumperCollector;
use Yiisoft\Yii\Debug\Collector\Web\MiddlewareCollector;
use Yiisoft\Yii\Debug\Collector\Web\RequestCollector;
use Yiisoft\Yii\Debug\Collector\Web\WebAppInfoCollector;
Expand All @@ -43,6 +44,7 @@
FilesystemStreamCollector::class,
HttpStreamCollector::class,
ExceptionCollector::class,
VarDumperCollector::class,
],
'collectors.web' => [
WebAppInfoCollector::class,
Expand Down
44 changes: 44 additions & 0 deletions src/Collector/VarDumperCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Collector;

final class VarDumperCollector implements SummaryCollectorInterface
{
use CollectorTrait;

private array $vars = [];

public function collectVar(mixed $variable, string $line): void

Check warning on line 13 in src/Collector/VarDumperCollector.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperCollector.php#L13

Added line #L13 was not covered by tests
{
$this->vars[] = [
'variable' => $variable,
'line' => $line,
];

Check warning on line 18 in src/Collector/VarDumperCollector.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperCollector.php#L15-L18

Added lines #L15 - L18 were not covered by tests
}

public function getCollected(): array

Check warning on line 21 in src/Collector/VarDumperCollector.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperCollector.php#L21

Added line #L21 was not covered by tests
{
if (!$this->isActive()) {
return [];

Check warning on line 24 in src/Collector/VarDumperCollector.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperCollector.php#L23-L24

Added lines #L23 - L24 were not covered by tests
}

return [
'var-dumper' => $this->vars,
];

Check warning on line 29 in src/Collector/VarDumperCollector.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperCollector.php#L27-L29

Added lines #L27 - L29 were not covered by tests
}

public function getSummary(): array

Check warning on line 32 in src/Collector/VarDumperCollector.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperCollector.php#L32

Added line #L32 was not covered by tests
{
if (!$this->isActive()) {
return [];

Check warning on line 35 in src/Collector/VarDumperCollector.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperCollector.php#L34-L35

Added lines #L34 - L35 were not covered by tests
}

return [
'var-dumper' => [
'total' => count($this->vars),
],
];

Check warning on line 42 in src/Collector/VarDumperCollector.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperCollector.php#L38-L42

Added lines #L38 - L42 were not covered by tests
}
}
42 changes: 42 additions & 0 deletions src/Collector/VarDumperHandlerInterfaceProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Collector;

use Yiisoft\VarDumper\HandlerInterface;

final class VarDumperHandlerInterfaceProxy implements HandlerInterface

Check failure on line 9 in src/Collector/VarDumperHandlerInterfaceProxy.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

UndefinedClass

src/Collector/VarDumperHandlerInterfaceProxy.php:9:55: UndefinedClass: Class, interface or enum named Yiisoft\VarDumper\HandlerInterface does not exist (see https://psalm.dev/019)

Check failure on line 9 in src/Collector/VarDumperHandlerInterfaceProxy.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/Collector/VarDumperHandlerInterfaceProxy.php:9:55: UndefinedClass: Class, interface or enum named Yiisoft\VarDumper\HandlerInterface does not exist (see https://psalm.dev/019)

Check failure on line 9 in src/Collector/VarDumperHandlerInterfaceProxy.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

UndefinedClass

src/Collector/VarDumperHandlerInterfaceProxy.php:9:55: UndefinedClass: Class, interface or enum named Yiisoft\VarDumper\HandlerInterface does not exist (see https://psalm.dev/019)

Check failure on line 9 in src/Collector/VarDumperHandlerInterfaceProxy.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedClass

src/Collector/VarDumperHandlerInterfaceProxy.php:9:55: UndefinedClass: Class, interface or enum named Yiisoft\VarDumper\HandlerInterface does not exist (see https://psalm.dev/019)
{
public function __construct(

Check warning on line 11 in src/Collector/VarDumperHandlerInterfaceProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperHandlerInterfaceProxy.php#L11

Added line #L11 was not covered by tests
private HandlerInterface $decorated,
private VarDumperCollector $collector,
) {
}

Check warning on line 15 in src/Collector/VarDumperHandlerInterfaceProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperHandlerInterfaceProxy.php#L15

Added line #L15 was not covered by tests

public function handle(mixed $variable, int $depth, bool $highlight = false): void

Check warning on line 17 in src/Collector/VarDumperHandlerInterfaceProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperHandlerInterfaceProxy.php#L17

Added line #L17 was not covered by tests
{
$stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

Check warning on line 19 in src/Collector/VarDumperHandlerInterfaceProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperHandlerInterfaceProxy.php#L19

Added line #L19 was not covered by tests

$callStack = null;
foreach ($stack as $value) {
if (!isset($value['file'])) {
continue;

Check warning on line 24 in src/Collector/VarDumperHandlerInterfaceProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperHandlerInterfaceProxy.php#L21-L24

Added lines #L21 - L24 were not covered by tests
}
if (str_ends_with($value['file'], '/var-dumper/src/functions.php')) {
continue;

Check warning on line 27 in src/Collector/VarDumperHandlerInterfaceProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperHandlerInterfaceProxy.php#L26-L27

Added lines #L26 - L27 were not covered by tests
}
if (str_ends_with($value['file'], '/var-dumper/src/VarDumper.php')) {
continue;

Check warning on line 30 in src/Collector/VarDumperHandlerInterfaceProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperHandlerInterfaceProxy.php#L29-L30

Added lines #L29 - L30 were not covered by tests
}
$callStack = $value;
break;

Check warning on line 33 in src/Collector/VarDumperHandlerInterfaceProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperHandlerInterfaceProxy.php#L32-L33

Added lines #L32 - L33 were not covered by tests
}

$this->collector->collectVar(
$variable,
$callStack === null ? '' : $callStack['file'] . ':' . $callStack['line']
);
$this->decorated->handle($variable, $depth, $highlight);

Check warning on line 40 in src/Collector/VarDumperHandlerInterfaceProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Collector/VarDumperHandlerInterfaceProxy.php#L36-L40

Added lines #L36 - L40 were not covered by tests
}
}

0 comments on commit 3f90d3f

Please sign in to comment.