Test Failed
Push — master ( 93f81f...e78505 )
by Jean-Christophe
78:02
created

DevCacheTrait::_getAnnotationsEngineInstance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
cc 3
nc 3
nop 0
crap 12
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
	private static function _getAnnotationsEngineInstance(){
30
		if(\class_exists('Ubiquity\\attributes\\AttributesEngine',true)){
31
			return new \Ubiquity\attributes\AttributesEngine();
1 ignored issue
show
Bug introduced by
The type Ubiquity\attributes\AttributesEngine 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...
32
		}elseif(\class_exists('Ubiquity\\annotations\\AnnotationsEngine',true)){
33
			return new \Ubiquity\annotations\AnnotationsEngine();
34
		}
35 192
	}
36 192
37
	public static function getAnnotationsEngineInstance(){
38
		return self::$annotationsEngine??=self::_getAnnotationsEngineInstance();
39
	}
40
41
	private static function initialGetCacheDirectory(&$config) {
42
		return $config['cache']['directory'] ??= 'cache' . \DS;
43
	}
44
45 53
	/**
46 53
	 * Starts the cache in dev mode, for generating the other caches
47
	 * Do not use in production
48
	 *
49
	 * @param array $config
50
	 */
51
	public static function start(&$config) {
52
		self::$cacheDirectory = self::initialGetCacheDirectory($config);
53
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
54
		self::getAnnotationsEngineInstance()->start($cacheDirectory);
55
		self::getCacheInstance($config, $cacheDirectory, '.cache')->init();
56
	}
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
	public static function checkCache(&$config, $silent = false) {
76
		$dirs = self::getCacheDirectories($config, $silent);
77
		foreach ($dirs as $dir) {
78
			self::safeMkdir($dir);
79
		}
80 53
		return $dirs;
81 53
	}
82 53
83 53
	/**
84 53
	 * Returns an associative array of cache folders (annotations, models, controllers, queries, views, seo, git, contents)
85 53
	 *
86
	 * @param array $config
87
	 * @param boolean $silent
88
	 * @return string[]
89
	 */
90
	public static function getCacheDirectories(&$config, $silent = false) {
91
		$cacheDirectory = self::initialGetCacheDirectory($config);
92
		$rootDS = \ROOT . \DS;
93
		if (! $silent) {
94
			echo "cache directory is " . UFileSystem::cleanPathname($rootDS . $cacheDirectory) . "\n";
95
		}
96
		$cacheDirectory = $rootDS . $cacheDirectory . \DS;
97
		$modelsDir = str_replace("\\", \DS, $config['mvcNS']['models']);
98
		$controllersDir = str_replace("\\", \DS, $config['mvcNS']['controllers']);
99
		$annotationCacheDir = $cacheDirectory . 'annotations';
100 53
		$modelsCacheDir = $cacheDirectory . $modelsDir;
101 53
		$queriesCacheDir = $cacheDirectory . 'queries';
102 53
		$controllersCacheDir = $cacheDirectory . $controllersDir;
103
		$viewsCacheDir = $cacheDirectory . 'views';
104
		$seoCacheDir = $cacheDirectory . 'seo';
105
		$gitCacheDir = $cacheDirectory . 'git';
106
		$contentsCacheDir = $cacheDirectory . 'contents';
107
		return [
108
			'annotations' => $annotationCacheDir,
109
			'models' => $modelsCacheDir,
110
			'controllers' => $controllersCacheDir,
111 39
			'queries' => $queriesCacheDir,
112 39
			'views' => $viewsCacheDir,
113 39
			'seo' => $seoCacheDir,
114 39
			'git' => $gitCacheDir,
115
			'contents' => $contentsCacheDir
116 39
		];
117
	}
118
119
	private static function safeMkdir($dir) {
120
		if (! \is_dir($dir))
121
			return \mkdir($dir, 0777, true);
122
	}
123
124
	/**
125
	 * Deletes files from a cache type
126 41
	 *
127 41
	 * @param array $config
128 41
	 * @param string $type
129 41
	 */
130 10
	public static function clearCache(&$config, $type = 'all') {
131
		$cacheDirectories = self::checkCache($config);
132 41
		$cacheDirs = [
133 41
			'annotations',
134 41
			'controllers',
135 41
			'models',
136 41
			'queries',
137 41
			'views',
138 41
			'contents'
139 41
		];
140 41
		foreach ($cacheDirs as $typeRef) {
141 41
			self::_clearCache($cacheDirectories, $type, $typeRef);
142 41
		}
143
	}
144 41
145 41
	private static function _clearCache($cacheDirectories, $type, $typeRef) {
146 41
		if ($type === 'all' || $type === $typeRef)
147 41
			UFileSystem::deleteAllFilesFromFolder($cacheDirectories[$typeRef]);
148 41
	}
149 41
150 41
	/**
151 41
	 *
152
	 * @param array $config
153
	 * @param string $type
154
	 * @param boolean $silent
155 39
	 */
156 39
	public static function initCache(&$config, $type = 'all', $silent = false) {
157 2
		self::checkCache($config, $silent);
158 38
		self::start($config);
159
		if ($type === 'all' || $type === 'models') {
160
			self::initModelsCache($config, false, $silent);
161
		}
162
		if ($type === 'all' || $type === 'controllers') {
163
			if (\class_exists('\\Ubiquity\\security\\acl\\AclManager')) {
164
				self::getAnnotationsEngineInstance()->registerAcls();
165
			}
166
			self::initRouterCache($config, $silent);
167
		}
168
		if ($type === 'all' || $type === 'rest') {
169
			self::initRestCache($config, $silent);
170
		}
171
	}
172
}
173
174