Passed
Push — master ( be71ba...d26f6c )
by Jean-Christophe
03:55
created

CacheManager::getAbsoluteCacheDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
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 AbstractDataCache
0 ignored issues
show
Bug introduced by
The type Ubiquity\cache\AbstractDataCache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
	 */
29
	public static $cache;
30
	private static $cacheDirectory;
31
32 4
	public static function start(&$config) {
33 4
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
34 4
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
35 4
		Annotations::$config ['cache'] = new AnnotationCache ( $cacheDirectory . '/annotations' );
36 4
		self::register ( Annotations::getManager () );
37 4
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
38 4
	}
39
40
	/**
41
	 * Starts the cache for production
42
	 *
43
	 * @param array $config
44
	 */
45 32
	public static function startProd(&$config) {
46 32
		self::$cacheDirectory = self::initialGetCacheDirectory ( $config );
47 32
		$cacheDirectory = \ROOT . \DS . self::$cacheDirectory;
48 32
		self::getCacheInstance ( $config, $cacheDirectory, ".cache" );
49 32
	}
50
51 32
	protected static function getCacheInstance(&$config, $cacheDirectory, $postfix) {
52 32
		$cacheSystem = 'Ubiquity\cache\system\ArrayCache';
53 32
		$cacheParams = [ ];
54 32
		if (! isset ( self::$cache )) {
55 32
			if (isset ( $config ["cache"] ["system"] )) {
56 32
				$cacheSystem = $config ["cache"] ["system"];
57
			}
58 32
			if (isset ( $config ["cache"] ["params"] )) {
59 32
				$cacheParams = $config ["cache"] ["params"];
60
			}
61 32
			self::$cache = new $cacheSystem ( $cacheDirectory, $postfix, $cacheParams );
62
		}
63 32
		return self::$cache;
64
	}
65
66 32
	private static function initialGetCacheDirectory(&$config) {
67 32
		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 2
	public static function checkCache(&$config, $silent = false) {
83 2
		$dirs = self::getCacheDirectories ( $config, $silent );
84 2
		foreach ( $dirs as $dir ) {
85 2
			self::safeMkdir ( $dir );
86
		}
87 2
		return $dirs;
88
	}
89
90 4
	public static function getCacheDirectories(&$config, $silent = false) {
91 4
		$cacheDirectory = self::initialGetCacheDirectory ( $config );
92 4
		$rootDS = \ROOT . \DS;
93 4
		if (! $silent) {
94 2
			echo "cache directory is " . UFileSystem::cleanPathname ( $rootDS . $cacheDirectory ) . "\n";
95
		}
96 4
		$cacheDirectory = $rootDS . $cacheDirectory . \DS;
97 4
		$modelsDir = str_replace ( "\\", \DS, $config ["mvcNS"] ["models"] );
98 4
		$controllersDir = str_replace ( "\\", \DS, $config ["mvcNS"] ["controllers"] );
99 4
		$annotationCacheDir = $cacheDirectory . "annotations";
100 4
		$modelsCacheDir = $cacheDirectory . $modelsDir;
101 4
		$queriesCacheDir = $cacheDirectory . "queries";
102 4
		$controllersCacheDir = $cacheDirectory . $controllersDir;
103 4
		$viewsCacheDir = $cacheDirectory . "views";
104 4
		$seoCacheDir = $cacheDirectory . "seo";
105 4
		$gitCacheDir = $cacheDirectory . "git";
106 4
		$contentsCacheDir = $cacheDirectory . "contents";
107 4
		return [ "annotations" => $annotationCacheDir,"models" => $modelsCacheDir,"controllers" => $controllersCacheDir,"queries" => $queriesCacheDir,"views" => $viewsCacheDir,"seo" => $seoCacheDir,"git" => $gitCacheDir,"contents" => $contentsCacheDir ];
108
	}
109
110 2
	private static function safeMkdir($dir) {
111 2
		if (! is_dir ( $dir ))
112
			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 6
	protected static function _getFiles(&$config, $type, $silent = false) {
140 6
		$typeNS = $config ["mvcNS"] [$type];
141 6
		$typeDir = \ROOT . \DS . str_replace ( "\\", \DS, $typeNS );
142 6
		if (! $silent)
143 2
			echo \ucfirst ( $type ) . " directory is " . \ROOT . $typeNS . "\n";
144 6
		return UFileSystem::glob_recursive ( $typeDir . \DS . '*' );
145
	}
146
147 4
	private static function register(AnnotationManager $annotationManager) {
148 4
		$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 4
	}
153
}
154