Passed
Push — master ( 71ddc3...43463e )
by Jean-Christophe
09:14
created

CacheManager::getAbsoluteCacheDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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