Ubiquity  2.3.12
php rapid development framework
ModelsCacheTrait.php
Go to the documentation of this file.
1 <?php
2 
7 
14 
25 
26  abstract protected static function _getFiles(&$config, $type, $silent = false);
27  private static $modelsDatabaseKey = 'models' . \DIRECTORY_SEPARATOR . '_modelsDatabases';
28 
29  public static function createOrmModelCache($classname) {
30  $key = self::getModelCacheKey ( $classname );
31  if (isset ( self::$cache )) {
32  $p = new ModelParser ();
33  $p->parse ( $classname );
34  self::$cache->store ( $key, $p->asArray (), 'models' );
35  return self::$cache->fetch ( $key );
36  }
37  }
38 
39  public static function getOrmModelCache($classname) {
40  return self::$cache->fetch ( self::getModelCacheKey ( $classname ) );
41  }
42 
43  public static function getModelCacheKey($classname) {
44  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  public static function initModelsCache(&$config, $forChecking = false, $silent = false) {
55  $modelsDb = [ ];
56  $files = self::getModelsFiles ( $config, $silent );
57  foreach ( $files as $file ) {
58  if (is_file ( $file )) {
59  $model = ClassUtils::getClassFullNameFromFile ( $file );
60  if (! $forChecking) {
61  self::createOrmModelCache ( $model );
62  $db = 'default';
63  $ret = Reflexion::getAnnotationClass ( $model, '@database' );
64  if (\sizeof ( $ret ) > 0) {
65  $db = $ret [0]->name;
66  if (! isset ( $config ['database'] [$db] )) {
67  throw new UbiquityException ( $db . ' connection is not defined in config array' );
68  }
69  }
70  $modelsDb [$model] = $db;
72  }
73  }
74  }
75  if (! $forChecking) {
76  self::$cache->store ( self::$modelsDatabaseKey, $modelsDb, 'models' );
77  }
78  if (! $silent) {
79  echo "Models cache reset\n";
80  }
81  }
82 
89  public static function modelsCacheUpdated(&$config) {
90  $result = false;
91  $files = self::getModelsFiles ( $config, true );
92  foreach ( $files as $file ) {
93  if (\is_file ( $file )) {
94  $model = ClassUtils::getClassFullNameFromFile ( $file );
95  $p = new ModelParser ();
96  $p->parse ( $model );
97  if (! self::modelCacheExists ( $model ) || self::getOrmModelCache ( $model ) != $p->asArray ()) {
98  $result [$model] = true;
99  }
100  }
101  }
102  return $result;
103  }
104 
112  public static function getModelsFiles(&$config, $silent = false) {
113  return self::_getFiles ( $config, 'models', $silent );
114  }
115 
123  public static function getModels(&$config, $silent = false, $databaseOffset = 'default') {
124  $result = [ ];
125  $files = self::getModelsFiles ( $config, $silent );
126  foreach ( $files as $file ) {
127  $className = ClassUtils::getClassFullNameFromFile ( $file );
128  $db = 'default';
129  $ret = Reflexion::getAnnotationClass ( $className, '@database' );
130  if (\sizeof ( $ret ) > 0) {
131  $db = $ret [0]->name;
132  }
133  if ($db === $databaseOffset) {
134  $result [] = $className;
135  }
136  }
137  return $result;
138  }
139 
140  public static function getModelsDatabases() {
141  if (self::$cache->exists ( self::$modelsDatabaseKey )) {
142  return self::$cache->fetch ( self::$modelsDatabaseKey );
143  }
144  return [ ];
145  }
146 
155  public static function warmUpModels(&$config, $offset = 'default', $models = null) {
156  $models ??= self::getModels ( $config, true, $offset );
157  foreach ( $models as $model ) {
158  OrmUtils::getModelMetadata ( $model );
159  Reflexion::getPropertiesAndValues ( new $model () );
160  }
161  }
162 }
static initModelsCache(&$config, $forChecking=false, $silent=false)
static modelsCacheUpdated(&$config)
Checks if the models cache is up to date.
static _getFiles(&$config, $type, $silent=false)
static warmUpModels(&$config, $offset='default', $models=null)
Preloads models metadatas.
static getPropertiesAndValues($instance, $props=NULL)
Definition: Reflexion.php:49
static getAnnotationClass($class, $annotation)
Definition: Reflexion.php:75
static getModelsFiles(&$config, $silent=false)
Returns an array of files corresponding to models.
static initClassValidators($class)
Parses a class and save validators in cache to use in dev only.
static getModelMetadata($className)
Definition: OrmUtils.php:20
static getModels(&$config, $silent=false, $databaseOffset='default')
Returns an array of the models class names.
static getClassFullNameFromFile($filePathName, $backSlash=false)
get the full name (name \ namespace) of a class from its file path result example: (string) "I\Am\The...
Definition: ClassUtils.php:70