| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\Framework\Http; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use Exception; |
| 7 |
use WP_REST_Request; |
| 8 |
use WP_REST_Response; |
| 9 |
use InvalidArgumentException; |
| 10 |
use FluentSupport\Framework\Validator\ValidationException; |
| 11 |
use FluentSupport\Framework\Database\Orm\ModelNotFoundException; |
| 12 |
|
| 13 |
class Route |
| 14 |
{ |
| 15 |
protected $app = null; |
| 16 |
|
| 17 |
protected $restNamespace = null; |
| 18 |
|
| 19 |
protected $uri = null; |
| 20 |
|
| 21 |
protected $compiled = null; |
| 22 |
|
| 23 |
protected $name = ''; |
| 24 |
|
| 25 |
protected $handler = null; |
| 26 |
|
| 27 |
protected $method = null; |
| 28 |
|
| 29 |
protected $options = []; |
| 30 |
|
| 31 |
protected $wheres = []; |
| 32 |
|
| 33 |
protected $policyHandler = null; |
| 34 |
|
| 35 |
protected $predefinedNamedRegx = [ |
| 36 |
'int' => '[0-9]+', |
| 37 |
'alpha' => '[a-zA-Z]+', |
| 38 |
'alpha_num' => '[a-zA-Z0-9]+', |
| 39 |
'alpha_num_dash' => '[a-zA-Z0-9-_]+' |
| 40 |
]; |
| 41 |
|
| 42 |
|
| 43 |
public function __construct($app, $restNamespace, $uri, $handler, $method, $name = '') |
| 44 |
{ |
| 45 |
$this->app = $app; |
| 46 |
$this->restNamespace = $restNamespace; |
| 47 |
$this->uri = $uri; |
| 48 |
$this->handler = $handler; |
| 49 |
$this->method = $method; |
| 50 |
$this->name = $name; |
| 51 |
} |
| 52 |
|
| 53 |
public static function create($app, $namespace, $uri, $handler, $method, $name) |
| 54 |
{ |
| 55 |
return new static($app, $namespace, $uri, $handler, $method, $name); |
| 56 |
} |
| 57 |
|
| 58 |
public function name($name) |
| 59 |
{ |
| 60 |
$this->name .= $name; |
| 61 |
|
| 62 |
return $this; |
| 63 |
} |
| 64 |
|
| 65 |
public function where($identifier, $value = null) |
| 66 |
{ |
| 67 |
if (!is_null($value)) { |
| 68 |
$this->wheres[$identifier] = $this->getValue($value); |
| 69 |
} else { |
| 70 |
foreach ($identifier as $key => $value) { |
| 71 |
$this->wheres[$key] = $this->getValue($value); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return $this; |
| 76 |
} |
| 77 |
|
| 78 |
public function int($identifiers) |
| 79 |
{ |
| 80 |
$identifiers = is_array($identifiers) ? $identifiers : func_get_args(); |
| 81 |
|
| 82 |
foreach ($identifiers as $identifier) { |
| 83 |
$this->wheres[$identifier] = '[0-9]+'; |
| 84 |
} |
| 85 |
|
| 86 |
return $this; |
| 87 |
} |
| 88 |
|
| 89 |
public function alpha($identifiers) |
| 90 |
{ |
| 91 |
$identifiers = is_array($identifiers) ? $identifiers : func_get_args(); |
| 92 |
|
| 93 |
foreach ($identifiers as $identifier) { |
| 94 |
$this->wheres[$identifier] = '[a-zA-Z]+'; |
| 95 |
} |
| 96 |
|
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
public function alphaNum($identifiers) |
| 101 |
{ |
| 102 |
$identifiers = is_array($identifiers) ? $identifiers : func_get_args(); |
| 103 |
|
| 104 |
foreach ($identifiers as $identifier) { |
| 105 |
$this->wheres[$identifier] = '[a-zA-Z0-9]+'; |
| 106 |
} |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
public function alphaNumDash($identifiers) |
| 112 |
{ |
| 113 |
$identifiers = is_array($identifiers) ? $identifiers : func_get_args(); |
| 114 |
|
| 115 |
foreach ($identifiers as $identifier) { |
| 116 |
$this->wheres[$identifier] = '[a-zA-Z0-9-_]+'; |
| 117 |
} |
| 118 |
|
| 119 |
return $this; |
| 120 |
} |
| 121 |
|
| 122 |
public function withPolicy($handler) |
| 123 |
{ |
| 124 |
$this->policyHandler = $handler; |
| 125 |
} |
| 126 |
|
| 127 |
public function register() |
| 128 |
{ |
| 129 |
$this->setOptions(); |
| 130 |
|
| 131 |
$uri = $this->compileRoute($this->uri); |
| 132 |
|
| 133 |
return register_rest_route($this->restNamespace, "/{$uri}", $this->options); |
| 134 |
} |
| 135 |
|
| 136 |
protected function setOptions() |
| 137 |
{ |
| 138 |
$this->options = [ |
| 139 |
'args' => [], |
| 140 |
'methods' => $this->method, |
| 141 |
'callback' => [$this, 'callback'], |
| 142 |
'permission_callback' => [$this, 'permissionCallback'] |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
protected function getValue($value) |
| 147 |
{ |
| 148 |
if (array_key_exists($value, $this->predefinedNamedRegx)) { |
| 149 |
return $this->predefinedNamedRegx[$value]; |
| 150 |
} |
| 151 |
|
| 152 |
return $value; |
| 153 |
} |
| 154 |
|
| 155 |
protected function getPolicyHandler($policyHandler) |
| 156 |
{ |
| 157 |
if ($policyHandler instanceof Closure) { |
| 158 |
return function() use ($policyHandler) { |
| 159 |
$policyHandler($this->app->request); |
| 160 |
}; |
| 161 |
} |
| 162 |
|
| 163 |
if (strpos($policyHandler, '@') !== false) return $policyHandler; |
| 164 |
|
| 165 |
if (strpos($policyHandler, '::') !== false) return $policyHandler; |
| 166 |
|
| 167 |
if (!function_exists($policyHandler)) { |
| 168 |
if (is_string($this->handler) && strpos($this->handler, '@') !== false) { |
| 169 |
list($_, $method) = explode('@', $this->handler); |
| 170 |
$policyHandler = $policyHandler . '@' . $method; |
| 171 |
} else if (is_array($this->handler)) { |
| 172 |
$policyHandler = $policyHandler . '@' . $this->handler[1]; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return $policyHandler; |
| 177 |
} |
| 178 |
|
| 179 |
protected function compileRoute($uri) |
| 180 |
{ |
| 181 |
$params = []; |
| 182 |
|
| 183 |
$compiledUri = preg_replace_callback('#/{(.*?)}#', function($match) use (&$params, $uri) { |
| 184 |
// Default regx |
| 185 |
$regx = '[^\s(?!/)]+'; |
| 186 |
|
| 187 |
$param = trim($match[1]); |
| 188 |
|
| 189 |
if ($isOptional = strpos($param, '?')) { |
| 190 |
$param = trim($param, '?'); |
| 191 |
} |
| 192 |
|
| 193 |
if (in_array($param, $params)) { |
| 194 |
throw new InvalidArgumentException( |
| 195 |
"Duplicate parameter name \"{$param}\" found in {$uri}." |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
$params[] = $param; |
| 200 |
|
| 201 |
if (isset($this->wheres[$param])) { |
| 202 |
$regx = $this->wheres[$param]; |
| 203 |
} |
| 204 |
|
| 205 |
$pattern = "/(?P<" . $param . ">" . $regx . ")"; |
| 206 |
|
| 207 |
if ($isOptional) { |
| 208 |
$pattern = "(?:" . $pattern . ")?"; |
| 209 |
} |
| 210 |
|
| 211 |
$this->options['args'][$param]['required'] = !$isOptional; |
| 212 |
|
| 213 |
return $pattern; |
| 214 |
|
| 215 |
}, $uri); |
| 216 |
|
| 217 |
return $this->compiled = $compiledUri; |
| 218 |
} |
| 219 |
|
| 220 |
public function callback(WP_REST_Request $request) |
| 221 |
{ |
| 222 |
try { |
| 223 |
$this->setRestRequest($request); |
| 224 |
|
| 225 |
$response = $this->app->call( |
| 226 |
$this->app->parseRestHandler($this->handler), |
| 227 |
array_values($request->get_url_params()) |
| 228 |
); |
| 229 |
|
| 230 |
if (!($response instanceof WP_REST_Response)) { |
| 231 |
if (is_wp_error($response)) { |
| 232 |
$response = $this->sendWPError($response); |
| 233 |
} else { |
| 234 |
$response = $this->app->response->sendSuccess($response); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return $response; |
| 239 |
|
| 240 |
} catch (ValidationException $e) { |
| 241 |
return $this->app->response->sendError( |
| 242 |
$e->errors(), $e->getCode() |
| 243 |
); |
| 244 |
} catch (ModelNotFoundException $e) { |
| 245 |
return $this->app->response->sendError([ |
| 246 |
'message' => $e->getMessage() |
| 247 |
], 404); |
| 248 |
} catch (Exception $e) { |
| 249 |
return $this->app->response->sendError([ |
| 250 |
'message' => $e->getMessage() |
| 251 |
], $e->getCode()); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
public function permissionCallback(WP_REST_Request $request) |
| 256 |
{ |
| 257 |
$this->setRestRequest($request); |
| 258 |
|
| 259 |
if (!$this->policyHandler) { |
| 260 |
return true; |
| 261 |
} |
| 262 |
|
| 263 |
$policyHandler = $this->app->parsePolicyHandler( |
| 264 |
$this->getPolicyHandler($this->policyHandler) |
| 265 |
); |
| 266 |
|
| 267 |
return $this->app->call($policyHandler, $request->get_url_params()); |
| 268 |
} |
| 269 |
|
| 270 |
protected function setRestRequest($request) |
| 271 |
{ |
| 272 |
if (!$this->app->bound('wprestrequest')) { |
| 273 |
$this->app->instance('wprestrequest', $request); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
protected function sendWPError($response) |
| 278 |
{ |
| 279 |
$code = $response->get_error_code(); |
| 280 |
|
| 281 |
return $this->app->response->sendError( |
| 282 |
$response->get_error_messages(), |
| 283 |
is_numeric($code) ? $code : null |
| 284 |
); |
| 285 |
} |
| 286 |
} |
| 287 |
|