Passed
Push — master ( 8220c0...484027 )
by Jean-Christophe
09:22
created

CacheManager   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Test Coverage

Coverage 78.89%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 31
eloc 101
c 2
b 0
f 0
dl 0
loc 227
ccs 71
cts 90
cp 0.7889
rs 9.92

17 Methods

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