-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheClearer.php
199 lines (177 loc) · 5.81 KB
/
CacheClearer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\Bundle\CoreBundle;
use FilesystemIterator;
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
class CacheClearer
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var ExposedRoutesExtractorInterface
*/
private $fosJsRoutesExtractor;
/**
* @var string
*/
private $cacheDir;
/**
* @var string
*/
private $kernelContainerClass;
/**
* @var bool
*/
private $installed;
/**
* @var array
*/
private $routingLocales = [];
/**
* @var array
*/
private $cacheTypes = [];
/**
* @var Filesystem
*/
private $fileSystem;
/**
* @var CacheWarmerInterface
*/
private $warmer;
/**
* @var array
*/
private $cachesToClear;
public function __construct(
LoggerInterface $zikulaLogger,
CacheWarmerInterface $warmer,
ExposedRoutesExtractorInterface $fosJsRoutesExtractor,
string $cacheDir,
string $kernelContainerClass,
string $installed,
array $routingLocales = []
) {
$this->logger = $zikulaLogger;
$this->warmer = $warmer;
$this->fosJsRoutesExtractor = $fosJsRoutesExtractor;
$this->cacheDir = $cacheDir;
$this->kernelContainerClass = $kernelContainerClass;
$this->installed = '0.0.0' !== $installed;
$this->routingLocales = $routingLocales;
$this->fileSystem = new Filesystem();
$this->cachesToClear = [];
}
/**
* The cache is not cleared on demand.
* Calling 'clear' will store caches to clear
* This ensures no duplication and defers actual clearing
* until kernel.terminate event
* @see \Zikula\Bundle\CoreBundle\EventListener\CacheClearListener::doClearCache
*/
public function clear(string $type): void
{
if (!isset($this->cachesToClear[$type])) {
foreach ($this->cachesToClear as $value) {
if (0 === mb_strpos($type, $value)) {
return;
}
}
$this->cachesToClear[$type] = $type;
}
}
/**
* @internal
* This is not a public api
*/
public function doClear(): void
{
if (empty($this->cachesToClear)) {
return;
}
if (!count($this->cacheTypes)) {
$this->initialiseCacheTypeMap();
}
foreach ($this->cachesToClear as $type) {
foreach ($this->cacheTypes as $cacheType => $files) {
if (0 !== mb_strpos($cacheType, $type)) {
continue;
}
foreach ($files as $file) {
if (is_dir($file)) {
// Do not delete the folder itself, but all files in it.
// Otherwise Symfony somehow can't create the folder anymore.
$file = new FilesystemIterator($file);
}
// This silently ignores non existing files.
$this->fileSystem->remove($file);
}
$this->logger->notice(sprintf('Cache cleared: %s', $cacheType));
}
}
if (function_exists('opcache_reset') && filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) {
// This is a brute force clear of _all_ the cached files
// because simply clearing the files in $this->cachesToClear isn't enough.
// Perhaps if we could discern exactly which files to invalidate, we could
// take a more precise approach with @opcache_invalidate($file, true).
@opcache_reset();
$this->logger->notice('OPCache cleared!');
}
// the cache must be warmed after deleting files
$this->warmer->warmUp($this->cacheDir);
}
private function initialiseCacheTypeMap()
{
$fosJsRoutingFiles = [];
if ($this->installed) {
// avoid accessing FOS extractor before/during installation
// because this requires request context
foreach ($this->routingLocales as $locale) {
$fosJsRoutingFiles[] = $this->fosJsRoutesExtractor->getCachePath($locale);
}
}
$cacheFolder = $this->cacheDir . DIRECTORY_SEPARATOR;
$this->cacheTypes = [
'symfony.routing.generator' => [
$cacheFolder . 'url_generating_routes.php',
$cacheFolder . 'url_generating_routes.php.meta'
],
'symfony.routing.matcher' => [
$cacheFolder . 'url_matching_routes.php',
$cacheFolder . 'url_matching_routes.php.meta'
],
'symfony.routing.fosjs' => $fosJsRoutingFiles,
'symfony.config' => [
// clearing the container class will force all other container files
// to be rebuilt so there is no need to delete all of them
// nor a need to delete the container directory
$cacheFolder . $this->kernelContainerClass . '.php',
],
'symfony.translations' => [
$cacheFolder . '/translations'
],
'twig' => [
$cacheFolder . 'twig'
],
'purifier' => [
$cacheFolder . 'purifier'
],
'assets' => [
$cacheFolder . 'assets'
]
];
}
}