|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\cache\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UFileSystem; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* To be Used in dev mode, not in production |
|
9
|
|
|
* Ubiquity\cache\traits$DevCacheTrait |
|
10
|
|
|
* This class is part of Ubiquity |
|
11
|
|
|
* @author jc |
|
12
|
|
|
* @version 1.0.0 |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
trait DevCacheTrait { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array array of annotations name/class |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $registry; |
|
21
|
|
|
|
|
22
|
|
|
abstract protected static function getCacheInstance(&$config, $cacheDirectory, $postfix); |
|
23
|
|
|
|
|
24
|
|
|
abstract protected static function initRestCache(&$config, $silent = false); |
|
25
|
|
|
|
|
26
|
|
|
abstract protected static function initRouterCache(&$config, $silent = false); |
|
27
|
|
|
|
|
28
|
|
|
abstract public static function initModelsCache(&$config, $forChecking = false, $silent = false); |
|
29
|
|
|
|
|
30
|
|
|
private static function initialGetCacheDirectory(&$config) { |
|
31
|
|
|
return $config['cache']['directory'] ??= 'cache' . \DS; |
|
32
|
|
|
} |
|
33
|
|
|
/** |
|
34
|
|
|
* Starts the cache in dev mode, for generating the other caches |
|
35
|
|
|
* Do not use in production |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $config |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function start(&$config) { |
|
40
|
|
|
self::$registry=[ |
|
41
|
|
|
'id' => 'Ubiquity\annotations\IdAnnotation', |
|
42
|
|
|
'manyToOne' => 'Ubiquity\annotations\ManyToOneAnnotation', |
|
43
|
|
|
'oneToMany' => 'Ubiquity\annotations\OneToManyAnnotation', |
|
44
|
|
|
'manyToMany' => 'Ubiquity\annotations\ManyToManyAnnotation', |
|
45
|
|
|
'joinColumn' => 'Ubiquity\annotations\JoinColumnAnnotation', |
|
46
|
|
|
'table' => 'Ubiquity\annotations\TableAnnotation', |
|
47
|
|
|
'database' => 'Ubiquity\annotations\DatabaseAnnotation', |
|
48
|
|
|
'transient' => 'Ubiquity\annotations\TransientAnnotation', |
|
49
|
|
|
'column' => 'Ubiquity\annotations\ColumnAnnotation', |
|
50
|
|
|
'validator' => 'Ubiquity\annotations\ValidatorAnnotation', |
|
51
|
|
|
'transformer' => 'Ubiquity\annotations\TransformerAnnotation', |
|
52
|
|
|
'joinTable' => 'Ubiquity\annotations\JoinTableAnnotation', |
|
53
|
|
|
'requestMapping' => 'Ubiquity\annotations\router\RouteAnnotation', |
|
54
|
|
|
'route' => 'Ubiquity\annotations\router\RouteAnnotation', |
|
55
|
|
|
'get' => 'Ubiquity\annotations\router\GetAnnotation', |
|
56
|
|
|
'getMapping' => 'Ubiquity\annotations\router\GetAnnotation', |
|
57
|
|
|
'post' => 'Ubiquity\annotations\router\PostAnnotation', |
|
58
|
|
|
'postMapping' => 'Ubiquity\annotations\router\PostAnnotation', |
|
59
|
|
|
'put' => 'Ubiquity\annotations\router\PutAnnotation', |
|
60
|
|
|
'putMapping' => 'Ubiquity\annotations\router\PutAnnotation', |
|
61
|
|
|
'patch' => 'Ubiquity\annotations\router\PatchAnnotation', |
|
62
|
|
|
'patchMapping' => 'Ubiquity\annotations\router\PatchAnnotation', |
|
63
|
|
|
'delete' => 'Ubiquity\annotations\router\DeleteAnnotation', |
|
64
|
|
|
'deleteMapping' => 'Ubiquity\annotations\router\DeleteAnnotation', |
|
65
|
|
|
'options' => 'Ubiquity\annotations\router\OptionsAnnotation', |
|
66
|
|
|
'optionsMapping' => 'Ubiquity\annotations\router\OptionsAnnotation', |
|
67
|
|
|
'var' => 'mindplay\annotations\standard\VarAnnotation', |
|
68
|
|
|
'yuml' => 'Ubiquity\annotations\YumlAnnotation', |
|
69
|
|
|
'rest' => 'Ubiquity\annotations\rest\RestAnnotation', |
|
70
|
|
|
'authorization' => 'Ubiquity\annotations\rest\AuthorizationAnnotation', |
|
71
|
|
|
'injected' => 'Ubiquity\annotations\di\InjectedAnnotation', |
|
72
|
|
|
'autowired' => 'Ubiquity\annotations\di\AutowiredAnnotation' |
|
73
|
|
|
]; |
|
74
|
|
|
self::$cacheDirectory = self::initialGetCacheDirectory($config); |
|
|
|
|
|
|
75
|
|
|
$cacheDirectory = \ROOT . \DS . self::$cacheDirectory; |
|
76
|
|
|
Annotations::$config['cache'] = new AnnotationCache($cacheDirectory . '/annotations'); |
|
|
|
|
|
|
77
|
|
|
self::register(Annotations::getManager()); |
|
78
|
|
|
self::getCacheInstance($config, $cacheDirectory, '.cache')->init(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $nameClasses an array of name=>class annotations |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function registerAnnotations(string $nameClasses):void{ |
|
85
|
|
|
foreach ($nameClasses as $name=>$class){ |
|
|
|
|
|
|
86
|
|
|
self::$registry[$name]=$class; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected static function register(AnnotationManager $annotationManager) { |
|
|
|
|
|
|
91
|
|
|
$annotationManager->registry = \array_merge($annotationManager->registry, self::$registry); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Checks the existence of cache subdirectories and returns an array of cache folders |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $config |
|
98
|
|
|
* @param boolean $silent |
|
99
|
|
|
* @return string[] |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function checkCache(&$config, $silent = false) { |
|
102
|
|
|
$dirs = self::getCacheDirectories($config, $silent); |
|
103
|
|
|
foreach ($dirs as $dir) { |
|
104
|
|
|
self::safeMkdir($dir); |
|
105
|
|
|
} |
|
106
|
|
|
return $dirs; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns an associative array of cache folders (annotations, models, controllers, queries, views, seo, git, contents) |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $config |
|
113
|
|
|
* @param boolean $silent |
|
114
|
|
|
* @return string[] |
|
115
|
|
|
*/ |
|
116
|
|
|
public static function getCacheDirectories(&$config, $silent = false) { |
|
117
|
|
|
$cacheDirectory = self::initialGetCacheDirectory($config); |
|
118
|
|
|
$rootDS = \ROOT . \DS; |
|
119
|
|
|
if (! $silent) { |
|
120
|
|
|
echo "cache directory is " . UFileSystem::cleanPathname($rootDS . $cacheDirectory) . "\n"; |
|
121
|
|
|
} |
|
122
|
|
|
$cacheDirectory = $rootDS . $cacheDirectory . \DS; |
|
123
|
|
|
$modelsDir = str_replace("\\", \DS, $config['mvcNS']['models']); |
|
124
|
|
|
$controllersDir = str_replace("\\", \DS, $config['mvcNS']['controllers']); |
|
125
|
|
|
$annotationCacheDir = $cacheDirectory . 'annotations'; |
|
126
|
|
|
$modelsCacheDir = $cacheDirectory . $modelsDir; |
|
127
|
|
|
$queriesCacheDir = $cacheDirectory . 'queries'; |
|
128
|
|
|
$controllersCacheDir = $cacheDirectory . $controllersDir; |
|
129
|
|
|
$viewsCacheDir = $cacheDirectory . 'views'; |
|
130
|
|
|
$seoCacheDir = $cacheDirectory . 'seo'; |
|
131
|
|
|
$gitCacheDir = $cacheDirectory . 'git'; |
|
132
|
|
|
$contentsCacheDir = $cacheDirectory . 'contents'; |
|
133
|
|
|
return [ |
|
134
|
|
|
'annotations' => $annotationCacheDir, |
|
135
|
|
|
'models' => $modelsCacheDir, |
|
136
|
|
|
'controllers' => $controllersCacheDir, |
|
137
|
|
|
'queries' => $queriesCacheDir, |
|
138
|
|
|
'views' => $viewsCacheDir, |
|
139
|
|
|
'seo' => $seoCacheDir, |
|
140
|
|
|
'git' => $gitCacheDir, |
|
141
|
|
|
'contents' => $contentsCacheDir |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
private static function safeMkdir($dir) { |
|
146
|
|
|
if (! \is_dir($dir)) |
|
147
|
|
|
return \mkdir($dir, 0777, true); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Deletes files from a cache type |
|
152
|
|
|
* |
|
153
|
|
|
* @param array $config |
|
154
|
|
|
* @param string $type |
|
155
|
|
|
*/ |
|
156
|
|
|
public static function clearCache(&$config, $type = 'all') { |
|
157
|
|
|
$cacheDirectories = self::checkCache($config); |
|
158
|
|
|
$cacheDirs = [ |
|
159
|
|
|
'annotations', |
|
160
|
|
|
'controllers', |
|
161
|
|
|
'models', |
|
162
|
|
|
'queries', |
|
163
|
|
|
'views', |
|
164
|
|
|
'contents' |
|
165
|
|
|
]; |
|
166
|
|
|
foreach ($cacheDirs as $typeRef) { |
|
167
|
|
|
self::_clearCache($cacheDirectories, $type, $typeRef); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
private static function _clearCache($cacheDirectories, $type, $typeRef) { |
|
172
|
|
|
if ($type === 'all' || $type === $typeRef) |
|
173
|
|
|
UFileSystem::deleteAllFilesFromFolder($cacheDirectories[$typeRef]); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* |
|
178
|
|
|
* @param array $config |
|
179
|
|
|
* @param string $type |
|
180
|
|
|
* @param boolean $silent |
|
181
|
|
|
*/ |
|
182
|
|
|
public static function initCache(&$config, $type = 'all', $silent = false) { |
|
183
|
|
|
self::checkCache($config, $silent); |
|
184
|
|
|
self::start($config); |
|
185
|
|
|
if ($type === 'all' || $type === 'models'){ |
|
186
|
|
|
self::initModelsCache($config, false, $silent); |
|
187
|
|
|
} |
|
188
|
|
|
if ($type === 'all' || $type === 'controllers'){ |
|
189
|
|
|
self::initRouterCache($config, $silent); |
|
190
|
|
|
} |
|
191
|
|
|
if ($type === 'all' || $type === 'rest'){ |
|
192
|
|
|
self::initRestCache($config, $silent); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
|