Passed
Push — master ( 2c93a3...b8d7dd )
by Jean-Christophe
03:19
created

CacheManager   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 31
eloc 87
dl 0
loc 147
ccs 68
cts 85
cp 0.8
rs 9.92
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A startProd() 0 4 1
A start() 0 6 1
A getAbsoluteCacheDirectory() 0 2 1
A clearCache() 0 5 2
A getCacheSubDirectory() 0 2 1
A getCacheInstance() 0 13 4
A register() 0 22 1
A initialGetCacheDirectory() 0 2 1
A _clearCache() 0 3 3
A getCacheDirectory() 0 2 1
A getCacheDirectories() 0 18 2
B initCache() 0 9 7
A checkCache() 0 6 2
A _getFiles() 0 6 2
A safeMkdir() 0 3 2
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 2
	public static function start(&$config) {
25 2
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
26 2
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
27 2
		Annotations::$config ['cache'] = new AnnotationCache ( $cacheDirectory . '/annotations' );
28 2
		self::register ( Annotations::getManager () );
29 2
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
30 2
	}
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
	public static function getCacheDirectory() {
63
		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 1
	public static function checkCache(&$config, $silent = false) {
75 1
		$dirs = self::getCacheDirectories ( $config, $silent );
76 1
		foreach ( $dirs as $dir ) {
77 1
			self::safeMkdir ( $dir );
78
		}
79 1
		return $dirs;
80
	}
81
82 2
	public static function getCacheDirectories(&$config, $silent = false) {
83 2
		$cacheDirectory = self::initialGetCacheDirectory ( $config );
84 2
		$rootDS=\ROOT . \DS ;
85 2
		if (! $silent) {
86 1
			echo "cache directory is " . UFileSystem::cleanPathname ( $rootDS. $cacheDirectory ) . "\n";
87
		}
88 2
		$cacheDirectory=$rootDS . $cacheDirectory . \DS;
89 2
		$modelsDir = str_replace ( "\\", \DS, $config ["mvcNS"] ["models"] );
90 2
		$controllersDir = str_replace ( "\\", \DS, $config ["mvcNS"] ["controllers"] );
91 2
		$annotationCacheDir = $cacheDirectory . "annotations";
92 2
		$modelsCacheDir = $cacheDirectory . $modelsDir;
93 2
		$queriesCacheDir = $cacheDirectory . "queries";
94 2
		$controllersCacheDir = $cacheDirectory . $controllersDir;
95 2
		$viewsCacheDir = $cacheDirectory . "views";
96 2
		$seoCacheDir = $cacheDirectory . "seo";
97 2
		$gitCacheDir = $cacheDirectory . "git";
98 2
		$contentsCacheDir = $cacheDirectory . "contents";
99 2
		return [ "annotations" => $annotationCacheDir,"models" => $modelsCacheDir,"controllers" => $controllersCacheDir,"queries" => $queriesCacheDir,"views" => $viewsCacheDir,"seo" => $seoCacheDir,"git" => $gitCacheDir,"contents"=>$contentsCacheDir ];
100
	}
101
102 1
	private static function safeMkdir($dir) {
103 1
		if (! is_dir ( $dir ))
104
			return mkdir ( $dir, 0777, true );
105 1
	}
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 1
	public static function initCache(&$config, $type = "all", $silent = false) {
121 1
		self::checkCache ( $config, $silent );
122 1
		self::start ( $config );
123 1
		if ($type === "all" || $type === "models")
124
			self::initModelsCache ( $config, false, $silent );
125 1
		if ($type === "all" || $type === "controllers")
126 1
			self::initRouterCache ( $config, $silent );
127 1
		if ($type === "all" || $type === "rest")
128
			self::initRestCache ( $config, $silent );
129 1
	}
130
131 4
	protected static function _getFiles(&$config, $type, $silent = false) {
132 4
		$typeNS = $config ["mvcNS"] [$type];
133 4
		$typeDir = \ROOT . \DS . str_replace ( "\\", \DS, $typeNS );
134 4
		if (! $silent)
135 1
			echo \ucfirst ( $type ) . " directory is " . \ROOT . $typeNS . "\n";
136 4
		return UFileSystem::glob_recursive ( $typeDir . \DS . '*' );
137
	}
138
139 2
	private static function register(AnnotationManager $annotationManager) {
140 2
		$annotationManager->registry = array_merge ( $annotationManager->registry, [ 
141 2
				'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 2
	}
163
}
164