Skip to content

Commit b68024c

Browse files
committed
Change in addArgs() to allow consecutive additions to the same endpoint.
1 parent 63d33bc commit b68024c

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/Generator.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function __construct(string $endpoint, array $method, Config $config, $ar
6969
$this->path = $this->config->getPath();
7070
$this->version = $this->config->getVersion();
7171

72+
// Currently not operational
7273
if(!is_null($args)) {
7374
$this->args = $this->array_adjust($method, $args);
7475
}
@@ -85,6 +86,7 @@ public function __construct(string $endpoint, array $method, Config $config, $ar
8586
*/
8687
public function route() {
8788

89+
// Iterates to generate the different endpoints for each added HTTP method
8890
foreach ($this->method as $httpMethod) {
8991
register_rest_route(
9092
$this->namespace . '/' . $this->version,
@@ -107,33 +109,38 @@ public function route() {
107109
*/
108110
public function generate() {
109111

112+
// Create configured directory if it doesn't exist
110113
if (!is_dir($this->path)) {
111114
mkdir($this->path, 0755, true);
112115
}
113116

117+
// Check if file already exists before proceeding
114118
if(!is_file($this->path . '/' . ucfirst($this->endpoint) . '.php')) {
119+
115120
$class = new CT(ucfirst($this->endpoint));
116121

117122
$class
118123
->addComment("Controller class for callbacks and permissions.\nRoute --> " . sprintf($this->config->getPsr() . '\%s', ucfirst($this->endpoint)))
119124
->addComment('@since ' . $this->config->getVersion());
120125

126+
// Iterates callback creation for all methods allowed
121127
foreach ($this->method as $function) {
122128
$function = $class->addMethod(strtolower($function) . ucfirst($this->endpoint))
123129
->addComment('Handles ' . $function . ' requests to the endpoint.')
124130
->addComment('@return \WP_Rest_Response')
125131
->setBody('return new \WP_Rest_Response();');
126132

127-
$function->addParameter('request') // $items = [] // &$items = []
133+
$function->addParameter('request')
128134
->setType('\WP_Rest_Request');
129135
}
130136

137+
// Permissions callback is a single function per controller class
131138
$permissions = $class->addMethod('permissions')
132139
->addComment('Authenticate or limitate requests to the endpoint.')
133140
->addComment('@return bool')
134141
->setBody("// Your conditions.\nreturn true;");
135142

136-
$permissions->addParameter('request') // $items = [] // &$items = []
143+
$permissions->addParameter('request')
137144
->setType('\WP_Rest_Request');
138145

139146
// to generate PHP code simply cast to string or use echo:
@@ -202,9 +209,22 @@ public function getArgs($method) {
202209
return '';
203210
}
204211

212+
/**
213+
* Insert method arguments in the class $args variable.
214+
*
215+
* @since 1.0.4
216+
* @param string $method HTTP method.
217+
* @param array $args Arguments to add with their respective attributes.
218+
*
219+
* @return void
220+
*/
205221
public function addArgs(string $method, array $args) {
206222

207-
$this->args[$method] = $args;
223+
if(!$this->args[$method]) {
224+
$this->args[$method] = $args;
225+
} else {
226+
array_push($this->args[$method], $args);
227+
}
208228
}
209229

210230
/**
@@ -220,7 +240,15 @@ public function autoload() {
220240
$autoload->setTempDirectory(__DIR__ . '/temp');
221241
$autoload->register();
222242
}
223-
243+
/**
244+
* Helpers in combining multidimensional arrays for different methods.
245+
*
246+
* @since 1.0.2
247+
* @param array $a First array
248+
* @param array $b Second array
249+
*
250+
* @return array_combine()
251+
*/
224252
public function array_adjust($a, $b) {
225253

226254
$acount = count($a);

0 commit comments

Comments
 (0)