Passed
Push — master ( d06d2d...52dbea )
by Jean-Christophe
01:29
created

CacheManager::getCacheDirectories()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 2
dl 0
loc 17
rs 9.7666
c 0
b 0
f 0
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
	const ROOT=ROOT;
18
	const DS=DS;
0 ignored issues
show
Bug introduced by
The constant Ubiquity\cache\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
	
20
	/**
21
	 *
22
	 * @var AbstractDataCache
23
	 */
24
	public static $cache;
25
	private static $cacheDirectory;
26
27
	public static function start(&$config) {
28
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
29
		$cacheDirectory = self::ROOT . self::DS . self::$cacheDirectory;
30
		Annotations::$config ['cache'] = new AnnotationCache ( $cacheDirectory . '/annotations' );
31
		self::register ( Annotations::getManager () );
32
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
33
	}
34
35
	/**
36
	 * Starts the cache for production
37
	 *
38
	 * @param array $config
39
	 */
40
	public static function startProd(&$config) {
41
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
42
		$cacheDirectory = self::ROOT . self::DS . self::$cacheDirectory;
43
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
44
	}
45
46
	protected static function getCacheInstance(&$config, $cacheDirectory, $postfix) {
47
		$cacheSystem = 'Ubiquity\cache\system\ArrayCache';
48
		$cacheParams = [ ];
49
		if (! isset ( self::$cache )) {
50
			if (isset ( $config ["cache"] ["system"] )) {
51
				$cacheSystem = $config ["cache"] ["system"];
52
			}
53
			if (isset ( $config ["cache"] ["params"] )) {
54
				$cacheParams = $config ["cache"] ["params"];
55
			}
56
			self::$cache = new $cacheSystem ( $cacheDirectory, $postfix, $cacheParams );
57
		}
58
		return self::$cache;
59
	}
60
61
	private static function initialGetCacheDirectory(&$config) {
62
		$cacheDirectory = @$config ["cache"] ["directory"];
63
		if (! isset ( $cacheDirectory )) {
64
			$config ["cache"] ["directory"] = "cache/";
65
			$cacheDirectory = $config ["cache"] ["directory"];
66
		}
67
		return $cacheDirectory;
68
	}
69
70
	public static function getCacheDirectory() {
71
		return self::$cacheDirectory;
72
	}
73
	
74
	public static function getCacheSubDirectory($subDirectory) {
75
		return self::ROOT.self::DS.self::$cacheDirectory.self::DS.$subDirectory;
76
	}
77
78
	public static function checkCache(&$config, $silent = false) {
79
		$dirs = self::getCacheDirectories ( $config, $silent );
80
		foreach ( $dirs as $dir ) {
81
			self::safeMkdir ( $dir );
82
		}
83
		return $dirs;
84
	}
85
86
	public static function getCacheDirectories(&$config, $silent = false) {
87
		$cacheDirectory = self::initialGetCacheDirectory ( $config );
88
		$rootDS=self::ROOT . self::DS ;
89
		if (! $silent) {
90
			echo "cache directory is " . UFileSystem::cleanPathname ( $rootDS. $cacheDirectory ) . "\n";
91
		}
92
		$modelsDir = str_replace ( "\\", self::DS, $config ["mvcNS"] ["models"] );
93
		$controllersDir = str_replace ( "\\", self::DS, $config ["mvcNS"] ["controllers"] );
94
		$annotationCacheDir = $rootDS . $cacheDirectory . self::DS . "annotations";
95
		$modelsCacheDir = $rootDS . $cacheDirectory . self::DS . $modelsDir;
96
		$queriesCacheDir = $rootDS . $cacheDirectory . self::DS. "queries";
97
		$controllersCacheDir = $rootDS . $cacheDirectory . self::DS . $controllersDir;
98
		$viewsCacheDir = $rootDS . $cacheDirectory . self::DS . "views";
99
		$seoCacheDir = $rootDS . $cacheDirectory . self::DS . "seo";
100
		$gitCacheDir = $rootDS . $cacheDirectory . self::DS . "git";
101
		$contentsCacheDir = $rootDS. $cacheDirectory . self::DS . "contents";
102
		return [ "annotations" => $annotationCacheDir,"models" => $modelsCacheDir,"controllers" => $controllersCacheDir,"queries" => $queriesCacheDir,"views" => $viewsCacheDir,"seo" => $seoCacheDir,"git" => $gitCacheDir,"contents"=>$contentsCacheDir ];
103
	}
104
105
	private static function safeMkdir($dir) {
106
		if (! is_dir ( $dir ))
107
			return mkdir ( $dir, 0777, true );
108
	}
109
110
	public static function clearCache(&$config, $type = "all") {
111
		$cacheDirectories = self::checkCache ( $config );
112
		$cacheDirs = [ "annotations","controllers","models","queries","views","contents" ];
113
		foreach ( $cacheDirs as $typeRef ) {
114
			self::_clearCache ( $cacheDirectories, $type, $typeRef );
115
		}
116
	}
117
118
	private static function _clearCache($cacheDirectories, $type, $typeRef) {
119
		if ($type === "all" || $type === $typeRef)
120
			UFileSystem::deleteAllFilesFromFolder ( $cacheDirectories [$typeRef] );
121
	}
122
123
	public static function initCache(&$config, $type = "all", $silent = false) {
124
		self::checkCache ( $config, $silent );
125
		self::start ( $config );
126
		if ($type === "all" || $type === "models")
127
			self::initModelsCache ( $config, false, $silent );
128
		if ($type === "all" || $type === "controllers")
129
			self::initRouterCache ( $config, $silent );
130
		if ($type === "all" || $type === "rest")
131
			self::initRestCache ( $config, $silent );
132
	}
133
134
	protected static function _getFiles(&$config, $type, $silent = false) {
135
		$typeNS = $config ["mvcNS"] [$type];
136
		$typeDir = self::ROOT . self::DS . str_replace ( "\\", self::DS, $typeNS );
137
		if (! $silent)
138
			echo \ucfirst ( $type ) . " directory is " . self::ROOT . $typeNS . "\n";
139
		return UFileSystem::glob_recursive ( $typeDir . self::DS . '*' );
140
	}
141
142
	private static function register(AnnotationManager $annotationManager) {
143
		$annotationManager->registry = array_merge ( $annotationManager->registry, [ 
144
				'id' => 'Ubiquity\annotations\IdAnnotation',
145
				'manyToOne' => 'Ubiquity\annotations\ManyToOneAnnotation',
146
				'oneToMany' => 'Ubiquity\annotations\OneToManyAnnotation',
147
				'manyToMany' => 'Ubiquity\annotations\ManyToManyAnnotation',
148
				'joinColumn' => 'Ubiquity\annotations\JoinColumnAnnotation',
149
				'table' => 'Ubiquity\annotations\TableAnnotation',
150
				'transient' => 'Ubiquity\annotations\TransientAnnotation',
151
				'column' => 'Ubiquity\annotations\ColumnAnnotation',
152
				'validator' => 'Ubiquity\annotations\ValidatorAnnotation',
153
				'joinTable' => 'Ubiquity\annotations\JoinTableAnnotation',
154
				'requestMapping' => 'Ubiquity\annotations\router\RouteAnnotation',
155
				'route' => 'Ubiquity\annotations\router\RouteAnnotation',
156
				'get' => 'Ubiquity\annotations\router\GetAnnotation','getMapping' => 'Ubiquity\annotations\router\GetAnnotation',
157
				'post' => 'Ubiquity\annotations\router\PostAnnotation','postMapping' => 'Ubiquity\annotations\router\PostAnnotation',
158
				'var' => 'mindplay\annotations\standard\VarAnnotation',
159
				'yuml' => 'Ubiquity\annotations\YumlAnnotation',
160
				'rest' => 'Ubiquity\annotations\rest\RestAnnotation',
161
				'authorization' => 'Ubiquity\annotations\rest\AuthorizationAnnotation',
162
				'injected' =>'Ubiquity\annotations\di\InjectedAnnotation',
163
				'autowired' =>'Ubiquity\annotations\di\AutowiredAnnotation'
164
		] );
165
	}
166
}
167