Test Failed
Push — master ( d715ae...97f10b )
by Jean-Christophe
21:46
created

DevCacheTrait::safeMkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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