Passed
Push — master ( b68a78...d29a76 )
by Jean-Christophe
16:10 queued 03:52
created

DevCacheTrait::initCache()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.048

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 10
c 2
b 0
f 2
dl 0
loc 14
ccs 10
cts 11
cp 0.9091
rs 8.4444
cc 8
nc 12
nop 3
crap 8.048
1
<?php
2
namespace Ubiquity\cache\traits;
3
4
use Ubiquity\utils\base\UFileSystem;
5
use Ubiquity\annotations\AnnotationsEngineInterface;
6
7
/**
8
 * To be Used in dev mode, not in production
9
 * Ubiquity\cache\traits$DevCacheTrait
10
 * This class is part of Ubiquity
11
 *
12
 * @author jc
13
 * @version 1.0.2
14
 *
15
 * @property string $cacheDirectory
16
 */
17
trait DevCacheTrait {
18
19
	private static AnnotationsEngineInterface $annotationsEngine;
20
21
	abstract protected static function getCacheInstance(&$config, $cacheDirectory, $postfix);
22
23
	abstract protected static function initRestCache(&$config, $silent = false);
24
25
	abstract protected static function initRouterCache(&$config, $silent = false);
26
27
	abstract public static function initModelsCache(&$config, $forChecking = false, $silent = false);
28
29 19
	private static function _getAnnotationsEngineInstance(){
30 19
		if(\class_exists('Ubiquity\\attributes\\AttributesEngine',true)){
31
			return new \Ubiquity\attributes\AttributesEngine();
32 19
		}elseif(\class_exists('Ubiquity\\annotations\\AnnotationsEngine',true)){
33 19
			return new \Ubiquity\annotations\AnnotationsEngine();
34
		}
35
	}
36
37 60
	public static function getAnnotationsEngineInstance(){
38 60
		return self::$annotationsEngine??=self::_getAnnotationsEngineInstance();
39
	}
40
41 192
	private static function initialGetCacheDirectory(&$config) {
42 192
		return $config['cache']['directory'] ??= 'cache' . \DS;
43
	}
44
45
	/**
46
	 * Starts the cache in dev mode, for generating the other caches
47
	 * Do not use in production
48
	 *
49
	 * @param array $config
50
	 */
51 54
	public static function start(&$config) {
52 54
		self::$cacheDirectory = self::initialGetCacheDirectory($config);
53 54
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
54 54
		self::getAnnotationsEngineInstance()->start($cacheDirectory);
55 54
		self::getCacheInstance($config, $cacheDirectory, '.cache')->init();
56 54
	}
57
58
	/**
59
	 *
60
	 * @param array $nameClasses
61
	 *        	an array of name=>class annotations
62
	 */
63
	public static function registerAnnotations(array $nameClasses): void {
64
		self::getAnnotationsEngineInstance()->registerAnnotations($nameClasses);
65
	}
66
67
68
	/**
69
	 * Checks the existence of cache subdirectories and returns an array of cache folders
70
	 *
71
	 * @param array $config
72
	 * @param boolean $silent
73
	 * @return string[]
74
	 */
75 39
	public static function checkCache(&$config, $silent = false) {
76 39
		$dirs = self::getCacheDirectories($config, $silent);
77 39
		foreach ($dirs as $dir) {
78 39
			self::safeMkdir($dir);
79
		}
80 39
		return $dirs;
81
	}
82
83
	/**
84
	 * Returns an associative array of cache folders (annotations, models, controllers, queries, views, seo, git, contents)
85
	 *
86
	 * @param array $config
87
	 * @param boolean $silent
88
	 * @return string[]
89
	 */
90 41
	public static function getCacheDirectories(&$config, $silent = false) {
91 41
		$cacheDirectory = self::initialGetCacheDirectory($config);
92 41
		$rootDS = \ROOT . \DS;
93 41
		if (! $silent) {
94 10
			echo "cache directory is " . UFileSystem::cleanPathname($rootDS . $cacheDirectory) . "\n";
95
		}
96 41
		$cacheDirectory = $rootDS . $cacheDirectory . \DS;
97 41
		$modelsDir = str_replace("\\", \DS, $config['mvcNS']['models']);
98 41
		$controllersDir = str_replace("\\", \DS, $config['mvcNS']['controllers']);
99 41
		$annotationCacheDir = $cacheDirectory . 'annotations';
100 41
		$modelsCacheDir = $cacheDirectory . $modelsDir;
101 41
		$queriesCacheDir = $cacheDirectory . 'queries';
102 41
		$controllersCacheDir = $cacheDirectory . $controllersDir;
103 41
		$viewsCacheDir = $cacheDirectory . 'views';
104 41
		$seoCacheDir = $cacheDirectory . 'seo';
105 41
		$gitCacheDir = $cacheDirectory . 'git';
106 41
		$contentsCacheDir = $cacheDirectory . 'contents';
107
		return [
108 41
			'annotations' => $annotationCacheDir,
109 41
			'models' => $modelsCacheDir,
110 41
			'controllers' => $controllersCacheDir,
111 41
			'queries' => $queriesCacheDir,
112 41
			'views' => $viewsCacheDir,
113 41
			'seo' => $seoCacheDir,
114 41
			'git' => $gitCacheDir,
115 41
			'contents' => $contentsCacheDir
116
		];
117
	}
118
119 39
	private static function safeMkdir($dir) {
120 39
		if (! \is_dir($dir))
121 2
			return \mkdir($dir, 0777, true);
122 38
	}
123
124
	/**
125
	 * Deletes files from a cache type
126
	 *
127
	 * @param array $config
128
	 * @param string $type
129
	 */
130
	public static function clearCache(&$config, $type = 'all') {
131
		$cacheDirectories = self::checkCache($config);
132
		$cacheDirs = [
133
			'annotations',
134
			'controllers',
135
			'models',
136
			'queries',
137
			'views',
138
			'contents'
139
		];
140
		foreach ($cacheDirs as $typeRef) {
141
			self::_clearCache($cacheDirectories, $type, $typeRef);
142
		}
143
	}
144
145
	private static function _clearCache($cacheDirectories, $type, $typeRef) {
146
		if ($type === 'all' || $type === $typeRef)
147
			UFileSystem::deleteAllFilesFromFolder($cacheDirectories[$typeRef]);
148
	}
149
150
	/**
151
	 *
152
	 * @param array $config
153
	 * @param string $type
154
	 * @param boolean $silent
155
	 */
156 37
	public static function initCache(&$config, $type = 'all', $silent = false) {
157 37
		self::checkCache($config, $silent);
158 37
		self::start($config);
159 37
		if ($type === 'all' || $type === 'models') {
160 34
			self::initModelsCache($config, false, $silent);
161
		}
162 37
		if ($type === 'all' || $type === 'controllers') {
163 8
			if (\class_exists('\\Ubiquity\\security\\acl\\AclManager')) {
164
				self::getAnnotationsEngineInstance()->registerAcls();
165
			}
166 8
			self::initRouterCache($config, $silent);
167
		}
168 37
		if ($type === 'all' || $type === 'rest') {
169 6
			self::initRestCache($config, $silent);
170
		}
171 37
	}
172
}
173
174