|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\Component\Cache; |
|
6
|
|
|
|
|
7
|
|
|
use YamlStandards\Model\Component\Cache\Exception\CustomPathToCacheDirectoryNotFound; |
|
8
|
|
|
|
|
9
|
|
|
class NativeCache implements CacheInterface |
|
10
|
|
|
{ |
|
11
|
|
|
public const CACHE_FILE_NAME = 'yaml-standards.cache'; |
|
12
|
|
|
public const CONFIG_NUMBER_FOR_CONFIG_FILE = 0; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @inheritDoc |
|
16
|
|
|
* |
|
17
|
|
|
* @throws \YamlStandards\Model\Component\Cache\Exception\CustomPathToCacheDirectoryNotFound |
|
18
|
|
|
*/ |
|
19
|
4 |
|
public function getCachedPathToFiles(array $pathToFiles, int $configNumber, string $pathToCacheDir): array |
|
20
|
|
|
{ |
|
21
|
4 |
|
if (is_dir($pathToCacheDir) === false) { |
|
22
|
|
|
throw new CustomPathToCacheDirectoryNotFound(sprintf('Custom path to cache directory %s was not found', $pathToCacheDir)); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
4 |
|
if (file_exists($pathToCacheDir . self::CACHE_FILE_NAME) === false) { |
|
26
|
|
|
return $pathToFiles; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
4 |
|
$cacheFileContent = file_get_contents($pathToCacheDir . self::CACHE_FILE_NAME); |
|
30
|
4 |
|
$filesModifiedTimeIndexedByPathToFileAndConfigNumber = unserialize($cacheFileContent, ['allowed_classes' => false]); |
|
31
|
|
|
|
|
32
|
4 |
|
if (array_key_exists($configNumber, $filesModifiedTimeIndexedByPathToFileAndConfigNumber) === false) { |
|
33
|
|
|
return $pathToFiles; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
4 |
|
$changedFiles = []; |
|
37
|
4 |
|
foreach ($pathToFiles as $pathToFile) { |
|
38
|
4 |
|
if (array_key_exists($pathToFile, $filesModifiedTimeIndexedByPathToFileAndConfigNumber[$configNumber])) { |
|
39
|
4 |
|
if (filemtime($pathToFile) !== $filesModifiedTimeIndexedByPathToFileAndConfigNumber[$configNumber][$pathToFile]) { |
|
40
|
4 |
|
$changedFiles[] = $pathToFile; |
|
41
|
|
|
} |
|
42
|
|
|
} else { |
|
43
|
2 |
|
$changedFiles[] = $pathToFile; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
return $changedFiles; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritDoc |
|
52
|
|
|
* |
|
53
|
|
|
* @throws \YamlStandards\Model\Component\Cache\Exception\CustomPathToCacheDirectoryNotFound |
|
54
|
|
|
*/ |
|
55
|
5 |
|
public function cacheFiles(array $pathToFiles, int $configNumber, string $pathToCacheDir): void |
|
56
|
|
|
{ |
|
57
|
5 |
|
$filesModifiedTimeIndexedByPathToFileAndConfigNumber = [[]]; |
|
58
|
|
|
|
|
59
|
5 |
|
if (is_dir($pathToCacheDir) === false) { |
|
60
|
|
|
throw new CustomPathToCacheDirectoryNotFound(sprintf('Custom path to cache directory %s was not found', $pathToCacheDir)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
5 |
|
if (file_exists($pathToCacheDir . self::CACHE_FILE_NAME)) { |
|
64
|
1 |
|
$filesModifiedTimeIndexedByPathToFileAndConfigNumber = unserialize(file_get_contents($pathToCacheDir . self::CACHE_FILE_NAME), ['allowed_classes' => false]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
5 |
|
foreach ($pathToFiles as $pathToFile) { |
|
68
|
5 |
|
$filesModifiedTimeIndexedByPathToFileAndConfigNumber[$configNumber][$pathToFile] = filemtime($pathToFile); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
5 |
|
file_put_contents($pathToCacheDir . self::CACHE_FILE_NAME, serialize($filesModifiedTimeIndexedByPathToFileAndConfigNumber)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritDoc |
|
76
|
|
|
* |
|
77
|
|
|
* @throws \YamlStandards\Model\Component\Cache\Exception\CustomPathToCacheDirectoryNotFound |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function deleteCacheFileIfConfigFileWasChanged(string $pathToConfigFile, string $pathToCacheDir): void |
|
80
|
|
|
{ |
|
81
|
1 |
|
if (count($this->getCachedPathToFiles([$pathToConfigFile], self::CONFIG_NUMBER_FOR_CONFIG_FILE, $pathToCacheDir)) > 0) { |
|
82
|
1 |
|
if (file_exists($pathToCacheDir . self::CACHE_FILE_NAME)) { |
|
83
|
1 |
|
unlink($pathToCacheDir . self::CACHE_FILE_NAME); |
|
84
|
|
|
} |
|
85
|
1 |
|
$this->cacheFiles([$pathToConfigFile], self::CONFIG_NUMBER_FOR_CONFIG_FILE, $pathToCacheDir); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|