Test Failed
Push — master ( befc15...123c13 )
by Jean-Christophe
18:33
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
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
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\Controller;
6
use Ubiquity\controllers\Router;
7
use Ubiquity\controllers\Startup;
8
use Ubiquity\controllers\StartupAsync;
9
use Ubiquity\domains\DDDManager;
10
use Ubiquity\utils\base\UIntrospection;
11
use Ubiquity\utils\base\UString;
12
use Ubiquity\utils\http\UResponse;
13
14
/**
15
 *
16
 * Ubiquity\cache\traits$RouterCacheTrait
17
 * This class is part of Ubiquity
18
 *
19
 * @author jcheron <[email protected]>
20
 * @version 1.0.12
21
 * @property \Ubiquity\cache\system\AbstractDataCache $cache
22
 *
23
 */
24
trait RouterCacheTrait {
25
26 11
	abstract public static function getControllers($subClass = "\\Ubiquity\\controllers\\Controller", $backslash = false, $includeSubclass = false, $includeAbstract = false);
27 11
28 11
	public static function controllerCacheUpdated(&$config) {
29 11
		$result = false;
30 11
		$domain=DDDManager::getActiveDomain();
31 3
		$newRoutes = self::parseControllerFiles ( $config, true ,$domain!='');
32
		$ctrls = self::getControllerCacheByDomain(false,$domain);
33 11
		if ($newRoutes ['default'] != $ctrls && !(false)) {
34 11
			$result ['default'] = true;
35
		}
36
		$ctrls = self::getControllerCacheByDomain ( true,$domain );
37 11
		if ($newRoutes ['rest'] != $ctrls) {
38
			$result ['rest'] = true;
39
		}
40
		return $result;
41
	}
42
43
	public static function storeDynamicRoutes($isRest = false) {
44
		$routes = Router::getRoutes ();
45
		$part = ($isRest) ? 'rest' : 'default';
46
		self::$cache->store ( 'controllers/routes.' . $part, $routes, 'controllers' );
47
	}
48
49
	private static function storeRouteResponse($key, $response) {
50
		$cache = [ 'content-type' => UResponse::$headers ['Content-Type'] ?? 'text/html','content' => $response ];
51
		self::$cache->store ( 'controllers/' . $key, $cache, 'controllers' );
52 1
		return $response;
53 1
	}
54
55
	private static function getRouteKey($routePath) {
56 1
		if (\is_array ( $routePath )) {
57
			return 'path' . \md5 ( \implode ( '', $routePath ) );
58
		}
59
		return 'path' . \md5 ( Router::slashPath ( $routePath ) );
60
	}
61
62
	/**
63
	 *
64 87
	 * @param boolean $isRest
65 87
	 * @return array
66 87
	 */
67 87
	public static function getControllerCache($isRest = false) {
68
		$key = ($isRest) ? 'rest' : 'default';
69
		if (self::$cache->exists ( 'controllers/routes.' . $key )) {
70
			return self::$cache->fetch ( 'controllers/routes.' . $key );
71
		}
72
		return [ ];
73
	}
74
75
	/**
76
	 *
77
	 * @param boolean $isRest
78
	 * @param string $domain
79
	 * @return array
80
	 */
81
	public static function getControllerCacheByDomain(bool $isRest = false,string $domain=''): array {
82
		$key = ($isRest) ? 'rest' : 'default';
83
		if (self::$cache->exists ( 'controllers/routes.' . $key )) {
84
			if($domain=='') {
85
				return self::$cache->fetch('controllers/routes.' . $key);
86
			}else{
87
				$ns=Startup::getNS();
88
				$routes=self::$cache->fetch('controllers/routes.' . $key);
89
				$result=[];
90
				foreach ($routes as $k=>$route){
91
					if(isset($route['controller'])){
92
						if(UString::startswith($route['controller'],$ns)) {
93
							$result[$k]=$route;
94
						}
95
					}else{
96
						foreach ($route as $method=>$routePart){
97
							if(UString::startswith($routePart['controller'],$ns)) {
98
								$result[$k][$method]=$routePart;
99
							}
100
						}
101
					}
102
				}
103
				return $result;
104
			}
105
		}
106
		return [ ];
107
	}
108 1
109 1
	/**
110 1
	 *
111
	 * @param boolean $isRest
112
	 * @return array
113 1
	 */
114
	public static function getControllerCacheIndex($isRest = false) {
115
		$key = ($isRest) ? 'rest-index' : 'default-index';
116
		if (self::$cache->exists ( 'controllers/routes.' . $key )) {
117
			return self::$cache->fetch ( 'controllers/routes.' . $key );
118
		}
119
		return [ ];
120
	}
121
122
	public static function getRouteCache($routePath, $routeArray, $duration) {
123
		$key = self::getRouteKey ( $routePath );
124
125 2
		if (self::$cache->exists ( 'controllers/' . $key ) && ! self::expired ( $key, $duration )) {
126 2
			$response = self::$cache->fetch ( 'controllers/' . $key );
127 2
			if ($ct = $response ['content-type'] ?? false) {
128
				UResponse::setContentType ( $ct );
129
			}
130 1
			return $response ['content'] ?? '';
131 1
		} else {
132 1
			$response = Startup::runAsString ( $routeArray );
133 1
			return self::storeRouteResponse ( $key, $response );
134 1
		}
135 1
	}
136 1
137
	protected static function expired($key, $duration) {
138
		return self::$cache->expired ( "controllers/" . $key, $duration ) === true;
139 1
	}
140 1
141 1
	public static function isExpired($routePath, $duration) {
142 1
		return self::expired ( self::getRouteKey ( $routePath ), $duration );
143
	}
144
145
	public static function setExpired($routePath) {
146
		$key = self::getRouteKey ( $routePath );
147 1
		if (self::$cache->exists ( 'controllers/' . $key )) {
148
			self::$cache->remove ( 'controllers/' . $key );
149
		}
150 1
	}
151 1
152 1
	public static function setRouteCache($routePath) {
153 1
		$key = self::getRouteKey ( $routePath );
154 1
		$response = Startup::runAsString ( $routePath );
155
		return self::storeRouteResponse ( $key, $response );
156
	}
157
158
	public static function addAdminRoutes() {
159
		self::addControllerCache ( 'Ubiquity\controllers\Admin' );
160
	}
161
162
	public static function getRoutes() {
163
		$result = self::getControllerCache ();
164
		return $result;
165
	}
166
167
	public static function getControllerRoutes($controllerClass, $isRest = false) {
168
		$result = [ ];
169
		$ctrlCache = self::getControllerCache ( $isRest );
170
		foreach ( $ctrlCache as $path => $routeAttributes ) {
171
			if (isset ( $routeAttributes ['controller'] )) {
172
				if ($routeAttributes ['controller'] === $controllerClass) {
173
					$result [$path] = $routeAttributes;
174
				}
175
			} else {
176
				$firstValue = current ( $routeAttributes );
177
				if (isset ( $firstValue ) && isset ( $firstValue ['controller'] )) {
178
					if ($firstValue ['controller'] === $controllerClass) {
179
						$result [$path] = $routeAttributes;
180
					}
181
				}
182
			}
183
		}
184
		return $result;
185
	}
186
187
	public static function addRoute($path, $controller, $action = 'index', $methods = null, $name = '', $isRest = false, $priority = 0, $callback = null) {
188
		$controllerCache = self::getControllerCache ( $isRest );
189
		Router::addRouteToRoutes ( $controllerCache, $path, $controller, $action, $methods, $name, false, null, [ ], $priority, $callback );
190
		self::$cache->store ( 'controllers/routes.' . ($isRest ? 'rest' : 'default'), $controllerCache, 'controllers' );
191
	}
192
193
	public static function addRoutes($pathArray, $controller, $action = 'index', $methods = null, $name = '') {
194
		self::addRoutes_ ( $pathArray, $controller, $action, $methods, $name, false );
195
	}
196
197
	public static function addRestRoutes($pathArray, $controller, $action = 'index', $methods = null, $name = '') {
198
		self::addRoutes_ ( $pathArray, $controller, $action, $methods, $name, true );
199
	}
200
201
	private static function addRoutes_($pathArray, $controller, $action = 'index', $methods = null, $name = '', $isRest = false) {
202
		$controllerCache = self::getControllerCache ( $isRest );
203
		$postfix = 'default';
204
		if ($isRest) {
205
			$postfix = 'rest';
206
		}
207
		Router::addRoutesToRoutes ( $controllerCache, $pathArray, $controller, $action, $methods, $name );
208
		self::$cache->store ( 'controllers/routes.' . $postfix, $controllerCache, 'controllers' );
209
	}
210
211
	/**
212
	 * Preloads controllers.
213
	 * To use only with async servers (Swoole, Workerman)
214
	 *
215
	 * @param ?array $controllers
216
	 */
217
	public static function warmUpControllers($controllers = null) {
218
		$controllers ??= self::getControllers ();
219
		foreach ( $controllers as $ctrl ) {
220
			$controller = StartupAsync::getControllerInstance ( $ctrl );
221
			$binary = UIntrospection::implementsMethod ( $controller, 'isValid', Controller::class ) ? 1 : 0;
222
			$binary += UIntrospection::implementsMethod ( $controller, 'initialize', Controller::class ) ? 2 : 0;
223
			$binary += UIntrospection::implementsMethod ( $controller, 'finalize', Controller::class ) ? 4 : 0;
224
			$controller->_binaryCalls = $binary;
225
		}
226
	}
227
}
228