Passed
Push — master ( 8ceacb...8c98e8 )
by Jean-Christophe
15:18
created

CacheManager::getCacheDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\domains\DDDManager;
12
use Ubiquity\utils\base\UFileSystem;
13
14
use Ubiquity\controllers\Startup;
15
use Ubiquity\cache\traits\DevCacheTrait;
16
use Ubiquity\cache\traits\DevRouterCacheTrait;
17
18
/**
19
 * Manager for caches (Router, Rest, models).
20
 * Ubiquity\cache$CacheManager
21
 * This class is part of Ubiquity
22
 *
23
 * @author jcheron <[email protected]>
24
 * @version 1.0.6
25
 *
26
 */
27
class CacheManager {
28
	use DevCacheTrait,RouterCacheTrait,DevRouterCacheTrait,ModelsCacheTrait,RestCacheTrait;
29
30
	/**
31
	 *
32
	 * @var \Ubiquity\cache\system\AbstractDataCache
33
	 */
34
	public static $cache;
35
36
	private static $cacheDirectory;
37
38
	/**
39
	 * Starts the cache for production
40
	 *
41
	 * @param array $config
42
	 */
43 217
	public static function startProd(&$config) {
44 217
		self::$cacheDirectory = self::initialGetCacheDirectory($config);
45 217
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
46 217
		self::getCacheInstance($config, $cacheDirectory, '.cache');
47 217
	}
48
49
	/**
50
	 * Starts the cache from a controller
51
	 */
52
	public static function startProdFromCtrl() {
53
		$config = &Startup::$config;
54
		$cacheD = \ROOT . \DS . ($config['cache']['directory'] ??= 'cache' . \DS);
55
		$cacheSystem = $config['cache']['system'] ?? 'Ubiquity\\cache\\system\\ArrayCache';
56
		self::$cache = new $cacheSystem($cacheD, '.cache', $config['cache']['params'] ?? []);
57
	}
58
59 222
	protected static function getCacheInstance(&$config, $cacheDirectory, $postfix) {
60 222
		if (! isset(self::$cache)) {
61 216
			$cacheSystem = $config['cache']['system'] ?? 'Ubiquity\\cache\\system\\ArrayCache';
62 216
			$cacheParams = $config['cache']['params'] ?? [];
63
64 216
			self::$cache = new $cacheSystem($cacheDirectory, $postfix, $cacheParams);
65
		}
66 222
		return self::$cache;
67
	}
68
69
70
	/**
71
	 * Returns the relative cache directory
72
	 *
73
	 * @return string
74
	 */
75 2
	public static function getCacheDirectory() {
76 2
		return self::$cacheDirectory;
77
	}
78
79
	/**
80
	 * Returns the absolute cache directory
81
	 *
82
	 * @return string
83
	 */
84 1
	public static function getAbsoluteCacheDirectory() {
85 1
		return \ROOT . \DS . self::$cacheDirectory;
86
	}
87
88
	/**
89
	 * Returns an absolute cache subdirectory
90
	 *
91
	 * @param string $subDirectory
92
	 * @return string
93
	 */
94 8
	public static function getCacheSubDirectory($subDirectory) {
95 8
		return \ROOT . \DS . self::$cacheDirectory . \DS . $subDirectory;
96
	}
97
98
99
	/**
100
	 * Returns an array of all defined routes, included REST routes
101
	 *
102
	 * @return array
103
	 */
104
	public static function getAllRoutes() {
105
		$routes = self::getControllerCache();
106
		return \array_merge($routes, self::getControllerCache(true));
107
	}
108
109
	/**
110
	 * Returns an array of files from type $type
111
	 *
112
	 * @param array $config
113
	 * @param string $type
114
	 * @param boolean $silent
115
	 * @return array
116
	 */
117 50
	protected static function _getFiles(&$config, $type, $silent = false,$domain=null) {
118 50
		if($domain==null){
119 50
			$domainBase=Startup::getActiveDomainBase();
120
		}else{
121
			$domainBase=DDDManager::getDomainBase($domain);
122
		}
123 50
		$typeNS = $domainBase.($config['mvcNS'][$type])??$type;
124 50
		$typeDir = \ROOT . \DS . \str_replace("\\", \DS, $typeNS);
125 50
		if (! $silent) {
126 11
			echo \ucfirst($type) . ' directory is ' . \realpath($typeDir) . "\n";
127
		}
128 50
		return UFileSystem::glob_recursive($typeDir . \DS . '*.php');
129
	}
130
131
	/**
132
	 * Returns an array of all files from type $type
133
	 *
134
	 * @param array $config
135
	 * @param string $type
136
	 * @param boolean $silent
137
	 * @return array
138
	 */
139 47
	protected static function _getAllFiles(&$config, $type, $silent = false): array {
140 47
		$domains=DDDManager::getDomains();
141 47
		$result=[];
142 47
		foreach ($domains as $domain){
143
			$result=\array_merge($result,self::_getFiles($config,$type,$silent,$domain));
144
		}
145 47
		$result=\array_merge($result, self::_getFiles($config,$type,$silent,''));
146 47
		return $result;
147
	}
148
}
149