Passed
Push — master ( 2492ae...ecf533 )
by Jean-Christophe
11:25
created

DevCacheTrait::registerAnnotations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 4
c 3
b 0
f 2
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
namespace Ubiquity\cache\traits;
3
4
use Ubiquity\utils\base\UFileSystem;
5
use mindplay\annotations\AnnotationCache;
6
use mindplay\annotations\AnnotationManager;
7
use mindplay\annotations\Annotations;
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.1
16
 *
17
 */
18
trait DevCacheTrait {
19
20
	/**
21
	 *
22
	 * @var array array of annotations name/class
23
	 */
24
	protected static $registry;
25
26
	abstract protected static function getCacheInstance(&$config, $cacheDirectory, $postfix);
27
28
	abstract protected static function initRestCache(&$config, $silent = false);
29
30
	abstract protected static function initRouterCache(&$config, $silent = false);
31
32
	abstract public static function initModelsCache(&$config, $forChecking = false, $silent = false);
33
34 192
	private static function initialGetCacheDirectory(&$config) {
35 192
		return $config['cache']['directory'] ??= 'cache' . \DS;
36
	}
37
38
	/**
39
	 * Starts the cache in dev mode, for generating the other caches
40
	 * Do not use in production
41
	 *
42
	 * @param array $config
43
	 */
44 53
	public static function start(&$config) {
45 53
		self::$registry = [
46
			'id' => 'Ubiquity\annotations\IdAnnotation',
47
			'manyToOne' => 'Ubiquity\annotations\ManyToOneAnnotation',
48
			'oneToMany' => 'Ubiquity\annotations\OneToManyAnnotation',
49
			'manyToMany' => 'Ubiquity\annotations\ManyToManyAnnotation',
50
			'joinColumn' => 'Ubiquity\annotations\JoinColumnAnnotation',
51
			'table' => 'Ubiquity\annotations\TableAnnotation',
52
			'database' => 'Ubiquity\annotations\DatabaseAnnotation',
53
			'transient' => 'Ubiquity\annotations\TransientAnnotation',
54
			'column' => 'Ubiquity\annotations\ColumnAnnotation',
55
			'validator' => 'Ubiquity\annotations\ValidatorAnnotation',
56
			'transformer' => 'Ubiquity\annotations\TransformerAnnotation',
57
			'joinTable' => 'Ubiquity\annotations\JoinTableAnnotation',
58
			'requestMapping' => 'Ubiquity\annotations\router\RouteAnnotation',
59
			'route' => 'Ubiquity\annotations\router\RouteAnnotation',
60
			'get' => 'Ubiquity\annotations\router\GetAnnotation',
61
			'getMapping' => 'Ubiquity\annotations\router\GetAnnotation',
62
			'post' => 'Ubiquity\annotations\router\PostAnnotation',
63
			'postMapping' => 'Ubiquity\annotations\router\PostAnnotation',
64
			'put' => 'Ubiquity\annotations\router\PutAnnotation',
65
			'putMapping' => 'Ubiquity\annotations\router\PutAnnotation',
66
			'patch' => 'Ubiquity\annotations\router\PatchAnnotation',
67
			'patchMapping' => 'Ubiquity\annotations\router\PatchAnnotation',
68
			'delete' => 'Ubiquity\annotations\router\DeleteAnnotation',
69
			'deleteMapping' => 'Ubiquity\annotations\router\DeleteAnnotation',
70
			'options' => 'Ubiquity\annotations\router\OptionsAnnotation',
71
			'optionsMapping' => 'Ubiquity\annotations\router\OptionsAnnotation',
72
			'var' => 'mindplay\annotations\standard\VarAnnotation',
73
			'yuml' => 'Ubiquity\annotations\YumlAnnotation',
74
			'rest' => 'Ubiquity\annotations\rest\RestAnnotation',
75
			'authorization' => 'Ubiquity\annotations\rest\AuthorizationAnnotation',
76
			'injected' => 'Ubiquity\annotations\di\InjectedAnnotation',
77
			'autowired' => 'Ubiquity\annotations\di\AutowiredAnnotation'
78
		];
79 53
		self::$cacheDirectory = self::initialGetCacheDirectory($config);
0 ignored issues
show
Bug Best Practice introduced by
The property cacheDirectory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80 53
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
81 53
		Annotations::$config['cache'] = new AnnotationCache($cacheDirectory . '/annotations');
82 53
		self::register(Annotations::getManager());
83 53
		self::getCacheInstance($config, $cacheDirectory, '.cache')->init();
84 53
	}
85
86
	/**
87
	 *
88
	 * @param array $nameClasses
89
	 *        	an array of name=>class annotations
90
	 */
91
	public static function registerAnnotations(array $nameClasses): void {
92
		$annotationManager = Annotations::getManager();
93
		foreach ($nameClasses as $name => $class) {
94
			self::$registry[$name] = $class;
95
			$annotationManager->registry[$name] = $class;
96
		}
97
	}
98
99 53
	protected static function register(AnnotationManager $annotationManager) {
100 53
		$annotationManager->registry = \array_merge($annotationManager->registry, self::$registry);
101 53
	}
102
103
	/**
104
	 * Checks the existence of cache subdirectories and returns an array of cache folders
105
	 *
106
	 * @param array $config
107
	 * @param boolean $silent
108
	 * @return string[]
109
	 */
110 39
	public static function checkCache(&$config, $silent = false) {
111 39
		$dirs = self::getCacheDirectories($config, $silent);
112 39
		foreach ($dirs as $dir) {
113 39
			self::safeMkdir($dir);
114
		}
115 39
		return $dirs;
116
	}
117
118
	/**
119
	 * Returns an associative array of cache folders (annotations, models, controllers, queries, views, seo, git, contents)
120
	 *
121
	 * @param array $config
122
	 * @param boolean $silent
123
	 * @return string[]
124
	 */
125 41
	public static function getCacheDirectories(&$config, $silent = false) {
126 41
		$cacheDirectory = self::initialGetCacheDirectory($config);
127 41
		$rootDS = \ROOT . \DS;
128 41
		if (! $silent) {
129 10
			echo "cache directory is " . UFileSystem::cleanPathname($rootDS . $cacheDirectory) . "\n";
130
		}
131 41
		$cacheDirectory = $rootDS . $cacheDirectory . \DS;
132 41
		$modelsDir = str_replace("\\", \DS, $config['mvcNS']['models']);
133 41
		$controllersDir = str_replace("\\", \DS, $config['mvcNS']['controllers']);
134 41
		$annotationCacheDir = $cacheDirectory . 'annotations';
135 41
		$modelsCacheDir = $cacheDirectory . $modelsDir;
136 41
		$queriesCacheDir = $cacheDirectory . 'queries';
137 41
		$controllersCacheDir = $cacheDirectory . $controllersDir;
138 41
		$viewsCacheDir = $cacheDirectory . 'views';
139 41
		$seoCacheDir = $cacheDirectory . 'seo';
140 41
		$gitCacheDir = $cacheDirectory . 'git';
141 41
		$contentsCacheDir = $cacheDirectory . 'contents';
142
		return [
143 41
			'annotations' => $annotationCacheDir,
144 41
			'models' => $modelsCacheDir,
145 41
			'controllers' => $controllersCacheDir,
146 41
			'queries' => $queriesCacheDir,
147 41
			'views' => $viewsCacheDir,
148 41
			'seo' => $seoCacheDir,
149 41
			'git' => $gitCacheDir,
150 41
			'contents' => $contentsCacheDir
151
		];
152
	}
153
154 39
	private static function safeMkdir($dir) {
155 39
		if (! \is_dir($dir))
156 2
			return \mkdir($dir, 0777, true);
157 38
	}
158
159
	/**
160
	 * Deletes files from a cache type
161
	 *
162
	 * @param array $config
163
	 * @param string $type
164
	 */
165
	public static function clearCache(&$config, $type = 'all') {
166
		$cacheDirectories = self::checkCache($config);
167
		$cacheDirs = [
168
			'annotations',
169
			'controllers',
170
			'models',
171
			'queries',
172
			'views',
173
			'contents'
174
		];
175
		foreach ($cacheDirs as $typeRef) {
176
			self::_clearCache($cacheDirectories, $type, $typeRef);
177
		}
178
	}
179
180
	private static function _clearCache($cacheDirectories, $type, $typeRef) {
181
		if ($type === 'all' || $type === $typeRef)
182
			UFileSystem::deleteAllFilesFromFolder($cacheDirectories[$typeRef]);
183
	}
184
185
	/**
186
	 *
187
	 * @param array $config
188
	 * @param string $type
189
	 * @param boolean $silent
190
	 */
191 37
	public static function initCache(&$config, $type = 'all', $silent = false) {
192 37
		self::checkCache($config, $silent);
193 37
		self::start($config);
194 37
		if ($type === 'all' || $type === 'models') {
195 34
			self::initModelsCache($config, false, $silent);
196
		}
197 37
		if ($type === 'all' || $type === 'controllers') {
198 8
			if (\class_exists('\\Ubiquity\\security\\acl\\AclManager')) {
199
				\Ubiquity\security\acl\AclManager::registerAnnotations($config);
1 ignored issue
show
Bug introduced by
The type Ubiquity\security\acl\AclManager 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...
200
			}
201 8
			self::initRouterCache($config, $silent);
202
		}
203 37
		if ($type === 'all' || $type === 'rest') {
204 6
			self::initRestCache($config, $silent);
205
		}
206 37
	}
207
}
208
209