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