Passed
Push — master ( 2f96fd...1abe95 )
by Jean-Christophe
06:03
created

RouterCacheTrait::addControllerCache()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 3
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\cache\traits;
4
5
use Ubiquity\controllers\Startup;
6
use Ubiquity\controllers\Router;
7
use Ubiquity\cache\parser\ControllerParser;
8
use Ubiquity\cache\ClassUtils;
9
use Ubiquity\utils\base\UArray;
10
use Ubiquity\cache\CacheManager;
11
12
/**
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.3
16
 * @property \Ubiquity\cache\system\AbstractDataCache $cache
17
 *
18
 */
19
trait RouterCacheTrait {
20
21
	abstract protected static function _getFiles(&$config, $type, $silent = false);
22
23
	private static function addControllerCache($classname) {
24
		$parser = new ControllerParser ();
25
		try {
26
			$parser->parse ( $classname );
27
			return $parser->asArray ();
28
		} catch ( \Exception $e ) {
29
			// Nothing to do
30
		}
31
		return [ ];
32
	}
33
34 2
	private static function initRouterCache(&$config, $silent = false) {
35 2
		$routes = [ "rest" => [ ],"default" => [ ] ];
36 2
		$files = self::getControllersFiles ( $config, $silent );
37 2
		foreach ( $files as $file ) {
38 2
			if (is_file ( $file )) {
39 2
				$controller = ClassUtils::getClassFullNameFromFile ( $file );
40 2
				$parser = new ControllerParser ();
41
				try {
42 2
					$parser->parse ( $controller );
43 2
					$ret = $parser->asArray ();
44 2
					$key = ($parser->isRest ()) ? "rest" : "default";
45 2
					$routes [$key] = \array_merge ( $routes [$key], $ret );
46 1
				} catch ( \Exception $e ) {
47
					// Nothing to do
48
				}
49
			}
50
		}
51 2
		self::$cache->store ( "controllers/routes.default", "return " . UArray::asPhpArray ( $routes ["default"], "array" ) . ";", 'controllers' );
52 2
		self::$cache->store ( "controllers/routes.rest", "return " . UArray::asPhpArray ( $routes ["rest"], "array" ) . ";", 'controllers' );
53 2
		if (! $silent) {
54 2
			echo "Router cache reset\n";
55
		}
56 2
	}
57
58
	public static function storeDynamicRoutes($isRest = false) {
59
		$routes = Router::getRoutes ();
60
		$part = ($isRest) ? 'rest' : 'default';
61
		self::$cache->store ( "controllers/routes." . $part, "return " . UArray::asPhpArray ( $routes, "array" ) . ";", 'controllers' );
62
	}
63
64
	private static function storeRouteResponse($key, $response) {
65
		self::setKeyExpired ( $key, false );
66
		self::$cache->store ( "controllers/" . $key, $response, 'controllers', false );
67
		return $response;
68
	}
69
70
	private static function getRouteKey($routePath) {
71
		if (is_array ( $routePath )) {
72
			return "path" . \md5 ( \implode ( "", $routePath ) );
73
		}
74
		return "path" . \md5 ( Router::slashPath ( $routePath ) );
75
	}
76
77
	/**
78
	 *
79
	 * @param boolean $isRest
80
	 * @return array
81
	 */
82 41
	public static function getControllerCache($isRest = false) {
83 41
		$key = ($isRest) ? "rest" : "default";
84 41
		if (self::$cache->exists ( "controllers/routes." . $key ))
85 38
			return self::$cache->fetch ( "controllers/routes." . $key );
86 4
		return [ ];
87
	}
88
89
	public static function getRouteCache($routePath, $routeArray, $duration) {
90
		$key = self::getRouteKey ( $routePath );
91
92
		if (self::$cache->exists ( "controllers/" . $key ) && ! self::expired ( $key, $duration )) {
93
			$response = self::$cache->file_get_contents ( "controllers/" . $key );
94
			return $response;
95
		} else {
96
			$response = Startup::runAsString ( $routeArray );
97
			return self::storeRouteResponse ( $key, $response );
98
		}
99
	}
100
101
	protected static function expired($key, $duration) {
102
		return self::$cache->expired ( "controllers/" . $key, $duration ) === true;
103
	}
104
105
	public static function isExpired($routePath, $duration) {
106
		return self::expired ( self::getRouteKey ( $routePath ), $duration );
107
	}
108
109
	public static function setExpired($routePath) {
110
		$key = self::getRouteKey ( $routePath );
111
		if (self::$cache->exists ( "controllers/" . $key )) {
112
			self::$cache->remove ( "controllers/" . $key );
113
		}
114
	}
115
116
	public static function setRouteCache($routePath) {
117
		$key = self::getRouteKey ( $routePath );
118
		$response = Startup::runAsString ( $routePath );
119
		return self::storeRouteResponse ( $key, $response );
120
	}
121
122
	public static function addAdminRoutes() {
123
		self::addControllerCache ( "Ubiquity\controllers\Admin" );
124
	}
125
126 2
	public static function getRoutes() {
127 2
		$result = self::getControllerCache ();
128 2
		return $result;
129
	}
130
131 1
	public static function getControllerRoutes($controllerClass, $isRest = false) {
132 1
		$result = [ ];
133 1
		$ctrlCache = self::getControllerCache ( $isRest );
134 1
		foreach ( $ctrlCache as $path => $routeAttributes ) {
135 1
			if (isset ( $routeAttributes ["controller"] )) {
136 1
				if ($routeAttributes ["controller"] === $controllerClass) {
137 1
					$result [$path] = $routeAttributes;
138
				}
139
			} else {
140 1
				$firstValue = current ( $routeAttributes );
141 1
				if (isset ( $firstValue ) && isset ( $firstValue ["controller"] )) {
142 1
					if ($firstValue ["controller"] === $controllerClass) {
143 1
						$result [$path] = $routeAttributes;
144
					}
145
				}
146
			}
147
		}
148 1
		return $result;
149
	}
150
151
	public static function addRoute($path, $controller, $action = "index", $methods = null, $name = "") {
152
		$controllerCache = self::getControllerCache ();
153
		Router::addRouteToRoutes ( $controllerCache, $path, $controller, $action, $methods, $name );
154
		self::$cache->store ( "controllers/routes.default", "return " . UArray::asPhpArray ( $controllerCache, "array" ) . ";", 'controllers' );
155
	}
156
157
	public static function addRoutes($pathArray, $controller, $action = "index", $methods = null, $name = "") {
158
		self::addRoutes_ ( $pathArray, $controller, $action, $methods, $name, false );
159
	}
160
161
	public static function addRestRoutes($pathArray, $controller, $action = "index", $methods = null, $name = "") {
162
		self::addRoutes_ ( $pathArray, $controller, $action, $methods, $name, true );
163
	}
164
165
	private static function addRoutes_($pathArray, $controller, $action = "index", $methods = null, $name = "", $isRest = false) {
166
		$controllerCache = self::getControllerCache ( $isRest );
167
		$postfix = "default";
168
		if ($isRest) {
169
			$postfix = "rest";
170
		}
171
		Router::addRoutesToRoutes ( $controllerCache, $pathArray, $controller, $action, $methods, $name );
172
		self::$cache->store ( "controllers/routes." . $postfix, "return " . UArray::asPhpArray ( $controllerCache, "array" ) . ";", 'controllers' );
173
	}
174
175 4
	public static function getControllersFiles(&$config, $silent = false) {
176 4
		return self::_getFiles ( $config, "controllers", $silent );
177
	}
178
179 3
	public static function getControllers($subClass = "\\Ubiquity\\controllers\\Controller", $backslash = false, $includeSubclass = false,$includeAbstract=false) {
180 3
		$result = [ ];
181 3
		if ($includeSubclass) {
182 1
			$result [] = $subClass;
183
		}
184 3
		$config = Startup::getConfig ();
185 3
		$files = self::getControllersFiles ( $config, true );
186
		try {
187 3
			$restCtrls = CacheManager::getRestCache ();
188 1
		} catch ( \Exception $e ) {
189 1
			$restCtrls = [ ];
190
		}
191 3
		foreach ( $files as $file ) {
192 3
			if (is_file ( $file )) {
193 3
				$controllerClass = ClassUtils::getClassFullNameFromFile ( $file, $backslash );
194 3
				if (isset ( $restCtrls [$controllerClass] ) === false) {
195 3
					$r = new \ReflectionClass ( $controllerClass );
196 3
					if ($r->isSubclassOf ( $subClass ) && ($includeAbstract || ! $r->isAbstract ())) {
197 3
						$result [] = $controllerClass;
198
					}
199
				}
200
			}
201
		}
202 3
		return $result;
203
	}
204
}
205