Passed
Push — master ( 5bce84...50f645 )
by Jean-Christophe
16:11
created

DevCacheTrait::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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