-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.php
140 lines (121 loc) · 3.99 KB
/
Cache.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
<?php
namespace Bolt;
use Bolt\Common\Deprecated;
use Bolt\Filesystem\CompositeFilesystemInterface;
use Bolt\Filesystem\Exception\IOException;
use Bolt\Filesystem\Handler\DirectoryInterface;
use Bolt\Filesystem\Handler\HandlerInterface;
use Doctrine\Common\Cache\FilesystemCache;
/**
* Simple, file based cache for volatile data. Useful for storing non-vital
* information like feeds, and other stuff that can be recovered easily.
*
* @author Bob den Otter <[email protected]>
* @author Gawain Lynch <[email protected]>
* @author Carson Full <[email protected]>
*/
class Cache extends FilesystemCache
{
/** Max cache age. Default 10 minutes. */
const DEFAULT_MAX_AGE = 600;
/** Default cache file extension. */
const EXTENSION = '.data';
/** @var CompositeFilesystemInterface */
private $filesystem;
/** @var int */
private $umask;
/**
* Cache constructor.
*
* @param string $directory
* @param string $extension
* @param int $umask
* @param CompositeFilesystemInterface $filesystem
*/
public function __construct($directory, $extension = self::EXTENSION, $umask = 0002, CompositeFilesystemInterface $filesystem = null)
{
umask($umask);
$this->filesystem = $filesystem;
$this->umask = $umask;
parent::__construct($directory, $extension, $umask);
}
/**
* @deprecated Deprecated since 3.0, to be removed in 4.0. Use flushAll() instead.
*/
public function clearCache()
{
Deprecated::method(3.0, 'flushAll');
$this->flushAll();
return [
'successfiles' => 0,
'failedfiles' => 0,
'failed' => [],
'successfolders' => 0,
'failedfolders' => 0,
'log' => '',
];
}
/**
* Clear the cache. Both the doctrine FilesystemCache, as well as twig and thumbnail temp files.
*
* @return bool
*/
protected function doFlush()
{
// Clear Doctrine's folder.
$result = parent::doFlush();
if ($this->filesystem instanceof CompositeFilesystemInterface) {
$cacheFs = $this->filesystem->getFilesystem('cache');
// Clear our cached configuration
if ($cacheFs->has('config-cache.json')) {
$cacheFs->delete('config-cache.json');
}
// Clear our own cache folder.
$this->flushDirectory($cacheFs->getDir('/development'));
$this->flushDirectory($cacheFs->getDir('/exception'));
$this->flushDirectory($cacheFs->getDir('/production'));
$this->flushDirectory($cacheFs->getDir('/profiler'));
$this->flushDirectory($cacheFs->getDir('/trans'));
// Clear the thumbs folder.
$this->flushDirectory($this->filesystem->getFilesystem('web')->getDir('/thumbs'));
// We need to recreate our base Doctrine cache directory, as it
// will be a subdirectory of one of the ones we just wiped.
$this->createPathIfNeeded();
}
return $result;
}
/**
* Helper function for doFlush().
*
* @param DirectoryInterface $directory
*/
private function flushDirectory(DirectoryInterface $directory)
{
if (!$directory->exists()) {
return;
}
$files = $directory->find()
->ignoreDotFiles()
->ignoreVCS()
;
/** @var HandlerInterface $file */
foreach ($files as $file) {
try {
$file->delete();
} catch (IOException $e) {
}
}
}
/**
* Create base path path if needed.
*
* @return bool
*/
private function createPathIfNeeded()
{
if (!is_dir($this->directory)) {
return @mkdir($this->directory, 0777 & (~$this->umask), true) && !is_dir($this->directory);
}
return true;
}
}