Passed
Push — master ( d0407d...3b1a32 )
by Jean-Christophe
14:53
created

ModelsCacheTrait::getModelsFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Cache traits
5
 */
6
namespace Ubiquity\cache\traits;
7
8
use Ubiquity\orm\parser\ModelParser;
9
use Ubiquity\cache\ClassUtils;
10
use Ubiquity\contents\validation\ValidatorsManager;
11
use Ubiquity\orm\parser\Reflexion;
12
use Ubiquity\exceptions\UbiquityException;
13
use Ubiquity\orm\OrmUtils;
14
15
/**
16
 *
17
 * Ubiquity\cache\traits$ModelsCacheTrait
18
 * This class is part of Ubiquity
19
 *
20
 * @author jcheron <[email protected]>
21
 * @version 1.0.3
22
 * @property \Ubiquity\cache\system\AbstractDataCache $cache
23
 */
24
trait ModelsCacheTrait {
25
26
	abstract protected static function _getFiles(&$config, $type, $silent = false);
27
	private static $modelsDatabaseKey = 'models' . \DIRECTORY_SEPARATOR . '_modelsDatabases';
28
29 7
	public static function createOrmModelCache($classname) {
30 7
		$key = self::getModelCacheKey ( $classname );
31 7
		if (isset ( self::$cache )) {
32 7
			$p = new ModelParser ();
33 7
			$p->parse ( $classname );
34 7
			self::$cache->store ( $key, $p->asArray (), 'models' );
35 7
			return self::$cache->fetch ( $key );
36
		}
37
	}
38
39 34
	public static function getOrmModelCache($classname) {
40 34
		return self::$cache->fetch ( self::getModelCacheKey ( $classname ) );
41
	}
42
43 40
	public static function getModelCacheKey($classname) {
44 40
		return \str_replace ( "\\", \DS, $classname );
45
	}
46
47 11
	public static function modelCacheExists($classname) {
48 11
		$key = self::getModelCacheKey ( $classname );
49 11
		if (isset ( self::$cache ))
50 11
			return self::$cache->exists ( $key );
51
		return false;
52
	}
53
54 6
	public static function initModelsCache(&$config, $forChecking = false, $silent = false) {
55 6
		$modelsDb = [ ];
56 6
		$files = self::getModelsFiles ( $config, $silent );
57 6
		foreach ( $files as $file ) {
58 6
			if (is_file ( $file )) {
59 6
				$model = ClassUtils::getClassFullNameFromFile ( $file );
60 6
				if (! $forChecking) {
61 6
					self::createOrmModelCache ( $model );
62 6
					$db = 'default';
63 6
					$ret = Reflexion::getAnnotationClass ( $model, '@database' );
64 6
					if (\sizeof ( $ret ) > 0) {
65 6
						$db = $ret [0]->name;
66 6
						if (! isset ( $config ['database'] [$db] )) {
67
							throw new UbiquityException ( $db . ' connection is not defined in config array' );
68
						}
69
					}
70 6
					$modelsDb [$model] = $db;
71 6
					ValidatorsManager::initClassValidators ( $model );
72
				}
73
			}
74
		}
75 6
		if (! $forChecking) {
76 6
			self::$cache->store ( self::$modelsDatabaseKey, $modelsDb, 'models' );
0 ignored issues
show
Bug introduced by
$modelsDb of type array is incompatible with the type string expected by parameter $code of Ubiquity\cache\system\AbstractDataCache::store(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
			self::$cache->store ( self::$modelsDatabaseKey, /** @scrutinizer ignore-type */ $modelsDb, 'models' );
Loading history...
77
		}
78 6
		if (! $silent) {
79 6
			echo "Models cache reset\n";
80
		}
81 6
	}
82
83
	/**
84
	 * Checks if the models cache is up to date
85
	 *
86
	 * @param array $config
87
	 * @return boolean|array
88
	 */
89 11
	public static function modelsCacheUpdated(&$config) {
90 11
		$result = false;
91 11
		$files = self::getModelsFiles ( $config, true );
92 11
		foreach ( $files as $file ) {
93 11
			if (\is_file ( $file )) {
94 11
				$model = ClassUtils::getClassFullNameFromFile ( $file );
95 11
				$p = new ModelParser ();
96 11
				$p->parse ( $model );
97 11
				if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) {
98
					$result [$model] = true;
99
				}
100
			}
101
		}
102 11
		return $result;
103
	}
104
105
	/**
106
	 * Returns an array of files corresponding to models
107
	 *
108
	 * @param array $config
109
	 * @param boolean $silent
110
	 * @return array
111
	 */
112 20
	public static function getModelsFiles(&$config, $silent = false) {
113 20
		return self::_getFiles ( $config, 'models', $silent );
114
	}
115
116
	/**
117
	 * Returns an array of the models class names
118
	 *
119
	 * @param array $config
120
	 * @param boolean $silent
121
	 * @return string[]
122
	 */
123 6
	public static function getModels(&$config, $silent = false, $databaseOffset = 'default') {
124 6
		$result = [ ];
125 6
		$files = self::getModelsFiles ( $config, $silent );
126 6
		foreach ( $files as $file ) {
127 6
			$className = ClassUtils::getClassFullNameFromFile ( $file );
128 6
			$db = 'default';
129 6
			$ret = Reflexion::getAnnotationClass ( $className, '@database' );
130 6
			if (\sizeof ( $ret ) > 0) {
131 6
				$db = $ret [0]->name;
132
			}
133 6
			if ($db === $databaseOffset) {
134 6
				$result [] = $className;
135
			}
136
		}
137 6
		return $result;
138
	}
139
140 37
	public static function getModelsDatabases() {
141 37
		if (self::$cache->exists ( self::$modelsDatabaseKey )) {
142 37
			return self::$cache->fetch ( self::$modelsDatabaseKey );
143
		}
144
		return [ ];
145
	}
146
147
	/**
148
	 * Preloads models metadatas.
149
	 * To use only with async servers (Swoole, Workerman)
150
	 *
151
	 * @param array $config
152
	 * @param string $offset
153
	 * @param ?array $models
154
	 */
155
	public static function warmUpModels(&$config, $offset = 'default', $models = null) {
156
		$models ??= self::getModels ( $config, true, $offset );
157
		foreach ( $models as $model ) {
158
			OrmUtils::getModelMetadata ( $model );
159
			Reflexion::getPropertiesAndValues ( new $model () );
160
		}
161
	}
162
}