Test Failed
Push — master ( d715ae...97f10b )
by Jean-Christophe
21:46
created

CacheManager::safeMkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
use mindplay\annotations\AnnotationCache;
13
use mindplay\annotations\AnnotationManager;
14
use mindplay\annotations\Annotations;
15
use Ubiquity\controllers\Startup;
16
use Ubiquity\cache\traits\DevCacheTrait;
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.5
25
 *
26
 */
27
class CacheManager {
28
	use DevCacheTrait,RouterCacheTrait,ModelsCacheTrait,RestCacheTrait;
0 ignored issues
show
Bug introduced by
The trait Ubiquity\cache\traits\ModelsCacheTrait requires the property $name which is not provided by Ubiquity\cache\CacheManager.
Loading history...
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 53
	public static function startProd(&$config) {
44 53
		self::$cacheDirectory = self::initialGetCacheDirectory($config);
45 53
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
46 53
		self::getCacheInstance($config, $cacheDirectory, '.cache');
47 53
	}
48 53
49 53
	public static function startProdFromCtrl() {
50
		$config = &Startup::$config;
51
		$cacheD = \ROOT . \DS . ($config['cache']['directory'] ??= 'cache' . \DS);
52
		$cacheSystem = $config['cache']['system'] ?? 'Ubiquity\\cache\\system\\ArrayCache';
53
		self::$cache = new $cacheSystem($cacheD, '.cache', $config['cache']['params'] ?? []);
54
	}
55
56 186
	protected static function getCacheInstance(&$config, $cacheDirectory, $postfix) {
57 186
		if (! isset(self::$cache)) {
58 186
			$cacheSystem = $config['cache']['system'] ?? 'Ubiquity\\cache\\system\\ArrayCache';
59 186
			$cacheParams = $config['cache']['params'] ?? [];
60 186
61
			self::$cache = new $cacheSystem($cacheDirectory, $postfix, $cacheParams);
62
		}
63
		return self::$cache;
64
	}
65
66
67
	/**
68
	 * Returns the relative cache directory
69 192
	 *
70 192
	 * @return string
71 185
	 */
72 185
	public static function getCacheDirectory() {
73
		return self::$cacheDirectory;
74 185
	}
75
76 192
	/**
77
	 * Returns the absolute cache directory
78
	 *
79 192
	 * @return string
80 192
	 */
81
	public static function getAbsoluteCacheDirectory() {
82
		return \ROOT . \DS . self::$cacheDirectory;
83
	}
84
85
	/**
86
	 * Returns an absolute cache subdirectory
87
	 *
88 2
	 * @param string $subDirectory
89 2
	 * @return string
90
	 */
91
	public static function getCacheSubDirectory($subDirectory) {
92
		return \ROOT . \DS . self::$cacheDirectory . \DS . $subDirectory;
93
	}
94
95
96
	/**
97 2
	 * Returns an array of all defined routes, included REST routes
98 2
	 *
99
	 * @return array
100
	 */
101
	public static function getAllRoutes() {
102
		$routes = self::getControllerCache();
103
		return \array_merge($routes, self::getControllerCache(true));
104
	}
105
106
	/**
107 8
	 * Returns an array of files from type $type
108 8
	 *
109
	 * @param array $config
110
	 * @param string $type
111
	 * @param boolean $silent
112
	 * @return array
113
	 */
114
	protected static function _getFiles(&$config, $type, $silent = false) {
115
		$typeNS = $config['mvcNS'][$type];
116
		$typeDir = \ROOT . \DS . \str_replace("\\", \DS, $typeNS);
117
		if (! $silent)
118 39
			echo \ucfirst($type) . ' directory is ' . \ROOT . $typeNS . "\n";
119 39
		return UFileSystem::glob_recursive($typeDir . \DS . '*.php');
120 39
	}
121
}
122