Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ModelsCacheTrait.php
Go to the documentation of this file.
1<?php
2
7
14
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 public static function createOrmModelCache(string $classname) {
32 $key = self::getModelCacheKey ( $classname );
33 if (isset ( self::$cache )) {
34 $p = new ModelParser ();
35 $p->parse ( $classname );
36 self::$cache->store ( $key, $p->asArray (), 'models' );
37 return self::$cache->fetch ( $key );
38 }
39 }
40
41 public static function getOrmModelCache(string $classname) {
42 return self::$cache->fetch ( self::getModelCacheKey ( $classname ) );
43 }
44
45 public static function getModelCacheKey(string $classname): string {
46 return \str_replace ( "\\", \DS, $classname );
47 }
48
49 public static function modelCacheExists(string $classname): bool {
50 $key = self::getModelCacheKey ( $classname );
51 if (isset ( self::$cache ))
52 return self::$cache->exists ( $key );
53 return false;
54 }
55
56 public static function initModelsCache(array &$config, bool $forChecking = false, bool $silent = false): void {
57 $modelsDb = [ ];
58 $files = self::getAllModelsFiles( $config, $silent );
59 foreach ( $files as $file ) {
60 if (\is_file ( $file )) {
61 $model = ClassUtils::getClassFullNameFromFile ( $file );
62 if($model!=null) {
63 if (!\class_exists($model)) {
64 if (\file_exists($file)) {
65 include $file;
66 }
67 }
68 if (!$forChecking) {
69 self::createOrmModelCache($model);
70 $db = 'default';
71 $ret = Reflexion::getAnnotationClass($model, 'database');
72 if (\count($ret) > 0) {
73 $db = $ret [0]->name;
74 if (!isset ($config ['database'] [$db])) {
75 throw new UbiquityException ($db . ' connection is not defined in config array');
76 }
77 }
78 $modelsDb [$model] = $db;
79 ValidatorsManager::initClassValidators($model);
80 }
81 }
82 }
83 }
84 if (! $forChecking) {
85 self::$cache->store ( self::$modelsDatabaseKey, $modelsDb, 'models' );
86 }
87 if (! $silent) {
88 echo "Models cache reset\n";
89 }
90 }
91
98 public static function modelsCacheUpdated(array &$config): array {
99 $result = [];
100 $files = self::getModelsFiles ( $config, true );
101 foreach ( $files as $file ) {
102 if (\is_file ( $file )) {
103 $model = ClassUtils::getClassFullNameFromFile ( $file );
104 $p = new ModelParser ();
105 $p->parse ( $model );
106 if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) {
107 $result [$model] = true;
108 }
109 }
110 }
111 return $result;
112 }
113
121 public static function getModelsFiles(array &$config, bool $silent = false): array {
122 return self::_getFiles ( $config, 'models', $silent );
123 }
124
132 public static function getAllModelsFiles(array &$config, bool $silent = false): array {
133 return self::_getAllFiles ( $config, 'models', $silent );
134 }
135
144 public static function getModels(array &$config, bool $silent = false, string $databaseOffset = 'default'): array {
145 $result = [];
146 $files = self::getModelsFiles($config, $silent);
147 foreach ($files as $file) {
148 $className = ClassUtils::getClassFullNameFromFile($file);
149 if($className!=null) {
150 if (\class_exists($className, true)) {
151 $db = 'default';
152 $ret = Reflexion::getAnnotationClass($className, 'database');
153 if (\count($ret) > 0) {
154 $db = $ret[0]->name;
155 }
156 if ($databaseOffset == null || $db === $databaseOffset) {
157 $result[] = $className;
158 }
159 }
160 }
161 }
162 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 public static function getModelsDatabases(): array {
177 if (self::$cache->exists ( self::$modelsDatabaseKey )) {
178 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
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}
Manipulates class and namespace names Ubiquity\cache$ClassUtils This class is part of Ubiquity.
Ubiquity\cache\traits$ModelsCacheTrait This class is part of Ubiquity.
static getAllModelsFiles(array &$config, bool $silent=false)
Returns an array of all model files.
static modelsCacheUpdated(array &$config)
Checks if the models cache is up to date.
static warmUpModels(array &$config, string $offset='default', ?array $models=null)
Preloads models metadatas.
static getModelCacheKey(string $classname)
static _getFiles(array &$config, string $type, bool $silent=false)
static getModels(array &$config, bool $silent=false, string $databaseOffset='default')
Returns an array of the models class names.
static modelCacheExists(string $classname)
static _getAllFiles(array &$config, string $type, bool $silent=false)
static initModelsCache(array &$config, bool $forChecking=false, bool $silent=false)
static storeModelsDatabases(array $modelsDatabases)
static createOrmModelCache(string $classname)
static getModelsFiles(array &$config, bool $silent=false)
Returns an array of files corresponding to models.
static getOrmModelCache(string $classname)
static getModelsNamespace(string $databaseOffset='default')
Object/relational mapping utilities.
Definition OrmUtils.php:17
Reflection utilities in dev environment only.
Definition Reflexion.php:17