1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A custom file of function polyfills to get around php-scoper using global functions |
7
|
|
|
* in function_exist calls. |
8
|
|
|
* |
9
|
|
|
* This file should have the same namespace used in scoper.inc.php config. |
10
|
|
|
* |
11
|
|
|
* @package PinkCrab\Route |
12
|
|
|
* @author Glynn Quelch [email protected] |
13
|
|
|
* @since 0.0.1 |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace PinkCrab\Route\Route; |
17
|
|
|
|
18
|
|
|
use PinkCrab\Route\Route\Route; |
19
|
|
|
use PinkCrab\Route\Route_Factory; |
20
|
|
|
use PinkCrab\Route\Route_Exception; |
21
|
|
|
use PinkCrab\Route\Route\Abstract_Route; |
22
|
|
|
|
23
|
|
|
class Route_Group extends Abstract_Route { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Route[] |
27
|
|
|
*/ |
28
|
|
|
protected array $routes = array(); |
29
|
|
|
|
30
|
|
|
protected Route_Factory $route_factory; |
31
|
|
|
protected string $route; |
32
|
|
|
|
33
|
|
|
public function __construct( string $namespace, string $route ) { |
34
|
|
|
$this->route = $route; |
35
|
|
|
$this->namespace = $namespace; |
36
|
|
|
$this->route_factory = new Route_Factory( $namespace ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the value of route |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function get_route(): string { |
45
|
|
|
return $this->route; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates a get request. |
50
|
|
|
* |
51
|
|
|
* @param callable(\WP_REST_Request): (\WP_HTTP_Response|\WP_Error) $callable |
52
|
|
|
* @return Route |
53
|
|
|
*/ |
54
|
|
|
public function get( callable $callable ): Route { |
55
|
|
|
$route = $this->route_factory->get( $this->route, $callable ); |
56
|
|
|
$route->namespace( $this->namespace ); |
57
|
|
|
$this->routes[ Route::GET ] = $route; |
58
|
|
|
return $route; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Creates a post request. |
63
|
|
|
* |
64
|
|
|
* @param callable(\WP_REST_Request): (\WP_HTTP_Response|\WP_Error) $callable |
65
|
|
|
* @return Route |
66
|
|
|
*/ |
67
|
|
|
public function post( callable $callable ): Route { |
68
|
|
|
$route = $this->route_factory->post( $this->route, $callable ); |
69
|
|
|
$route->namespace( $this->namespace ); |
70
|
|
|
$this->routes[ Route::POST ] = $route; |
71
|
|
|
return $route; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates a put request. |
76
|
|
|
* |
77
|
|
|
* @param callable(\WP_REST_Request): (\WP_HTTP_Response|\WP_Error) $callable |
78
|
|
|
* @return Route |
79
|
|
|
*/ |
80
|
|
|
public function put( callable $callable ): Route { |
81
|
|
|
$route = $this->route_factory->put( $this->route, $callable ); |
82
|
|
|
$route->namespace( $this->namespace ); |
83
|
|
|
$this->routes[ Route::PUT ] = $route; |
84
|
|
|
return $route; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Creates a patch request. |
89
|
|
|
* |
90
|
|
|
* @param callable(\WP_REST_Request): (\WP_HTTP_Response|\WP_Error) $callable |
91
|
|
|
* @return Route |
92
|
|
|
*/ |
93
|
|
|
public function patch( callable $callable ): Route { |
94
|
|
|
$route = $this->route_factory->patch( $this->route, $callable ); |
95
|
|
|
$route->namespace( $this->namespace ); |
96
|
|
|
$this->routes[ Route::PATCH ] = $route; |
97
|
|
|
return $route; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Creates a delete request. |
102
|
|
|
* |
103
|
|
|
* @param callable(\WP_REST_Request): (\WP_HTTP_Response|\WP_Error) $callable |
104
|
|
|
* @return Route |
105
|
|
|
*/ |
106
|
|
|
public function delete( callable $callable ): Route { |
107
|
|
|
$route = $this->route_factory->delete( $this->route, $callable ); |
108
|
|
|
$route->namespace( $this->namespace ); |
109
|
|
|
$this->routes[ Route::DELETE ] = $route; |
110
|
|
|
return $route; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Adds a route ot the collection |
115
|
|
|
* |
116
|
|
|
* @param Route $route |
117
|
|
|
* @deprecated 0.0.2 This is not really used and should be removed in a future version. |
118
|
|
|
* @return self |
119
|
|
|
*/ |
120
|
|
|
public function add_rest_route( Route $route ): self { |
121
|
|
|
$this->routes[ $route->get_method() ] = $route; |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns all the current routes. |
127
|
|
|
* |
128
|
|
|
* @return Route[] |
129
|
|
|
*/ |
130
|
|
|
public function get_rest_routes(): array { |
131
|
|
|
return $this->routes; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Checks if a specific method is defined. |
136
|
|
|
* |
137
|
|
|
* @param string $method |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function method_exists( string $method ): bool { |
141
|
|
|
return array_key_exists( \strtoupper( $method ), $this->routes ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Checks we have routes. |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function has_routes(): bool { |
150
|
|
|
return ! empty( $this->routes ); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|