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
|
31 |
|
public static function createOrmModelCache($classname) { |
30
|
31 |
|
$key = self::getModelCacheKey ( $classname ); |
31
|
31 |
|
if (isset ( self::$cache )) { |
32
|
31 |
|
$p = new ModelParser (); |
33
|
31 |
|
$p->parse ( $classname ); |
34
|
31 |
|
self::$cache->store ( $key, $p->asArray (), 'models' ); |
35
|
31 |
|
return self::$cache->fetch ( $key ); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
9 |
|
public static function getOrmModelCache($classname) { |
40
|
9 |
|
return self::$cache->fetch ( self::getModelCacheKey ( $classname ) ); |
41
|
|
|
} |
42
|
|
|
|
43
|
40 |
|
public static function getModelCacheKey($classname) { |
44
|
40 |
|
return \str_replace ( "\\", \DS, $classname ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function modelCacheExists($classname) { |
48
|
|
|
$key = self::getModelCacheKey ( $classname ); |
49
|
|
|
if (isset ( self::$cache )) |
50
|
|
|
return self::$cache->exists ( $key ); |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
31 |
|
public static function initModelsCache(&$config, $forChecking = false, $silent = false) { |
55
|
31 |
|
$modelsDb = [ ]; |
56
|
31 |
|
$files = self::getModelsFiles ( $config, $silent ); |
57
|
31 |
|
foreach ( $files as $file ) { |
58
|
31 |
|
if (\is_file ( $file )) { |
59
|
31 |
|
$model = ClassUtils::getClassFullNameFromFile ( $file ); |
60
|
31 |
|
if(!\class_exists($model)){ |
61
|
|
|
if(\file_exists($file)){ |
62
|
|
|
include $file; |
63
|
|
|
} |
64
|
|
|
} |
65
|
31 |
|
if (! $forChecking) { |
66
|
31 |
|
self::createOrmModelCache ( $model ); |
67
|
31 |
|
$db = 'default'; |
68
|
31 |
|
$ret = Reflexion::getAnnotationClass ( $model, 'database' ); |
69
|
31 |
|
if (\count ( $ret ) > 0) { |
70
|
|
|
$db = $ret [0]->name; |
71
|
|
|
if (! isset ( $config ['database'] [$db] )) { |
72
|
|
|
throw new UbiquityException ( $db . ' connection is not defined in config array' ); |
73
|
|
|
} |
74
|
|
|
} |
75
|
31 |
|
$modelsDb [$model] = $db; |
76
|
31 |
|
ValidatorsManager::initClassValidators ( $model ); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
31 |
|
if (! $forChecking) { |
81
|
31 |
|
self::$cache->store ( self::$modelsDatabaseKey, $modelsDb, 'models' ); |
82
|
|
|
} |
83
|
31 |
|
if (! $silent) { |
84
|
2 |
|
echo "Models cache reset\n"; |
85
|
|
|
} |
86
|
31 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Checks if the models cache is up to date |
90
|
|
|
* |
91
|
|
|
* @param array $config |
92
|
|
|
* @return boolean|array |
93
|
|
|
*/ |
94
|
|
|
public static function modelsCacheUpdated(&$config) { |
95
|
|
|
$result = false; |
96
|
|
|
$files = self::getModelsFiles ( $config, true ); |
97
|
|
|
foreach ( $files as $file ) { |
98
|
|
|
if (\is_file ( $file )) { |
99
|
|
|
$model = ClassUtils::getClassFullNameFromFile ( $file ); |
100
|
|
|
$p = new ModelParser (); |
101
|
|
|
$p->parse ( $model ); |
102
|
|
|
if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) { |
103
|
|
|
$result [$model] = true; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
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
|
33 |
|
public static function getModelsFiles(&$config, $silent = false) { |
118
|
33 |
|
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
|
2 |
|
public static function getModels(&$config, $silent = false, $databaseOffset = 'default') { |
129
|
2 |
|
$result = []; |
130
|
2 |
|
$files = self::getModelsFiles($config, $silent); |
131
|
2 |
|
foreach ($files as $file) { |
132
|
2 |
|
$className = ClassUtils::getClassFullNameFromFile($file); |
133
|
2 |
|
if (\class_exists($className, true)) { |
134
|
2 |
|
$db = 'default'; |
135
|
2 |
|
$ret = Reflexion::getAnnotationClass($className, 'database'); |
136
|
2 |
|
if (\count($ret) > 0) { |
137
|
|
|
$db = $ret[0]->name; |
138
|
|
|
} |
139
|
2 |
|
if ($db === $databaseOffset) { |
140
|
2 |
|
$result[] = $className; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
2 |
|
return $result; |
145
|
|
|
} |
146
|
|
|
|
147
|
34 |
|
public static function getModelsDatabases() { |
148
|
34 |
|
if (self::$cache->exists ( self::$modelsDatabaseKey )) { |
149
|
34 |
|
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
|
|
|
} |