-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathclass-wp-rest-resolvable-route.php
More file actions
147 lines (135 loc) · 3.24 KB
/
class-wp-rest-resolvable-route.php
File metadata and controls
147 lines (135 loc) · 3.24 KB
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
<?php
/**
* REST API: WP_REST_Resolvable_Route class
*
* @package WordPress
* @subpackage REST_API
* @since X.X.0
*/
/**
* Just-in-time resolvable route for REST API routes.
*
* @since X.X.0
*/
class WP_REST_Resolvable_Route implements ArrayAccess, IteratorAggregate, Countable {
protected $namespace;
protected $route;
/**
* The callable used to resolve the route.
*
* @since X.X.0
* @var callable
*/
protected $callable;
/**
* The resolved route definition.
*
* @since X.X.0
* @var array|null
*/
protected $resolved = null;
/**
* Constructor.
*
* @since X.X.0
*
* @param callable $closure The callable used to resolve the route. Returns a single route definition.
*/
public function __construct( string $namespace, string $route, callable $closure ) {
$this->namespace = $namespace;
$this->route = $route;
$this->callable = $closure;
}
/**
* Invokes the callable to resolve, if needed.
*
* Routes can only be resolved once, the first time they're used. Any
* subsequent calls will return the same resolved definition, which may
* be modified by reference if needed.
*
* @since X.X.0
*
* @return array The resolved route definition.
*/
public function __invoke() {
if ( ! $this->resolved ) {
$this->resolved = call_user_func( $this->callable );
// Normalize the result.
$this->resolved = normalize_rest_endpoint_options( $this->namespace, $this->route, $this->resolved );
}
return $this->resolved;
}
/**
* Checks a single array key exists in the resolved route definition.
*
* @since X.X.0
*
* @param string $key The key to check.
* @return bool True if the key exists, false otherwise.
*/
#[ReturnTypeWillChange]
public function offsetExists( $k ) {
$this->__invoke();
return isset( $this->resolved[ $k ] );
}
/**
* Gets a single array key from the resolved route definition.
*
* @since X.X.0
*
* @param string $key The key to retrieve.
* @return mixed The value of the key, or null if not set. Returns by reference, so it can be modified if needed.
*/
#[ReturnTypeWillChange]
public function &offsetGet( $k ) {
$this->__invoke();
return $this->resolved[ $k ];
}
/**
* Sets a single array key in the resolved route definition.
*
* @since X.X.0
*
* @param string $key The key to set.
* @param mixed $value The value to set.
*/
#[ReturnTypeWillChange]
public function offsetSet( $k, $v ) {
$this->__invoke();
$this->resolved[ $k ] = $v;
}
/**
* Unsets a single array key in the resolved route definition.
*
* @since X.X.0
*
* @param string $key The key to unset.
*/
#[ReturnTypeWillChange]
public function offsetUnset( $k ) {
$this->__invoke();
unset( $this->resolved[ $k ] );
}
/**
* Gets an iterator for the resolved route definition.
*
* @since X.X.0
*
* @return Traversable An iterator for the resolved route definition.
*/
public function getIterator(): Traversable {
$this->__invoke();
return new ArrayIterator( $this->resolved );
}
/**
* Counts the number of elements in the resolved route definition.
*
* @since X.X.0
*
* @return int The number of elements in the resolved route definition.
*/
public function count(): int {
$this->__invoke();
return count( $this->resolved );
}
}