1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\cache\traits; |
4
|
|
|
|
5
|
|
|
use Ubiquity\annotations\AnnotationsEngineInterface; |
6
|
|
|
use Ubiquity\cache\system\AbstractDataCache; |
7
|
|
|
use Ubiquity\config\Configuration; |
8
|
|
|
use Ubiquity\utils\base\UFileSystem; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* To be Used in dev mode, not in production |
12
|
|
|
* Ubiquity\cache\traits$DevCacheTrait |
13
|
|
|
* This class is part of Ubiquity |
14
|
|
|
* |
15
|
|
|
* @author jc |
16
|
|
|
* @version 1.0.3 |
17
|
|
|
* |
18
|
|
|
* @property string $cacheDirectory |
19
|
|
|
*/ |
20
|
|
|
trait DevCacheTrait { |
21
|
|
|
|
22
|
|
|
private static AnnotationsEngineInterface $annotationsEngine; |
23
|
|
|
|
24
|
|
|
abstract protected static function getCacheInstance(array &$config, string $cacheDirectory, string $postfix): AbstractDataCache; |
25
|
|
|
|
26
|
|
|
abstract protected static function initRestCache(array &$config, bool $silent = false): void; |
27
|
|
|
|
28
|
|
|
abstract protected static function initRouterCache(array &$config, bool $silent = false): void; |
29
|
|
|
|
30
|
|
|
abstract public static function initModelsCache(array &$config, bool $forChecking = false, bool $silent = false): void; |
31
|
|
|
|
32
|
19 |
|
private static function _getAnnotationsEngineInstance(): ?AnnotationsEngineInterface { |
33
|
19 |
|
if (\class_exists('Ubiquity\\attributes\\AttributesEngine', true)) { |
34
|
|
|
return new \Ubiquity\attributes\AttributesEngine(); |
35
|
19 |
|
} elseif (\class_exists('Ubiquity\\annotations\\AnnotationsEngine', true)) { |
36
|
19 |
|
return new \Ubiquity\annotations\AnnotationsEngine(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
59 |
|
public static function getAnnotationsEngineInstance(): ?AnnotationsEngineInterface { |
41
|
59 |
|
return self::$annotationsEngine ??= self::_getAnnotationsEngineInstance(); |
42
|
|
|
} |
43
|
|
|
|
44
|
230 |
|
private static function initialGetCacheDirectory(array &$config): string { |
45
|
230 |
|
return $config['cache']['directory'] ??= 'cache' . \DS; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Starts the cache in dev mode, for generating the other caches |
50
|
|
|
* Do not use in production |
51
|
|
|
* |
52
|
|
|
* @param array $config |
53
|
|
|
*/ |
54
|
53 |
|
public static function start(array &$config) { |
55
|
53 |
|
self::$cacheDirectory = self::initialGetCacheDirectory($config); |
56
|
53 |
|
$cacheDirectory = \ROOT . \DS . self::$cacheDirectory; |
57
|
53 |
|
self::getAnnotationsEngineInstance()->start($cacheDirectory); |
58
|
53 |
|
self::getCacheInstance($config, $cacheDirectory, '.cache')->init(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* @param array $nameClasses |
64
|
|
|
* an array of name=>class annotations |
65
|
|
|
*/ |
66
|
|
|
public static function registerAnnotations(array $nameClasses): void { |
67
|
|
|
self::getAnnotationsEngineInstance()->registerAnnotations($nameClasses); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks the existence of cache subdirectories and returns an array of cache folders |
73
|
|
|
* |
74
|
|
|
* @param array $config |
75
|
|
|
* @param boolean $silent |
76
|
|
|
* @return string[] |
77
|
|
|
*/ |
78
|
39 |
|
public static function checkCache(array &$config, bool $silent = false): array { |
79
|
39 |
|
$dirs = self::getCacheDirectories($config, $silent); |
80
|
39 |
|
foreach ($dirs as $dir) { |
81
|
39 |
|
self::safeMkdir($dir); |
82
|
|
|
} |
83
|
39 |
|
return $dirs; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns an associative array of cache folders (annotations, models, controllers, queries, views, seo, git, contents) |
88
|
|
|
* |
89
|
|
|
* @param array $config |
90
|
|
|
* @param boolean $silent |
91
|
|
|
* @return string[] |
92
|
|
|
*/ |
93
|
41 |
|
public static function getCacheDirectories(array &$config, bool $silent = false): array { |
94
|
41 |
|
$cacheDirectory = self::initialGetCacheDirectory($config); |
95
|
41 |
|
$rootDS = \ROOT . \DS; |
96
|
41 |
|
if (!$silent) { |
97
|
10 |
|
echo "cache directory is " . UFileSystem::cleanPathname($rootDS . $cacheDirectory) . "\n"; |
98
|
|
|
} |
99
|
41 |
|
$cacheDirectory = $rootDS . $cacheDirectory . \DS; |
100
|
41 |
|
$modelsDir = \str_replace("\\", \DS, $config['mvcNS']['models']); |
101
|
41 |
|
$controllersDir = \str_replace("\\", \DS, $config['mvcNS']['controllers']); |
102
|
41 |
|
$annotationCacheDir = $cacheDirectory . 'annotations'; |
103
|
41 |
|
$modelsCacheDir = $cacheDirectory . $modelsDir; |
104
|
41 |
|
$queriesCacheDir = $cacheDirectory . 'queries'; |
105
|
41 |
|
$controllersCacheDir = $cacheDirectory . $controllersDir; |
106
|
41 |
|
$viewsCacheDir = $cacheDirectory . 'views'; |
107
|
41 |
|
$seoCacheDir = $cacheDirectory . 'seo'; |
108
|
41 |
|
$gitCacheDir = $cacheDirectory . 'git'; |
109
|
41 |
|
$contentsCacheDir = $cacheDirectory . 'contents'; |
110
|
41 |
|
$configCacheDir = $cacheDirectory . 'config'; |
111
|
41 |
|
return [ |
112
|
41 |
|
'annotations' => $annotationCacheDir, |
113
|
41 |
|
'models' => $modelsCacheDir, |
114
|
41 |
|
'controllers' => $controllersCacheDir, |
115
|
41 |
|
'queries' => $queriesCacheDir, |
116
|
41 |
|
'views' => $viewsCacheDir, |
117
|
41 |
|
'seo' => $seoCacheDir, |
118
|
41 |
|
'git' => $gitCacheDir, |
119
|
41 |
|
'contents' => $contentsCacheDir, |
120
|
41 |
|
'config' => $configCacheDir |
121
|
41 |
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
39 |
|
private static function safeMkdir(string $dir): bool { |
125
|
39 |
|
if (!\is_dir($dir)) { |
126
|
2 |
|
return \mkdir($dir, 0777, true); |
127
|
|
|
} |
128
|
39 |
|
return true; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Deletes files from a cache type |
133
|
|
|
* |
134
|
|
|
* @param array $config |
135
|
|
|
* @param string $type |
136
|
|
|
*/ |
137
|
|
|
public static function clearCache(array &$config, string $type = 'all') { |
138
|
|
|
$cacheDirectories = self::checkCache($config); |
139
|
|
|
$cacheDirs = [ |
140
|
|
|
'annotations', |
141
|
|
|
'controllers', |
142
|
|
|
'models', |
143
|
|
|
'queries', |
144
|
|
|
'views', |
145
|
|
|
'contents', |
146
|
|
|
'config' |
147
|
|
|
]; |
148
|
|
|
foreach ($cacheDirs as $typeRef) { |
149
|
|
|
self::_clearCache($cacheDirectories, $type, $typeRef); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private static function _clearCache($cacheDirectories, $type, $typeRef) { |
154
|
|
|
if ($type === 'all' || $type === $typeRef) { |
155
|
|
|
UFileSystem::deleteAllFilesFromFolder($cacheDirectories[$typeRef]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* |
161
|
|
|
* @param array $config |
162
|
|
|
* @param string $type |
163
|
|
|
* @param boolean $silent |
164
|
|
|
*/ |
165
|
37 |
|
public static function initCache(array &$config, string $type = 'all', bool $silent = false): void { |
166
|
37 |
|
self::checkCache($config, $silent); |
167
|
37 |
|
self::start($config); |
168
|
37 |
|
if ($type === 'all' || $type === 'models') { |
169
|
34 |
|
self::initModelsCache($config, false, $silent); |
170
|
|
|
} |
171
|
37 |
|
if ($type === 'all' || $type === 'controllers') { |
172
|
8 |
|
if (\class_exists('\\Ubiquity\\security\\acl\\AclManager')) { |
173
|
|
|
self::getAnnotationsEngineInstance()->registerAcls(); |
174
|
|
|
} |
175
|
8 |
|
self::initRouterCache($config, $silent); |
176
|
|
|
} |
177
|
37 |
|
if ($type === 'all' || $type === 'acls') { |
178
|
5 |
|
if (\class_exists('\\Ubiquity\\security\\acl\\AclManager')) { |
179
|
|
|
\Ubiquity\security\acl\AclManager::initCache($config); |
180
|
|
|
} |
181
|
|
|
} |
182
|
37 |
|
if ($type === 'all' || $type === 'rest') { |
183
|
6 |
|
self::initRestCache($config, $silent); |
184
|
|
|
} |
185
|
37 |
|
if ($type === 'all' || $type === 'config') { |
186
|
5 |
|
Configuration::generateCache($silent); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|