Passed
Push — master ( 5c03cc...d5672a )
by Jean-Christophe
09:14
created

RouterModifierTrait::put()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 7
crap 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.3
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
			$path = ControllerParser::parseMethodPath ( $method, $path );
37 36
			self::_addRoute ( $method, $routesArray, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority, $callback );
38
		}
39 36
	}
40
41 36
	private static function _addRoute(\ReflectionMethod $method, &$routesArray, $path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0, $callback = null) {
42 36
		$result = [ ];
43 36
		ControllerParser::parseRouteArray ( $result, $controller, [ "path" => $path,"methods" => $methods,"name" => $name,"cache" => $cache,"duration" => $duration,"requirements" => $requirements,"priority" => $priority,"callback" => $callback ], $method, $action );
44 36
		if ($priority <= 0) {
45 36
			foreach ( $result as $k => $v ) {
46 36
				$routesArray [$k] = $v;
47
			}
48
		} else {
49
			$count = \count ( $routesArray );
50
			$newArray = [ ];
51
			foreach ( $routesArray as $k => $v ) {
52
				if ($priority < $count --) {
53
					$newArray [$k] = $v;
54
				} else {
55
					break;
56
				}
57
			}
58
			$routesArray = array_diff_key ( $routesArray, $newArray );
59
			foreach ( $result as $k => $v ) {
60
				$newArray [$k] = $v;
61
			}
62
			foreach ( $routesArray as $k => $v ) {
63
				$newArray [$k] = $v;
64
			}
65
			$routesArray = $newArray;
66
		}
67 36
	}
68
69 36
	public static function addCallableRoute($path, $callable, $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
70 36
		if (is_callable ( $callable )) {
71 36
			$reflectionFunction = new \ReflectionFunction ( $callable );
72 36
			$path = ControllerParser::parseMethodPath ( $reflectionFunction, $path );
73 36
			self::_addCallableRoute ( $reflectionFunction, self::$routes, $path, $callable, $methods, $name, $cache, $duration, $requirements, $priority );
74
		}
75 36
	}
76
77 36
	public static function get($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
78 36
		self::addCallableRoute ( $path, $callable, [ 'get' ], $name, $cache, $duration, $requirements, $priority );
79 36
	}
80
81
	public static function post($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
82
		self::addCallableRoute ( $path, $callable, [ 'post' ], $name, $cache, $duration, $requirements, $priority );
83
	}
84
85
	public static function delete($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
86
		self::addCallableRoute ( $path, $callable, [ 'delete' ], $name, $cache, $duration, $requirements, $priority );
87
	}
88
89
	public static function put($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
90
		self::addCallableRoute ( $path, $callable, [ 'put' ], $name, $cache, $duration, $requirements, $priority );
91
	}
92
93
	public static function patch($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
94
		self::addCallableRoute ( $path, $callable, [ 'patch' ], $name, $cache, $duration, $requirements, $priority );
95
	}
96
97
	public static function options($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
98
		self::addCallableRoute ( $path, $callable, [ 'options' ], $name, $cache, $duration, $requirements, $priority );
99
	}
100
101 36
	private static function _addCallableRoute(\ReflectionFunction $reflectionFunction, &$routesArray, $path, $callable, $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
102 36
		$result = [ ];
103 36
		CallableParser::parseRouteArray ( $result, $callable, [ "path" => $path,"methods" => $methods,"name" => $name,"cache" => $cache,"duration" => $duration,"requirements" => $requirements,"priority" => $priority ], $reflectionFunction );
104 36
		foreach ( $result as $k => $v ) {
105 36
			$routesArray [$k] = $v;
106
		}
107 36
	}
108
109
	public static function addRoutesToRoutes(&$routesArray, $paths, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
110
		if (\class_exists ( $controller )) {
111
			$method = new \ReflectionMethod ( $controller, $action );
112
			foreach ( $paths as $path ) {
113
				self::_addRoute ( $method, $routesArray, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority );
114
			}
115
		}
116
	}
117
}
118
119