Lines
63.63%
7 / 11
Methods
0.00%
0 / 2
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| write | 70.00% | 7 / 10 | 0.00% | 0 / 1 | 3.24 | ||
| 10 | class FileDriver extends Driver | |
| 11 | { | |
| 12 | private string $path; | |
| 13 | ||
| 14 | public function __construct(string $path) | |
| 15 | { | |
| 16 | $this->path = $path; | |
| 17 | } | |
| 18 | ||
| 19 | public function write(string $line): void | |
| 20 | { | |
| 21 | $logDir = FileSystem::normalizePath(FileSystem::rootPath() . DIRECTORY_SEPARATOR ."logs"); | |
| 22 | ||
| 23 | if (!is_dir($logDir)) { | |
| 24 | mkdir($logDir, 0755, true); | |
| 25 | } | |
| 26 | ||
| 27 | $logPath = FileSystem::normalizePath($logDir . DIRECTORY_SEPARATOR . $this->path); | |
| 28 | ||
| 29 | // Try to open file and handle any errors. | |
| 30 | // This is the "sink itself failed" case — we cannot report the | |
| 31 | // failure through the logging system without risking recursion, so | |
| 32 | // PHP's error_log is the intentional last-resort destination. | |
| 33 | $file = fopen($logPath, "a"); | |
| 34 | if ($file === false) { | |
| 35 | error_log("Failed to open log file: " . $logPath); | |
| 36 | return; | |
| 37 | } | |
| 38 | ||
| 39 | // Strip ANSI color codes for file logging | |
| 40 | fwrite($file, preg_replace('/\033\[[0-9;]*m/', '', $line)); | |
| 41 | fclose($file); | |
| 42 | } | |
| 43 | } |