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
|
|
|
abstract protected static function _getAllFiles(&$config, $type, $silent = false): array ; |
28
|
|
|
|
29
|
|
|
private static $modelsDatabaseKey = 'models' . \DIRECTORY_SEPARATOR . '_modelsDatabases'; |
30
|
|
|
|
31
|
35 |
|
public static function createOrmModelCache($classname) { |
32
|
35 |
|
$key = self::getModelCacheKey ( $classname ); |
33
|
35 |
|
if (isset ( self::$cache )) { |
34
|
35 |
|
$p = new ModelParser (); |
35
|
35 |
|
$p->parse ( $classname ); |
36
|
35 |
|
self::$cache->store ( $key, $p->asArray (), 'models' ); |
37
|
35 |
|
return self::$cache->fetch ( $key ); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
40 |
|
public static function getOrmModelCache($classname) { |
42
|
40 |
|
return self::$cache->fetch ( self::getModelCacheKey ( $classname ) ); |
43
|
|
|
} |
44
|
|
|
|
45
|
75 |
|
public static function getModelCacheKey($classname) { |
46
|
75 |
|
return \str_replace ( "\\", \DS, $classname ); |
47
|
|
|
} |
48
|
|
|
|
49
|
12 |
|
public static function modelCacheExists($classname) { |
50
|
12 |
|
$key = self::getModelCacheKey ( $classname ); |
51
|
12 |
|
if (isset ( self::$cache )) |
52
|
12 |
|
return self::$cache->exists ( $key ); |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
35 |
|
public static function initModelsCache(&$config, $forChecking = false, $silent = false) { |
57
|
35 |
|
$modelsDb = [ ]; |
58
|
35 |
|
$files = self::getAllModelsFiles( $config, $silent ); |
59
|
35 |
|
foreach ( $files as $file ) { |
60
|
35 |
|
if (\is_file ( $file )) { |
61
|
35 |
|
$model = ClassUtils::getClassFullNameFromFile ( $file ); |
62
|
35 |
|
if(!\class_exists($model)){ |
63
|
|
|
if(\file_exists($file)){ |
64
|
|
|
include $file; |
65
|
|
|
} |
66
|
|
|
} |
67
|
35 |
|
if (! $forChecking) { |
68
|
35 |
|
self::createOrmModelCache ( $model ); |
69
|
35 |
|
$db = 'default'; |
70
|
35 |
|
$ret = Reflexion::getAnnotationClass ( $model, 'database' ); |
71
|
35 |
|
if (\count ( $ret ) > 0) { |
72
|
4 |
|
$db = $ret [0]->name; |
73
|
4 |
|
if (! isset ( $config ['database'] [$db] )) { |
74
|
|
|
throw new UbiquityException ( $db . ' connection is not defined in config array' ); |
75
|
|
|
} |
76
|
|
|
} |
77
|
35 |
|
$modelsDb [$model] = $db; |
78
|
35 |
|
ValidatorsManager::initClassValidators ( $model ); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
35 |
|
if (! $forChecking) { |
83
|
35 |
|
self::$cache->store ( self::$modelsDatabaseKey, $modelsDb, 'models' ); |
84
|
|
|
} |
85
|
35 |
|
if (! $silent) { |
86
|
6 |
|
echo "Models cache reset\n"; |
87
|
|
|
} |
88
|
35 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Checks if the models cache is up to date |
92
|
|
|
* |
93
|
|
|
* @param array $config |
94
|
|
|
* @return boolean|array |
95
|
|
|
*/ |
96
|
12 |
|
public static function modelsCacheUpdated(&$config) { |
97
|
12 |
|
$result = false; |
98
|
12 |
|
$files = self::getModelsFiles ( $config, true ); |
99
|
12 |
|
foreach ( $files as $file ) { |
100
|
12 |
|
if (\is_file ( $file )) { |
101
|
12 |
|
$model = ClassUtils::getClassFullNameFromFile ( $file ); |
102
|
12 |
|
$p = new ModelParser (); |
103
|
12 |
|
$p->parse ( $model ); |
104
|
12 |
|
if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) { |
105
|
|
|
$result [$model] = true; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
12 |
|
return $result; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns an array of files corresponding to models |
114
|
|
|
* |
115
|
|
|
* @param array $config |
116
|
|
|
* @param boolean $silent |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
14 |
|
public static function getModelsFiles(&$config, $silent = false) { |
120
|
14 |
|
return self::_getFiles ( $config, 'models', $silent ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns an array of all model files |
125
|
|
|
* |
126
|
|
|
* @param array $config |
127
|
|
|
* @param boolean $silent |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
35 |
|
public static function getAllModelsFiles(&$config, $silent = false) { |
131
|
35 |
|
return self::_getAllFiles ( $config, 'models', $silent ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns an array of the models class names |
136
|
|
|
* |
137
|
|
|
* @param array $config |
138
|
|
|
* @param boolean $silent |
139
|
|
|
* @param ?string $databaseOffset |
140
|
|
|
* @return string[] |
141
|
|
|
*/ |
142
|
5 |
|
public static function getModels(&$config, $silent = false, $databaseOffset = 'default') { |
143
|
5 |
|
$result = []; |
144
|
5 |
|
$files = self::getModelsFiles($config, $silent); |
145
|
5 |
|
foreach ($files as $file) { |
146
|
5 |
|
$className = ClassUtils::getClassFullNameFromFile($file); |
147
|
5 |
|
if (\class_exists($className, true)) { |
148
|
5 |
|
$db = 'default'; |
149
|
5 |
|
$ret = Reflexion::getAnnotationClass($className, 'database'); |
150
|
5 |
|
if (\count($ret) > 0) { |
151
|
3 |
|
$db = $ret[0]->name; |
152
|
|
|
} |
153
|
5 |
|
if ($databaseOffset==null || $db === $databaseOffset) { |
154
|
5 |
|
$result[] = $className; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
5 |
|
return $result; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public static function getModelsNamespace(string $databaseOffset='default'): ?string { |
162
|
|
|
$modelsDatabases=self::getModelsDatabases(); |
163
|
|
|
foreach ($modelsDatabases as $model=>$offset){ |
164
|
|
|
if($offset===$databaseOffset){ |
165
|
|
|
$rc=new \ReflectionClass($model); |
166
|
|
|
return $rc->getNamespaceName(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
return null; |
170
|
|
|
} |
171
|
|
|
|
172
|
72 |
|
public static function getModelsDatabases(): array { |
173
|
72 |
|
if (self::$cache->exists ( self::$modelsDatabaseKey )) { |
174
|
72 |
|
return self::$cache->fetch ( self::$modelsDatabaseKey ); |
175
|
|
|
} |
176
|
|
|
return [ ]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public static function getDatabases(): array { |
180
|
|
|
return \array_keys(\array_flip(self::getModelsDatabases())); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Preloads models metadatas. |
185
|
|
|
* To use only with async servers (Swoole, Workerman) |
186
|
|
|
* |
187
|
|
|
* @param array $config |
188
|
|
|
* @param string $offset |
189
|
|
|
* @param ?array $models |
190
|
|
|
*/ |
191
|
|
|
public static function warmUpModels(&$config, $offset = 'default', $models = null) { |
192
|
|
|
$models ??= self::getModels ( $config, true, $offset ); |
193
|
|
|
foreach ( $models as $model ) { |
194
|
|
|
OrmUtils::getModelMetadata ( $model ); |
195
|
|
|
Reflexion::getPropertiesAndValues ( new $model () ); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |