Completed
Push — master ( efe174...d93d05 )
by Jean-Christophe
01:58
created

CacheManager::getClassNameFromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace micro\cache;
3
use mindplay\annotations\Annotations;
4
use mindplay\annotations\AnnotationCache;
5
use mindplay\annotations\AnnotationManager;
6
use micro\orm\parser\ModelParser;
7
use micro\utils\JArray;
8
9
class CacheManager {
10
	public static $cache;
11
	private static $routes=[];
12
13
	public static function start(&$config){
14
		$cacheDirectory=ROOT.DS.self::getCacheDirectory($config);
15
		Annotations::$config['cache'] = new AnnotationCache($cacheDirectory.'/annotations');
16
		self::register(Annotations::getManager());
17
		self::$cache=new AnnotationCache($cacheDirectory);
18
	}
19
20
	public static function startProd(&$config){
21
		$cacheDirectory=ROOT.DS.self::getCacheDirectory($config);
22
		self::$cache=new AnnotationCache($cacheDirectory);
23
	}
24
25
	public static function getControllerCache(){
26
		return self::$cache->fetch("controllers/routes");
27
	}
28
29
	public static function getCacheDirectory(&$config){
30
		$cacheDirectory=@$config["cacheDirectory"];
31
		if(!isset($cacheDirectory)){
32
			$config["cacheDirectory"]="cache/";
33
			$cacheDirectory=$config["cacheDirectory"];
34
		}
35
		return $cacheDirectory;
36
	}
37
38
	public static function createOrmModelCache($className){
39
		$key=\str_replace("\\", DIRECTORY_SEPARATOR, $className);
40
		if(!self::$cache->exists($key)){
41
			$p=new ModelParser();
42
			$p->parse($className);
43
			self::$cache->store($key, $p->__toString());
44
		}
45
		return self::$cache->fetch($key);
46
	}
47
48
	private static function addControllerCache($classname){
49
			$parser=new ControllerParser();
50
			try {
51
				$parser->parse($classname);
52
				self::$routes=\array_merge($parser->asArray(),self::$routes);
53
			} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
54
			}
55
56
	}
57
58
	private static function checkCache(&$config){
59
		$cacheDirectory=self::getCacheDirectory($config);
60
		$modelsDir=str_replace("\\", DS, $config["mvcNS"]["models"]);
61
		$controllersDir=str_replace("\\", DS, $config["mvcNS"]["controllers"]);
62
		echo "cache directory is ".ROOT.DS.$cacheDirectory."\n";
63
		$annotationCacheDir=ROOT.DS.$cacheDirectory.DS."annotations";
64
		$modelsCacheDir=ROOT.DS.$cacheDirectory.DS.$modelsDir;
65
		$controllersCacheDir=ROOT.DS.$cacheDirectory.DS.$controllersDir;
66
		self::safeMkdir($annotationCacheDir);
67
		self::safeMkdir($modelsCacheDir);
68
		self::safeMkdir($controllersCacheDir);
69
		return ["annotations"=>$annotationCacheDir,"models"=>$modelsCacheDir,"controllers"=>$controllersCacheDir];
70
	}
71
72
	private static function safeMkdir($dir){
73
		if(!is_dir($dir))
74
			return mkdir($dir,0777,true);
75
	}
76
77
	private static function deleteAllFilesFromFolder($folder){
78
		$files = glob($folder.'/*');
79
		foreach($files as $file){
80
			if(is_file($file))
81
				unlink($file);
82
		}
83
	}
84
	public static function clearCache(&$config,$type="all"){
85
		$cacheDirectories=self::checkCache($config);
86
		if($type==="all"){
87
			self::deleteAllFilesFromFolder($cacheDirectories["annotations"]);
88
		}
89
		if($type==="all" || $type==="controllers")
90
			self::deleteAllFilesFromFolder($cacheDirectories["controllers"]);
91
		if($type==="all" || $type==="models")
92
			self::deleteAllFilesFromFolder($cacheDirectories["models"]);
93
	}
94
95
	public static function initCache(&$config,$type="all"){
96
		self::checkCache($config);
97
		self::start($config);
98
		if($type==="all" || $type==="models")
99
			self::initModelsCache($config);
100
		if($type==="all" || $type==="controllers")
101
			self::initControllersCache($config);
102
	}
103
104
	private static function initModelsCache(&$config){
105
		$modelsNS=$config["mvcNS"]["models"];
106
		$modelsDir=ROOT.DS.str_replace("\\", DS, $modelsNS);
107
		echo "Models directory is ".ROOT.$modelsNS."\n";
108
		$files = glob($modelsDir.DS.'*');
109
		$namespace="";
110
		if(isset($modelsNS) && $modelsNS!=="")
111
			$namespace=$modelsNS."\\";
112
		foreach($files as $file){
113
			if(is_file($file)){
114
				$model=self::getClassNameFromFile($file,$namespace);
115
				new $model();
116
			}
117
		}
118
	}
119
120
	private static function getClassNameFromFile($file,$namespace=""){
121
		$fileName=pathinfo($file, PATHINFO_FILENAME);
122
		return $namespace.ucfirst($fileName);
123
	}
124
125
	private static function initControllersCache(&$config){
126
		$controllersNS=$config["mvcNS"]["controllers"];
127
		$controllersDir=ROOT.DS.str_replace("\\", DS, $controllersNS);
128
		echo "Controllers directory is ".ROOT.$controllersNS."\n";
129
		$files = glob($controllersDir.DS.'*');
130
		$namespace="";
131
		if(isset($controllersNS) && $controllersNS!=="")
132
			$namespace=$controllersNS."\\";
133
		foreach($files as $file){
134
			if(is_file($file)){
135
				$controller=self::getClassNameFromFile($file,$namespace);
136
				self::addControllerCache($controller);
137
			}
138
		}
139
		if($config["debug"])
140
			self::addAdminRoutes();
141
		self::$cache->store("controllers/routes", "return ".JArray::asPhpArray(self::$routes,"array").";");
142
	}
143
144
	private static function register(AnnotationManager $annotationManager){
145
		$annotationManager->registry=array_merge($annotationManager->registry,[
146
				'id' => 'micro\annotations\IdAnnotation',
147
				'manyToOne' => 'micro\annotations\ManyToOneAnnotation',
148
				'oneToMany' => 'micro\annotations\OneToManyAnnotation',
149
				'manyToMany' => 'micro\annotations\ManyToManyAnnotation',
150
				'joinColumn' => 'micro\annotations\JoinColumnAnnotation',
151
				'table' => 'micro\annotations\TableAnnotation',
152
				'transient' => 'micro\annotations\TransientAnnotation',
153
				'column' => 'micro\annotations\ColumnAnnotation',
154
				'joinTable' => 'micro\annotations\JoinTableAnnotation',
155
				'route' => 'micro\annotations\router\RouteAnnotation'
156
		]);
157
	}
158
159
	public static function addAdminRoutes(){
160
		self::addControllerCache("micro\controllers\Admin");
161
	}
162
163
	public static function getRoutes(){
164
		$result=self::getControllerCache();
165
		return $result;
166
	}
167
}
168