Passed
Push — master ( 8ceacb...8c98e8 )
by Jean-Christophe
15:18
created

RouterCacheTrait::addRoute()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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

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
	abstract public static function getControllers($subClass = "\\Ubiquity\\controllers\\Controller", $backslash = false, $includeSubclass = false, $includeAbstract = false);
27
28 11
	public static function controllerCacheUpdated(&$config) {
29 11
		$result = false;
30 11
		$domain=DDDManager::getActiveDomain();
31 11
		$newRoutes = self::parseControllerFiles ( $config, true ,$domain!='');
32 11
		$ctrls = self::getControllerCacheByDomain(false,$domain);
33 11
		if ($newRoutes ['default'] != $ctrls && !(false)) {
34 3
			$result ['default'] = true;
35
		}
36 11
		$ctrls = self::getControllerCacheByDomain ( true,$domain );
37 11
		if ($newRoutes ['rest'] != $ctrls) {
38
			$result ['rest'] = true;
39
		}
40 11
		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
		return $response;
53
	}
54
55 1
	private static function getRouteKey($routePath) {
56 1
		if (\is_array ( $routePath )) {
57
			return 'path' . \md5 ( \implode ( '', $routePath ) );
58
		}
59 1
		return 'path' . \md5 ( Router::slashPath ( $routePath ) );
60
	}
61
62
	/**
63
	 *
64
	 * @param boolean $isRest
65
	 * @return array
66
	 */
67 87
	public static function getControllerCache($isRest = false) {
68 87
		$key = ($isRest) ? 'rest' : 'default';
69 87
		if (self::$cache->exists ( 'controllers/routes.' . $key )) {
70 87
			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 11
	public static function getControllerCacheByDomain(bool $isRest = false,string $domain=''): array {
82 11
		$key = ($isRest) ? 'rest' : 'default';
83 11
		if (self::$cache->exists ( 'controllers/routes.' . $key )) {
84 11
			if($domain=='') {
85 11
				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
109
	/**
110
	 *
111
	 * @param boolean $isRest
112
	 * @return array
113
	 */
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
		if (self::$cache->exists ( 'controllers/' . $key ) && ! self::expired ( $key, $duration )) {
126
			$response = self::$cache->fetch ( 'controllers/' . $key );
127
			if ($ct = $response ['content-type'] ?? false) {
128
				UResponse::setContentType ( $ct );
129
			}
130
			return $response ['content'] ?? '';
131
		} else {
132
			$response = Startup::runAsString ( $routeArray );
133
			return self::storeRouteResponse ( $key, $response );
134
		}
135
	}
136
137
	protected static function expired($key, $duration) {
138
		return self::$cache->expired ( "controllers/" . $key, $duration ) === true;
139
	}
140
141
	public static function isExpired($routePath, $duration) {
142
		return self::expired ( self::getRouteKey ( $routePath ), $duration );
143
	}
144
145 1
	public static function setExpired($routePath) {
146 1
		$key = self::getRouteKey ( $routePath );
147 1
		if (self::$cache->exists ( 'controllers/' . $key )) {
148
			self::$cache->remove ( 'controllers/' . $key );
149
		}
150 1
	}
151
152
	public static function setRouteCache($routePath) {
153
		$key = self::getRouteKey ( $routePath );
154
		$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 2
	public static function getRoutes() {
163 2
		$result = self::getControllerCache ();
164 2
		return $result;
165
	}
166
167 1
	public static function getControllerRoutes($controllerClass, $isRest = false) {
168 1
		$result = [ ];
169 1
		$ctrlCache = self::getControllerCache ( $isRest );
170 1
		foreach ( $ctrlCache as $path => $routeAttributes ) {
171 1
			if (isset ( $routeAttributes ['controller'] )) {
172 1
				if ($routeAttributes ['controller'] === $controllerClass) {
173 1
					$result [$path] = $routeAttributes;
174
				}
175
			} else {
176 1
				$firstValue = current ( $routeAttributes );
177 1
				if (isset ( $firstValue ) && isset ( $firstValue ['controller'] )) {
178 1
					if ($firstValue ['controller'] === $controllerClass) {
179 1
						$result [$path] = $routeAttributes;
180
					}
181
				}
182
			}
183
		}
184 1
		return $result;
185
	}
186
187 1
	public static function addRoute($path, $controller, $action = 'index', $methods = null, $name = '', $isRest = false, $priority = 0, $callback = null) {
188 1
		$controllerCache = self::getControllerCache ( $isRest );
189 1
		Router::addRouteToRoutes ( $controllerCache, $path, $controller, $action, $methods, $name, false, null, [ ], $priority, $callback );
190 1
		self::$cache->store ( 'controllers/routes.' . ($isRest ? 'rest' : 'default'), $controllerCache, 'controllers' );
191 1
	}
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