Passed
Push — master ( f762b7...fe4615 )
by Jean-Christophe
11:36
created

RouterCacheTrait::getControllersFiles()   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
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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\utils\base\UIntrospection;
10
use Ubiquity\utils\http\UResponse;
11
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.11
20
 * @property \Ubiquity\cache\system\AbstractDataCache $cache
21
 *
22
 */
23
trait RouterCacheTrait {
24
25 11
	public static function controllerCacheUpdated(&$config) {
26 11
		$result = false;
27 11
		$newRoutes = self::parseControllerFiles ( $config, true );
28 11
		$ctrls = self::getControllerCache ();
29 11
		if ($newRoutes ['default'] != $ctrls) {
30 3
			$result ['default'] = true;
31
		}
32 11
		$ctrls = self::getControllerCache ( true );
33 11
		if ($newRoutes ['rest'] != $ctrls) {
34
			$result ['rest'] = true;
35
		}
36 11
		return $result;
37
	}
38
39
	public static function storeDynamicRoutes($isRest = false) {
40
		$routes = Router::getRoutes ();
41
		$part = ($isRest) ? 'rest' : 'default';
42
		self::$cache->store ( 'controllers/routes.' . $part, $routes, 'controllers' );
43
	}
44
45
	private static function storeRouteResponse($key, $response) {
46
		$cache = [ 'content-type' => UResponse::$headers ['Content-Type'] ?? 'text/html','content' => $response ];
47
		self::$cache->store ( 'controllers/' . $key, $cache, 'controllers' );
48
		return $response;
49
	}
50
51 1
	private static function getRouteKey($routePath) {
52 1
		if (\is_array ( $routePath )) {
53
			return 'path' . \md5 ( \implode ( '', $routePath ) );
54
		}
55 1
		return 'path' . \md5 ( Router::slashPath ( $routePath ) );
56
	}
57
58
	/**
59
	 *
60
	 * @param boolean $isRest
61
	 * @return array
62
	 */
63 88
	public static function getControllerCache($isRest = false) {
64 88
		$key = ($isRest) ? 'rest' : 'default';
65 88
		if (self::$cache->exists ( 'controllers/routes.' . $key ))
66 88
			return self::$cache->fetch ( 'controllers/routes.' . $key );
67
		return [ ];
68
	}
69
70
	public static function getRouteCache($routePath, $routeArray, $duration) {
71
		$key = self::getRouteKey ( $routePath );
72
73
		if (self::$cache->exists ( 'controllers/' . $key ) && ! self::expired ( $key, $duration )) {
74
			$response = self::$cache->fetch ( 'controllers/' . $key );
75
			if ($ct = $response ['content-type'] ?? false) {
76
				UResponse::setContentType ( $ct );
77
			}
78
			return $response ['content'] ?? '';
79
		} else {
80
			$response = Startup::runAsString ( $routeArray );
81
			return self::storeRouteResponse ( $key, $response );
82
		}
83
	}
84
85
	protected static function expired($key, $duration) {
86
		return self::$cache->expired ( "controllers/" . $key, $duration ) === true;
87
	}
88
89
	public static function isExpired($routePath, $duration) {
90
		return self::expired ( self::getRouteKey ( $routePath ), $duration );
91
	}
92
93 1
	public static function setExpired($routePath) {
94 1
		$key = self::getRouteKey ( $routePath );
95 1
		if (self::$cache->exists ( 'controllers/' . $key )) {
96
			self::$cache->remove ( 'controllers/' . $key );
97
		}
98 1
	}
99
100
	public static function setRouteCache($routePath) {
101
		$key = self::getRouteKey ( $routePath );
102
		$response = Startup::runAsString ( $routePath );
103
		return self::storeRouteResponse ( $key, $response );
104
	}
105
106
	public static function addAdminRoutes() {
107
		self::addControllerCache ( 'Ubiquity\controllers\Admin' );
108
	}
109
110 2
	public static function getRoutes() {
111 2
		$result = self::getControllerCache ();
112 2
		return $result;
113
	}
114
115 1
	public static function getControllerRoutes($controllerClass, $isRest = false) {
116 1
		$result = [ ];
117 1
		$ctrlCache = self::getControllerCache ( $isRest );
118 1
		foreach ( $ctrlCache as $path => $routeAttributes ) {
119 1
			if (isset ( $routeAttributes ['controller'] )) {
120 1
				if ($routeAttributes ['controller'] === $controllerClass) {
121 1
					$result [$path] = $routeAttributes;
122
				}
123
			} else {
124 1
				$firstValue = current ( $routeAttributes );
125 1
				if (isset ( $firstValue ) && isset ( $firstValue ['controller'] )) {
126 1
					if ($firstValue ['controller'] === $controllerClass) {
127 1
						$result [$path] = $routeAttributes;
128
					}
129
				}
130
			}
131
		}
132 1
		return $result;
133
	}
134
135 1
	public static function addRoute($path, $controller, $action = 'index', $methods = null, $name = '', $isRest = false, $priority = 0, $callback = null) {
136 1
		$controllerCache = self::getControllerCache ( $isRest );
137 1
		Router::addRouteToRoutes ( $controllerCache, $path, $controller, $action, $methods, $name, false, null, [ ], $priority, $callback );
138 1
		self::$cache->store ( 'controllers/routes.' . ($isRest ? 'rest' : 'default'), $controllerCache, 'controllers' );
139 1
	}
140
141
	public static function addRoutes($pathArray, $controller, $action = 'index', $methods = null, $name = '') {
142
		self::addRoutes_ ( $pathArray, $controller, $action, $methods, $name, false );
143
	}
144
145
	public static function addRestRoutes($pathArray, $controller, $action = 'index', $methods = null, $name = '') {
146
		self::addRoutes_ ( $pathArray, $controller, $action, $methods, $name, true );
147
	}
148
149
	private static function addRoutes_($pathArray, $controller, $action = 'index', $methods = null, $name = '', $isRest = false) {
150
		$controllerCache = self::getControllerCache ( $isRest );
151
		$postfix = 'default';
152
		if ($isRest) {
153
			$postfix = 'rest';
154
		}
155
		Router::addRoutesToRoutes ( $controllerCache, $pathArray, $controller, $action, $methods, $name );
156
		self::$cache->store ( 'controllers/routes.' . $postfix, $controllerCache, 'controllers' );
157
	}
158
159
160
	/**
161
	 * Preloads controllers.
162
	 * To use only with async servers (Swoole, Workerman)
163
	 *
164
	 * @param ?array $controllers
165
	 */
166
	public static function warmUpControllers($controllers = null) {
167
		$controllers ??= self::getControllers ();
0 ignored issues
show
Bug introduced by
The method getControllers() does not exist on Ubiquity\cache\traits\RouterCacheTrait. Did you maybe mean getControllerRoutes()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
		$controllers ??= self::/** @scrutinizer ignore-call */ getControllers ();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
168
		foreach ( $controllers as $ctrl ) {
169
			$controller = StartupAsync::getControllerInstance ( $ctrl );
170
			$binary = UIntrospection::implementsMethod ( $controller, 'isValid', Controller::class ) ? 1 : 0;
171
			$binary += UIntrospection::implementsMethod ( $controller, 'initialize', Controller::class ) ? 2 : 0;
172
			$binary += UIntrospection::implementsMethod ( $controller, 'finalize', Controller::class ) ? 4 : 0;
173
			$controller->_binaryCalls = $binary;
174
		}
175
	}
176
}
177