-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-router.php
152 lines (134 loc) · 3.17 KB
/
class-router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Router
*
* @package EG_Routing_Plugin
*/
class EG_Router {
/**
* Holds the namespace of our endpoints
*
* @var $namespace
*/
protected $namespace;
/**
* Supported methods.
*
* @var array
*/
protected static $methods = array(
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
);
/**
* Holds an array of data to build our routes
*
* @var $routes
*/
private $routes = array();
/**
* Holds our option slug for the storage of routes in the options table.
*
* @var $option_slug
*/
protected static $option_slug = 'eg_routes';
/**
* Initalise our class.
*
* @param string $namespace Our API namespace.
*/
public function __construct( $namespace = '' ) {
$this->namespace = $namespace;
}
/**
* Get an array of route paramiters
*
* @return array
*/
public function get_routes() {
return $this->routes;
}
/**
* Get the API namespace.
*
* @return string
*/
public function get_namespace() {
return $this->namespace;
}
/**
* Compiles our routes.
*/
public static function compile() {
$routes = get_option( self::$option_slug, array() );
foreach ( $routes as $route_data ) {
register_rest_route(
$route_data['namespace'],
$route_data['route'],
$route_data['args'],
$route_data['override']
);
}
}
/**
* Cache the routes
*/
public function cache() {
$cached_routes = get_option( self::$option_slug, array() );
if ( $this->routes !== $cached_routes ) {
update_option( self::$option_slug, $this->routes );
}
}
/**
* Adds the paramiters needed to add a route to $this->routes
*
* @param string $route The route.
* @param array $parameters Additional arguments.
* @param bool $override Overide existing endpoints.
* @throws Exception When missing uses array peramiter.
*
* @return void
*/
public function add( $route, Array $parameters = array(), $override = false ) {
if ( ! isset( $parameters['uses'] ) ) {
throw new Exception( "Missing array key 'uses'" );
}
$args = array(
'methods' => $parameters['method'],
'callback' => $parameters['uses'],
'args' => isset( $parameters['args'] ) && is_array( $parameters['args'] ) ? $parameters['args'] : array(),
);
if ( strpos( $parameters['uses'], '@' ) !== false ) {
$parts = explode( '@', $parameters['uses'] );
$args['callback'] = array( new $parts[0], $parts[1] );
}
$this->routes[] = array(
'namespace' => $this->namespace,
'route' => $route,
'args' => $args,
'override' => $override,
);
}
/**
* Nice trick to resolve our routers methods
*
* @param string $method The name of the method that was called.
* @param array $parameters The parameters that were passed to the method.
* @throws InvalidArgumentException When a method is not found.
*
* @return mixed
*/
public function __call( $method, Array $parameters = array() ) {
if ( in_array( strtoupper( $method ), static::$methods, true ) ) {
$parameters[1]['method'] = strtoupper( $method );
return call_user_func_array( array( $this, 'add' ), $parameters );
}
throw new InvalidArgumentException( "Method {$method} not defined" );
}
}