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

RouterModifierTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 58.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 26
dl 0
loc 76
ccs 28
cts 48
cp 0.5833
rs 10
c 1
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A post() 0 2 1
A put() 0 2 1
A addRouteToRoutes() 0 4 2
A delete() 0 2 1
A addRoute() 0 2 1
A patch() 0 2 1
A addCallableRoute() 0 4 2
A options() 0 2 1
A get() 0 2 1
A _addCallableRoute() 0 5 2
A addRoutesToRoutes() 0 5 3
A _addRoute() 0 5 2
1
<?php
2
3
namespace Ubiquity\controllers\traits;
4
5
use Ubiquity\cache\parser\ControllerParser;
6
use Ubiquity\cache\parser\CallableParser;
7
8
/**
9
 * Ubiquity\controllers\traits$RouterModifierTrait
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.1
14
 *
15
 */
16
trait RouterModifierTrait {
17
18
	/**
19
	 *
20
	 * @param string $path
21
	 * @param string $controller
22
	 * @param string $action
23
	 * @param array|null $methods
24
	 * @param string $name
25
	 * @param boolean $cache
26
	 * @param int $duration
27
	 * @param array $requirements
28
	 */
29 36
	public static function addRoute($path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
30 36
		self::addRouteToRoutes ( self::$routes, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority );
31 36
	}
32
33 36
	public static function addRouteToRoutes(&$routesArray, $path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0, $callback = null) {
34 36
		if (\class_exists ( $controller )) {
35 36
			$method = new \ReflectionMethod ( $controller, $action );
36 36
			self::_addRoute ( $method, $routesArray, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority, $callback );
37
		}
38 36
	}
39
40 36
	private static function _addRoute(\ReflectionMethod $method, &$routesArray, $path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0, $callback = null) {
41 36
		$result = [ ];
42 36
		ControllerParser::parseRouteArray ( $result, $controller, [ "path" => $path,"methods" => $methods,"name" => $name,"cache" => $cache,"duration" => $duration,"requirements" => $requirements,"priority" => $priority,"callback" => $callback ], $method, $action );
43 36
		foreach ( $result as $k => $v ) {
44 36
			$routesArray [$k] = $v;
45
		}
46 36
	}
47
48 36
	public static function addCallableRoute($path, $callable, $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
49 36
		if (is_callable ( $callable )) {
50 36
			$reflectionFunction = new \ReflectionFunction ( $callable );
51 36
			self::_addCallableRoute ( $reflectionFunction, self::$routes, $path, $callable, $methods, $name, $cache, $duration, $requirements, $priority );
52
		}
53 36
	}
54
55 36
	public static function get($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
56 36
		self::addCallableRoute ( $path, $callable, [ 'get' ], $name, $cache, $duration, $requirements, $priority );
57 36
	}
58
59
	public static function post($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
60
		self::addCallableRoute ( $path, $callable, [ 'post' ], $name, $cache, $duration, $requirements, $priority );
61
	}
62
63
	public static function delete($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
64
		self::addCallableRoute ( $path, $callable, [ 'delete' ], $name, $cache, $duration, $requirements, $priority );
65
	}
66
67
	public static function put($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
68
		self::addCallableRoute ( $path, $callable, [ 'put' ], $name, $cache, $duration, $requirements, $priority );
69
	}
70
71
	public static function patch($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
72
		self::addCallableRoute ( $path, $callable, [ 'patch' ], $name, $cache, $duration, $requirements, $priority );
73
	}
74
75
	public static function options($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
76
		self::addCallableRoute ( $path, $callable, [ 'options' ], $name, $cache, $duration, $requirements, $priority );
77
	}
78
79 36
	private static function _addCallableRoute(\ReflectionFunction $reflectionFunction, &$routesArray, $path, $callable, $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
80 36
		$result = [ ];
81 36
		CallableParser::parseRouteArray ( $result, $callable, [ "path" => $path,"methods" => $methods,"name" => $name,"cache" => $cache,"duration" => $duration,"requirements" => $requirements,"priority" => $priority ], $reflectionFunction );
82 36
		foreach ( $result as $k => $v ) {
83 36
			$routesArray [$k] = $v;
84
		}
85 36
	}
86
87
	public static function addRoutesToRoutes(&$routesArray, $paths, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
88
		if (\class_exists ( $controller )) {
89
			$method = new \ReflectionMethod ( $controller, $action );
90
			foreach ( $paths as $path ) {
91
				self::_addRoute ( $method, $routesArray, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority );
92
			}
93
		}
94
	}
95
}
96
97