Passed
Push — master ( c1d44b...f8fb84 )
by Jean-Christophe
06:07
created

CacheManager::getAllRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 12
	public static function start(&$config) {
33 12
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
34 12
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
35 12
		Annotations::$config ['cache'] = new AnnotationCache ( $cacheDirectory . '/annotations' );
36 12
		self::register ( Annotations::getManager () );
37 12
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
38 12
	}
39
40
	/**
41
	 * Starts the cache for production
42
	 *
43
	 * @param array $config
44
	 */
45 77
	public static function startProd(&$config) {
46 77
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
47 77
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
48 77
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
49 77
	}
50
51 81
	protected static function getCacheInstance(&$config, $cacheDirectory, $postfix) {
52 81
		$cacheSystem = 'Ubiquity\cache\system\ArrayCache';
53 81
		$cacheParams = [ ];
54 81
		if (! isset ( self::$cache )) {
55 76
			if (isset ( $config ["cache"] ["system"] )) {
56 76
				$cacheSystem = $config ["cache"] ["system"];
57
			}
58 76
			if (isset ( $config ["cache"] ["params"] )) {
59 76
				$cacheParams = $config ["cache"] ["params"];
60
			}
61 76
			self::$cache = new $cacheSystem ( $cacheDirectory, $postfix, $cacheParams );
62
		}
63 81
		return self::$cache;
64
	}
65
66 81
	private static function initialGetCacheDirectory(&$config) {
67 81
		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 16
	public static function getCacheSubDirectory($subDirectory) {
79 16
		return \ROOT . \DS . self::$cacheDirectory . \DS . $subDirectory;
80
	}
81
82 8
	public static function checkCache(&$config, $silent = false) {
83 8
		$dirs = self::getCacheDirectories ( $config, $silent );
84 8
		foreach ( $dirs as $dir ) {
85 8
			self::safeMkdir ( $dir );
86
		}
87 8
		return $dirs;
88
	}
89
90 10
	public static function getCacheDirectories(&$config, $silent = false) {
91 10
		$cacheDirectory = self::initialGetCacheDirectory ( $config );
92 10
		$rootDS = \ROOT . \DS;
93 10
		if (! $silent) {
94 8
			echo "cache directory is " . UFileSystem::cleanPathname ( $rootDS . $cacheDirectory ) . "\n";
95
		}
96 10
		$cacheDirectory = $rootDS . $cacheDirectory . \DS;
97 10
		$modelsDir = str_replace ( "\\", \DS, $config ["mvcNS"] ["models"] );
98 10
		$controllersDir = str_replace ( "\\", \DS, $config ["mvcNS"] ["controllers"] );
99 10
		$annotationCacheDir = $cacheDirectory . "annotations";
100 10
		$modelsCacheDir = $cacheDirectory . $modelsDir;
101 10
		$queriesCacheDir = $cacheDirectory . "queries";
102 10
		$controllersCacheDir = $cacheDirectory . $controllersDir;
103 10
		$viewsCacheDir = $cacheDirectory . "views";
104 10
		$seoCacheDir = $cacheDirectory . "seo";
105 10
		$gitCacheDir = $cacheDirectory . "git";
106 10
		$contentsCacheDir = $cacheDirectory . "contents";
107 10
		return [ "annotations" => $annotationCacheDir,"models" => $modelsCacheDir,"controllers" => $controllersCacheDir,"queries" => $queriesCacheDir,"views" => $viewsCacheDir,"seo" => $seoCacheDir,"git" => $gitCacheDir,"contents" => $contentsCacheDir ];
108
	}
109
110 8
	private static function safeMkdir($dir) {
111 8
		if (! is_dir ( $dir ))
112 2
			return mkdir ( $dir, 0777, true );
113 7
	}
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 6
	public static function initCache(&$config, $type = "all", $silent = false) {
129 6
		self::checkCache ( $config, $silent );
130 6
		self::start ( $config );
131 6
		if ($type === "all" || $type === "models")
132 4
			self::initModelsCache ( $config, false, $silent );
133 6
		if ($type === "all" || $type === "controllers")
134 6
			self::initRouterCache ( $config, $silent );
135 5
		if ($type === "all" || $type === "rest")
136 5
			self::initRestCache ( $config, $silent );
137 5
	}
138
139
	public static function getAllRoutes() {
140
		$routes = self::getControllerCache ();
141
		return array_merge ( $routes, self::getControllerCache ( true ) );
142
	}
143
144 14
	protected static function _getFiles(&$config, $type, $silent = false) {
145 14
		$typeNS = $config ["mvcNS"] [$type];
146 14
		$typeDir = \ROOT . \DS . str_replace ( "\\", \DS, $typeNS );
147 14
		if (! $silent)
148 9
			echo \ucfirst ( $type ) . " directory is " . \ROOT . $typeNS . "\n";
149 14
		return UFileSystem::glob_recursive ( $typeDir . \DS . '*' );
150
	}
151
152 12
	private static function register(AnnotationManager $annotationManager) {
153 12
		$annotationManager->registry = array_merge ( $annotationManager->registry, [
154 12
																						'id' => 'Ubiquity\annotations\IdAnnotation',
155
																						'manyToOne' => 'Ubiquity\annotations\ManyToOneAnnotation',
156
																						'oneToMany' => 'Ubiquity\annotations\OneToManyAnnotation',
157
																						'manyToMany' => 'Ubiquity\annotations\ManyToManyAnnotation',
158
																						'joinColumn' => 'Ubiquity\annotations\JoinColumnAnnotation',
159
																						'table' => 'Ubiquity\annotations\TableAnnotation',
160
																						'transient' => 'Ubiquity\annotations\TransientAnnotation',
161
																						'column' => 'Ubiquity\annotations\ColumnAnnotation',
162
																						'validator' => 'Ubiquity\annotations\ValidatorAnnotation',
163
																						'joinTable' => 'Ubiquity\annotations\JoinTableAnnotation',
164
																						'requestMapping' => 'Ubiquity\annotations\router\RouteAnnotation',
165
																						'route' => 'Ubiquity\annotations\router\RouteAnnotation',
166
																						'get' => 'Ubiquity\annotations\router\GetAnnotation',
167
																						'getMapping' => 'Ubiquity\annotations\router\GetAnnotation',
168
																						'post' => 'Ubiquity\annotations\router\PostAnnotation',
169
																						'postMapping' => 'Ubiquity\annotations\router\PostAnnotation',
170
																						'var' => 'mindplay\annotations\standard\VarAnnotation',
171
																						'yuml' => 'Ubiquity\annotations\YumlAnnotation',
172
																						'rest' => 'Ubiquity\annotations\rest\RestAnnotation',
173
																						'authorization' => 'Ubiquity\annotations\rest\AuthorizationAnnotation',
174
																						'injected' => 'Ubiquity\annotations\di\InjectedAnnotation',
175
																						'autowired' => 'Ubiquity\annotations\di\AutowiredAnnotation' ] );
176 12
	}
177
}
178