1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Cache managment |
5
|
|
|
*/ |
6
|
|
|
namespace Ubiquity\cache; |
7
|
|
|
|
8
|
|
|
use Ubiquity\cache\traits\ModelsCacheTrait; |
9
|
|
|
use Ubiquity\cache\traits\RestCacheTrait; |
10
|
|
|
use Ubiquity\cache\traits\RouterCacheTrait; |
11
|
|
|
use Ubiquity\utils\base\UFileSystem; |
12
|
|
|
|
13
|
|
|
use Ubiquity\controllers\Startup; |
14
|
|
|
use Ubiquity\cache\traits\DevCacheTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Manager for caches (Router, Rest, models). |
18
|
|
|
* Ubiquity\cache$CacheManager |
19
|
|
|
* This class is part of Ubiquity |
20
|
|
|
* |
21
|
|
|
* @author jcheron <[email protected]> |
22
|
|
|
* @version 1.0.5 |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class CacheManager { |
26
|
|
|
use DevCacheTrait,RouterCacheTrait,ModelsCacheTrait,RestCacheTrait; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @var \Ubiquity\cache\system\AbstractDataCache |
31
|
|
|
*/ |
32
|
|
|
public static $cache; |
33
|
|
|
|
34
|
|
|
private static $cacheDirectory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Starts the cache for production |
38
|
|
|
* |
39
|
|
|
* @param array $config |
40
|
|
|
*/ |
41
|
186 |
|
public static function startProd(&$config) { |
42
|
186 |
|
self::$cacheDirectory = self::initialGetCacheDirectory($config); |
43
|
186 |
|
$cacheDirectory = \ROOT . \DS . self::$cacheDirectory; |
44
|
186 |
|
self::getCacheInstance($config, $cacheDirectory, '.cache'); |
45
|
186 |
|
} |
46
|
|
|
|
47
|
|
|
public static function startProdFromCtrl() { |
48
|
|
|
$config = &Startup::$config; |
49
|
|
|
$cacheD = \ROOT . \DS . ($config['cache']['directory'] ??= 'cache' . \DS); |
50
|
|
|
$cacheSystem = $config['cache']['system'] ?? 'Ubiquity\\cache\\system\\ArrayCache'; |
51
|
|
|
self::$cache = new $cacheSystem($cacheD, '.cache', $config['cache']['params'] ?? []); |
52
|
|
|
} |
53
|
|
|
|
54
|
192 |
|
protected static function getCacheInstance(&$config, $cacheDirectory, $postfix) { |
55
|
192 |
|
if (! isset(self::$cache)) { |
56
|
185 |
|
$cacheSystem = $config['cache']['system'] ?? 'Ubiquity\\cache\\system\\ArrayCache'; |
57
|
185 |
|
$cacheParams = $config['cache']['params'] ?? []; |
58
|
|
|
|
59
|
185 |
|
self::$cache = new $cacheSystem($cacheDirectory, $postfix, $cacheParams); |
60
|
|
|
} |
61
|
192 |
|
return self::$cache; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the relative cache directory |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
2 |
|
public static function getCacheDirectory() { |
71
|
2 |
|
return self::$cacheDirectory; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the absolute cache directory |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
2 |
|
public static function getAbsoluteCacheDirectory() { |
80
|
2 |
|
return \ROOT . \DS . self::$cacheDirectory; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns an absolute cache subdirectory |
85
|
|
|
* |
86
|
|
|
* @param string $subDirectory |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
8 |
|
public static function getCacheSubDirectory($subDirectory) { |
90
|
8 |
|
return \ROOT . \DS . self::$cacheDirectory . \DS . $subDirectory; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns an array of all defined routes, included REST routes |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public static function getAllRoutes() { |
100
|
|
|
$routes = self::getControllerCache(); |
101
|
|
|
return \array_merge($routes, self::getControllerCache(true)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns an array of files from type $type |
106
|
|
|
* |
107
|
|
|
* @param array $config |
108
|
|
|
* @param string $type |
109
|
|
|
* @param boolean $silent |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
51 |
|
protected static function _getFiles(&$config, $type, $silent = false) { |
113
|
51 |
|
$typeNS = $config['mvcNS'][$type]; |
114
|
51 |
|
$typeDir = \ROOT . \DS . \str_replace("\\", \DS, $typeNS); |
115
|
51 |
|
if (! $silent) |
116
|
11 |
|
echo \ucfirst($type) . ' directory is ' . \ROOT . $typeNS . "\n"; |
117
|
51 |
|
return UFileSystem::glob_recursive($typeDir . \DS . '*.php'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|