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.
This commit is contained in:
2026-04-30 20:29:56 +00:00
parent 8ae7da5259
commit ada3c7875d
2 changed files with 92 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
<?php
namespace IndifferentKetchup\Codex\Detective;
/**
* Match a regex against the source path of the log file
*
* Returns a configured weight when the LogFileInterface exposes a non-null
* path that matches $pattern. Returns false when no path is known
* (StringLogFile, StreamLogFile) or when the pattern does not match. Pattern
* is compared against the full path; anchor with $ for strict suffix matching.
*
* @package IndifferentKetchup\Codex\Detective
*/
class FilenameDetector extends Detector
{
protected ?string $pattern = null;
protected float $weight = 0.95;
public function setPattern(string $pattern): static
{
$this->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;
}
}