Skip to content

Commit c1be5e6

Browse files
authored
feat(debug): emit ItemsDebugged on debug (#796)
1 parent 00aa6ea commit c1be5e6

File tree

4 files changed

+69
-13
lines changed

4 files changed

+69
-13
lines changed

src/Tempest/Debug/src/Debug.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,35 @@
44

55
namespace Tempest\Debug;
66

7+
use Exception;
78
use Symfony\Component\VarDumper\Cloner\VarCloner;
89
use Symfony\Component\VarDumper\Dumper\CliDumper;
910
use Symfony\Component\VarDumper\VarDumper;
1011
use Tempest\Container\GenericContainer;
12+
use Tempest\EventBus\EventBus;
1113
use Tempest\Highlight\Themes\TerminalStyle;
1214
use Tempest\Log\LogConfig;
1315

1416
final readonly class Debug
1517
{
16-
private function __construct(private ?LogConfig $logConfig = null)
17-
{
18+
private function __construct(
19+
private ?LogConfig $logConfig = null,
20+
private ?EventBus $eventBus = null,
21+
) {
1822
}
1923

2024
public static function resolve(): self
2125
{
22-
if (! class_exists(GenericContainer::class)) {
23-
return new self();
24-
}
25-
26-
if (! class_exists(LogConfig::class)) {
26+
try {
27+
$container = GenericContainer::instance();
28+
29+
return new self(
30+
logConfig: $container?->get(LogConfig::class),
31+
eventBus: $container?->get(EventBus::class),
32+
);
33+
} catch (Exception) {
2734
return new self();
2835
}
29-
30-
$container = GenericContainer::instance();
31-
32-
$logConfig = $container?->get(LogConfig::class);
33-
34-
return new self($logConfig);
3536
}
3637

3738
public function log(array $items, bool $writeToLog = true, bool $writeToOut = true): void
@@ -46,6 +47,8 @@ public function log(array $items, bool $writeToLog = true, bool $writeToOut = tr
4647
if ($writeToOut) {
4748
$this->writeToOut($items, $callPath);
4849
}
50+
51+
$this->eventBus?->dispatch(new ItemsDebugged($items));
4952
}
5053

5154
private function writeToLog(array $items, string $callPath): void
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Debug;
6+
7+
final class ItemsDebugged
8+
{
9+
public function __construct(
10+
public array $items,
11+
) {
12+
}
13+
}

src/Tempest/Debug/src/functions.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ function ll(mixed ...$input): void
4040
}
4141
}
4242

43+
if (! function_exists('le')) {
44+
/**
45+
* Emits a `ItemsDebugged` event.
46+
* @see \Tempest\Debug\Debug::log()
47+
*/
48+
function le(mixed ...$input): void
49+
{
50+
Debug::resolve()->log($input, writeToOut: false, writeToLog: false);
51+
}
52+
}
53+
4354
if (! function_exists('dd')) {
4455
/**
4556
* Writes the given `$input` to the logs, dumps it, and stops the execution of the script.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Integration\Debug;
6+
7+
use stdClass;
8+
use Tempest\Debug\Debug;
9+
use Tempest\Debug\ItemsDebugged;
10+
use Tempest\EventBus\EventBus;
11+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
12+
13+
/**
14+
* @internal
15+
*/
16+
final class DebugTest extends FrameworkIntegrationTestCase
17+
{
18+
public function test_event(): void
19+
{
20+
$class = new stdClass();
21+
22+
$eventBus = $this->container->get(EventBus::class);
23+
$eventBus->listen(ItemsDebugged::class, function (ItemsDebugged $event) use ($class): void {
24+
$this->assertSame(['foo', $class], $event->items);
25+
});
26+
27+
Debug::resolve()->log(['foo', $class], writeToLog: false, writeToOut: false);
28+
}
29+
}

0 commit comments

Comments
 (0)