1 | <?php |
||
23 | class Router implements HasActions { |
||
24 | /** |
||
25 | * Resource's vendor prefix. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $vendor; |
||
30 | |||
31 | /** |
||
32 | * Resource's version. |
||
33 | * |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $version; |
||
37 | |||
38 | /** |
||
39 | * Valid methods and their HTTP verb(s). |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $methods = array( |
||
44 | 'get' => 'GET', |
||
45 | 'post' => 'POST', |
||
46 | 'put' => 'PUT', |
||
47 | 'patch' => 'PATCH', |
||
48 | 'delete' => 'DELETE', |
||
49 | 'editable' => 'POST, PUT, PATCH', |
||
50 | 'all' => 'GET, POST, PUT, PATCH, DELETE', |
||
51 | ); |
||
52 | |||
53 | /** |
||
54 | * Endpoints registered for the resource. |
||
55 | * |
||
56 | * @var Endpoint[] |
||
57 | */ |
||
58 | protected $endpoints = array(); |
||
59 | |||
60 | /** |
||
61 | * Returns all the resource endpoints. |
||
62 | * |
||
63 | * @return Endpoint[] |
||
64 | */ |
||
65 | 12 | public function get_endpoints() { |
|
68 | |||
69 | /** |
||
70 | * Sets the resource's vendor prefix. |
||
71 | * |
||
72 | * @param string $vendor |
||
73 | * |
||
74 | * @return $this |
||
75 | */ |
||
76 | 63 | public function set_vendor( $vendor ) { |
|
81 | |||
82 | /** |
||
83 | * Sets the resource's version. |
||
84 | * |
||
85 | * @param int $version |
||
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | 63 | public function set_version( $version ) { |
|
94 | |||
95 | /** |
||
96 | * Registers all of the routes with the WP-API. |
||
97 | * |
||
98 | * Runs on the `rest_api_init` hook. Registers all of the routes loaded |
||
99 | * on the router into the WordPress REST API. |
||
100 | * |
||
101 | * @throws VendorNotSetException |
||
102 | * @throws VersionNotSetException |
||
103 | */ |
||
104 | 45 | public function register() { |
|
121 | |||
122 | /** |
||
123 | * Registers a set of routes with a shared set of options. |
||
124 | * |
||
125 | * Allows you to group routes together with shared set of options, including |
||
126 | * a route prefix, shared guards, and common parameter validation or sanitization. |
||
127 | * |
||
128 | * @param array $options |
||
129 | * @param callable $callback |
||
130 | */ |
||
131 | 12 | public function group( array $options, $callback ) { |
|
140 | |||
141 | /** |
||
142 | * Magic __call method. |
||
143 | * |
||
144 | * All of the endpoints registration method calls pass through here. This validates whether the method |
||
145 | * is a valid endpoint type to register, and creates a new endpoint with the passed options. |
||
146 | * |
||
147 | * @param string $name |
||
148 | * @param array $arguments |
||
149 | * |
||
150 | * @return Endpoint |
||
151 | * |
||
152 | * @throws UnknownMethodException |
||
153 | * @throws MissingArgumentException |
||
154 | * @throws InvalidArgumentException |
||
155 | */ |
||
156 | 57 | public function __call( $name, $arguments ) { |
|
180 | |||
181 | /** |
||
182 | * Sets the passed options on the endpoint. |
||
183 | * |
||
184 | * Only sets endpoints matching setters in the Endpoint class. |
||
185 | * |
||
186 | * @param Endpoint $endpoint |
||
187 | * @param array $options |
||
188 | * |
||
189 | * @return Endpoint |
||
190 | * @throws MalformedRouteException |
||
191 | */ |
||
192 | 21 | protected function set_options( Endpoint $endpoint, array $options ) { |
|
207 | |||
208 | /** |
||
209 | * Generates the resource's namespace. |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | 39 | protected function get_namespace() { |
|
216 | |||
217 | /** |
||
218 | * {@inheritDoc} |
||
219 | * |
||
220 | * @return array[] |
||
221 | */ |
||
222 | public function action_hooks() { |
||
230 | } |
||
231 |