From 8ae7da52591310fc0dd05c3ff1c1933535bc68db Mon Sep 17 00:00:00 2001 From: indifferentketchup Date: Thu, 30 Apr 2026 20:29:21 +0000 Subject: [PATCH] Expose source path on LogFileInterface Add LogFileInterface::getPath(): ?string so detectors can dispatch on a filename hint when the original path is known. Default implementation on the abstract LogFile base returns null; PathLogFile records its constructor argument. StringLogFile and StreamLogFile inherit the null default. Tests cover both the path and null-fallback cases. --- src/Log/File/LogFile.php | 11 +++++++++++ src/Log/File/LogFileInterface.php | 11 +++++++++++ src/Log/File/PathLogFile.php | 1 + test/tests/Log/File/PathLogFileTest.php | 8 ++++++++ test/tests/Log/File/StringLogFileTest.php | 7 +++++++ 5 files changed, 38 insertions(+) diff --git a/src/Log/File/LogFile.php b/src/Log/File/LogFile.php index 73328f6..b1d4c4e 100644 --- a/src/Log/File/LogFile.php +++ b/src/Log/File/LogFile.php @@ -10,6 +10,7 @@ namespace IndifferentKetchup\Codex\Log\File; abstract class LogFile implements LogFileInterface { protected ?string $content = null; + protected ?string $path = null; /** * Get the log file content @@ -20,4 +21,14 @@ abstract class LogFile implements LogFileInterface { return $this->content; } + + /** + * Get the source path of the log file when one is known + * + * @return string|null + */ + public function getPath(): ?string + { + return $this->path; + } } \ No newline at end of file diff --git a/src/Log/File/LogFileInterface.php b/src/Log/File/LogFileInterface.php index bcdafd2..f95da80 100644 --- a/src/Log/File/LogFileInterface.php +++ b/src/Log/File/LogFileInterface.php @@ -15,4 +15,15 @@ interface LogFileInterface * @return string */ public function getContent(): string; + + /** + * Get the source path of the log file when one is known + * + * Returns null for log files without a filesystem origin (string content, + * arbitrary streams). Concrete implementations should return the path used + * to construct them when applicable. + * + * @return string|null + */ + public function getPath(): ?string; } \ No newline at end of file diff --git a/src/Log/File/PathLogFile.php b/src/Log/File/PathLogFile.php index c4467bb..8c73dec 100644 --- a/src/Log/File/PathLogFile.php +++ b/src/Log/File/PathLogFile.php @@ -22,6 +22,7 @@ class PathLogFile extends LogFile throw new InvalidArgumentException("File '" . $path . "' not found."); } + $this->path = $path; $this->content = file_get_contents($path); } } \ No newline at end of file diff --git a/test/tests/Log/File/PathLogFileTest.php b/test/tests/Log/File/PathLogFileTest.php index b75eb3a..1c28ef0 100644 --- a/test/tests/Log/File/PathLogFileTest.php +++ b/test/tests/Log/File/PathLogFileTest.php @@ -14,4 +14,12 @@ class PathLogFileTest extends TestCase $this->assertStringEqualsFile($path, $logFile->getContent()); } + + public function testGetPathReturnsConstructorArgument(): void + { + $path = __DIR__ . "/../../../data/simple.log"; + $logFile = new PathLogFile($path); + + $this->assertSame($path, $logFile->getPath()); + } } diff --git a/test/tests/Log/File/StringLogFileTest.php b/test/tests/Log/File/StringLogFileTest.php index a3b0d14..11a66c2 100644 --- a/test/tests/Log/File/StringLogFileTest.php +++ b/test/tests/Log/File/StringLogFileTest.php @@ -14,4 +14,11 @@ class StringLogFileTest extends TestCase $this->assertEquals($content, $logFile->getContent()); } + + public function testGetPathReturnsNull(): void + { + $logFile = new StringLogFile("anything"); + + $this->assertNull($logFile->getPath()); + } }