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