Passed
Pull Request — master (#201)
by Jean-Christophe
26:40
created

DevCacheTrait::safeMkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace Ubiquity\cache\traits;
3
4
use Ubiquity\cache\system\AbstractDataCache;
5
use Ubiquity\config\Configuration;
0 ignored issues
show
Bug introduced by
The type Ubiquity\config\Configuration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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