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 ;
33 if (isset ( self::$cache )) {
35 $p->parse ( $classname );
36 self::$cache->store ( $key, $p->asArray (),
'models' );
37 return self::$cache->fetch ( $key );
42 return self::$cache->fetch ( self::getModelCacheKey ( $classname ) );
46 return \str_replace (
"\\", \DS, $classname );
50 $key = self::getModelCacheKey ( $classname );
51 if (isset ( self::$cache ))
52 return self::$cache->exists ( $key );
56 public static function initModelsCache(array &$config,
bool $forChecking =
false,
bool $silent =
false): void {
58 $files = self::getAllModelsFiles( $config, $silent );
59 foreach ( $files as $file ) {
60 if (\is_file ( $file )) {
61 $model = ClassUtils::getClassFullNameFromFile ( $file );
63 if (!\class_exists($model)) {
64 if (\file_exists($file)) {
69 self::createOrmModelCache($model);
71 $ret = Reflexion::getAnnotationClass($model,
'database');
72 if (\count($ret) > 0) {
74 if (!isset ($config [
'database'] [$db])) {
75 throw new UbiquityException ($db .
' connection is not defined in config array');
78 $modelsDb [$model] = $db;
79 ValidatorsManager::initClassValidators($model);
85 self::$cache->store ( self::$modelsDatabaseKey, $modelsDb,
'models' );
88 echo
"Models cache reset\n";
100 $files = self::getModelsFiles ( $config,
true );
101 foreach ( $files as $file ) {
102 if (\is_file ( $file )) {
103 $model = ClassUtils::getClassFullNameFromFile ( $file );
105 $p->parse ( $model );
106 if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) {
107 $result [$model] =
true;
121 public static function getModelsFiles(array &$config,
bool $silent =
false): array {
122 return self::_getFiles ( $config,
'models', $silent );
133 return self::_getAllFiles ( $config,
'models', $silent );
144 public static function getModels(array &$config,
bool $silent =
false,
string $databaseOffset =
'default'): array {
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)) {
152 $ret = Reflexion::getAnnotationClass($className,
'database');
153 if (\count($ret) > 0) {
156 if ($databaseOffset ==
null || $db === $databaseOffset) {
157 $result[] = $className;
166 $modelsDatabases=self::getModelsDatabases();
167 foreach ($modelsDatabases as $model=>$offset){
168 if($offset===$databaseOffset){
169 $rc=new \ReflectionClass($model);
170 return $rc->getNamespaceName();
177 if (self::$cache->exists ( self::$modelsDatabaseKey )) {
178 return self::$cache->fetch ( self::$modelsDatabaseKey );
184 self::$cache->store ( self::$modelsDatabaseKey, $modelsDatabases,
'models' );
188 return \array_keys(\array_flip(self::getModelsDatabases()));
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 () );
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 $modelsDatabaseKey
static getModelsDatabases()
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.
Reflection utilities in dev environment only.