Completed
Push — master ( fcf532...996d51 )
by Jean-Christophe
05:34
created

CacheManager::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 6
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\cache;
4
5
use Ubiquity\cache\traits\ModelsCacheTrait;
6
use Ubiquity\cache\traits\RestCacheTrait;
7
use Ubiquity\cache\traits\RouterCacheTrait;
8
use Ubiquity\utils\base\UFileSystem;
9
use mindplay\annotations\AnnotationCache;
10
use mindplay\annotations\AnnotationManager;
11
use mindplay\annotations\Annotations;
12
13
/**
14
 * Manager for caches (Router, Rest, models)
15
 * Ubiquity\cache$CacheManager
16
 * This class is part of Ubiquity
17
 *
18
 * @author jcheron <[email protected]>
19
 * @version 1.0.0
20
 *
21
 */
22
class CacheManager {
23
	use RouterCacheTrait,ModelsCacheTrait,RestCacheTrait;
24
25
	/**
26
	 *
27
	 * @var \Ubiquity\cache\system\AbstractDataCache
28
	 */
29
	public static $cache;
30
	private static $cacheDirectory;
31
32 5
	public static function start(&$config) {
33 5
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
34 5
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
35 5
		Annotations::$config ['cache'] = new AnnotationCache ( $cacheDirectory . '/annotations' );
36 5
		self::register ( Annotations::getManager () );
37 5
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
38 5
	}
39
40
	/**
41
	 * Starts the cache for production
42
	 *
43
	 * @param array $config
44
	 */
45 36
	public static function startProd(&$config) {
46 36
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
47 36
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
48 36
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
49 36
	}
50
51 37
	protected static function getCacheInstance(&$config, $cacheDirectory, $postfix) {
52 37
		$cacheSystem = 'Ubiquity\cache\system\ArrayCache';
53 37
		$cacheParams = [ ];
54 37
		if (! isset ( self::$cache )) {
55 37
			if (isset ( $config ["cache"] ["system"] )) {
56 37
				$cacheSystem = $config ["cache"] ["system"];
57
			}
58 37
			if (isset ( $config ["cache"] ["params"] )) {
59 37
				$cacheParams = $config ["cache"] ["params"];
60
			}
61 37
			self::$cache = new $cacheSystem ( $cacheDirectory, $postfix, $cacheParams );
62
		}
63 37
		return self::$cache;
64
	}
65
66 37
	private static function initialGetCacheDirectory(&$config) {
67 37
		return $config ["cache"] ["directory"] ?? ($config ["cache"] ["directory"] = "cache" . \DS);
68
	}
69
70 1
	public static function getCacheDirectory() {
71 1
		return self::$cacheDirectory;
72
	}
73
74 2
	public static function getAbsoluteCacheDirectory() {
75 2
		return \ROOT . \DS . self::$cacheDirectory;
76
	}
77
78
	public static function getCacheSubDirectory($subDirectory) {
79
		return \ROOT . \DS . self::$cacheDirectory . \DS . $subDirectory;
80
	}
81
82 3
	public static function checkCache(&$config, $silent = false) {
83 3
		$dirs = self::getCacheDirectories ( $config, $silent );
84 3
		foreach ( $dirs as $dir ) {
85 3
			self::safeMkdir ( $dir );
86
		}
87 3
		return $dirs;
88
	}
89
90 5
	public static function getCacheDirectories(&$config, $silent = false) {
91 5
		$cacheDirectory = self::initialGetCacheDirectory ( $config );
92 5
		$rootDS = \ROOT . \DS;
93 5
		if (! $silent) {
94 3
			echo "cache directory is " . UFileSystem::cleanPathname ( $rootDS . $cacheDirectory ) . "\n";
95
		}
96 5
		$cacheDirectory = $rootDS . $cacheDirectory . \DS;
97 5
		$modelsDir = str_replace ( "\\", \DS, $config ["mvcNS"] ["models"] );
98 5
		$controllersDir = str_replace ( "\\", \DS, $config ["mvcNS"] ["controllers"] );
99 5
		$annotationCacheDir = $cacheDirectory . "annotations";
100 5
		$modelsCacheDir = $cacheDirectory . $modelsDir;
101 5
		$queriesCacheDir = $cacheDirectory . "queries";
102 5
		$controllersCacheDir = $cacheDirectory . $controllersDir;
103 5
		$viewsCacheDir = $cacheDirectory . "views";
104 5
		$seoCacheDir = $cacheDirectory . "seo";
105 5
		$gitCacheDir = $cacheDirectory . "git";
106 5
		$contentsCacheDir = $cacheDirectory . "contents";
107 5
		return [ "annotations" => $annotationCacheDir,"models" => $modelsCacheDir,"controllers" => $controllersCacheDir,"queries" => $queriesCacheDir,"views" => $viewsCacheDir,"seo" => $seoCacheDir,"git" => $gitCacheDir,"contents" => $contentsCacheDir ];
108
	}
109
110 3
	private static function safeMkdir($dir) {
111 3
		if (! is_dir ( $dir ))
112 1
			return mkdir ( $dir, 0777, true );
113 2
	}
114
115
	public static function clearCache(&$config, $type = "all") {
116
		$cacheDirectories = self::checkCache ( $config );
117
		$cacheDirs = [ "annotations","controllers","models","queries","views","contents" ];
118
		foreach ( $cacheDirs as $typeRef ) {
119
			self::_clearCache ( $cacheDirectories, $type, $typeRef );
120
		}
121
	}
122
123
	private static function _clearCache($cacheDirectories, $type, $typeRef) {
124
		if ($type === "all" || $type === $typeRef)
125
			UFileSystem::deleteAllFilesFromFolder ( $cacheDirectories [$typeRef] );
126
	}
127
128 2
	public static function initCache(&$config, $type = "all", $silent = false) {
129 2
		self::checkCache ( $config, $silent );
130 2
		self::start ( $config );
131 2
		if ($type === "all" || $type === "models")
132
			self::initModelsCache ( $config, false, $silent );
133 2
		if ($type === "all" || $type === "controllers")
134 2
			self::initRouterCache ( $config, $silent );
135 2
		if ($type === "all" || $type === "rest")
136 1
			self::initRestCache ( $config, $silent );
137 2
	}
138
139 7
	protected static function _getFiles(&$config, $type, $silent = false) {
140 7
		$typeNS = $config ["mvcNS"] [$type];
141 7
		$typeDir = \ROOT . \DS . str_replace ( "\\", \DS, $typeNS );
142 7
		if (! $silent)
143 3
			echo \ucfirst ( $type ) . " directory is " . \ROOT . $typeNS . "\n";
144 7
		return UFileSystem::glob_recursive ( $typeDir . \DS . '*' );
145
	}
146
147 5
	private static function register(AnnotationManager $annotationManager) {
148 5
		$annotationManager->registry = array_merge ( $annotationManager->registry, [ 'id' => 'Ubiquity\annotations\IdAnnotation','manyToOne' => 'Ubiquity\annotations\ManyToOneAnnotation','oneToMany' => 'Ubiquity\annotations\OneToManyAnnotation','manyToMany' => 'Ubiquity\annotations\ManyToManyAnnotation','joinColumn' => 'Ubiquity\annotations\JoinColumnAnnotation',
149
				'table' => 'Ubiquity\annotations\TableAnnotation','transient' => 'Ubiquity\annotations\TransientAnnotation','column' => 'Ubiquity\annotations\ColumnAnnotation','validator' => 'Ubiquity\annotations\ValidatorAnnotation','joinTable' => 'Ubiquity\annotations\JoinTableAnnotation','requestMapping' => 'Ubiquity\annotations\router\RouteAnnotation',
150
				'route' => 'Ubiquity\annotations\router\RouteAnnotation','get' => 'Ubiquity\annotations\router\GetAnnotation','getMapping' => 'Ubiquity\annotations\router\GetAnnotation','post' => 'Ubiquity\annotations\router\PostAnnotation','postMapping' => 'Ubiquity\annotations\router\PostAnnotation','var' => 'mindplay\annotations\standard\VarAnnotation',
151
				'yuml' => 'Ubiquity\annotations\YumlAnnotation','rest' => 'Ubiquity\annotations\rest\RestAnnotation','authorization' => 'Ubiquity\annotations\rest\AuthorizationAnnotation','injected' => 'Ubiquity\annotations\di\InjectedAnnotation','autowired' => 'Ubiquity\annotations\di\AutowiredAnnotation' ] );
152 5
	}
153
}
154