1
|
|
|
<?php |
2
|
|
|
namespace Ubiquity\cache\traits; |
3
|
|
|
|
4
|
|
|
use Ubiquity\orm\parser\ModelParser; |
5
|
|
|
use Ubiquity\cache\ClassUtils; |
6
|
|
|
use Ubiquity\contents\validation\ValidatorsManager; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author jc |
10
|
|
|
* @static array $cache |
11
|
|
|
*/ |
12
|
|
|
trait ModelsCacheTrait{ |
13
|
|
|
abstract protected static function _getFiles(&$config,$type,$silent=false); |
14
|
|
|
|
15
|
1 |
|
public static function createOrmModelCache($classname) { |
16
|
1 |
|
$key=self::getModelCacheKey($classname); |
17
|
1 |
|
if(isset(self::$cache)){ |
18
|
1 |
|
if (!self::$cache->exists($key)) { |
19
|
1 |
|
$p=new ModelParser(); |
20
|
1 |
|
$p->parse($classname); |
21
|
1 |
|
self::$cache->store($key, $p->__toString(),'models'); |
22
|
|
|
} |
23
|
1 |
|
return self::$cache->fetch($key); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
5 |
|
public static function getOrmModelCache($classname) { |
28
|
5 |
|
return self::$cache->fetch(self::getModelCacheKey($classname)); |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
public static function getModelCacheKey($classname){ |
32
|
6 |
|
return \str_replace("\\", \DS, $classname); |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public static function modelCacheExists($classname){ |
36
|
2 |
|
$key=self::getModelCacheKey($classname); |
37
|
2 |
|
if(isset(self::$cache)) |
38
|
2 |
|
return self::$cache->exists($key); |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public static function initModelsCache(&$config,$forChecking=false,$silent=false) { |
43
|
1 |
|
$files=self::getModelsFiles($config,$silent); |
44
|
1 |
|
foreach ( $files as $file ) { |
45
|
1 |
|
if (is_file($file)) { |
46
|
1 |
|
$model=ClassUtils::getClassFullNameFromFile($file); |
47
|
1 |
|
if(!$forChecking){ |
48
|
1 |
|
self::createOrmModelCache($model); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
1 |
|
if(!$forChecking){ |
53
|
1 |
|
ValidatorsManager::initModelsValidators($config); |
54
|
|
|
} |
55
|
1 |
|
if(!$silent){ |
56
|
1 |
|
echo "Models cache reset\n"; |
57
|
|
|
} |
58
|
1 |
|
} |
59
|
|
|
|
60
|
4 |
|
public static function getModelsFiles(&$config,$silent=false){ |
61
|
4 |
|
return self::_getFiles($config, "models",$silent); |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
public static function getModels(&$config,$silent=false){ |
65
|
4 |
|
$result=[]; |
66
|
4 |
|
$files=self::getModelsFiles($config,$silent); |
67
|
4 |
|
foreach ($files as $file){ |
68
|
4 |
|
$result[]=ClassUtils::getClassFullNameFromFile($file); |
69
|
|
|
} |
70
|
4 |
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|