Passed
Push — master ( 64da0c...c6d718 )
by Jean-Christophe
11:31
created

CacheManager::_clearCache()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 3
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 12
rs 10
c 0
b 0
f 0
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
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.1
23
 *
24
 */
25
class CacheManager {
26
	use RouterCacheTrait,ModelsCacheTrait,RestCacheTrait;
1 ignored issue
show
Bug introduced by
The trait Ubiquity\cache\traits\ModelsCacheTrait requires the property $name which is not provided by Ubiquity\cache\CacheManager.
Loading history...
27
28
	/**
29
	 *
30
	 * @var \Ubiquity\cache\system\AbstractDataCache
31
	 */
32
	public static $cache;
33
	private static $cacheDirectory;
34
35
	/**
36
	 * Starts the cache in dev mode, for generating the other caches
37
	 * Do not use in production
38
	 *
39
	 * @param array $config
40
	 */
41 23
	public static function start(&$config) {
42 23
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
43 23
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
44 23
		Annotations::$config ['cache'] = new AnnotationCache ( $cacheDirectory . '/annotations' );
45 23
		self::register ( Annotations::getManager () );
46 23
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
47 23
	}
48
49
	/**
50
	 * Starts the cache for production
51
	 *
52
	 * @param array $config
53
	 */
54 101
	public static function startProd(&$config) {
55 101
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
56 101
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
57 101
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
58 101
	}
59
60 107
	protected static function getCacheInstance(&$config, $cacheDirectory, $postfix) {
61 107
		$cacheSystem = 'Ubiquity\cache\system\ArrayCache';
62 107
		$cacheParams = [ ];
63 107
		if (! isset ( self::$cache )) {
64 100
			if (isset ( $config ["cache"] ["system"] )) {
65 100
				$cacheSystem = $config ["cache"] ["system"];
66
			}
67 100
			if (isset ( $config ["cache"] ["params"] )) {
68 100
				$cacheParams = $config ["cache"] ["params"];
69
			}
70 100
			self::$cache = new $cacheSystem ( $cacheDirectory, $postfix, $cacheParams );
71
		}
72 107
		return self::$cache;
73
	}
74
75 107
	private static function initialGetCacheDirectory(&$config) {
76 107
		return $config ["cache"] ["directory"] ?? ($config ["cache"] ["directory"] = "cache" . \DS);
77
	}
78
79
	/**
80
	 * Returns the relative cache directory
81
	 *
82
	 * @return string
83
	 */
84 2
	public static function getCacheDirectory() {
85 2
		return self::$cacheDirectory;
86
	}
87
88
	/**
89
	 * Returns the absolute cache directory
90
	 *
91
	 * @return string
92
	 */
93 2
	public static function getAbsoluteCacheDirectory() {
94 2
		return \ROOT . \DS . self::$cacheDirectory;
95
	}
96
97
	/**
98
	 * Returns an absolute cache subdirectory
99
	 *
100
	 * @param string $subDirectory
101
	 * @return string
102
	 */
103 17
	public static function getCacheSubDirectory($subDirectory) {
104 17
		return \ROOT . \DS . self::$cacheDirectory . \DS . $subDirectory;
105
	}
106
107
	/**
108
	 * Checks the existence of cache subdirectories and returns an array of cache folders
109
	 *
110
	 * @param array $config
111
	 * @param boolean $silent
112
	 * @return string[]
113
	 */
114 9
	public static function checkCache(&$config, $silent = false) {
115 9
		$dirs = self::getCacheDirectories ( $config, $silent );
116 9
		foreach ( $dirs as $dir ) {
117 9
			self::safeMkdir ( $dir );
118
		}
119 9
		return $dirs;
120
	}
121
122
	/**
123
	 * Returns an associative array of cache folders (annotations, models, controllers, queries, views, seo, git, contents)
124
	 *
125
	 * @param array $config
126
	 * @param boolean $silent
127
	 * @return string[]
128
	 */
129 11
	public static function getCacheDirectories(&$config, $silent = false) {
130 11
		$cacheDirectory = self::initialGetCacheDirectory ( $config );
131 11
		$rootDS = \ROOT . \DS;
132 11
		if (! $silent) {
133 9
			echo "cache directory is " . UFileSystem::cleanPathname ( $rootDS . $cacheDirectory ) . "\n";
134
		}
135 11
		$cacheDirectory = $rootDS . $cacheDirectory . \DS;
136 11
		$modelsDir = str_replace ( "\\", \DS, $config ["mvcNS"] ["models"] );
137 11
		$controllersDir = str_replace ( "\\", \DS, $config ["mvcNS"] ["controllers"] );
138 11
		$annotationCacheDir = $cacheDirectory . "annotations";
139 11
		$modelsCacheDir = $cacheDirectory . $modelsDir;
140 11
		$queriesCacheDir = $cacheDirectory . "queries";
141 11
		$controllersCacheDir = $cacheDirectory . $controllersDir;
142 11
		$viewsCacheDir = $cacheDirectory . "views";
143 11
		$seoCacheDir = $cacheDirectory . "seo";
144 11
		$gitCacheDir = $cacheDirectory . "git";
145 11
		$contentsCacheDir = $cacheDirectory . "contents";
146 11
		return [ "annotations" => $annotationCacheDir,"models" => $modelsCacheDir,"controllers" => $controllersCacheDir,"queries" => $queriesCacheDir,"views" => $viewsCacheDir,"seo" => $seoCacheDir,"git" => $gitCacheDir,"contents" => $contentsCacheDir ];
147
	}
148
149 9
	private static function safeMkdir($dir) {
150 9
		if (! is_dir ( $dir ))
151 2
			return mkdir ( $dir, 0777, true );
152 8
	}
153
154
	/**
155
	 * Deletes files from a cache type
156
	 *
157
	 * @param array $config
158
	 * @param string $type
159
	 */
160
	public static function clearCache(&$config, $type = "all") {
161
		$cacheDirectories = self::checkCache ( $config );
162
		$cacheDirs = [ "annotations","controllers","models","queries","views","contents" ];
163
		foreach ( $cacheDirs as $typeRef ) {
164
			self::_clearCache ( $cacheDirectories, $type, $typeRef );
165
		}
166
	}
167
168
	private static function _clearCache($cacheDirectories, $type, $typeRef) {
169
		if ($type === "all" || $type === $typeRef)
170
			UFileSystem::deleteAllFilesFromFolder ( $cacheDirectories [$typeRef] );
171
	}
172
173
	/**
174
	 *
175
	 * @param array $config
176
	 * @param string $type
177
	 * @param boolean $silent
178
	 */
179 7
	public static function initCache(&$config, $type = "all", $silent = false) {
180 7
		self::checkCache ( $config, $silent );
181 7
		self::start ( $config );
182 7
		if ($type === "all" || $type === "models")
183 4
			self::initModelsCache ( $config, false, $silent );
184 7
		if ($type === "all" || $type === "controllers")
185 7
			self::initRouterCache ( $config, $silent );
186 6
		if ($type === "all" || $type === "rest")
187 5
			self::initRestCache ( $config, $silent );
188 6
	}
189
190
	/**
191
	 * Returns an array of all defined routes, included REST routes
192
	 *
193
	 * @return array
194
	 */
195
	public static function getAllRoutes() {
196
		$routes = self::getControllerCache ();
197
		return array_merge ( $routes, self::getControllerCache ( true ) );
198
	}
199
200
	/**
201
	 * Returns an array of files from type $type
202
	 *
203
	 * @param array $config
204
	 * @param string $type
205
	 * @param boolean $silent
206
	 * @return array
207
	 */
208 21
	protected static function _getFiles(&$config, $type, $silent = false) {
209 21
		$typeNS = $config ["mvcNS"] [$type];
210 21
		$typeDir = \ROOT . \DS . str_replace ( "\\", \DS, $typeNS );
211 21
		if (! $silent)
212 10
			echo \ucfirst ( $type ) . " directory is " . \ROOT . $typeNS . "\n";
213 21
		return UFileSystem::glob_recursive ( $typeDir . \DS . '*.php' );
214
	}
215
216 23
	private static function register(AnnotationManager $annotationManager) {
217 23
		$annotationManager->registry = array_merge ( $annotationManager->registry, [
218 23
																						'id' => 'Ubiquity\annotations\IdAnnotation',
219
																						'manyToOne' => 'Ubiquity\annotations\ManyToOneAnnotation',
220
																						'oneToMany' => 'Ubiquity\annotations\OneToManyAnnotation',
221
																						'manyToMany' => 'Ubiquity\annotations\ManyToManyAnnotation',
222
																						'joinColumn' => 'Ubiquity\annotations\JoinColumnAnnotation',
223
																						'table' => 'Ubiquity\annotations\TableAnnotation',
224
																						'database' => 'Ubiquity\annotations\DatabaseAnnotation',
225
																						'transient' => 'Ubiquity\annotations\TransientAnnotation',
226
																						'column' => 'Ubiquity\annotations\ColumnAnnotation',
227
																						'validator' => 'Ubiquity\annotations\ValidatorAnnotation',
228
																						'transformer' => 'Ubiquity\annotations\TransformerAnnotation',
229
																						'joinTable' => 'Ubiquity\annotations\JoinTableAnnotation',
230
																						'requestMapping' => 'Ubiquity\annotations\router\RouteAnnotation',
231
																						'route' => 'Ubiquity\annotations\router\RouteAnnotation',
232
																						'get' => 'Ubiquity\annotations\router\GetAnnotation',
233
																						'getMapping' => 'Ubiquity\annotations\router\GetAnnotation',
234
																						'post' => 'Ubiquity\annotations\router\PostAnnotation',
235
																						'postMapping' => 'Ubiquity\annotations\router\PostAnnotation',
236
																						'var' => 'mindplay\annotations\standard\VarAnnotation',
237
																						'yuml' => 'Ubiquity\annotations\YumlAnnotation',
238
																						'rest' => 'Ubiquity\annotations\rest\RestAnnotation',
239
																						'authorization' => 'Ubiquity\annotations\rest\AuthorizationAnnotation',
240
																						'injected' => 'Ubiquity\annotations\di\InjectedAnnotation',
241
																						'autowired' => 'Ubiquity\annotations\di\AutowiredAnnotation' ] );
242 23
	}
243
}
244