Passed
Push — master ( 83996b...17a193 )
by Jean-Christophe
09:22
created

RouterCacheTrait::addRoute()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 8
crap 6

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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