Passed
Push — master ( b7467b...db04eb )
by Atanas
02:41
created

HasRoutesTrait::addRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
namespace Obsidian\Routing;
4
5
use Closure;
6
use Obsidian\Controllers\WordPress;
7
8
/**
9
 * Allow objects to have routes
10
 */
11
trait HasRoutesTrait {
12
	/**
13
	 * Array of registered routes
14
	 *
15
	 * @var RouteInterface[]
16
	 */
17
	protected $routes = [];
18
19
	/**
20
	 * Get registered routes
21
	 *
22
	 * @return RouteInterface[]
23
	 */
24
	public function getRoutes() {
25
		return $this->routes;
26
	}
27
28
	/**
29
	 * Add a route
30
	 *
31
	 * @param  RouteInterface $route
32
	 * @return RouteInterface
33
	 */
34
	public function addRoute( $route ) {
35
		$this->routes[] = $route;
36
		return $route;
37
	}
38
39
	/**
40
	 * Create and add a new route
41
	 *
42
	 * @param  string[]            $methods
43
	 * @param  mixed               $target
44
	 * @param  string|Closure|null $handler
45
	 * @return RouteInterface
46
	 */
47
	public function route( $methods, $target, $handler = null ) {
48
		if ( $handler === null ) {
49
			$handler = WordPress::class . '@handle';
50
		}
51
52
		$route = new Route( $methods, $target, $handler );
53
		return $this->addRoute( $route );
54
	}
55
56
	/**
57
	 * Create and add a route group
58
	 *
59
	 * @param  string         $target
60
	 * @param  Closure        $callable
61
	 * @return RouteInterface
62
	 */
63
	public function group( $target, Closure $callable ) {
64
		$routeGroup = new RouteGroup( $target, $callable );
65
		return $this->addRoute( $routeGroup );
66
	}
67
68
	/**
69
	 * Create and add a route for the GET and HEAD methods
70
	 *
71
	 * @param  mixed               $target
72
	 * @param  string|Closure|null $handler
73
	 * @return RouteInterface
74
	 */
75
	public function get( $target, $handler = null ) {
76
		return $this->route( ['GET', 'HEAD'], $target, $handler );
77
	}
78
79
	/**
80
	 * Create and add a route for the POST method
81
	 *
82
	 * @param  mixed               $target
83
	 * @param  string|Closure|null $handler
84
	 * @return RouteInterface
85
	 */
86
	public function post( $target, $handler = null ) {
87
		return $this->route( ['POST'], $target, $handler );
88
	}
89
90
	/**
91
	 * Create and add a route for the PUT method
92
	 *
93
	 * @param  mixed               $target
94
	 * @param  string|Closure|null $handler
95
	 * @return RouteInterface
96
	 */
97
	public function put( $target, $handler = null ) {
98
		return $this->route( ['PUT'], $target, $handler );
99
	}
100
101
	/**
102
	 * Create and add a route for the PATCH method
103
	 *
104
	 * @param  mixed               $target
105
	 * @param  string|Closure|null $handler
106
	 * @return RouteInterface
107
	 */
108
	public function patch( $target, $handler = null ) {
109
		return $this->route( ['PATCH'], $target, $handler );
110
	}
111
112
	/**
113
	 * Create and add a route for the DELETE method
114
	 *
115
	 * @param  mixed               $target
116
	 * @param  string|Closure|null $handler
117
	 * @return RouteInterface
118
	 */
119
	public function delete( $target, $handler = null ) {
120
		return $this->route( ['DELETE'], $target, $handler );
121
	}
122
123
	/**
124
	 * Create and add a route for the OPTIONS method
125
	 *
126
	 * @param  mixed               $target
127
	 * @param  string|Closure|null $handler
128
	 * @return RouteInterface
129
	 */
130
	public function options( $target, $handler = null ) {
131
		return $this->route( ['OPTIONS'], $target, $handler );
132
	}
133
134
	/**
135
	 * Create and add a route for all supported methods
136
	 *
137
	 * @param  mixed               $target
138
	 * @param  string|Closure|null $handler
139
	 * @return RouteInterface
140
	 */
141
	public function any( $target, $handler = null ) {
142
		return $this->route( ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $target, $handler );
143
	}
144
}
145