Skip to content

Commit 7c60a60

Browse files
committed
Dumper: added support for uninitialized lazy objects
1 parent 7db37d0 commit 7c60a60

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

src/Tracy/Dumper/Exposer.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ final class Exposer
2121
{
2222
public static function exposeObject(object $obj, Value $value, Describer $describer): void
2323
{
24+
if (PHP_VERSION_ID >= 80400 && (new \ReflectionClass($obj))->isUninitializedLazyObject($obj)) {
25+
self::exposeLazyObject($obj, $describer, $value);
26+
return;
27+
}
28+
2429
$values = get_mangled_object_vars($obj);
2530
$props = self::getProperties($obj::class);
2631

@@ -276,4 +281,23 @@ public static function exposeDsMap(
276281
$describer->addPropertyTo($value, (string) $i++, new Ds\Pair($k, $v));
277282
}
278283
}
284+
285+
286+
private static function exposeLazyObject(object $obj, Describer $describer, Value $value): void
287+
{
288+
$rc = new \ReflectionClass($obj);
289+
foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
290+
if (!$prop->isLazy($obj)) {
291+
$describer->addPropertyTo(
292+
$value,
293+
$prop->getName(),
294+
$prop->getValue($obj),
295+
Value::PropertyPublic,
296+
described: $describer->describeEnumProperty($obj::class, $prop->getName(), $prop->getValue($obj)),
297+
);
298+
}
299+
}
300+
301+
$value->value .= ' (lazy)';
302+
}
279303
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* Test: Tracy\Dumper::toHtml() & lazy object
5+
* @phpversion 8.4
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Tester\Assert;
11+
use Tracy\Dumper;
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
class LazyClass
17+
{
18+
public function __construct(
19+
public int $id,
20+
public string $title,
21+
) {
22+
}
23+
}
24+
25+
$rc = new ReflectionClass(LazyClass::class);
26+
$ghost = $rc->newLazyGhost(function () {});
27+
28+
// new ghost
29+
Assert::match(
30+
<<<'XX'
31+
<pre class="tracy-dump tracy-light"><span class="tracy-dump-object">LazyClass (lazy)</span> <span class="tracy-dump-hash">#%d%</span></pre>
32+
XX,
33+
Dumper::toHtml($ghost, [Dumper::DEPTH => 3]),
34+
);
35+
36+
// preinitialized property
37+
$rc->getProperty('id')->setRawValueWithoutLazyInitialization($ghost, 123);
38+
39+
Assert::match(
40+
<<<'XX'
41+
<pre class="tracy-dump tracy-light"
42+
><span class="tracy-toggle"><span class="tracy-dump-object">LazyClass (lazy)</span> <span class="tracy-dump-hash">#%d%</span></span>
43+
<div><span class="tracy-dump-indent"> </span><span class="tracy-dump-public">id</span>: <span class="tracy-dump-number">123</span>
44+
</div></pre>
45+
XX,
46+
Dumper::toHtml($ghost, [Dumper::DEPTH => 3]),
47+
);
48+
49+
// proxy
50+
$proxy = $rc->newLazyProxy(function () { return new LazyClass; });
51+
Assert::match(
52+
<<<'XX'
53+
<pre class="tracy-dump tracy-light"><span class="tracy-dump-object">LazyClass (lazy)</span> <span class="tracy-dump-hash">#%d%</span></pre>
54+
XX,
55+
Dumper::toHtml($proxy, [Dumper::DEPTH => 3]),
56+
);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* Test: Tracy\Dumper::toText() & lazy object
5+
* @phpversion 8.4
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Tester\Assert;
11+
use Tracy\Dumper;
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
class LazyClass
17+
{
18+
public function __construct(
19+
public int $id,
20+
public string $title,
21+
) {
22+
}
23+
}
24+
25+
$rc = new ReflectionClass(LazyClass::class);
26+
$ghost = $rc->newLazyGhost(function () {});
27+
28+
// new ghost
29+
Assert::match(
30+
<<<'XX'
31+
LazyClass (lazy) #%d%
32+
XX,
33+
Dumper::toText($ghost, [Dumper::DEPTH => 3]),
34+
);
35+
36+
// preinitialized property
37+
$rc->getProperty('id')->setRawValueWithoutLazyInitialization($ghost, 123);
38+
39+
Assert::match(
40+
<<<'XX'
41+
LazyClass (lazy) #%d%
42+
id: 123
43+
XX,
44+
Dumper::toText($ghost, [Dumper::DEPTH => 3]),
45+
);
46+
47+
// proxy
48+
$proxy = $rc->newLazyProxy(function () { return new LazyClass; });
49+
Assert::match(
50+
<<<'XX'
51+
LazyClass (lazy) #%d%
52+
XX,
53+
Dumper::toText($proxy, [Dumper::DEPTH => 3]),
54+
);

0 commit comments

Comments
 (0)