|
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
|
|
|
use Ubiquity\orm\OrmUtils; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
* Ubiquity\cache\traits$ModelsCacheTrait |
|
19
|
|
|
* This class is part of Ubiquity |
|
20
|
|
|
* |
|
21
|
|
|
* @author jcheron <[email protected]> |
|
22
|
|
|
* @version 1.0.3 |
|
23
|
|
|
* @property \Ubiquity\cache\system\AbstractDataCache $cache |
|
24
|
|
|
*/ |
|
25
|
|
|
trait ModelsCacheTrait { |
|
26
|
|
|
|
|
27
|
|
|
abstract protected static function _getFiles(&$config, $type, $silent = false); |
|
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->__toString (), 'models' ); |
|
35
|
7 |
|
return self::$cache->fetch ( $key ); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
35 |
|
public static function getOrmModelCache($classname) { |
|
40
|
35 |
|
return self::$cache->fetch ( self::getModelCacheKey ( $classname ) ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
41 |
|
public static function getModelCacheKey($classname) { |
|
44
|
41 |
|
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
|
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
|
|
|
$db = $ret [0]->name; |
|
66
|
|
|
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 ( 'models\_modelsDatabases', 'return ' . UArray::asPhpArray ( $modelsDb, 'array' ) . ';', 'models' ); |
|
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
|
12 |
|
public static function modelsCacheUpdated(&$config) { |
|
90
|
12 |
|
$result = false; |
|
91
|
12 |
|
$files = self::getModelsFiles ( $config, true ); |
|
92
|
12 |
|
foreach ( $files as $file ) { |
|
93
|
12 |
|
if (is_file ( $file )) { |
|
94
|
12 |
|
$model = ClassUtils::getClassFullNameFromFile ( $file ); |
|
95
|
12 |
|
$p = new ModelParser (); |
|
96
|
12 |
|
$p->parse ( $model ); |
|
97
|
12 |
|
if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) { |
|
98
|
12 |
|
$result [$model] = true; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
12 |
|
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
|
21 |
|
public static function getModelsFiles(&$config, $silent = false) { |
|
113
|
21 |
|
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
|
|
|
$db = $ret [0]->name; |
|
132
|
|
|
} |
|
133
|
6 |
|
if ($db === $databaseOffset) { |
|
134
|
6 |
|
$result [] = $className; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
6 |
|
return $result; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
38 |
|
public static function getModelsDatabases() { |
|
141
|
38 |
|
if (self::$cache->exists ( 'models\_modelsDatabases' )) { |
|
142
|
38 |
|
return self::$cache->fetch ( 'models\_modelsDatabases' ); |
|
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 = $models ?? self::getModels ( $config, true, $offset ); |
|
157
|
|
|
foreach ( $models as $model ) { |
|
158
|
|
|
OrmUtils::getModelMetadata ( $model ); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|