|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace micro\cache; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\annotations\Annotations; |
|
6
|
|
|
use mindplay\annotations\AnnotationCache; |
|
7
|
|
|
use mindplay\annotations\AnnotationManager; |
|
8
|
|
|
use micro\controllers\Router; |
|
9
|
|
|
use micro\utils\FsUtils; |
|
10
|
|
|
use micro\cache\traits\RouterCacheTrait; |
|
11
|
|
|
use micro\cache\traits\ModelsCacheTrait; |
|
12
|
|
|
use micro\cache\traits\RestCacheTrait; |
|
13
|
|
|
|
|
14
|
|
|
class CacheManager { |
|
15
|
|
|
use RouterCacheTrait,ModelsCacheTrait,RestCacheTrait; |
|
16
|
|
|
|
|
17
|
|
|
public static $cache; |
|
18
|
|
|
private static $cacheDirectory; |
|
19
|
|
|
|
|
20
|
|
|
public static function start(&$config) { |
|
21
|
|
|
self::$cacheDirectory=self::initialGetCacheDirectory($config); |
|
22
|
|
|
$cacheDirectory=ROOT . DS . self::$cacheDirectory; |
|
23
|
|
|
Annotations::$config['cache']=new AnnotationCache($cacheDirectory . '/annotations'); |
|
24
|
|
|
self::register(Annotations::getManager()); |
|
25
|
|
|
self::getCacheInstance($config, $cacheDirectory, ".cache"); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public static function startProd(&$config) { |
|
29
|
|
|
self::$cacheDirectory=self::initialGetCacheDirectory($config); |
|
30
|
|
|
$cacheDirectory=ROOT . DS . self::$cacheDirectory; |
|
31
|
|
|
self::getCacheInstance($config,$cacheDirectory, ".cache"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected static function getCacheInstance(&$config,$cacheDirectory,$postfix){ |
|
35
|
|
|
$cacheSystem='micro\cache\system\ArrayCache'; |
|
36
|
|
|
if(!isset(self::$cache)){ |
|
37
|
|
|
if(isset($config["cache"]["system"])){ |
|
38
|
|
|
$cacheSystem=$config["cache"]["system"]; |
|
39
|
|
|
} |
|
40
|
|
|
self::$cache=new $cacheSystem($cacheDirectory,$postfix); |
|
41
|
|
|
} |
|
42
|
|
|
return self::$cache; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private static function initialGetCacheDirectory(&$config) { |
|
46
|
|
|
$cacheDirectory=@$config["cache"]["directory"]; |
|
47
|
|
|
if (!isset($cacheDirectory)) { |
|
48
|
|
|
$config["cache"]["directory"]="cache/"; |
|
49
|
|
|
$cacheDirectory=$config["cache"]["directory"]; |
|
50
|
|
|
} |
|
51
|
|
|
return $cacheDirectory; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function getCacheDirectory() { |
|
55
|
|
|
return self::$cacheDirectory; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function checkCache(&$config,$silent=false) { |
|
59
|
|
|
$dirs=self::getCacheDirectories($config,$silent); |
|
60
|
|
|
foreach ($dirs as $dir){ |
|
61
|
|
|
self::safeMkdir($dir); |
|
62
|
|
|
} |
|
63
|
|
|
return $dirs; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function getCacheDirectories(&$config,$silent=false){ |
|
67
|
|
|
$cacheDirectory=self::initialGetCacheDirectory($config); |
|
68
|
|
|
if(!$silent){ |
|
69
|
|
|
echo "cache directory is " . FsUtils::cleanPathname(ROOT . DS . $cacheDirectory) . "\n"; |
|
70
|
|
|
} |
|
71
|
|
|
$modelsDir=str_replace("\\", DS, $config["mvcNS"]["models"]); |
|
72
|
|
|
$controllersDir=str_replace("\\", DS, $config["mvcNS"]["controllers"]); |
|
73
|
|
|
$annotationCacheDir=ROOT . DS . $cacheDirectory . DS . "annotations"; |
|
74
|
|
|
$modelsCacheDir=ROOT . DS . $cacheDirectory . DS . $modelsDir; |
|
75
|
|
|
$queriesCacheDir=ROOT . DS . $cacheDirectory . DS . "queries"; |
|
76
|
|
|
$controllersCacheDir=ROOT . DS . $cacheDirectory . DS . $controllersDir; |
|
77
|
|
|
$viewsCacheDir=ROOT . DS . $cacheDirectory . DS . "views"; |
|
78
|
|
|
return [ "annotations" => $annotationCacheDir,"models" => $modelsCacheDir,"controllers" => $controllersCacheDir,"queries" => $queriesCacheDir ,"views"=>$viewsCacheDir]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private static function safeMkdir($dir) { |
|
82
|
|
|
if (!is_dir($dir)) |
|
83
|
|
|
return mkdir($dir, 0777, true); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public static function clearCache(&$config, $type="all") { |
|
87
|
|
|
$cacheDirectories=self::checkCache($config); |
|
88
|
|
|
$cacheDirs=["annotations","controllers","models","queries","views"]; |
|
89
|
|
|
foreach ($cacheDirs as $typeRef){ |
|
90
|
|
|
self::_clearCache($cacheDirectories, $type, $typeRef); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private static function _clearCache($cacheDirectories,$type,$typeRef){ |
|
95
|
|
|
if ($type === "all" || $type === $typeRef) |
|
96
|
|
|
FsUtils::deleteAllFilesFromFolder($cacheDirectories[$typeRef]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function initCache(&$config, $type="all",$silent=false) { |
|
100
|
|
|
self::checkCache($config,$silent); |
|
101
|
|
|
self::start($config); |
|
102
|
|
|
if ($type === "all" || $type === "models") |
|
103
|
|
|
self::initModelsCache($config,false,$silent); |
|
104
|
|
|
if ($type === "all" || $type === "controllers") |
|
105
|
|
|
self::initRouterCache($config,$silent); |
|
106
|
|
|
if ($type === "all" || $type === "rest") |
|
107
|
|
|
self::initRestCache($config,$silent); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private static function _getFiles(&$config,$type,$silent=false){ |
|
|
|
|
|
|
111
|
|
|
$typeNS=$config["mvcNS"][$type]; |
|
112
|
|
|
$typeDir=ROOT . DS . str_replace("\\", DS, $typeNS); |
|
113
|
|
|
if(!$silent) |
|
114
|
|
|
echo \ucfirst($type)." directory is " . ROOT . $typeNS . "\n"; |
|
115
|
|
|
return FsUtils::glob_recursive($typeDir . DS . '*'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private static function register(AnnotationManager $annotationManager) { |
|
119
|
|
|
$annotationManager->registry=array_merge($annotationManager->registry, [ |
|
120
|
|
|
'id' => 'micro\annotations\IdAnnotation', |
|
121
|
|
|
'manyToOne' => 'micro\annotations\ManyToOneAnnotation', |
|
122
|
|
|
'oneToMany' => 'micro\annotations\OneToManyAnnotation', |
|
123
|
|
|
'manyToMany' => 'micro\annotations\ManyToManyAnnotation', |
|
124
|
|
|
'joinColumn' => 'micro\annotations\JoinColumnAnnotation', |
|
125
|
|
|
'table' => 'micro\annotations\TableAnnotation', |
|
126
|
|
|
'transient' => 'micro\annotations\TransientAnnotation', |
|
127
|
|
|
'column' => 'micro\annotations\ColumnAnnotation', |
|
128
|
|
|
'joinTable' => 'micro\annotations\JoinTableAnnotation', |
|
129
|
|
|
'route' => 'micro\annotations\router\RouteAnnotation', |
|
130
|
|
|
'var' => 'mindplay\annotations\standard\VarAnnotation', |
|
131
|
|
|
'yuml' => 'micro\annotations\YumlAnnotation', |
|
132
|
|
|
'rest' => 'micro\annotations\rest\RestAnnotation', |
|
133
|
|
|
'authorization' => 'micro\annotations\rest\AuthorizationAnnotation' |
|
134
|
|
|
]); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|