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