Completed
Push — master ( 31895c...135d28 )
by Jean-Christophe
02:15
created

CacheManager::deleteAllFilesFromFolder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
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 getCacheDirectory(&$config){
26
		$cacheDirectory=@$config["cacheDirectory"];
27
		if(!isset($cacheDirectory)){
28
			$config["cacheDirectory"]="cache/";
29
			$cacheDirectory=$config["cacheDirectory"];
30
		}
31
		return $cacheDirectory;
32
	}
33
34
	public static function createOrmModelCache($className){
35
		$key=\str_replace("\\", DIRECTORY_SEPARATOR, $className);
36
		if(!self::$cache->exists($key)){
37
			$p=new ModelParser();
38
			$p->parse($className);
39
			self::$cache->store($key, $p->__toString());
40
		}
41
		return self::$cache->fetch($key);
42
	}
43
44
	private static function addControllerCache($classname){
45
			$parser=new ControllerParser();
46
			try {
47
				$parser->parse($classname);
48
				self::$routes=\array_merge($parser->asArray(),self::$routes);
49
			} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class micro\cache\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
50
			}
51
52
	}
53
54
	private static function checkCache(&$config){
55
		$cacheDirectory=self::getCacheDirectory($config);
56
		$modelsDir=str_replace("\\", DS, $config["mvcNS"]["models"]);
57
		$controllersDir=str_replace("\\", DS, $config["mvcNS"]["controllers"]);
58
		echo "cache directory is ".ROOT.DS.$cacheDirectory."\n";
59
		$annotationCacheDir=ROOT.DS.$cacheDirectory.DS."annotations";
60
		$modelsCacheDir=ROOT.DS.$cacheDirectory.DS.$modelsDir;
61
		$controllersCacheDir=ROOT.DS.$cacheDirectory.DS.$controllersDir;
62
		self::safeMkdir($annotationCacheDir);
63
		self::safeMkdir($modelsCacheDir);
64
		self::safeMkdir($controllersCacheDir);
65
		return ["annotations"=>$annotationCacheDir,"models"=>$modelsCacheDir,"controllers"=>$controllersCacheDir];
66
	}
67
68
	private static function safeMkdir($dir){
69
		if(!is_dir($dir))
70
			return mkdir($dir,0777,true);
71
	}
72
73
	private static function deleteAllFilesFromFolder($folder){
74
		$files = glob($folder.'/*');
75
		foreach($files as $file){
76
			if(is_file($file))
77
				unlink($file);
78
		}
79
	}
80
	public static function clearCache(&$config,$type="all"){
81
		$cacheDirectories=self::checkCache($config);
82
		if($type==="all"){
83
			self::deleteAllFilesFromFolder($cacheDirectories["annotations"]);
84
		}
85
		if($type==="all" || $type==="controllers")
86
			self::deleteAllFilesFromFolder($cacheDirectories["controllers"]);
87
		if($type==="all" || $type==="models")
88
			self::deleteAllFilesFromFolder($cacheDirectories["models"]);
89
	}
90
91
	public static function initCache(&$config,$type="all"){
92
		self::checkCache($config);
93
		self::start($config);
94
		if($type==="all" || $type==="models")
95
			self::initModelsCache($config);
96
		if($type==="all" || $type==="controllers")
97
			self::initControllersCache($config);
98
	}
99
100
	private static function initModelsCache(&$config){
101
		$modelsNS=$config["mvcNS"]["models"];
102
		$modelsDir=ROOT.DS.str_replace("\\", DS, $modelsNS);
103
		echo "Models directory is ".ROOT.$modelsNS."\n";
104
		$files = glob($modelsDir.DS.'*');
105
		$namespace="";
106
		if(isset($modelsNS) && $modelsNS!=="")
107
			$namespace=$modelsNS."\\";
108 View Code Duplication
		foreach($files as $file){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
			if(is_file($file)){
110
				$fileName=pathinfo($file, PATHINFO_FILENAME);
111
				$model=$namespace.ucfirst($fileName);
112
				new $model();
113
			}
114
		}
115
	}
116
117
	private static function initControllersCache(&$config){
118
		$controllersNS=$config["mvcNS"]["controllers"];
119
		$controllersDir=ROOT.DS.str_replace("\\", DS, $controllersNS);
120
		echo "Controllers directory is ".ROOT.$controllersNS."\n";
121
		$files = glob($controllersDir.DS.'*');
122
		$namespace="";
123
		if(isset($controllersNS) && $controllersNS!=="")
124
			$namespace=$controllersNS."\\";
125 View Code Duplication
		foreach($files as $file){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
			if(is_file($file)){
127
				$fileName=pathinfo($file, PATHINFO_FILENAME);
128
				$controller=$namespace.ucfirst($fileName);
129
				self::addControllerCache($controller);
130
			}
131
		}
132
		self::$cache->store("controllers/routes", "return ".JArray::asPhpArray(self::$routes,"array").";");
133
	}
134
135
	private static function register(AnnotationManager $annotationManager){
136
		$annotationManager->registry=array_merge($annotationManager->registry,[
137
				'id' => 'micro\annotations\IdAnnotation',
138
				'manyToOne' => 'micro\annotations\ManyToOneAnnotation',
139
				'oneToMany' => 'micro\annotations\OneToManyAnnotation',
140
				'manyToMany' => 'micro\annotations\ManyToManyAnnotation',
141
				'joinColumn' => 'micro\annotations\JoinColumnAnnotation',
142
				'table' => 'micro\annotations\TableAnnotation',
143
				'transient' => 'micro\annotations\TransientAnnotation',
144
				'column' => 'micro\annotations\ColumnAnnotation',
145
				'joinTable' => 'micro\annotations\JoinTableAnnotation',
146
				'route' => 'micro\annotations\router\RouteAnnotation'
147
		]);
148
	}
149
150
}