From ada3c7875d6248f0f379f7ce3b43ae6d5d59b1b6 Mon Sep 17 00:00:00 2001 From: indifferentketchup Date: Thu, 30 Apr 2026 20:29:56 +0000 Subject: [PATCH] Add FilenameDetector for path-based log type dispatch New Detector that matches a configured regex against LogFileInterface::getPath(). Returns a settable weight on match (default 0.95) and false otherwise, including when the log file has no known path (StringLogFile, StreamLogFile). Lets game-specific Detectives prefer the filename hint over content signatures when an upload's original name is preserved. --- src/Detective/FilenameDetector.php | 45 ++++++++++++++++++ test/tests/Detective/FilenameDetectorTest.php | 47 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/Detective/FilenameDetector.php create mode 100644 test/tests/Detective/FilenameDetectorTest.php diff --git a/src/Detective/FilenameDetector.php b/src/Detective/FilenameDetector.php new file mode 100644 index 0000000..e7ede67 --- /dev/null +++ b/src/Detective/FilenameDetector.php @@ -0,0 +1,45 @@ +pattern = $pattern; + return $this; + } + + public function setWeight(float $weight): static + { + $this->weight = $weight; + return $this; + } + + public function detect(): bool|float + { + $path = $this->logFile->getPath(); + if ($path === null || $this->pattern === null) { + return false; + } + + if (preg_match($this->pattern, $path) === 1) { + return $this->weight; + } + + return false; + } +} diff --git a/test/tests/Detective/FilenameDetectorTest.php b/test/tests/Detective/FilenameDetectorTest.php new file mode 100644 index 0000000..5e10e04 --- /dev/null +++ b/test/tests/Detective/FilenameDetectorTest.php @@ -0,0 +1,47 @@ +setPattern('/simple\.log$/') + ->setWeight(0.9); + $detector->setLogFile(new PathLogFile(__DIR__ . "/../../data/simple.log")); + + $this->assertSame(0.9, $detector->detect()); + } + + public function testReturnsFalseWhenPatternDoesNotMatch(): void + { + $detector = (new FilenameDetector()) + ->setPattern('/notthere\.txt$/'); + $detector->setLogFile(new PathLogFile(__DIR__ . "/../../data/simple.log")); + + $this->assertFalse($detector->detect()); + } + + public function testReturnsFalseWhenLogFileHasNoPath(): void + { + $detector = (new FilenameDetector()) + ->setPattern('/anything/'); + $detector->setLogFile(new StringLogFile("content")); + + $this->assertFalse($detector->detect()); + } + + public function testReturnsFalseWhenPatternUnset(): void + { + $detector = new FilenameDetector(); + $detector->setLogFile(new PathLogFile(__DIR__ . "/../../data/simple.log")); + + $this->assertFalse($detector->detect()); + } +}