Test Failed
Push — master ( befc15...123c13 )
by Jean-Christophe
18:33
created

RouterAdminTrait::getRouteInfoByDefaultRouting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.9666
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Ubiquity\controllers\traits;
4
5
use Ubiquity\controllers\Startup;
6
7
/**
8
 * Trait for admin part of Router class.
9
 * Ubiquity\controllers\traits$RouterAdminTrait
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.3
14
 * @property array $routes
15
 */
16
trait RouterAdminTrait {
17
18
	abstract public static function slashPath($path): string;
19
20
	/**
21
	 *
22
	 * @param string $controller
23
	 * @param string $action
24 1
	 * @return array|boolean
25 1
	 */
26 1
	public static function getRouteInfoByControllerAction($controller, $action) {
27 1
		foreach ( self::$routes as $routePath => $routeDetails ) {
28
			if (! isset ( $routeDetails ['controller'] )) {
29 1
				$routeDetails = \current ( $routeDetails );
30
			}
31
			if ($controller === str_replace('\\\\','\\',$routeDetails ['controller']) && $action === $routeDetails ['action']) {
32
				$routeDetails ['path'] = $routePath;
33
				return $routeDetails;
34 1
			}
35
		}
36
		return false;
37 1
	}
38 1
39 1
	public static function getRoutesPathByController($controller): array {
40 1
		$result = [ ];
41 1
		foreach ( self::$routes as $routePath => $routeDetails ) {
42 1
			if (! isset ( $routeDetails ['controller'] )) {
43 1
				foreach ( $routeDetails as $method => $routeInfo ) {
44
					if ($routeInfo ['controller'] === $controller) {
45
						$result [] = [ 'method' => $method,'url' => self::getRoutePathInfos ( $controller, $routePath, $routeInfo ) ];
46
					}
47 1
				}
48
			} else {
49
				if ($routeDetails ['controller'] === $controller) {
50
					$result [] = [ 'method' => '*','url' => self::getRoutePathInfos ( $controller, $routePath, $routeDetails ) ];
51
				}
52 1
			}
53
		}
54
		return $result;
55 1
	}
56 1
57 1
	public static function getRoutePathInfos($controller, $routePath, $routeInfo) {
58 1
		$method = new \ReflectionMethod ( $controller, $routeInfo ['action'] );
59 1
		$parameters = $method->getParameters ();
60 1
		$routeParams = $routeInfo ['parameters'];
61 1
		$pattern = "@\(.*?\)@";
62 1
		$params = [ ];
63 1
		foreach ( $routeParams as $param ) {
64
			if ($param === '*') {
65 1
				$params [] = $parameters [\count ( $params )]->getName ();
66 1
			} else {
67
				$index = ( int ) \filter_var ( $param, FILTER_SANITIZE_NUMBER_INT );
68
				$params [] = $parameters [$index]->getName ();
69 1
			}
70 1
		}
71 1
		$path = $routePath;
72
		foreach ( $params as $param ) {
73 1
			$path = \preg_replace ( $pattern, '{' . $param . '}', $path, 1 );
74
		}
75
		return $path;
76
	}
77
78
	/**
79
	 *
80
	 * @param string $path
81
	 * @return array|boolean
82
	 */
83
	public static function getRouteInfo($path) {
84
		$path = self::slashPath ( $path );
85
		foreach ( self::$routes as $routePath => $routeDetails ) {
86
			if (\preg_match ( "@^{$routePath}\$@s", $path, $matches ) || \stripslashes ( $routePath ) == $path) {
87
				if (! isset ( $routeDetails ['controller'] )) {
88
					return \current ( $routeDetails );
89
				} else
90
					return $routeDetails;
91
			}
92
		}
93
		return false;
94 1
	}
95 1
96 1
	public static function getAnnotations($controllerName, $actionName): array {
97 1
		$result = [ ];
98 1
		foreach ( self::$routes as $routePath => $routeDetails ) {
99
			if (! isset ( $routeDetails ['controller'] )) {
100 1
				$routeDetails = \current ( $routeDetails );
101 1
			}
102
			if ($routeDetails ['controller'] === $controllerName && $routeDetails ['action'] === $actionName)
103 1
				$result [$routePath] = $routeDetails;
104
		}
105
		return $result;
106
	}
107
108
	public static function filterRoutes($path) {
109
		$path = self::slashPath ( $path );
110
		$result = [ ];
111
		foreach ( self::$routes as $routePath => $routeDetails ) {
112
			if (\preg_match ( "@^{$routePath}.*?$@s", $path, $matches )) {
113
				$result [$routePath] = $routeDetails;
114
			}
115
		}
116
		return $result;
117
	}
118
	public static function getRouteInfoByDefaultRouting(string $url){
119
		$ns = Startup::getNS();
120
		$url=\trim($url,'/');
121
		$u = \explode("/", $url);
122
		$controller = $ns . $u[0];
123
		if (\count($u) > 1)
124
			$action = $u[1];
125
		else
126
			$action = "index";
127
		return self::getRouteInfoByControllerAction($controller,$action);
128
	}
129
}
130
131