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