Files
fewo-stommer/app/Process/Tester/EffectLedger.php
T

51 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Process\Tester;
/**
* The record of a tester run: every simulated effect and every violation, appended line by line to a file in the
* run's directory – it survives exceptions, and what an action swallowed is still there.
*/
final class EffectLedger
{
private static ?string $path = null;
public static function open(string $path): void
{
self::$path = $path;
}
public static function isOpen(): bool
{
return self::$path !== null;
}
/**
* @param array<string, mixed> $data
*/
public static function record(string $type, array $data = []): void
{
if (self::$path === null) {
return;
}
file_put_contents(self::$path, json_encode(['type' => $type, ...$data], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)."\n", FILE_APPEND | LOCK_EX);
}
/**
* @return list<array<string, mixed>>
*/
public static function entries(): array
{
if (self::$path === null || ! is_file(self::$path)) {
return [];
}
return array_values(array_map(fn (string $line): array => (array) json_decode($line, true), array_filter(explode("\n", (string) file_get_contents(self::$path)))));
}
public static function violations(): array
{
return array_values(array_filter(self::entries(), fn (array $entry): bool => ($entry['type'] ?? null) === 'violation'));
}
}