Skip to content

Commit

Permalink
Add debug
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed May 25, 2024
1 parent e23bb0a commit 344adfd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 19 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ vendor/
app/database.sqlite
composer.lock
*.cache.php
sessions/
sessions/
tempest.log
debug.log
31 changes: 31 additions & 0 deletions app/dump.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use Tempest\AppConfig;
use Tempest\Discovery\DiscoveryLocation;
use Tempest\Kernel;
use Tempest\Log\LogConfig;
use function Tempest\lw;

$appConfig = new AppConfig(
root: __DIR__ . '/..',
discoveryCache: true,
discoveryLocations: [
new DiscoveryLocation(
'App\\',
__DIR__ . '/../app/',
),
],
);

$kernel = new Kernel($appConfig);
$container = $kernel->init();

$container->config(new LogConfig(
debugLogPath: __DIR__ . '/debug.log',
));

lw(['hi'], a: ['ho']);
1 change: 1 addition & 0 deletions src/Log/LogConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public function __construct(
/** @var LogChannel[] */
public array $channels = [],
public string $prefix = 'tempest',
public ?string $debugLogPath = null,
) {
}
}
40 changes: 40 additions & 0 deletions src/Support/VarExport/Debug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Tempest\Support\VarExport;

use Stringable;
use Symfony\Component\VarDumper\VarDumper;
use Symfony\Component\VarExporter\VarExporter;
use Tempest\Log\LogConfig;

final readonly class Debug
{
public function __construct(private LogConfig $logConfig)
{
}

public function log(mixed ...$input): void
{
$handle = fopen($this->logConfig->debugLogPath, 'a');

foreach ($input as $key => $item) {
if ($item instanceof Stringable) {
$output = (string) $item;
} else {
$output = VarExporter::export($item);
}

fwrite($handle, "[{$key}] {$output}" . PHP_EOL);
}

fclose($handle);

VarDumper::dump(...$input);

$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

fwrite(STDOUT, PHP_EOL . "Called in " . $trace[1]['file'] . ':' . $trace[1]['line'] . PHP_EOL);
}
}
21 changes: 3 additions & 18 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

namespace Tempest {

use Psr\Log\LoggerInterface;
use ReflectionType;
use Reflector;
use Stringable;
use Symfony\Component\VarDumper\VarDumper;
use Tempest\Container\GenericContainer;
use Tempest\Events\EventBus;
use Tempest\Mapper\ObjectFactory;
use Tempest\Support\Reflection\Attributes;
use Tempest\Support\Reflection\TypeName;
use Tempest\Support\VarExport\Debug;

/**
* @template TClassName
Expand Down Expand Up @@ -70,25 +68,12 @@ function type(Reflector|ReflectionType $reflector): string

function lw(mixed ...$input): void
{
/** @var LoggerInterface $logger */
$logger = get(LoggerInterface::class);

foreach ($input as $key => $item) {
if ($item instanceof Stringable) {
$message = (string)$item;
} else {
$message = var_export($item, true);
}

$logger->debug("[{$key}] {$message}");
}

VarDumper::dump(...$input);
get(Debug::class)->log(...$input);
}

function ld(mixed ...$input): void
{
lw(...$input);
get(Debug::class)->log(...$input);
die();
}
}

0 comments on commit 344adfd

Please sign in to comment.