|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework\ClassResolver\Cache; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractPhpFileCache implements CacheInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var array<string,string> */ |
|
12
|
|
|
private static array $cache = []; |
|
13
|
|
|
|
|
14
|
|
|
private string $cacheDir; |
|
15
|
|
|
|
|
16
|
19 |
|
public function __construct(string $cacheDir) |
|
17
|
|
|
{ |
|
18
|
19 |
|
$this->cacheDir = $cacheDir; |
|
19
|
19 |
|
self::$cache = $this->getExistingCache(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @internal |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function resetCache(): void |
|
26
|
|
|
{ |
|
27
|
|
|
self::$cache = []; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @internal |
|
32
|
|
|
* |
|
33
|
|
|
* @return array<string,string> |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public static function all(): array |
|
36
|
|
|
{ |
|
37
|
2 |
|
return self::$cache; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
19 |
|
public function has(string $cacheKey): bool |
|
41
|
|
|
{ |
|
42
|
19 |
|
return isset(self::$cache[$cacheKey]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
14 |
|
public function get(string $cacheKey): string |
|
46
|
|
|
{ |
|
47
|
14 |
|
return self::$cache[$cacheKey]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getAll(): array |
|
51
|
|
|
{ |
|
52
|
|
|
return self::$cache; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
10 |
|
public function put(string $cacheKey, string $className): void |
|
56
|
|
|
{ |
|
57
|
10 |
|
self::$cache[$cacheKey] = $className; |
|
58
|
|
|
|
|
59
|
10 |
|
$fileContent = sprintf( |
|
60
|
|
|
'<?php return %s;', |
|
61
|
10 |
|
var_export(self::$cache, true) |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
10 |
|
file_put_contents($this->getAbsoluteCacheFilename(), $fileContent); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
abstract protected function getCacheFilename(): string; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return array<string,string> |
|
71
|
|
|
*/ |
|
72
|
19 |
|
private function getExistingCache(): array |
|
73
|
|
|
{ |
|
74
|
19 |
|
$filename = $this->getAbsoluteCacheFilename(); |
|
75
|
|
|
|
|
76
|
19 |
|
if (file_exists($filename)) { |
|
77
|
|
|
/** @var array<string,string> $content */ |
|
78
|
15 |
|
$content = require $filename; |
|
79
|
|
|
|
|
80
|
15 |
|
return $content; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
7 |
|
return []; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
19 |
|
private function getAbsoluteCacheFilename(): string |
|
87
|
|
|
{ |
|
88
|
19 |
|
if (!is_dir($this->cacheDir) |
|
89
|
19 |
|
&& !mkdir($concurrentDirectory = $this->cacheDir, 0777, true) |
|
90
|
19 |
|
&& !is_dir($concurrentDirectory)) { |
|
91
|
|
|
throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
19 |
|
return $this->cacheDir . '/' . $this->getCacheFilename(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|