Passed
Push — master ( 86ec6c...e9ffe4 )
by Jean-Christophe
10:31
created

ModelsCacheTrait::getModelsDatabases()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 0
crap 2.0625
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\utils\base\UArray;
13
use Ubiquity\exceptions\UbiquityException;
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
28 7
	public static function createOrmModelCache($classname) {
29 7
		$key = self::getModelCacheKey ( $classname );
30 7
		if (isset ( self::$cache )) {
31 7
			$p = new ModelParser ();
32 7
			$p->parse ( $classname );
33 7
			self::$cache->store ( $key, $p->__toString (), 'models' );
34 7
			return self::$cache->fetch ( $key );
35
		}
36
	}
37
38 35
	public static function getOrmModelCache($classname) {
39 35
		return self::$cache->fetch ( self::getModelCacheKey ( $classname ) );
40
	}
41
42 41
	public static function getModelCacheKey($classname) {
43 41
		return \str_replace ( "\\", \DS, $classname );
44
	}
45
46 12
	public static function modelCacheExists($classname) {
47 12
		$key = self::getModelCacheKey ( $classname );
48 12
		if (isset ( self::$cache ))
49 12
			return self::$cache->exists ( $key );
50
		return false;
51
	}
52
53 6
	public static function initModelsCache(&$config, $forChecking = false, $silent = false) {
54 6
		$modelsDb = [ ];
55 6
		$files = self::getModelsFiles ( $config, $silent );
56 6
		foreach ( $files as $file ) {
57 6
			if (is_file ( $file )) {
58 6
				$model = ClassUtils::getClassFullNameFromFile ( $file );
59 6
				if (! $forChecking) {
60 6
					self::createOrmModelCache ( $model );
61 6
					$db = 'default';
62 6
					$ret = Reflexion::getAnnotationClass ( $model, '@database' );
63 6
					if (\sizeof ( $ret ) > 0) {
64
						$db = $ret [0]->name;
65
						if (! isset ( $config ['database'] [$db] )) {
66
							throw new UbiquityException ( $db . ' connection is not defined in config array' );
67
						}
68
					}
69 6
					$modelsDb [$model] = $db;
70 6
					ValidatorsManager::initClassValidators ( $model );
71
				}
72
			}
73
		}
74 6
		if (! $forChecking) {
75 6
			self::$cache->store ( 'models\_modelsDatabases', 'return ' . UArray::asPhpArray ( $modelsDb, 'array' ) . ';', 'models' );
76
		}
77 6
		if (! $silent) {
78 6
			echo "Models cache reset\n";
79
		}
80 6
	}
81
82
	/**
83
	 * Checks if the models cache is up to date
84
	 *
85
	 * @param array $config
86
	 * @return boolean|array
87
	 */
88 12
	public static function modelsCacheUpdated(&$config) {
89 12
		$result = false;
90 12
		$files = self::getModelsFiles ( $config, true );
91 12
		foreach ( $files as $file ) {
92 12
			if (is_file ( $file )) {
93 12
				$model = ClassUtils::getClassFullNameFromFile ( $file );
94 12
				$p = new ModelParser ();
95 12
				$p->parse ( $model );
96 12
				if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) {
97
					$result [$model] = true;
98
				}
99
			}
100
		}
101 12
		return $result;
102
	}
103
104
	/**
105
	 * Returns an array of files corresponding to models
106
	 *
107
	 * @param array $config
108
	 * @param boolean $silent
109
	 * @return array
110
	 */
111 21
	public static function getModelsFiles(&$config, $silent = false) {
112 21
		return self::_getFiles ( $config, 'models', $silent );
113
	}
114
115
	/**
116
	 * Returns an array of the models class names
117
	 *
118
	 * @param array $config
119
	 * @param boolean $silent
120
	 * @return string[]
121
	 */
122 6
	public static function getModels(&$config, $silent = false, $databaseOffset = 'default') {
123 6
		$result = [ ];
124 6
		$files = self::getModelsFiles ( $config, $silent );
125 6
		foreach ( $files as $file ) {
126 6
			$className = ClassUtils::getClassFullNameFromFile ( $file );
127 6
			$db = 'default';
128 6
			$ret = Reflexion::getAnnotationClass ( $className, '@database' );
129 6
			if (\sizeof ( $ret ) > 0) {
130
				$db = $ret [0]->name;
131
			}
132 6
			if ($db === $databaseOffset) {
133 6
				$result [] = $className;
134
			}
135
		}
136 6
		return $result;
137
	}
138
139 38
	public static function getModelsDatabases() {
140 38
		if (self::$cache->exists ( 'models\_modelsDatabases' )) {
141 38
			return self::$cache->fetch ( 'models\_modelsDatabases' );
142
		}
143
		return [ ];
144
	}
145
}
146