Passed
Push — master ( b97787...c0f601 )
by Jean-Christophe
16:10
created

ModelsCacheTrait::getModels()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
c 1
b 0
f 1
dl 0
loc 17
ccs 13
cts 13
cp 1
rs 9.5555
cc 5
nc 6
nop 3
crap 5
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
 * @staticvar \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 35
	public static function createOrmModelCache($classname) {
30 35
		$key = self::getModelCacheKey ( $classname );
31 35
		if (isset ( self::$cache )) {
32 35
			$p = new ModelParser ();
33 35
			$p->parse ( $classname );
34 35
			self::$cache->store ( $key, $p->asArray (), 'models' );
35 35
			return self::$cache->fetch ( $key );
36
		}
37
	}
38
39 40
	public static function getOrmModelCache($classname) {
40 40
		return self::$cache->fetch ( self::getModelCacheKey ( $classname ) );
41
	}
42
43 75
	public static function getModelCacheKey($classname) {
44 75
		return \str_replace ( "\\", \DS, $classname );
45
	}
46
47 12
	public static function modelCacheExists($classname) {
48 12
		$key = self::getModelCacheKey ( $classname );
49 12
		if (isset ( self::$cache ))
50 12
			return self::$cache->exists ( $key );
51
		return false;
52
	}
53
54 35
	public static function initModelsCache(&$config, $forChecking = false, $silent = false) {
55 35
		$modelsDb = [ ];
56 35
		$files = self::getModelsFiles ( $config, $silent );
57 35
		foreach ( $files as $file ) {
58 35
			if (\is_file ( $file )) {
59 35
				$model = ClassUtils::getClassFullNameFromFile ( $file );
60 35
				if(!\class_exists($model)){
61
					if(\file_exists($file)){
62
						include $file;
63
					}
64
				}
65 35
				if (! $forChecking) {
66 35
					self::createOrmModelCache ( $model );
67 35
					$db = 'default';
68 35
					$ret = Reflexion::getAnnotationClass ( $model, 'database' );
69 35
					if (\count ( $ret ) > 0) {
70 4
						$db = $ret [0]->name;
71 4
						if (! isset ( $config ['database'] [$db] )) {
72
							throw new UbiquityException ( $db . ' connection is not defined in config array' );
73
						}
74
					}
75 35
					$modelsDb [$model] = $db;
76 35
					ValidatorsManager::initClassValidators ( $model );
77
				}
78
			}
79
		}
80 35
		if (! $forChecking) {
81 35
			self::$cache->store ( self::$modelsDatabaseKey, $modelsDb, 'models' );
82
		}
83 35
		if (! $silent) {
84 6
			echo "Models cache reset\n";
85
		}
86 35
	}
87
88
	/**
89
	 * Checks if the models cache is up to date
90
	 *
91
	 * @param array $config
92
	 * @return boolean|array
93
	 */
94 12
	public static function modelsCacheUpdated(&$config) {
95 12
		$result = false;
96 12
		$files = self::getModelsFiles ( $config, true );
97 12
		foreach ( $files as $file ) {
98 12
			if (\is_file ( $file )) {
99 12
				$model = ClassUtils::getClassFullNameFromFile ( $file );
100 12
				$p = new ModelParser ();
101 12
				$p->parse ( $model );
102 12
				if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) {
103
					$result [$model] = true;
104
				}
105
			}
106
		}
107 12
		return $result;
108
	}
109
110
	/**
111
	 * Returns an array of files corresponding to models
112
	 *
113
	 * @param array $config
114
	 * @param boolean $silent
115
	 * @return array
116
	 */
117 49
	public static function getModelsFiles(&$config, $silent = false) {
118 49
		return self::_getFiles ( $config, 'models', $silent );
119
	}
120
121
	/**
122
	 * Returns an array of the models class names
123
	 *
124
	 * @param array $config
125
	 * @param boolean $silent
126
	 * @return string[]
127
	 */
128 5
	public static function getModels(&$config, $silent = false, $databaseOffset = 'default') {
129 5
		$result = [];
130 5
		$files = self::getModelsFiles($config, $silent);
131 5
		foreach ($files as $file) {
132 5
			$className = ClassUtils::getClassFullNameFromFile($file);
133 5
			if (\class_exists($className, true)) {
134 5
				$db = 'default';
135 5
				$ret = Reflexion::getAnnotationClass($className, 'database');
136 5
				if (\count($ret) > 0) {
137 3
					$db = $ret[0]->name;
138
				}
139 5
				if ($db === $databaseOffset) {
140 5
					$result[] = $className;
141
				}
142
			}
143
		}
144 5
		return $result;
145
	}
146
147 72
	public static function getModelsDatabases() {
148 72
		if (self::$cache->exists ( self::$modelsDatabaseKey )) {
149 72
			return self::$cache->fetch ( self::$modelsDatabaseKey );
150
		}
151
		return [ ];
152
	}
153
154
	/**
155
	 * Preloads models metadatas.
156
	 * To use only with async servers (Swoole, Workerman)
157
	 *
158
	 * @param array $config
159
	 * @param string $offset
160
	 * @param ?array $models
161
	 */
162
	public static function warmUpModels(&$config, $offset = 'default', $models = null) {
163
		$models ??= self::getModels ( $config, true, $offset );
164
		foreach ( $models as $model ) {
165
			OrmUtils::getModelMetadata ( $model );
166
			Reflexion::getPropertiesAndValues ( new $model () );
167
		}
168
	}
169
}