• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

willy68 / pg-router / 16205018743

10 Jul 2025 08:08PM UTC coverage: 94.928% (-0.05%) from 94.978%
16205018743

push

github

willy68
First commit

3 of 3 new or added lines in 1 file covered. (100.0%)

17 existing lines in 4 files now uncovered.

917 of 966 relevant lines covered (94.93%)

8.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.44
/src/Cache/FileCache.php
1
<?php
2

3
namespace Pg\Router\Cache;
4

5
use Closure;
6
use RuntimeException;
7

8
class FileCache implements FileCacheInterface
9
{
10
    private static array|string|Closure $error_handler;
11

12
    private string $cacheDir;
13
    private bool $useCache;
14

15
    public function __construct(
6✔
16
        ?string $cacheDir = null,
17
        bool $useCache = false
18
    ) {
19
        self::$error_handler = static function (): void {
6✔
20
        };
1✔
21
        $this->cacheDir = $cacheDir ?? 'tmp' . DIRECTORY_SEPARATOR . 'pg-router-cache';
6✔
22
        $this->useCache = $useCache;
6✔
23
        $this->init();
6✔
24
    }
25

26
    public function init(): void
6✔
27
    {
28
        if (!$this->useCache || is_dir($this->cacheDir)) {
6✔
29
            return;
2✔
30
        }
31

32
        set_error_handler(self::$error_handler);
6✔
33
        $created = mkdir($this->cacheDir, 0755, true);
6✔
34
        restore_error_handler();
6✔
35

36
        if ($created === false) {
6✔
37
            throw new RuntimeException('Impossible to create Cache directory: ' . $this->cacheDir);
×
38
        }
39
    }
40

41
    public function clear(): void
1✔
42
    {
43
        if (!$this->useCache) {
1✔
UNCOV
44
            return;
×
45
        }
46

47
        $files = glob($this->cacheDir . '/*.php');
1✔
48
        foreach ($files as $file) {
1✔
49
            unlink($file);
1✔
50
        }
51
    }
52

53
    public function has(string $key): bool
4✔
54
    {
55
        if (!$this->useCache) {
4✔
56
            return false;
1✔
57
        }
58

59
        $cachePath = $this->getCachePath($key);
3✔
60
        return file_exists($cachePath);
3✔
61
    }
62

63
    private function getCachePath(string $key): string
5✔
64
    {
65
        return $this->cacheDir . DIRECTORY_SEPARATOR . md5($key) . '.php';
5✔
66
    }
67

68
    public function delete(string $key): void
1✔
69
    {
70
        if (!$this->useCache) {
1✔
UNCOV
71
            return;
×
72
        }
73

74
        $cachePath = $this->getCachePath($key);
1✔
75
        if (file_exists($cachePath)) {
1✔
76
            unlink($cachePath);
1✔
77
        }
78
    }
79

80
    public function fetch(string $key, callable $dataFetcher): mixed
1✔
81
    {
82
        $cachedData = $this->get($key);
1✔
83

84
        if ($cachedData !== null) {
1✔
85
            return $cachedData;
1✔
86
        }
87

88
        $result = $dataFetcher();
1✔
89
        $this->set($key, $result);
1✔
90

91
        return $result;
1✔
92
    }
93

94
    public function get(string $key): mixed
3✔
95
    {
96
        if (!$this->useCache) {
3✔
97
            return null;
1✔
98
        }
99

100
        $cachePath = $this->getCachePath($key);
2✔
101

102
        set_error_handler(self::$error_handler);
2✔
103
        $result = include $cachePath;
2✔
104
        restore_error_handler();
2✔
105

106
        if ($result === false) {
2✔
107
            return null;
1✔
108
        }
109

110
        return $result;
2✔
111
    }
112

113
    public function set(string $key, mixed $value): int|bool
6✔
114
    {
115
        if (!$this->useCache) {
6✔
116
            return false;
1✔
117
        }
118

119
        $cachePath = $this->getCachePath($key);
5✔
120

121
        if (file_exists($cachePath) && !is_writable($cachePath)) {
5✔
UNCOV
122
            throw new RuntimeException('Cache file is not writable: ' . $cachePath);
×
123
        }
124

125
        return file_put_contents(
5✔
126
            $cachePath,
5✔
127
            '<?php return ' . var_export($value, true) . ';',
5✔
128
            LOCK_EX
5✔
129
        );
5✔
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc