@@ -16,883 +16,883 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Restler |
| 18 | 18 | { |
| 19 | - // ================================================================== |
|
| 20 | - // |
|
| 21 | - // Public variables |
|
| 22 | - // |
|
| 23 | - // ------------------------------------------------------------------ |
|
| 24 | - |
|
| 25 | - const VERSION = '2.2.1'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * URL of the currently mapped service |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - public $url; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Http request method of the current request. |
|
| 35 | - * Any value between [GET, PUT, POST, DELETE] |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - public $request_method; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Requested data format. Instance of the current format class |
|
| 42 | - * which implements the iFormat interface |
|
| 43 | - * @var iFormat |
|
| 44 | - * @example jsonFormat, xmlFormat, yamlFormat etc |
|
| 45 | - */ |
|
| 46 | - public $request_format; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Data sent to the service |
|
| 50 | - * @var array |
|
| 51 | - */ |
|
| 52 | - public $request_data = array(); |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Used in production mode to store the URL Map to disk |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - public $cache_dir; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * base directory to locate format and auth files |
|
| 62 | - * @var string |
|
| 63 | - */ |
|
| 64 | - public $base_dir; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Name of an iRespond implementation class |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - public $response = 'DefaultResponse'; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Response data format. Instance of the current format class |
|
| 74 | - * which implements the iFormat interface |
|
| 75 | - * @var iFormat |
|
| 76 | - * @example jsonFormat, xmlFormat, yamlFormat etc |
|
| 77 | - */ |
|
| 78 | - public $response_format; |
|
| 79 | - |
|
| 80 | - // ================================================================== |
|
| 81 | - // |
|
| 82 | - // Private & Protected variables |
|
| 83 | - // |
|
| 84 | - // ------------------------------------------------------------------ |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * When set to false, it will run in debug mode and parse the |
|
| 88 | - * class files every time to map it to the URL |
|
| 89 | - * @var boolean |
|
| 90 | - */ |
|
| 91 | - protected $production_mode; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Associated array that maps urls to their respective class and method |
|
| 95 | - * @var array |
|
| 96 | - */ |
|
| 97 | - protected $routes = array(); |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Associated array that maps formats to their respective format class name |
|
| 101 | - * @var array |
|
| 102 | - */ |
|
| 103 | - protected $format_map = array(); |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Instance of the current api service class |
|
| 107 | - * @var object |
|
| 108 | - */ |
|
| 109 | - protected $service_class_instance; |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Name of the api method being called |
|
| 113 | - * @var string |
|
| 114 | - */ |
|
| 115 | - protected $service_method; |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * list of authentication classes |
|
| 119 | - * @var array |
|
| 120 | - */ |
|
| 121 | - protected $auth_classes = array(); |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * list of error handling classes |
|
| 125 | - * @var array |
|
| 126 | - */ |
|
| 127 | - protected $error_classes = array(); |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * HTTP status codes |
|
| 131 | - * @var array |
|
| 132 | - */ |
|
| 133 | - private $codes = array( |
|
| 134 | - 100 => 'Continue', |
|
| 135 | - 101 => 'Switching Protocols', |
|
| 136 | - 200 => 'OK', |
|
| 137 | - 201 => 'Created', |
|
| 138 | - 202 => 'Accepted', |
|
| 139 | - 203 => 'Non-Authoritative Information', |
|
| 140 | - 204 => 'No Content', |
|
| 141 | - 205 => 'Reset Content', |
|
| 142 | - 206 => 'Partial Content', |
|
| 143 | - 300 => 'Multiple Choices', |
|
| 144 | - 301 => 'Moved Permanently', |
|
| 145 | - 302 => 'Found', |
|
| 146 | - 303 => 'See Other', |
|
| 147 | - 304 => 'Not Modified', |
|
| 148 | - 305 => 'Use Proxy', |
|
| 149 | - 306 => '(Unused)', |
|
| 150 | - 307 => 'Temporary Redirect', |
|
| 151 | - 400 => 'Bad Request', |
|
| 152 | - 401 => 'Unauthorized', |
|
| 153 | - 402 => 'Payment Required', |
|
| 154 | - 403 => 'Forbidden', |
|
| 155 | - 404 => 'Not Found', |
|
| 156 | - 405 => 'Method Not Allowed', |
|
| 157 | - 406 => 'Not Acceptable', |
|
| 158 | - 407 => 'Proxy Authentication Required', |
|
| 159 | - 408 => 'Request Timeout', |
|
| 160 | - 409 => 'Conflict', |
|
| 161 | - 410 => 'Gone', |
|
| 162 | - 411 => 'Length Required', |
|
| 163 | - 412 => 'Precondition Failed', |
|
| 164 | - 413 => 'Request Entity Too Large', |
|
| 165 | - 414 => 'Request-URI Too Long', |
|
| 166 | - 415 => 'Unsupported Media Type', |
|
| 167 | - 416 => 'Requested Range Not Satisfiable', |
|
| 168 | - 417 => 'Expectation Failed', |
|
| 169 | - 500 => 'Internal Server Error', |
|
| 170 | - 501 => 'Not Implemented', |
|
| 171 | - 502 => 'Bad Gateway', |
|
| 172 | - 503 => 'Service Unavailable', |
|
| 173 | - 504 => 'Gateway Timeout', |
|
| 174 | - 505 => 'HTTP Version Not Supported' |
|
| 175 | - ); |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Caching of url map is enabled or not |
|
| 179 | - * @var boolean |
|
| 180 | - */ |
|
| 181 | - protected $cached; |
|
| 182 | - |
|
| 183 | - // ================================================================== |
|
| 184 | - // |
|
| 185 | - // Public functions |
|
| 186 | - // |
|
| 187 | - // ------------------------------------------------------------------ |
|
| 188 | - |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Constructor |
|
| 192 | - * @param boolean $production_mode When set to false, it will run in |
|
| 193 | - * debug mode and parse the class files every time to map it to the URL |
|
| 194 | - */ |
|
| 195 | - public function __construct($production_mode = false) |
|
| 196 | - { |
|
| 197 | - $this->production_mode = $production_mode; |
|
| 198 | - $this->cache_dir = getcwd(); |
|
| 199 | - $this->base_dir = RESTLER_PATH; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Store the url map cache if needed |
|
| 205 | - */ |
|
| 206 | - public function __destruct() |
|
| 207 | - { |
|
| 208 | - if ($this->production_mode && !($this->cached)) { |
|
| 209 | - $this->saveCache(); |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Use it in production mode to refresh the url map cache |
|
| 216 | - */ |
|
| 217 | - public function refreshCache() |
|
| 218 | - { |
|
| 219 | - $this->routes = array(); |
|
| 220 | - $this->cached = false; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Call this method and pass all the formats that should be |
|
| 226 | - * supported by the API. Accepts multiple parameters |
|
| 227 | - * @param string class name of the format class that implements iFormat |
|
| 228 | - * @example $restler->setSupportedFormats('JsonFormat', 'XmlFormat'...); |
|
| 229 | - */ |
|
| 230 | - public function setSupportedFormats() |
|
| 231 | - { |
|
| 232 | - $args = func_get_args(); |
|
| 233 | - $extensions = array(); |
|
| 234 | - foreach ($args as $class_name) { |
|
| 235 | - if (!is_string($class_name) || !class_exists($class_name)) { |
|
| 236 | - throw new Exception("$class_name is not a vaild Format Class."); |
|
| 237 | - } |
|
| 238 | - $obj = new $class_name; |
|
| 239 | - if (!($obj instanceof iFormat)) { |
|
| 240 | - throw new Exception('Invalid format class; must implement ' |
|
| 241 | - . 'iFormat interface'); |
|
| 242 | - } |
|
| 243 | - foreach ($obj->getMIMEMap() as $extension => $mime) { |
|
| 244 | - if (!isset($this->format_map[$extension])) { |
|
| 245 | - $this->format_map[$extension] = $class_name; |
|
| 246 | - } |
|
| 247 | - $mime = explode(',', $mime); |
|
| 248 | - if (!is_array($mime)) { |
|
| 249 | - $mime = array($mime); |
|
| 250 | - } |
|
| 251 | - foreach ($mime as $value) { |
|
| 252 | - if (!isset($this->format_map[$value])) { |
|
| 253 | - $this->format_map[$value] = $class_name; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - $extensions[".$extension"] = true; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - $this->format_map['default'] = $args[0]; |
|
| 260 | - $this->format_map['extensions'] = array_keys($extensions); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Add api classes throgh this method. All the public methods |
|
| 266 | - * that do not start with _ (underscore) will be will be exposed |
|
| 267 | - * as the public api by default. |
|
| 268 | - * |
|
| 269 | - * All the protected methods that do not start with _ (underscore) |
|
| 270 | - * will exposed as protected api which will require authentication |
|
| 271 | - * @param string $class name of the service class |
|
| 272 | - * @param string $basePath optional url prefix for mapping, uses |
|
| 273 | - * lowercase version of the class name when not specified |
|
| 274 | - * @throws Exception when supplied with invalid class name |
|
| 275 | - */ |
|
| 276 | - public function addAPIClass($class_name, $base_path = null) |
|
| 277 | - { |
|
| 278 | - if (!class_exists($class_name)) { |
|
| 279 | - throw new Exception("API class $class_name is missing."); |
|
| 280 | - } |
|
| 281 | - $this->loadCache(); |
|
| 282 | - if (!$this->cached) { |
|
| 283 | - if (is_null($base_path)) { |
|
| 284 | - $base_path = strtolower($class_name); |
|
| 285 | - $index = strrpos($class_name, '\\'); |
|
| 286 | - if ($index !== false) { |
|
| 287 | - $base_path = substr($base_path, $index + 1); |
|
| 288 | - } |
|
| 289 | - } else { |
|
| 290 | - $base_path = trim($base_path, '/'); |
|
| 291 | - } |
|
| 292 | - if (strlen($base_path) > 0) { |
|
| 293 | - $base_path .= '/'; |
|
| 294 | - } |
|
| 295 | - $this->generateMap($class_name, $base_path); |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * protected methods will need atleast one authentication class to be set |
|
| 302 | - * in order to allow that method to be executed |
|
| 303 | - * @param string $class_name of the authentication class |
|
| 304 | - * @param string $base_path optional url prefix for mapping |
|
| 305 | - */ |
|
| 306 | - public function addAuthenticationClass($class_name, $base_path = null) |
|
| 307 | - { |
|
| 308 | - $this->auth_classes[] = $class_name; |
|
| 309 | - $this->addAPIClass($class_name, $base_path); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Add class for custom error handling |
|
| 315 | - * @param string $class_name of the error handling class |
|
| 316 | - */ |
|
| 317 | - public function addErrorClass($class_name) |
|
| 318 | - { |
|
| 319 | - $this->error_classes[] = $class_name; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Convenience method to respond with an error message |
|
| 325 | - * @param int $statusCode http error code |
|
| 326 | - * @param string $errorMessage optional custom error message |
|
| 327 | - */ |
|
| 328 | - public function handleError($status_code, $error_message = null) |
|
| 329 | - { |
|
| 330 | - $method = "handle$status_code"; |
|
| 331 | - $handled = false; |
|
| 332 | - foreach ($this->error_classes as $class_name) { |
|
| 333 | - if (method_exists($class_name, $method)) { |
|
| 334 | - $obj = new $class_name(); |
|
| 335 | - $obj->restler = $this; |
|
| 336 | - $obj->$method(); |
|
| 337 | - $handled = true; |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - if ($handled) { |
|
| 341 | - return; |
|
| 342 | - } |
|
| 343 | - $message = $this->codes[$status_code] |
|
| 344 | - . (!$error_message ? '' : ': ' . $error_message); |
|
| 345 | - $this->setStatus($status_code); |
|
| 346 | - $responder = new $this->response(); |
|
| 347 | - $responder->restler = $this; |
|
| 348 | - $this->sendData($responder->__formatError($status_code, $message)); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * An initialize function to allow use of the restler error generation |
|
| 354 | - * functions for pre-processing and pre-routing of requests. |
|
| 355 | - */ |
|
| 356 | - public function init() |
|
| 357 | - { |
|
| 358 | - if (empty($this->format_map)) { |
|
| 359 | - $this->setSupportedFormats('JsonFormat'); |
|
| 360 | - } |
|
| 361 | - $this->url = $this->getPath(); |
|
| 362 | - $this->request_method = $this->getRequestMethod(); |
|
| 363 | - $this->response_format = $this->getResponseFormat(); |
|
| 364 | - $this->request_format = $this->getRequestFormat(); |
|
| 365 | - if (is_null($this->request_format)) { |
|
| 366 | - $this->request_format = $this->response_format; |
|
| 367 | - } |
|
| 368 | - if ($this->request_method == 'PUT' || $this->request_method == 'POST') { |
|
| 369 | - $this->request_data = $this->getRequestData(); |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Main function for processing the api request |
|
| 376 | - * and return the response |
|
| 377 | - * @throws Exception when the api service class is missing |
|
| 378 | - * @throws RestException to send error response |
|
| 379 | - */ |
|
| 380 | - public function handle() |
|
| 381 | - { |
|
| 382 | - $this->init(); |
|
| 383 | - $o = $this->mapUrlToMethod(); |
|
| 384 | - |
|
| 385 | - if (!isset($o->class_name)) { |
|
| 386 | - $this->handleError(404); |
|
| 387 | - } else { |
|
| 388 | - try { |
|
| 389 | - if ($o->method_flag) { |
|
| 390 | - $auth_method = '__isAuthenticated'; |
|
| 391 | - if (!count($this->auth_classes)) { |
|
| 392 | - throw new RestException(401); |
|
| 393 | - } |
|
| 394 | - foreach ($this->auth_classes as $auth_class) { |
|
| 395 | - $auth_obj = new $auth_class(); |
|
| 396 | - $auth_obj->restler = $this; |
|
| 397 | - $this->applyClassMetadata($auth_class, $auth_obj, $o); |
|
| 398 | - if (!method_exists($auth_obj, $auth_method)) { |
|
| 399 | - throw new RestException(401, 'Authentication Class ' |
|
| 400 | - . 'should implement iAuthenticate'); |
|
| 401 | - } else if (!$auth_obj->$auth_method()) { |
|
| 402 | - throw new RestException(401); |
|
| 403 | - } |
|
| 404 | - } |
|
| 405 | - } |
|
| 406 | - $this->applyClassMetadata(get_class($this->request_format), |
|
| 407 | - $this->request_format, $o); |
|
| 408 | - $pre_process = '_' . $this->request_format->getExtension() . '_' |
|
| 409 | - . $o->method_name; |
|
| 410 | - $this->service_method = $o->method_name; |
|
| 411 | - if ($o->method_flag == 2) { |
|
| 412 | - $o = unprotect($o); |
|
| 413 | - } |
|
| 414 | - $object = $this->service_class_instance = new $o->class_name(); |
|
| 415 | - $object->restler = $this; |
|
| 416 | - if (method_exists($o->class_name, $pre_process)) { |
|
| 417 | - call_user_func_array( |
|
| 418 | - array($object, $pre_process), $o->arguments |
|
| 419 | - ); |
|
| 420 | - } |
|
| 421 | - switch ($o->method_flag) { |
|
| 422 | - case 3: |
|
| 423 | - $reflection_method = new ReflectionMethod($object, |
|
| 424 | - $o->method_name); |
|
| 425 | - $reflection_method->setAccessible(true); |
|
| 426 | - $result = $reflection_method->invokeArgs($object, |
|
| 427 | - $o->arguments); |
|
| 428 | - break; |
|
| 429 | - case 2: |
|
| 430 | - case 1: |
|
| 431 | - default: |
|
| 432 | - $result = call_user_func_array(array( |
|
| 433 | - $object, |
|
| 434 | - $o->method_name), $o->arguments |
|
| 435 | - ); |
|
| 436 | - break; |
|
| 437 | - } |
|
| 438 | - } catch (RestException $e) { |
|
| 439 | - $this->handleError($e->getCode(), $e->getMessage()); |
|
| 440 | - } |
|
| 441 | - } |
|
| 442 | - $responder = new $this->response(); |
|
| 443 | - $responder->restler = $this; |
|
| 444 | - $this->applyClassMetadata($this->response, $responder, $o); |
|
| 445 | - if (isset($result) && $result !== null) { |
|
| 446 | - $result = $responder->__formatResponse($result); |
|
| 447 | - $this->sendData($result); |
|
| 448 | - } |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Encodes the response in the prefered format |
|
| 454 | - * and sends back |
|
| 455 | - * @param $data array php data |
|
| 456 | - */ |
|
| 457 | - public function sendData($data) |
|
| 458 | - { |
|
| 459 | - $data = $this->response_format->encode($data, |
|
| 460 | - !($this->production_mode) |
|
| 461 | - ); |
|
| 462 | - $post_process = '_' . $this->service_method . '_' |
|
| 463 | - . $this->response_format->getExtension(); |
|
| 464 | - if (isset($this->service_class_instance) |
|
| 465 | - && method_exists($this->service_class_instance, $post_process) |
|
| 466 | - ) { |
|
| 467 | - $data = call_user_func(array($this->service_class_instance, |
|
| 468 | - $post_process), $data); |
|
| 469 | - } |
|
| 470 | - header("Cache-Control: no-cache, must-revalidate"); |
|
| 471 | - header("Expires: 0"); |
|
| 472 | - header('Content-Type: ' . $this->response_format->getMIME()); |
|
| 473 | - //.'; charset=utf-8'); |
|
| 474 | - header("X-Powered-By: Luracast Restler v" . Restler::VERSION); |
|
| 19 | + // ================================================================== |
|
| 20 | + // |
|
| 21 | + // Public variables |
|
| 22 | + // |
|
| 23 | + // ------------------------------------------------------------------ |
|
| 24 | + |
|
| 25 | + const VERSION = '2.2.1'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * URL of the currently mapped service |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + public $url; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Http request method of the current request. |
|
| 35 | + * Any value between [GET, PUT, POST, DELETE] |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + public $request_method; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Requested data format. Instance of the current format class |
|
| 42 | + * which implements the iFormat interface |
|
| 43 | + * @var iFormat |
|
| 44 | + * @example jsonFormat, xmlFormat, yamlFormat etc |
|
| 45 | + */ |
|
| 46 | + public $request_format; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Data sent to the service |
|
| 50 | + * @var array |
|
| 51 | + */ |
|
| 52 | + public $request_data = array(); |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Used in production mode to store the URL Map to disk |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + public $cache_dir; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * base directory to locate format and auth files |
|
| 62 | + * @var string |
|
| 63 | + */ |
|
| 64 | + public $base_dir; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Name of an iRespond implementation class |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + public $response = 'DefaultResponse'; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Response data format. Instance of the current format class |
|
| 74 | + * which implements the iFormat interface |
|
| 75 | + * @var iFormat |
|
| 76 | + * @example jsonFormat, xmlFormat, yamlFormat etc |
|
| 77 | + */ |
|
| 78 | + public $response_format; |
|
| 79 | + |
|
| 80 | + // ================================================================== |
|
| 81 | + // |
|
| 82 | + // Private & Protected variables |
|
| 83 | + // |
|
| 84 | + // ------------------------------------------------------------------ |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * When set to false, it will run in debug mode and parse the |
|
| 88 | + * class files every time to map it to the URL |
|
| 89 | + * @var boolean |
|
| 90 | + */ |
|
| 91 | + protected $production_mode; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Associated array that maps urls to their respective class and method |
|
| 95 | + * @var array |
|
| 96 | + */ |
|
| 97 | + protected $routes = array(); |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Associated array that maps formats to their respective format class name |
|
| 101 | + * @var array |
|
| 102 | + */ |
|
| 103 | + protected $format_map = array(); |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Instance of the current api service class |
|
| 107 | + * @var object |
|
| 108 | + */ |
|
| 109 | + protected $service_class_instance; |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Name of the api method being called |
|
| 113 | + * @var string |
|
| 114 | + */ |
|
| 115 | + protected $service_method; |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * list of authentication classes |
|
| 119 | + * @var array |
|
| 120 | + */ |
|
| 121 | + protected $auth_classes = array(); |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * list of error handling classes |
|
| 125 | + * @var array |
|
| 126 | + */ |
|
| 127 | + protected $error_classes = array(); |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * HTTP status codes |
|
| 131 | + * @var array |
|
| 132 | + */ |
|
| 133 | + private $codes = array( |
|
| 134 | + 100 => 'Continue', |
|
| 135 | + 101 => 'Switching Protocols', |
|
| 136 | + 200 => 'OK', |
|
| 137 | + 201 => 'Created', |
|
| 138 | + 202 => 'Accepted', |
|
| 139 | + 203 => 'Non-Authoritative Information', |
|
| 140 | + 204 => 'No Content', |
|
| 141 | + 205 => 'Reset Content', |
|
| 142 | + 206 => 'Partial Content', |
|
| 143 | + 300 => 'Multiple Choices', |
|
| 144 | + 301 => 'Moved Permanently', |
|
| 145 | + 302 => 'Found', |
|
| 146 | + 303 => 'See Other', |
|
| 147 | + 304 => 'Not Modified', |
|
| 148 | + 305 => 'Use Proxy', |
|
| 149 | + 306 => '(Unused)', |
|
| 150 | + 307 => 'Temporary Redirect', |
|
| 151 | + 400 => 'Bad Request', |
|
| 152 | + 401 => 'Unauthorized', |
|
| 153 | + 402 => 'Payment Required', |
|
| 154 | + 403 => 'Forbidden', |
|
| 155 | + 404 => 'Not Found', |
|
| 156 | + 405 => 'Method Not Allowed', |
|
| 157 | + 406 => 'Not Acceptable', |
|
| 158 | + 407 => 'Proxy Authentication Required', |
|
| 159 | + 408 => 'Request Timeout', |
|
| 160 | + 409 => 'Conflict', |
|
| 161 | + 410 => 'Gone', |
|
| 162 | + 411 => 'Length Required', |
|
| 163 | + 412 => 'Precondition Failed', |
|
| 164 | + 413 => 'Request Entity Too Large', |
|
| 165 | + 414 => 'Request-URI Too Long', |
|
| 166 | + 415 => 'Unsupported Media Type', |
|
| 167 | + 416 => 'Requested Range Not Satisfiable', |
|
| 168 | + 417 => 'Expectation Failed', |
|
| 169 | + 500 => 'Internal Server Error', |
|
| 170 | + 501 => 'Not Implemented', |
|
| 171 | + 502 => 'Bad Gateway', |
|
| 172 | + 503 => 'Service Unavailable', |
|
| 173 | + 504 => 'Gateway Timeout', |
|
| 174 | + 505 => 'HTTP Version Not Supported' |
|
| 175 | + ); |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Caching of url map is enabled or not |
|
| 179 | + * @var boolean |
|
| 180 | + */ |
|
| 181 | + protected $cached; |
|
| 182 | + |
|
| 183 | + // ================================================================== |
|
| 184 | + // |
|
| 185 | + // Public functions |
|
| 186 | + // |
|
| 187 | + // ------------------------------------------------------------------ |
|
| 188 | + |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Constructor |
|
| 192 | + * @param boolean $production_mode When set to false, it will run in |
|
| 193 | + * debug mode and parse the class files every time to map it to the URL |
|
| 194 | + */ |
|
| 195 | + public function __construct($production_mode = false) |
|
| 196 | + { |
|
| 197 | + $this->production_mode = $production_mode; |
|
| 198 | + $this->cache_dir = getcwd(); |
|
| 199 | + $this->base_dir = RESTLER_PATH; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Store the url map cache if needed |
|
| 205 | + */ |
|
| 206 | + public function __destruct() |
|
| 207 | + { |
|
| 208 | + if ($this->production_mode && !($this->cached)) { |
|
| 209 | + $this->saveCache(); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Use it in production mode to refresh the url map cache |
|
| 216 | + */ |
|
| 217 | + public function refreshCache() |
|
| 218 | + { |
|
| 219 | + $this->routes = array(); |
|
| 220 | + $this->cached = false; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Call this method and pass all the formats that should be |
|
| 226 | + * supported by the API. Accepts multiple parameters |
|
| 227 | + * @param string class name of the format class that implements iFormat |
|
| 228 | + * @example $restler->setSupportedFormats('JsonFormat', 'XmlFormat'...); |
|
| 229 | + */ |
|
| 230 | + public function setSupportedFormats() |
|
| 231 | + { |
|
| 232 | + $args = func_get_args(); |
|
| 233 | + $extensions = array(); |
|
| 234 | + foreach ($args as $class_name) { |
|
| 235 | + if (!is_string($class_name) || !class_exists($class_name)) { |
|
| 236 | + throw new Exception("$class_name is not a vaild Format Class."); |
|
| 237 | + } |
|
| 238 | + $obj = new $class_name; |
|
| 239 | + if (!($obj instanceof iFormat)) { |
|
| 240 | + throw new Exception('Invalid format class; must implement ' |
|
| 241 | + . 'iFormat interface'); |
|
| 242 | + } |
|
| 243 | + foreach ($obj->getMIMEMap() as $extension => $mime) { |
|
| 244 | + if (!isset($this->format_map[$extension])) { |
|
| 245 | + $this->format_map[$extension] = $class_name; |
|
| 246 | + } |
|
| 247 | + $mime = explode(',', $mime); |
|
| 248 | + if (!is_array($mime)) { |
|
| 249 | + $mime = array($mime); |
|
| 250 | + } |
|
| 251 | + foreach ($mime as $value) { |
|
| 252 | + if (!isset($this->format_map[$value])) { |
|
| 253 | + $this->format_map[$value] = $class_name; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + $extensions[".$extension"] = true; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + $this->format_map['default'] = $args[0]; |
|
| 260 | + $this->format_map['extensions'] = array_keys($extensions); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Add api classes throgh this method. All the public methods |
|
| 266 | + * that do not start with _ (underscore) will be will be exposed |
|
| 267 | + * as the public api by default. |
|
| 268 | + * |
|
| 269 | + * All the protected methods that do not start with _ (underscore) |
|
| 270 | + * will exposed as protected api which will require authentication |
|
| 271 | + * @param string $class name of the service class |
|
| 272 | + * @param string $basePath optional url prefix for mapping, uses |
|
| 273 | + * lowercase version of the class name when not specified |
|
| 274 | + * @throws Exception when supplied with invalid class name |
|
| 275 | + */ |
|
| 276 | + public function addAPIClass($class_name, $base_path = null) |
|
| 277 | + { |
|
| 278 | + if (!class_exists($class_name)) { |
|
| 279 | + throw new Exception("API class $class_name is missing."); |
|
| 280 | + } |
|
| 281 | + $this->loadCache(); |
|
| 282 | + if (!$this->cached) { |
|
| 283 | + if (is_null($base_path)) { |
|
| 284 | + $base_path = strtolower($class_name); |
|
| 285 | + $index = strrpos($class_name, '\\'); |
|
| 286 | + if ($index !== false) { |
|
| 287 | + $base_path = substr($base_path, $index + 1); |
|
| 288 | + } |
|
| 289 | + } else { |
|
| 290 | + $base_path = trim($base_path, '/'); |
|
| 291 | + } |
|
| 292 | + if (strlen($base_path) > 0) { |
|
| 293 | + $base_path .= '/'; |
|
| 294 | + } |
|
| 295 | + $this->generateMap($class_name, $base_path); |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * protected methods will need atleast one authentication class to be set |
|
| 302 | + * in order to allow that method to be executed |
|
| 303 | + * @param string $class_name of the authentication class |
|
| 304 | + * @param string $base_path optional url prefix for mapping |
|
| 305 | + */ |
|
| 306 | + public function addAuthenticationClass($class_name, $base_path = null) |
|
| 307 | + { |
|
| 308 | + $this->auth_classes[] = $class_name; |
|
| 309 | + $this->addAPIClass($class_name, $base_path); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Add class for custom error handling |
|
| 315 | + * @param string $class_name of the error handling class |
|
| 316 | + */ |
|
| 317 | + public function addErrorClass($class_name) |
|
| 318 | + { |
|
| 319 | + $this->error_classes[] = $class_name; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Convenience method to respond with an error message |
|
| 325 | + * @param int $statusCode http error code |
|
| 326 | + * @param string $errorMessage optional custom error message |
|
| 327 | + */ |
|
| 328 | + public function handleError($status_code, $error_message = null) |
|
| 329 | + { |
|
| 330 | + $method = "handle$status_code"; |
|
| 331 | + $handled = false; |
|
| 332 | + foreach ($this->error_classes as $class_name) { |
|
| 333 | + if (method_exists($class_name, $method)) { |
|
| 334 | + $obj = new $class_name(); |
|
| 335 | + $obj->restler = $this; |
|
| 336 | + $obj->$method(); |
|
| 337 | + $handled = true; |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + if ($handled) { |
|
| 341 | + return; |
|
| 342 | + } |
|
| 343 | + $message = $this->codes[$status_code] |
|
| 344 | + . (!$error_message ? '' : ': ' . $error_message); |
|
| 345 | + $this->setStatus($status_code); |
|
| 346 | + $responder = new $this->response(); |
|
| 347 | + $responder->restler = $this; |
|
| 348 | + $this->sendData($responder->__formatError($status_code, $message)); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * An initialize function to allow use of the restler error generation |
|
| 354 | + * functions for pre-processing and pre-routing of requests. |
|
| 355 | + */ |
|
| 356 | + public function init() |
|
| 357 | + { |
|
| 358 | + if (empty($this->format_map)) { |
|
| 359 | + $this->setSupportedFormats('JsonFormat'); |
|
| 360 | + } |
|
| 361 | + $this->url = $this->getPath(); |
|
| 362 | + $this->request_method = $this->getRequestMethod(); |
|
| 363 | + $this->response_format = $this->getResponseFormat(); |
|
| 364 | + $this->request_format = $this->getRequestFormat(); |
|
| 365 | + if (is_null($this->request_format)) { |
|
| 366 | + $this->request_format = $this->response_format; |
|
| 367 | + } |
|
| 368 | + if ($this->request_method == 'PUT' || $this->request_method == 'POST') { |
|
| 369 | + $this->request_data = $this->getRequestData(); |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Main function for processing the api request |
|
| 376 | + * and return the response |
|
| 377 | + * @throws Exception when the api service class is missing |
|
| 378 | + * @throws RestException to send error response |
|
| 379 | + */ |
|
| 380 | + public function handle() |
|
| 381 | + { |
|
| 382 | + $this->init(); |
|
| 383 | + $o = $this->mapUrlToMethod(); |
|
| 384 | + |
|
| 385 | + if (!isset($o->class_name)) { |
|
| 386 | + $this->handleError(404); |
|
| 387 | + } else { |
|
| 388 | + try { |
|
| 389 | + if ($o->method_flag) { |
|
| 390 | + $auth_method = '__isAuthenticated'; |
|
| 391 | + if (!count($this->auth_classes)) { |
|
| 392 | + throw new RestException(401); |
|
| 393 | + } |
|
| 394 | + foreach ($this->auth_classes as $auth_class) { |
|
| 395 | + $auth_obj = new $auth_class(); |
|
| 396 | + $auth_obj->restler = $this; |
|
| 397 | + $this->applyClassMetadata($auth_class, $auth_obj, $o); |
|
| 398 | + if (!method_exists($auth_obj, $auth_method)) { |
|
| 399 | + throw new RestException(401, 'Authentication Class ' |
|
| 400 | + . 'should implement iAuthenticate'); |
|
| 401 | + } else if (!$auth_obj->$auth_method()) { |
|
| 402 | + throw new RestException(401); |
|
| 403 | + } |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | + $this->applyClassMetadata(get_class($this->request_format), |
|
| 407 | + $this->request_format, $o); |
|
| 408 | + $pre_process = '_' . $this->request_format->getExtension() . '_' |
|
| 409 | + . $o->method_name; |
|
| 410 | + $this->service_method = $o->method_name; |
|
| 411 | + if ($o->method_flag == 2) { |
|
| 412 | + $o = unprotect($o); |
|
| 413 | + } |
|
| 414 | + $object = $this->service_class_instance = new $o->class_name(); |
|
| 415 | + $object->restler = $this; |
|
| 416 | + if (method_exists($o->class_name, $pre_process)) { |
|
| 417 | + call_user_func_array( |
|
| 418 | + array($object, $pre_process), $o->arguments |
|
| 419 | + ); |
|
| 420 | + } |
|
| 421 | + switch ($o->method_flag) { |
|
| 422 | + case 3: |
|
| 423 | + $reflection_method = new ReflectionMethod($object, |
|
| 424 | + $o->method_name); |
|
| 425 | + $reflection_method->setAccessible(true); |
|
| 426 | + $result = $reflection_method->invokeArgs($object, |
|
| 427 | + $o->arguments); |
|
| 428 | + break; |
|
| 429 | + case 2: |
|
| 430 | + case 1: |
|
| 431 | + default: |
|
| 432 | + $result = call_user_func_array(array( |
|
| 433 | + $object, |
|
| 434 | + $o->method_name), $o->arguments |
|
| 435 | + ); |
|
| 436 | + break; |
|
| 437 | + } |
|
| 438 | + } catch (RestException $e) { |
|
| 439 | + $this->handleError($e->getCode(), $e->getMessage()); |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | + $responder = new $this->response(); |
|
| 443 | + $responder->restler = $this; |
|
| 444 | + $this->applyClassMetadata($this->response, $responder, $o); |
|
| 445 | + if (isset($result) && $result !== null) { |
|
| 446 | + $result = $responder->__formatResponse($result); |
|
| 447 | + $this->sendData($result); |
|
| 448 | + } |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Encodes the response in the prefered format |
|
| 454 | + * and sends back |
|
| 455 | + * @param $data array php data |
|
| 456 | + */ |
|
| 457 | + public function sendData($data) |
|
| 458 | + { |
|
| 459 | + $data = $this->response_format->encode($data, |
|
| 460 | + !($this->production_mode) |
|
| 461 | + ); |
|
| 462 | + $post_process = '_' . $this->service_method . '_' |
|
| 463 | + . $this->response_format->getExtension(); |
|
| 464 | + if (isset($this->service_class_instance) |
|
| 465 | + && method_exists($this->service_class_instance, $post_process) |
|
| 466 | + ) { |
|
| 467 | + $data = call_user_func(array($this->service_class_instance, |
|
| 468 | + $post_process), $data); |
|
| 469 | + } |
|
| 470 | + header("Cache-Control: no-cache, must-revalidate"); |
|
| 471 | + header("Expires: 0"); |
|
| 472 | + header('Content-Type: ' . $this->response_format->getMIME()); |
|
| 473 | + //.'; charset=utf-8'); |
|
| 474 | + header("X-Powered-By: Luracast Restler v" . Restler::VERSION); |
|
| 475 | 475 | if($this->production_mode){ |
| 476 | 476 | die($data); |
| 477 | 477 | }else{ |
| 478 | 478 | echo $data; |
| 479 | 479 | } |
| 480 | - } |
|
| 481 | - |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Sets the HTTP response status |
|
| 485 | - * @param int $code response code |
|
| 486 | - */ |
|
| 487 | - public function setStatus($code) |
|
| 488 | - { |
|
| 489 | - header("{$_SERVER['SERVER_PROTOCOL']} $code " . |
|
| 490 | - $this->codes[strval($code)]); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * Compare two strings and remove the common |
|
| 496 | - * sub string from the first string and return it |
|
| 497 | - * @param string $first |
|
| 498 | - * @param string $second |
|
| 499 | - * @param string $char optional, set it as |
|
| 500 | - * blank string for char by char comparison |
|
| 501 | - * @return string |
|
| 502 | - */ |
|
| 503 | - public function removeCommonPath($first, $second, $char = '/') |
|
| 504 | - { |
|
| 505 | - $first = explode($char, $first); |
|
| 506 | - $second = explode($char, $second); |
|
| 507 | - while (count($second)) { |
|
| 508 | - if ($first[0] == $second[0]) { |
|
| 509 | - array_shift($first); |
|
| 510 | - } else { |
|
| 511 | - break; |
|
| 512 | - } |
|
| 513 | - array_shift($second); |
|
| 514 | - } |
|
| 515 | - return implode($char, $first); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * Save cache to file |
|
| 521 | - */ |
|
| 522 | - public function saveCache() |
|
| 523 | - { |
|
| 524 | - $file = $this->cache_dir . '/routes.php'; |
|
| 525 | - $s = '$o=array();' . PHP_EOL; |
|
| 526 | - foreach ($this->routes as $key => $value) { |
|
| 527 | - $s .= PHP_EOL . PHP_EOL . PHP_EOL . |
|
| 528 | - "############### $key ###############" . PHP_EOL . PHP_EOL; |
|
| 529 | - $s .= '$o[\'' . $key . '\']=array();'; |
|
| 530 | - foreach ($value as $ke => $va) { |
|
| 531 | - $s .= PHP_EOL . PHP_EOL . "#==== $key $ke" . PHP_EOL . PHP_EOL; |
|
| 532 | - $s .= '$o[\'' . $key . '\'][\'' . $ke . '\']=' . str_replace( |
|
| 533 | - PHP_EOL, PHP_EOL . "\t", var_export($va, true) |
|
| 534 | - ) . ';'; |
|
| 535 | - } |
|
| 536 | - } |
|
| 537 | - $s .= PHP_EOL . 'return $o;'; |
|
| 538 | - $r = @file_put_contents($file, "<?php $s"); |
|
| 539 | - @chmod($file, 0777); |
|
| 540 | - if ($r === false) { |
|
| 541 | - throw new Exception( |
|
| 542 | - "The cache directory located at '$this->cache_dir' needs to " |
|
| 543 | - . "have the permissions set to read/write/execute for everyone" |
|
| 544 | - . " in order to save cache and improve performance."); |
|
| 545 | - } |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - // ================================================================== |
|
| 549 | - // |
|
| 550 | - // Protected functions |
|
| 551 | - // |
|
| 552 | - // ------------------------------------------------------------------ |
|
| 553 | - |
|
| 554 | - |
|
| 555 | - /** |
|
| 556 | - * Parses the requst url and get the api path |
|
| 557 | - * @return string api path |
|
| 558 | - */ |
|
| 559 | - protected function getPath() |
|
| 560 | - { |
|
| 561 | - $path = urldecode($this->removeCommonPath($_SERVER['REQUEST_URI'], |
|
| 562 | - $_SERVER['SCRIPT_NAME'])); |
|
| 563 | - $path = preg_replace('/(\/*\?.*$)|(\/$)/', '', $path); |
|
| 564 | - $path = str_replace($this->format_map['extensions'], '', $path); |
|
| 565 | - return $path; |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Parses the request to figure out the http request type |
|
| 571 | - * @return string which will be one of the following |
|
| 572 | - * [GET, POST, PUT, DELETE] |
|
| 573 | - * @example GET |
|
| 574 | - */ |
|
| 575 | - protected function getRequestMethod() |
|
| 576 | - { |
|
| 577 | - $method = $_SERVER['REQUEST_METHOD']; |
|
| 578 | - if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { |
|
| 579 | - $method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']; |
|
| 580 | - } |
|
| 581 | - //support for HEAD request |
|
| 582 | - if ($method == 'HEAD') { |
|
| 583 | - $method = 'GET'; |
|
| 584 | - } |
|
| 585 | - return $method; |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - |
|
| 589 | - /** |
|
| 590 | - * Parses the request to figure out format of the request data |
|
| 591 | - * @return iFormat any class that implements iFormat |
|
| 592 | - * @example JsonFormat |
|
| 593 | - */ |
|
| 594 | - protected function getRequestFormat() |
|
| 595 | - { |
|
| 596 | - $format = null; |
|
| 597 | - //check if client has sent any information on request format |
|
| 598 | - if (isset($_SERVER['CONTENT_TYPE'])) { |
|
| 599 | - $mime = explode(';', $_SERVER['CONTENT_TYPE']); |
|
| 600 | - $mime = $mime[0]; |
|
| 601 | - if ($mime == UrlEncodedFormat::MIME) { |
|
| 602 | - $format = new UrlEncodedFormat(); |
|
| 603 | - } else { |
|
| 604 | - if (isset($this->format_map[$mime])) { |
|
| 605 | - $format = $this->format_map[$mime]; |
|
| 606 | - $format = is_string($format) ? new $format : $format; |
|
| 607 | - $format->setMIME($mime); |
|
| 608 | - } |
|
| 609 | - } |
|
| 610 | - } |
|
| 611 | - return $format; |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Parses the request to figure out the best format for response |
|
| 617 | - * @return iFormat any class that implements iFormat |
|
| 618 | - * @example JsonFormat |
|
| 619 | - */ |
|
| 620 | - protected function getResponseFormat() |
|
| 621 | - { |
|
| 622 | - //check if client has specified an extension |
|
| 623 | - /** |
|
| 624 | - * @var iFormat |
|
| 625 | - */ |
|
| 626 | - $format = null; |
|
| 627 | - $extensions = explode('.', |
|
| 628 | - parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); |
|
| 629 | - while ($extensions) { |
|
| 630 | - $extension = array_pop($extensions); |
|
| 631 | - $extension = explode('/', $extension); |
|
| 632 | - $extension = array_shift($extension); |
|
| 633 | - if ($extension && isset($this->format_map[$extension])) { |
|
| 634 | - $format = $this->format_map[$extension]; |
|
| 635 | - $format = is_string($format) ? new $format : $format; |
|
| 636 | - $format->setExtension($extension); |
|
| 637 | - //echo "Extension $extension"; |
|
| 638 | - return $format; |
|
| 639 | - } |
|
| 640 | - } |
|
| 641 | - //check if client has sent list of accepted data formats |
|
| 642 | - if (isset($_SERVER['HTTP_ACCEPT'])) { |
|
| 643 | - $acceptList = array(); |
|
| 644 | - $accepts = explode(',', strtolower($_SERVER['HTTP_ACCEPT'])); |
|
| 645 | - if (!is_array($accepts)) { |
|
| 646 | - $accepts = array($accepts); |
|
| 647 | - } |
|
| 648 | - foreach ($accepts as $pos => $accept) { |
|
| 649 | - $parts = explode(';q=', trim($accept)); |
|
| 650 | - $type = array_shift($parts); |
|
| 651 | - $quality = count($parts) ? |
|
| 652 | - floatval(array_shift($parts)) : |
|
| 653 | - (1000 - $pos) / 1000; |
|
| 654 | - $acceptList[$type] = $quality; |
|
| 655 | - } |
|
| 656 | - arsort($acceptList); |
|
| 657 | - foreach ($acceptList as $accept => $quality) { |
|
| 658 | - if (isset($this->format_map[$accept])) { |
|
| 659 | - $format = $this->format_map[$accept]; |
|
| 660 | - $format = is_string($format) ? new $format : $format; |
|
| 661 | - $format->setMIME($accept); |
|
| 662 | - //echo "MIME $accept"; |
|
| 663 | - // Tell cache content is based on Accept header |
|
| 664 | - header("Vary: Accept"); |
|
| 665 | - return $format; |
|
| 666 | - } |
|
| 667 | - } |
|
| 668 | - } else { |
|
| 669 | - // RFC 2616: If no Accept header field is |
|
| 670 | - // present, then it is assumed that the |
|
| 671 | - // client accepts all media types. |
|
| 672 | - $_SERVER['HTTP_ACCEPT'] = '*/*'; |
|
| 673 | - } |
|
| 674 | - if (strpos($_SERVER['HTTP_ACCEPT'], '*') !== false) { |
|
| 675 | - if (strpos($_SERVER['HTTP_ACCEPT'], 'application/*') !== false) { |
|
| 676 | - $format = new JsonFormat; |
|
| 677 | - } else if (strpos($_SERVER['HTTP_ACCEPT'], 'text/*') !== false) { |
|
| 678 | - $format = new XmlFormat; |
|
| 679 | - } else if (strpos($_SERVER['HTTP_ACCEPT'], '*/*') !== false) { |
|
| 680 | - $format = new $this->format_map['default']; |
|
| 681 | - } |
|
| 682 | - } |
|
| 683 | - if (empty($format)) { |
|
| 684 | - // RFC 2616: If an Accept header field is present, and if the |
|
| 685 | - // server cannot send a response which is acceptable according to |
|
| 686 | - // the combined Accept field value, then the server SHOULD send |
|
| 687 | - // a 406 (not acceptable) response. |
|
| 688 | - header('HTTP/1.1 406 Not Acceptable'); |
|
| 689 | - die('406 Not Acceptable: The server was unable to ' . |
|
| 690 | - 'negotiate content for this request.'); |
|
| 691 | - } else { |
|
| 692 | - // Tell cache content is based ot Accept header |
|
| 693 | - header("Vary: Accept"); |
|
| 694 | - return $format; |
|
| 695 | - } |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - |
|
| 699 | - /** |
|
| 700 | - * Parses the request data and returns it |
|
| 701 | - * @return array php data |
|
| 702 | - */ |
|
| 703 | - protected function getRequestData() |
|
| 704 | - { |
|
| 705 | - try { |
|
| 706 | - $r = file_get_contents('php://input'); |
|
| 707 | - if (is_null($r)) { |
|
| 708 | - return $_GET; |
|
| 709 | - } |
|
| 710 | - $r = $this->request_format->decode($r); |
|
| 711 | - return is_null($r) ? array() : $r; |
|
| 712 | - } catch (RestException $e) { |
|
| 713 | - $this->handleError($e->getCode(), $e->getMessage()); |
|
| 714 | - } |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - |
|
| 718 | - protected function mapUrlToMethod() |
|
| 719 | - { |
|
| 720 | - if (!isset($this->routes[$this->request_method])) { |
|
| 721 | - return array(); |
|
| 722 | - } |
|
| 723 | - $urls = $this->routes[$this->request_method]; |
|
| 724 | - if (!$urls) { |
|
| 725 | - return array(); |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - $found = false; |
|
| 729 | - $this->request_data += $_GET; |
|
| 730 | - $params = array('request_data' => $this->request_data); |
|
| 731 | - $params += $this->request_data; |
|
| 732 | - $lc = strtolower($this->url); |
|
| 733 | - foreach ($urls as $url => $call) { |
|
| 734 | - //echo PHP_EOL.$url.' = '.$this->url.PHP_EOL; |
|
| 735 | - $call = (object) $call; |
|
| 736 | - if (strstr($url, ':')) { |
|
| 737 | - $regex = preg_replace('/\\\:([^\/]+)/', '(?P<$1>[^/]+)', |
|
| 738 | - preg_quote($url)); |
|
| 739 | - if (preg_match(":^$regex$:i", $this->url, $matches)) { |
|
| 740 | - foreach ($matches as $arg => $match) { |
|
| 741 | - if (isset($call->arguments[$arg])) { |
|
| 742 | - //flog("$arg => $match $args[$arg]"); |
|
| 743 | - $params[$arg] = $match; |
|
| 744 | - } |
|
| 745 | - } |
|
| 746 | - $found = true; |
|
| 747 | - break; |
|
| 748 | - } |
|
| 749 | - } else if ($url == $lc) { |
|
| 750 | - $found = true; |
|
| 751 | - break; |
|
| 752 | - } |
|
| 753 | - } |
|
| 754 | - if ($found) { |
|
| 755 | - //echo PHP_EOL."Found $url "; |
|
| 756 | - //print_r($call); |
|
| 757 | - $p = $call->defaults; |
|
| 758 | - foreach ($call->arguments as $key => $value) { |
|
| 759 | - //echo "$key => $value \n"; |
|
| 760 | - if (isset($params[$key])) { |
|
| 761 | - $p[$value] = $params[$key]; |
|
| 762 | - } |
|
| 763 | - } |
|
| 764 | - $call->arguments = $p; |
|
| 765 | - return $call; |
|
| 766 | - } |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - |
|
| 770 | - /** |
|
| 771 | - * Apply static and non-static properties defined in |
|
| 772 | - * the method information anotation |
|
| 773 | - * @param String $class_name |
|
| 774 | - * @param Object $instance instance of that class |
|
| 775 | - * @param Object $method_info method information and metadata |
|
| 776 | - */ |
|
| 777 | - protected function applyClassMetadata($class_name, $instance, $method_info) |
|
| 778 | - { |
|
| 779 | - if (isset($method_info->metadata[$class_name]) |
|
| 780 | - && is_array($method_info->metadata[$class_name]) |
|
| 781 | - ) { |
|
| 782 | - foreach ($method_info->metadata[$class_name] as |
|
| 783 | - $property => $value) { |
|
| 784 | - if (property_exists($class_name, $property)) { |
|
| 785 | - $reflection_property = |
|
| 786 | - new ReflectionProperty($class_name, $property); |
|
| 787 | - $reflection_property->setValue($instance, $value); |
|
| 788 | - } |
|
| 789 | - } |
|
| 790 | - } |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - |
|
| 794 | - protected function loadCache() |
|
| 795 | - { |
|
| 796 | - if ($this->cached !== null) { |
|
| 797 | - return; |
|
| 798 | - } |
|
| 799 | - $file = $this->cache_dir . '/routes.php'; |
|
| 800 | - $this->cached = false; |
|
| 801 | - |
|
| 802 | - if ($this->production_mode) { |
|
| 803 | - if (file_exists($file)) { |
|
| 804 | - $routes = include($file); |
|
| 805 | - } |
|
| 806 | - if (isset($routes) && is_array($routes)) { |
|
| 807 | - $this->routes = $routes; |
|
| 808 | - $this->cached = true; |
|
| 809 | - } |
|
| 810 | - } else { |
|
| 811 | - //@unlink($this->cache_dir . "/$name.php"); |
|
| 812 | - } |
|
| 813 | - } |
|
| 814 | - |
|
| 815 | - |
|
| 816 | - /** |
|
| 817 | - * Generates cachable url to method mapping |
|
| 818 | - * @param string $class_name |
|
| 819 | - * @param string $base_path |
|
| 820 | - */ |
|
| 821 | - protected function generateMap($class_name, $base_path = '') |
|
| 822 | - { |
|
| 823 | - $reflection = new ReflectionClass($class_name); |
|
| 824 | - $class_metadata = parse_doc($reflection->getDocComment()); |
|
| 825 | - $methods = $reflection->getMethods( |
|
| 826 | - ReflectionMethod::IS_PUBLIC + ReflectionMethod::IS_PROTECTED |
|
| 827 | - ); |
|
| 828 | - foreach ($methods as $method) { |
|
| 829 | - $doc = $method->getDocComment(); |
|
| 830 | - $arguments = array(); |
|
| 831 | - $defaults = array(); |
|
| 832 | - $metadata = $class_metadata + parse_doc($doc); |
|
| 833 | - $params = $method->getParameters(); |
|
| 834 | - $position = 0; |
|
| 835 | - foreach ($params as $param) { |
|
| 836 | - $arguments[$param->getName()] = $position; |
|
| 837 | - $defaults[$position] = $param->isDefaultValueAvailable() ? |
|
| 838 | - $param->getDefaultValue() : null; |
|
| 839 | - $position++; |
|
| 840 | - } |
|
| 841 | - $method_flag = $method->isProtected() ? |
|
| 842 | - (isRestlerCompatibilityModeEnabled() ? 2 : 3) : |
|
| 843 | - (isset($metadata['protected']) ? 1 : 0); |
|
| 844 | - |
|
| 845 | - //take note of the order |
|
| 846 | - $call = array( |
|
| 847 | - 'class_name' => $class_name, |
|
| 848 | - 'method_name' => $method->getName(), |
|
| 849 | - 'arguments' => $arguments, |
|
| 850 | - 'defaults' => $defaults, |
|
| 851 | - 'metadata' => $metadata, |
|
| 852 | - 'method_flag' => $method_flag |
|
| 853 | - ); |
|
| 854 | - $method_url = strtolower($method->getName()); |
|
| 855 | - if (preg_match_all( |
|
| 856 | - '/@url\s+(GET|POST|PUT|DELETE|HEAD|OPTIONS)[ \t]*\/?(\S*)/s', |
|
| 857 | - $doc, $matches, PREG_SET_ORDER) |
|
| 858 | - ) { |
|
| 859 | - foreach ($matches as $match) { |
|
| 860 | - $http_method = $match[1]; |
|
| 861 | - $url = rtrim($base_path . $match[2], '/'); |
|
| 862 | - $this->routes[$http_method][$url] = $call; |
|
| 863 | - } |
|
| 864 | - } elseif ($method_url[0] != '_') { |
|
| 865 | - //not prefixed with underscore |
|
| 866 | - // no configuration found so use convention |
|
| 867 | - if (preg_match_all('/^(GET|POST|PUT|DELETE|HEAD|OPTIONS)/i', |
|
| 868 | - $method_url, $matches) |
|
| 869 | - ) { |
|
| 870 | - $http_method = strtoupper($matches[0][0]); |
|
| 871 | - $method_url = substr($method_url, strlen($http_method)); |
|
| 872 | - } else { |
|
| 873 | - $http_method = 'GET'; |
|
| 874 | - } |
|
| 875 | - $url = $base_path |
|
| 876 | - . ($method_url == 'index' || $method_url == 'default' ? '' : |
|
| 877 | - $method_url); |
|
| 878 | - $url = rtrim($url, '/'); |
|
| 879 | - $this->routes[$http_method][$url] = $call; |
|
| 880 | - foreach ($params as $param) { |
|
| 881 | - if ($param->getName() == 'request_data') { |
|
| 882 | - break; |
|
| 883 | - } |
|
| 884 | - $url .= $url == '' ? ':' : '/:'; |
|
| 885 | - $url .= $param->getName(); |
|
| 886 | - $this->routes[$http_method][$url] = $call; |
|
| 887 | - } |
|
| 888 | - } |
|
| 889 | - } |
|
| 890 | - } |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Sets the HTTP response status |
|
| 485 | + * @param int $code response code |
|
| 486 | + */ |
|
| 487 | + public function setStatus($code) |
|
| 488 | + { |
|
| 489 | + header("{$_SERVER['SERVER_PROTOCOL']} $code " . |
|
| 490 | + $this->codes[strval($code)]); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * Compare two strings and remove the common |
|
| 496 | + * sub string from the first string and return it |
|
| 497 | + * @param string $first |
|
| 498 | + * @param string $second |
|
| 499 | + * @param string $char optional, set it as |
|
| 500 | + * blank string for char by char comparison |
|
| 501 | + * @return string |
|
| 502 | + */ |
|
| 503 | + public function removeCommonPath($first, $second, $char = '/') |
|
| 504 | + { |
|
| 505 | + $first = explode($char, $first); |
|
| 506 | + $second = explode($char, $second); |
|
| 507 | + while (count($second)) { |
|
| 508 | + if ($first[0] == $second[0]) { |
|
| 509 | + array_shift($first); |
|
| 510 | + } else { |
|
| 511 | + break; |
|
| 512 | + } |
|
| 513 | + array_shift($second); |
|
| 514 | + } |
|
| 515 | + return implode($char, $first); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * Save cache to file |
|
| 521 | + */ |
|
| 522 | + public function saveCache() |
|
| 523 | + { |
|
| 524 | + $file = $this->cache_dir . '/routes.php'; |
|
| 525 | + $s = '$o=array();' . PHP_EOL; |
|
| 526 | + foreach ($this->routes as $key => $value) { |
|
| 527 | + $s .= PHP_EOL . PHP_EOL . PHP_EOL . |
|
| 528 | + "############### $key ###############" . PHP_EOL . PHP_EOL; |
|
| 529 | + $s .= '$o[\'' . $key . '\']=array();'; |
|
| 530 | + foreach ($value as $ke => $va) { |
|
| 531 | + $s .= PHP_EOL . PHP_EOL . "#==== $key $ke" . PHP_EOL . PHP_EOL; |
|
| 532 | + $s .= '$o[\'' . $key . '\'][\'' . $ke . '\']=' . str_replace( |
|
| 533 | + PHP_EOL, PHP_EOL . "\t", var_export($va, true) |
|
| 534 | + ) . ';'; |
|
| 535 | + } |
|
| 536 | + } |
|
| 537 | + $s .= PHP_EOL . 'return $o;'; |
|
| 538 | + $r = @file_put_contents($file, "<?php $s"); |
|
| 539 | + @chmod($file, 0777); |
|
| 540 | + if ($r === false) { |
|
| 541 | + throw new Exception( |
|
| 542 | + "The cache directory located at '$this->cache_dir' needs to " |
|
| 543 | + . "have the permissions set to read/write/execute for everyone" |
|
| 544 | + . " in order to save cache and improve performance."); |
|
| 545 | + } |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + // ================================================================== |
|
| 549 | + // |
|
| 550 | + // Protected functions |
|
| 551 | + // |
|
| 552 | + // ------------------------------------------------------------------ |
|
| 553 | + |
|
| 554 | + |
|
| 555 | + /** |
|
| 556 | + * Parses the requst url and get the api path |
|
| 557 | + * @return string api path |
|
| 558 | + */ |
|
| 559 | + protected function getPath() |
|
| 560 | + { |
|
| 561 | + $path = urldecode($this->removeCommonPath($_SERVER['REQUEST_URI'], |
|
| 562 | + $_SERVER['SCRIPT_NAME'])); |
|
| 563 | + $path = preg_replace('/(\/*\?.*$)|(\/$)/', '', $path); |
|
| 564 | + $path = str_replace($this->format_map['extensions'], '', $path); |
|
| 565 | + return $path; |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Parses the request to figure out the http request type |
|
| 571 | + * @return string which will be one of the following |
|
| 572 | + * [GET, POST, PUT, DELETE] |
|
| 573 | + * @example GET |
|
| 574 | + */ |
|
| 575 | + protected function getRequestMethod() |
|
| 576 | + { |
|
| 577 | + $method = $_SERVER['REQUEST_METHOD']; |
|
| 578 | + if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { |
|
| 579 | + $method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']; |
|
| 580 | + } |
|
| 581 | + //support for HEAD request |
|
| 582 | + if ($method == 'HEAD') { |
|
| 583 | + $method = 'GET'; |
|
| 584 | + } |
|
| 585 | + return $method; |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + |
|
| 589 | + /** |
|
| 590 | + * Parses the request to figure out format of the request data |
|
| 591 | + * @return iFormat any class that implements iFormat |
|
| 592 | + * @example JsonFormat |
|
| 593 | + */ |
|
| 594 | + protected function getRequestFormat() |
|
| 595 | + { |
|
| 596 | + $format = null; |
|
| 597 | + //check if client has sent any information on request format |
|
| 598 | + if (isset($_SERVER['CONTENT_TYPE'])) { |
|
| 599 | + $mime = explode(';', $_SERVER['CONTENT_TYPE']); |
|
| 600 | + $mime = $mime[0]; |
|
| 601 | + if ($mime == UrlEncodedFormat::MIME) { |
|
| 602 | + $format = new UrlEncodedFormat(); |
|
| 603 | + } else { |
|
| 604 | + if (isset($this->format_map[$mime])) { |
|
| 605 | + $format = $this->format_map[$mime]; |
|
| 606 | + $format = is_string($format) ? new $format : $format; |
|
| 607 | + $format->setMIME($mime); |
|
| 608 | + } |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + return $format; |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Parses the request to figure out the best format for response |
|
| 617 | + * @return iFormat any class that implements iFormat |
|
| 618 | + * @example JsonFormat |
|
| 619 | + */ |
|
| 620 | + protected function getResponseFormat() |
|
| 621 | + { |
|
| 622 | + //check if client has specified an extension |
|
| 623 | + /** |
|
| 624 | + * @var iFormat |
|
| 625 | + */ |
|
| 626 | + $format = null; |
|
| 627 | + $extensions = explode('.', |
|
| 628 | + parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); |
|
| 629 | + while ($extensions) { |
|
| 630 | + $extension = array_pop($extensions); |
|
| 631 | + $extension = explode('/', $extension); |
|
| 632 | + $extension = array_shift($extension); |
|
| 633 | + if ($extension && isset($this->format_map[$extension])) { |
|
| 634 | + $format = $this->format_map[$extension]; |
|
| 635 | + $format = is_string($format) ? new $format : $format; |
|
| 636 | + $format->setExtension($extension); |
|
| 637 | + //echo "Extension $extension"; |
|
| 638 | + return $format; |
|
| 639 | + } |
|
| 640 | + } |
|
| 641 | + //check if client has sent list of accepted data formats |
|
| 642 | + if (isset($_SERVER['HTTP_ACCEPT'])) { |
|
| 643 | + $acceptList = array(); |
|
| 644 | + $accepts = explode(',', strtolower($_SERVER['HTTP_ACCEPT'])); |
|
| 645 | + if (!is_array($accepts)) { |
|
| 646 | + $accepts = array($accepts); |
|
| 647 | + } |
|
| 648 | + foreach ($accepts as $pos => $accept) { |
|
| 649 | + $parts = explode(';q=', trim($accept)); |
|
| 650 | + $type = array_shift($parts); |
|
| 651 | + $quality = count($parts) ? |
|
| 652 | + floatval(array_shift($parts)) : |
|
| 653 | + (1000 - $pos) / 1000; |
|
| 654 | + $acceptList[$type] = $quality; |
|
| 655 | + } |
|
| 656 | + arsort($acceptList); |
|
| 657 | + foreach ($acceptList as $accept => $quality) { |
|
| 658 | + if (isset($this->format_map[$accept])) { |
|
| 659 | + $format = $this->format_map[$accept]; |
|
| 660 | + $format = is_string($format) ? new $format : $format; |
|
| 661 | + $format->setMIME($accept); |
|
| 662 | + //echo "MIME $accept"; |
|
| 663 | + // Tell cache content is based on Accept header |
|
| 664 | + header("Vary: Accept"); |
|
| 665 | + return $format; |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | + } else { |
|
| 669 | + // RFC 2616: If no Accept header field is |
|
| 670 | + // present, then it is assumed that the |
|
| 671 | + // client accepts all media types. |
|
| 672 | + $_SERVER['HTTP_ACCEPT'] = '*/*'; |
|
| 673 | + } |
|
| 674 | + if (strpos($_SERVER['HTTP_ACCEPT'], '*') !== false) { |
|
| 675 | + if (strpos($_SERVER['HTTP_ACCEPT'], 'application/*') !== false) { |
|
| 676 | + $format = new JsonFormat; |
|
| 677 | + } else if (strpos($_SERVER['HTTP_ACCEPT'], 'text/*') !== false) { |
|
| 678 | + $format = new XmlFormat; |
|
| 679 | + } else if (strpos($_SERVER['HTTP_ACCEPT'], '*/*') !== false) { |
|
| 680 | + $format = new $this->format_map['default']; |
|
| 681 | + } |
|
| 682 | + } |
|
| 683 | + if (empty($format)) { |
|
| 684 | + // RFC 2616: If an Accept header field is present, and if the |
|
| 685 | + // server cannot send a response which is acceptable according to |
|
| 686 | + // the combined Accept field value, then the server SHOULD send |
|
| 687 | + // a 406 (not acceptable) response. |
|
| 688 | + header('HTTP/1.1 406 Not Acceptable'); |
|
| 689 | + die('406 Not Acceptable: The server was unable to ' . |
|
| 690 | + 'negotiate content for this request.'); |
|
| 691 | + } else { |
|
| 692 | + // Tell cache content is based ot Accept header |
|
| 693 | + header("Vary: Accept"); |
|
| 694 | + return $format; |
|
| 695 | + } |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + |
|
| 699 | + /** |
|
| 700 | + * Parses the request data and returns it |
|
| 701 | + * @return array php data |
|
| 702 | + */ |
|
| 703 | + protected function getRequestData() |
|
| 704 | + { |
|
| 705 | + try { |
|
| 706 | + $r = file_get_contents('php://input'); |
|
| 707 | + if (is_null($r)) { |
|
| 708 | + return $_GET; |
|
| 709 | + } |
|
| 710 | + $r = $this->request_format->decode($r); |
|
| 711 | + return is_null($r) ? array() : $r; |
|
| 712 | + } catch (RestException $e) { |
|
| 713 | + $this->handleError($e->getCode(), $e->getMessage()); |
|
| 714 | + } |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + |
|
| 718 | + protected function mapUrlToMethod() |
|
| 719 | + { |
|
| 720 | + if (!isset($this->routes[$this->request_method])) { |
|
| 721 | + return array(); |
|
| 722 | + } |
|
| 723 | + $urls = $this->routes[$this->request_method]; |
|
| 724 | + if (!$urls) { |
|
| 725 | + return array(); |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + $found = false; |
|
| 729 | + $this->request_data += $_GET; |
|
| 730 | + $params = array('request_data' => $this->request_data); |
|
| 731 | + $params += $this->request_data; |
|
| 732 | + $lc = strtolower($this->url); |
|
| 733 | + foreach ($urls as $url => $call) { |
|
| 734 | + //echo PHP_EOL.$url.' = '.$this->url.PHP_EOL; |
|
| 735 | + $call = (object) $call; |
|
| 736 | + if (strstr($url, ':')) { |
|
| 737 | + $regex = preg_replace('/\\\:([^\/]+)/', '(?P<$1>[^/]+)', |
|
| 738 | + preg_quote($url)); |
|
| 739 | + if (preg_match(":^$regex$:i", $this->url, $matches)) { |
|
| 740 | + foreach ($matches as $arg => $match) { |
|
| 741 | + if (isset($call->arguments[$arg])) { |
|
| 742 | + //flog("$arg => $match $args[$arg]"); |
|
| 743 | + $params[$arg] = $match; |
|
| 744 | + } |
|
| 745 | + } |
|
| 746 | + $found = true; |
|
| 747 | + break; |
|
| 748 | + } |
|
| 749 | + } else if ($url == $lc) { |
|
| 750 | + $found = true; |
|
| 751 | + break; |
|
| 752 | + } |
|
| 753 | + } |
|
| 754 | + if ($found) { |
|
| 755 | + //echo PHP_EOL."Found $url "; |
|
| 756 | + //print_r($call); |
|
| 757 | + $p = $call->defaults; |
|
| 758 | + foreach ($call->arguments as $key => $value) { |
|
| 759 | + //echo "$key => $value \n"; |
|
| 760 | + if (isset($params[$key])) { |
|
| 761 | + $p[$value] = $params[$key]; |
|
| 762 | + } |
|
| 763 | + } |
|
| 764 | + $call->arguments = $p; |
|
| 765 | + return $call; |
|
| 766 | + } |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + |
|
| 770 | + /** |
|
| 771 | + * Apply static and non-static properties defined in |
|
| 772 | + * the method information anotation |
|
| 773 | + * @param String $class_name |
|
| 774 | + * @param Object $instance instance of that class |
|
| 775 | + * @param Object $method_info method information and metadata |
|
| 776 | + */ |
|
| 777 | + protected function applyClassMetadata($class_name, $instance, $method_info) |
|
| 778 | + { |
|
| 779 | + if (isset($method_info->metadata[$class_name]) |
|
| 780 | + && is_array($method_info->metadata[$class_name]) |
|
| 781 | + ) { |
|
| 782 | + foreach ($method_info->metadata[$class_name] as |
|
| 783 | + $property => $value) { |
|
| 784 | + if (property_exists($class_name, $property)) { |
|
| 785 | + $reflection_property = |
|
| 786 | + new ReflectionProperty($class_name, $property); |
|
| 787 | + $reflection_property->setValue($instance, $value); |
|
| 788 | + } |
|
| 789 | + } |
|
| 790 | + } |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + |
|
| 794 | + protected function loadCache() |
|
| 795 | + { |
|
| 796 | + if ($this->cached !== null) { |
|
| 797 | + return; |
|
| 798 | + } |
|
| 799 | + $file = $this->cache_dir . '/routes.php'; |
|
| 800 | + $this->cached = false; |
|
| 801 | + |
|
| 802 | + if ($this->production_mode) { |
|
| 803 | + if (file_exists($file)) { |
|
| 804 | + $routes = include($file); |
|
| 805 | + } |
|
| 806 | + if (isset($routes) && is_array($routes)) { |
|
| 807 | + $this->routes = $routes; |
|
| 808 | + $this->cached = true; |
|
| 809 | + } |
|
| 810 | + } else { |
|
| 811 | + //@unlink($this->cache_dir . "/$name.php"); |
|
| 812 | + } |
|
| 813 | + } |
|
| 814 | + |
|
| 815 | + |
|
| 816 | + /** |
|
| 817 | + * Generates cachable url to method mapping |
|
| 818 | + * @param string $class_name |
|
| 819 | + * @param string $base_path |
|
| 820 | + */ |
|
| 821 | + protected function generateMap($class_name, $base_path = '') |
|
| 822 | + { |
|
| 823 | + $reflection = new ReflectionClass($class_name); |
|
| 824 | + $class_metadata = parse_doc($reflection->getDocComment()); |
|
| 825 | + $methods = $reflection->getMethods( |
|
| 826 | + ReflectionMethod::IS_PUBLIC + ReflectionMethod::IS_PROTECTED |
|
| 827 | + ); |
|
| 828 | + foreach ($methods as $method) { |
|
| 829 | + $doc = $method->getDocComment(); |
|
| 830 | + $arguments = array(); |
|
| 831 | + $defaults = array(); |
|
| 832 | + $metadata = $class_metadata + parse_doc($doc); |
|
| 833 | + $params = $method->getParameters(); |
|
| 834 | + $position = 0; |
|
| 835 | + foreach ($params as $param) { |
|
| 836 | + $arguments[$param->getName()] = $position; |
|
| 837 | + $defaults[$position] = $param->isDefaultValueAvailable() ? |
|
| 838 | + $param->getDefaultValue() : null; |
|
| 839 | + $position++; |
|
| 840 | + } |
|
| 841 | + $method_flag = $method->isProtected() ? |
|
| 842 | + (isRestlerCompatibilityModeEnabled() ? 2 : 3) : |
|
| 843 | + (isset($metadata['protected']) ? 1 : 0); |
|
| 844 | + |
|
| 845 | + //take note of the order |
|
| 846 | + $call = array( |
|
| 847 | + 'class_name' => $class_name, |
|
| 848 | + 'method_name' => $method->getName(), |
|
| 849 | + 'arguments' => $arguments, |
|
| 850 | + 'defaults' => $defaults, |
|
| 851 | + 'metadata' => $metadata, |
|
| 852 | + 'method_flag' => $method_flag |
|
| 853 | + ); |
|
| 854 | + $method_url = strtolower($method->getName()); |
|
| 855 | + if (preg_match_all( |
|
| 856 | + '/@url\s+(GET|POST|PUT|DELETE|HEAD|OPTIONS)[ \t]*\/?(\S*)/s', |
|
| 857 | + $doc, $matches, PREG_SET_ORDER) |
|
| 858 | + ) { |
|
| 859 | + foreach ($matches as $match) { |
|
| 860 | + $http_method = $match[1]; |
|
| 861 | + $url = rtrim($base_path . $match[2], '/'); |
|
| 862 | + $this->routes[$http_method][$url] = $call; |
|
| 863 | + } |
|
| 864 | + } elseif ($method_url[0] != '_') { |
|
| 865 | + //not prefixed with underscore |
|
| 866 | + // no configuration found so use convention |
|
| 867 | + if (preg_match_all('/^(GET|POST|PUT|DELETE|HEAD|OPTIONS)/i', |
|
| 868 | + $method_url, $matches) |
|
| 869 | + ) { |
|
| 870 | + $http_method = strtoupper($matches[0][0]); |
|
| 871 | + $method_url = substr($method_url, strlen($http_method)); |
|
| 872 | + } else { |
|
| 873 | + $http_method = 'GET'; |
|
| 874 | + } |
|
| 875 | + $url = $base_path |
|
| 876 | + . ($method_url == 'index' || $method_url == 'default' ? '' : |
|
| 877 | + $method_url); |
|
| 878 | + $url = rtrim($url, '/'); |
|
| 879 | + $this->routes[$http_method][$url] = $call; |
|
| 880 | + foreach ($params as $param) { |
|
| 881 | + if ($param->getName() == 'request_data') { |
|
| 882 | + break; |
|
| 883 | + } |
|
| 884 | + $url .= $url == '' ? ':' : '/:'; |
|
| 885 | + $url .= $param->getName(); |
|
| 886 | + $this->routes[$http_method][$url] = $call; |
|
| 887 | + } |
|
| 888 | + } |
|
| 889 | + } |
|
| 890 | + } |
|
| 891 | 891 | |
| 892 | 892 | } |
| 893 | 893 | |
| 894 | 894 | if (version_compare(PHP_VERSION, '5.3.0') < 0) { |
| 895 | - require_once 'compat.php'; |
|
| 895 | + require_once 'compat.php'; |
|
| 896 | 896 | } |
| 897 | 897 | |
| 898 | 898 | // ================================================================== |
@@ -916,10 +916,10 @@ discard block |
||
| 916 | 916 | { |
| 917 | 917 | |
| 918 | 918 | |
| 919 | - public function __construct($http_status_code, $error_message = null) |
|
| 920 | - { |
|
| 921 | - parent::__construct($error_message, $http_status_code); |
|
| 922 | - } |
|
| 919 | + public function __construct($http_status_code, $error_message = null) |
|
| 920 | + { |
|
| 921 | + parent::__construct($error_message, $http_status_code); |
|
| 922 | + } |
|
| 923 | 923 | |
| 924 | 924 | } |
| 925 | 925 | |
@@ -937,21 +937,21 @@ discard block |
||
| 937 | 937 | { |
| 938 | 938 | |
| 939 | 939 | |
| 940 | - /** |
|
| 941 | - * Result of an api call is passed to this method |
|
| 942 | - * to create a standard structure for the data |
|
| 943 | - * @param unknown_type $result can be a primitive or array or object |
|
| 944 | - */ |
|
| 945 | - public function __formatResponse($result); |
|
| 940 | + /** |
|
| 941 | + * Result of an api call is passed to this method |
|
| 942 | + * to create a standard structure for the data |
|
| 943 | + * @param unknown_type $result can be a primitive or array or object |
|
| 944 | + */ |
|
| 945 | + public function __formatResponse($result); |
|
| 946 | 946 | |
| 947 | 947 | |
| 948 | - /** |
|
| 949 | - * When the api call results in RestException this method |
|
| 950 | - * will be called to return the error message |
|
| 951 | - * @param int $status_code |
|
| 952 | - * @param String $message |
|
| 953 | - */ |
|
| 954 | - public function __formatError($status_code, $message); |
|
| 948 | + /** |
|
| 949 | + * When the api call results in RestException this method |
|
| 950 | + * will be called to return the error message |
|
| 951 | + * @param int $status_code |
|
| 952 | + * @param String $message |
|
| 953 | + */ |
|
| 954 | + public function __formatError($status_code, $message); |
|
| 955 | 955 | } |
| 956 | 956 | |
| 957 | 957 | /** |
@@ -968,21 +968,21 @@ discard block |
||
| 968 | 968 | { |
| 969 | 969 | |
| 970 | 970 | |
| 971 | - public function __formatResponse($result) |
|
| 972 | - { |
|
| 973 | - return $result; |
|
| 974 | - } |
|
| 971 | + public function __formatResponse($result) |
|
| 972 | + { |
|
| 973 | + return $result; |
|
| 974 | + } |
|
| 975 | 975 | |
| 976 | 976 | |
| 977 | - public function __formatError($statusCode, $message) |
|
| 978 | - { |
|
| 979 | - return array( |
|
| 980 | - 'error' => array( |
|
| 981 | - 'code' => $statusCode, |
|
| 982 | - 'message' => $message |
|
| 983 | - ) |
|
| 984 | - ); |
|
| 985 | - } |
|
| 977 | + public function __formatError($statusCode, $message) |
|
| 978 | + { |
|
| 979 | + return array( |
|
| 980 | + 'error' => array( |
|
| 981 | + 'code' => $statusCode, |
|
| 982 | + 'message' => $message |
|
| 983 | + ) |
|
| 984 | + ); |
|
| 985 | + } |
|
| 986 | 986 | |
| 987 | 987 | } |
| 988 | 988 | |
@@ -1000,11 +1000,11 @@ discard block |
||
| 1000 | 1000 | { |
| 1001 | 1001 | |
| 1002 | 1002 | |
| 1003 | - /** |
|
| 1004 | - * Auth function that is called when a protected method is requested |
|
| 1005 | - * @return boolean true or false |
|
| 1006 | - */ |
|
| 1007 | - public function __isAuthenticated(); |
|
| 1003 | + /** |
|
| 1004 | + * Auth function that is called when a protected method is requested |
|
| 1005 | + * @return boolean true or false |
|
| 1006 | + */ |
|
| 1007 | + public function __isAuthenticated(); |
|
| 1008 | 1008 | } |
| 1009 | 1009 | |
| 1010 | 1010 | /** |
@@ -1022,60 +1022,60 @@ discard block |
||
| 1022 | 1022 | { |
| 1023 | 1023 | |
| 1024 | 1024 | |
| 1025 | - /** |
|
| 1026 | - * Get Extension => MIME type mappings as an associative array |
|
| 1027 | - * @return array list of mime strings for the format |
|
| 1028 | - * @example array('json'=>'application/json'); |
|
| 1029 | - */ |
|
| 1030 | - public function getMIMEMap(); |
|
| 1025 | + /** |
|
| 1026 | + * Get Extension => MIME type mappings as an associative array |
|
| 1027 | + * @return array list of mime strings for the format |
|
| 1028 | + * @example array('json'=>'application/json'); |
|
| 1029 | + */ |
|
| 1030 | + public function getMIMEMap(); |
|
| 1031 | 1031 | |
| 1032 | 1032 | |
| 1033 | - /** |
|
| 1034 | - * Set the selected MIME type |
|
| 1035 | - * @param string $mime MIME type |
|
| 1036 | - */ |
|
| 1037 | - public function setMIME($mime); |
|
| 1033 | + /** |
|
| 1034 | + * Set the selected MIME type |
|
| 1035 | + * @param string $mime MIME type |
|
| 1036 | + */ |
|
| 1037 | + public function setMIME($mime); |
|
| 1038 | 1038 | |
| 1039 | 1039 | |
| 1040 | - /** |
|
| 1041 | - * Get selected MIME type |
|
| 1042 | - */ |
|
| 1043 | - public function getMIME(); |
|
| 1040 | + /** |
|
| 1041 | + * Get selected MIME type |
|
| 1042 | + */ |
|
| 1043 | + public function getMIME(); |
|
| 1044 | 1044 | |
| 1045 | 1045 | |
| 1046 | - /** |
|
| 1047 | - * Set the selected file extension |
|
| 1048 | - * @param string $extension file extension |
|
| 1049 | - */ |
|
| 1050 | - public function setExtension($extension); |
|
| 1046 | + /** |
|
| 1047 | + * Set the selected file extension |
|
| 1048 | + * @param string $extension file extension |
|
| 1049 | + */ |
|
| 1050 | + public function setExtension($extension); |
|
| 1051 | 1051 | |
| 1052 | 1052 | |
| 1053 | - /** |
|
| 1054 | - * Get the selected file extension |
|
| 1055 | - * @return string file extension |
|
| 1056 | - */ |
|
| 1057 | - public function getExtension(); |
|
| 1053 | + /** |
|
| 1054 | + * Get the selected file extension |
|
| 1055 | + * @return string file extension |
|
| 1056 | + */ |
|
| 1057 | + public function getExtension(); |
|
| 1058 | 1058 | |
| 1059 | 1059 | |
| 1060 | - /** |
|
| 1061 | - * Encode the given data in the format |
|
| 1062 | - * @param array $data resulting data that needs to |
|
| 1063 | - * be encoded in the given format |
|
| 1064 | - * @param boolean $human_readable set to true when restler |
|
| 1065 | - * is not running in production mode. Formatter has to |
|
| 1066 | - * make the encoded output more human readable |
|
| 1067 | - * @return string encoded string |
|
| 1068 | - */ |
|
| 1069 | - public function encode($data, $human_readable = false); |
|
| 1060 | + /** |
|
| 1061 | + * Encode the given data in the format |
|
| 1062 | + * @param array $data resulting data that needs to |
|
| 1063 | + * be encoded in the given format |
|
| 1064 | + * @param boolean $human_readable set to true when restler |
|
| 1065 | + * is not running in production mode. Formatter has to |
|
| 1066 | + * make the encoded output more human readable |
|
| 1067 | + * @return string encoded string |
|
| 1068 | + */ |
|
| 1069 | + public function encode($data, $human_readable = false); |
|
| 1070 | 1070 | |
| 1071 | 1071 | |
| 1072 | - /** |
|
| 1073 | - * Decode the given data from the format |
|
| 1074 | - * @param string $data data sent from client to |
|
| 1075 | - * the api in the given format. |
|
| 1076 | - * @return array associative array of the parsed data |
|
| 1077 | - */ |
|
| 1078 | - public function decode($data); |
|
| 1072 | + /** |
|
| 1073 | + * Decode the given data from the format |
|
| 1074 | + * @param string $data data sent from client to |
|
| 1075 | + * the api in the given format. |
|
| 1076 | + * @return array associative array of the parsed data |
|
| 1077 | + */ |
|
| 1078 | + public function decode($data); |
|
| 1079 | 1079 | } |
| 1080 | 1080 | |
| 1081 | 1081 | /** |
@@ -1091,57 +1091,57 @@ discard block |
||
| 1091 | 1091 | class UrlEncodedFormat implements iFormat |
| 1092 | 1092 | { |
| 1093 | 1093 | |
| 1094 | - const MIME = 'application/x-www-form-urlencoded'; |
|
| 1095 | - const EXTENSION = 'post'; |
|
| 1094 | + const MIME = 'application/x-www-form-urlencoded'; |
|
| 1095 | + const EXTENSION = 'post'; |
|
| 1096 | 1096 | |
| 1097 | 1097 | |
| 1098 | - public function getMIMEMap() |
|
| 1099 | - { |
|
| 1100 | - return array(self::EXTENSION => self::MIME); |
|
| 1101 | - } |
|
| 1098 | + public function getMIMEMap() |
|
| 1099 | + { |
|
| 1100 | + return array(self::EXTENSION => self::MIME); |
|
| 1101 | + } |
|
| 1102 | 1102 | |
| 1103 | 1103 | |
| 1104 | - public function getMIME() |
|
| 1105 | - { |
|
| 1106 | - return self::MIME; |
|
| 1107 | - } |
|
| 1104 | + public function getMIME() |
|
| 1105 | + { |
|
| 1106 | + return self::MIME; |
|
| 1107 | + } |
|
| 1108 | 1108 | |
| 1109 | 1109 | |
| 1110 | - public function getExtension() |
|
| 1111 | - { |
|
| 1112 | - return self::EXTENSION; |
|
| 1113 | - } |
|
| 1110 | + public function getExtension() |
|
| 1111 | + { |
|
| 1112 | + return self::EXTENSION; |
|
| 1113 | + } |
|
| 1114 | 1114 | |
| 1115 | 1115 | |
| 1116 | - public function setMIME($mime) |
|
| 1117 | - { |
|
| 1118 | - //do nothing |
|
| 1119 | - } |
|
| 1116 | + public function setMIME($mime) |
|
| 1117 | + { |
|
| 1118 | + //do nothing |
|
| 1119 | + } |
|
| 1120 | 1120 | |
| 1121 | 1121 | |
| 1122 | - public function setExtension($extension) |
|
| 1123 | - { |
|
| 1124 | - //do nothing |
|
| 1125 | - } |
|
| 1122 | + public function setExtension($extension) |
|
| 1123 | + { |
|
| 1124 | + //do nothing |
|
| 1125 | + } |
|
| 1126 | 1126 | |
| 1127 | 1127 | |
| 1128 | - public function encode($data, $human_readable = false) |
|
| 1129 | - { |
|
| 1130 | - return http_build_query($data); |
|
| 1131 | - } |
|
| 1128 | + public function encode($data, $human_readable = false) |
|
| 1129 | + { |
|
| 1130 | + return http_build_query($data); |
|
| 1131 | + } |
|
| 1132 | 1132 | |
| 1133 | 1133 | |
| 1134 | - public function decode($data) |
|
| 1135 | - { |
|
| 1136 | - parse_str($data, $r); |
|
| 1137 | - return $r; |
|
| 1138 | - } |
|
| 1134 | + public function decode($data) |
|
| 1135 | + { |
|
| 1136 | + parse_str($data, $r); |
|
| 1137 | + return $r; |
|
| 1138 | + } |
|
| 1139 | 1139 | |
| 1140 | 1140 | |
| 1141 | - public function __toString() |
|
| 1142 | - { |
|
| 1143 | - return $this->getExtension(); |
|
| 1144 | - } |
|
| 1141 | + public function __toString() |
|
| 1142 | + { |
|
| 1143 | + return $this->getExtension(); |
|
| 1144 | + } |
|
| 1145 | 1145 | |
| 1146 | 1146 | } |
| 1147 | 1147 | |
@@ -1158,158 +1158,158 @@ discard block |
||
| 1158 | 1158 | class JsonFormat implements iFormat |
| 1159 | 1159 | { |
| 1160 | 1160 | |
| 1161 | - const MIME = 'application/json,application/javascript'; |
|
| 1162 | - |
|
| 1163 | - static $mime = 'application/json'; |
|
| 1164 | - |
|
| 1165 | - const EXTENSION = 'json'; |
|
| 1166 | - |
|
| 1167 | - |
|
| 1168 | - public function getMIMEMap() |
|
| 1169 | - { |
|
| 1170 | - return array(self::EXTENSION => self::MIME); |
|
| 1171 | - } |
|
| 1172 | - |
|
| 1173 | - |
|
| 1174 | - public function getMIME() |
|
| 1175 | - { |
|
| 1176 | - return self::$mime; |
|
| 1177 | - } |
|
| 1178 | - |
|
| 1179 | - |
|
| 1180 | - public function getExtension() |
|
| 1181 | - { |
|
| 1182 | - return self::EXTENSION; |
|
| 1183 | - } |
|
| 1184 | - |
|
| 1185 | - |
|
| 1186 | - public function setMIME($mime) |
|
| 1187 | - { |
|
| 1188 | - self::$mime = $mime; |
|
| 1189 | - } |
|
| 1190 | - |
|
| 1191 | - |
|
| 1192 | - public function setExtension($extension) |
|
| 1193 | - { |
|
| 1194 | - //do nothing |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - |
|
| 1198 | - public function encode($data, $human_readable = false) |
|
| 1199 | - { |
|
| 1200 | - return $human_readable ? |
|
| 1201 | - $this->json_format(json_encode(object_to_array($data))) : |
|
| 1202 | - json_encode(object_to_array($data)); |
|
| 1203 | - } |
|
| 1204 | - |
|
| 1205 | - |
|
| 1206 | - public function decode($data) |
|
| 1207 | - { |
|
| 1208 | - $decoded = json_decode($data); |
|
| 1209 | - if (function_exists('json_last_error')) { |
|
| 1210 | - $message = ''; |
|
| 1211 | - switch (json_last_error()) { |
|
| 1212 | - case JSON_ERROR_NONE: |
|
| 1213 | - return object_to_array($decoded); |
|
| 1214 | - break; |
|
| 1215 | - case JSON_ERROR_DEPTH: |
|
| 1216 | - $message = 'maximum stack depth exceeded'; |
|
| 1217 | - break; |
|
| 1218 | - case JSON_ERROR_STATE_MISMATCH: |
|
| 1219 | - $message = 'underflow or the modes mismatch'; |
|
| 1220 | - break; |
|
| 1221 | - case JSON_ERROR_CTRL_CHAR: |
|
| 1222 | - $message = 'unexpected control character found'; |
|
| 1223 | - break; |
|
| 1224 | - case JSON_ERROR_SYNTAX: |
|
| 1225 | - $message = 'malformed JSON'; |
|
| 1226 | - break; |
|
| 1227 | - case JSON_ERROR_UTF8: |
|
| 1228 | - $message = 'malformed UTF-8 characters, '. |
|
| 1229 | - 'possibly incorrectly encoded'; |
|
| 1230 | - break; |
|
| 1231 | - default: |
|
| 1232 | - $message = 'unknown error'; |
|
| 1233 | - break; |
|
| 1234 | - } |
|
| 1235 | - throw new RestException(400, 'Error parsing JSON, ' . $message); |
|
| 1236 | - } else if (strlen($data) && $decoded === null || $decoded === $data) { |
|
| 1237 | - throw new RestException(400, 'Error parsing JSON'); |
|
| 1238 | - } |
|
| 1239 | - return object_to_array($decoded); |
|
| 1240 | - } |
|
| 1241 | - |
|
| 1242 | - |
|
| 1243 | - /** |
|
| 1244 | - * Pretty print JSON string |
|
| 1245 | - * @param string $json |
|
| 1246 | - * @return string formated json |
|
| 1247 | - */ |
|
| 1248 | - private function json_format($json) |
|
| 1249 | - { |
|
| 1250 | - $tab = " "; |
|
| 1251 | - $new_json = ""; |
|
| 1252 | - $indent_level = 0; |
|
| 1253 | - $in_string = false; |
|
| 1254 | - $len = strlen($json); |
|
| 1255 | - |
|
| 1256 | - for ($c = 0; $c < $len; $c++) { |
|
| 1257 | - $char = $json[$c]; |
|
| 1258 | - switch ($char) { |
|
| 1259 | - case '{': |
|
| 1260 | - case '[': |
|
| 1261 | - if (!$in_string) { |
|
| 1262 | - $new_json .= $char . "\n" . |
|
| 1263 | - str_repeat($tab, $indent_level + 1); |
|
| 1264 | - $indent_level++; |
|
| 1265 | - } else { |
|
| 1266 | - $new_json .= $char; |
|
| 1267 | - } |
|
| 1268 | - break; |
|
| 1269 | - case '}': |
|
| 1270 | - case ']': |
|
| 1271 | - if (!$in_string) { |
|
| 1272 | - $indent_level--; |
|
| 1273 | - $new_json .= "\n" . str_repeat($tab, $indent_level) |
|
| 1274 | - . $char; |
|
| 1275 | - } else { |
|
| 1276 | - $new_json .= $char; |
|
| 1277 | - } |
|
| 1278 | - break; |
|
| 1279 | - case ',': |
|
| 1280 | - if (!$in_string) { |
|
| 1281 | - $new_json .= ",\n" . str_repeat($tab, $indent_level); |
|
| 1282 | - } else { |
|
| 1283 | - $new_json .= $char; |
|
| 1284 | - } |
|
| 1285 | - break; |
|
| 1286 | - case ':': |
|
| 1287 | - if (!$in_string) { |
|
| 1288 | - $new_json .= ": "; |
|
| 1289 | - } else { |
|
| 1290 | - $new_json .= $char; |
|
| 1291 | - } |
|
| 1292 | - break; |
|
| 1293 | - case '"': |
|
| 1294 | - if ($c == 0) { |
|
| 1295 | - $in_string = true; |
|
| 1296 | - } else if ($c > 0 && $json[$c - 1] != '\\') { |
|
| 1297 | - $in_string = !$in_string; |
|
| 1298 | - } |
|
| 1299 | - default: |
|
| 1300 | - $new_json .= $char; |
|
| 1301 | - break; |
|
| 1302 | - } |
|
| 1303 | - } |
|
| 1304 | - |
|
| 1305 | - return $new_json; |
|
| 1306 | - } |
|
| 1307 | - |
|
| 1308 | - |
|
| 1309 | - public function __toString() |
|
| 1310 | - { |
|
| 1311 | - return $this->getExtension(); |
|
| 1312 | - } |
|
| 1161 | + const MIME = 'application/json,application/javascript'; |
|
| 1162 | + |
|
| 1163 | + static $mime = 'application/json'; |
|
| 1164 | + |
|
| 1165 | + const EXTENSION = 'json'; |
|
| 1166 | + |
|
| 1167 | + |
|
| 1168 | + public function getMIMEMap() |
|
| 1169 | + { |
|
| 1170 | + return array(self::EXTENSION => self::MIME); |
|
| 1171 | + } |
|
| 1172 | + |
|
| 1173 | + |
|
| 1174 | + public function getMIME() |
|
| 1175 | + { |
|
| 1176 | + return self::$mime; |
|
| 1177 | + } |
|
| 1178 | + |
|
| 1179 | + |
|
| 1180 | + public function getExtension() |
|
| 1181 | + { |
|
| 1182 | + return self::EXTENSION; |
|
| 1183 | + } |
|
| 1184 | + |
|
| 1185 | + |
|
| 1186 | + public function setMIME($mime) |
|
| 1187 | + { |
|
| 1188 | + self::$mime = $mime; |
|
| 1189 | + } |
|
| 1190 | + |
|
| 1191 | + |
|
| 1192 | + public function setExtension($extension) |
|
| 1193 | + { |
|
| 1194 | + //do nothing |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + |
|
| 1198 | + public function encode($data, $human_readable = false) |
|
| 1199 | + { |
|
| 1200 | + return $human_readable ? |
|
| 1201 | + $this->json_format(json_encode(object_to_array($data))) : |
|
| 1202 | + json_encode(object_to_array($data)); |
|
| 1203 | + } |
|
| 1204 | + |
|
| 1205 | + |
|
| 1206 | + public function decode($data) |
|
| 1207 | + { |
|
| 1208 | + $decoded = json_decode($data); |
|
| 1209 | + if (function_exists('json_last_error')) { |
|
| 1210 | + $message = ''; |
|
| 1211 | + switch (json_last_error()) { |
|
| 1212 | + case JSON_ERROR_NONE: |
|
| 1213 | + return object_to_array($decoded); |
|
| 1214 | + break; |
|
| 1215 | + case JSON_ERROR_DEPTH: |
|
| 1216 | + $message = 'maximum stack depth exceeded'; |
|
| 1217 | + break; |
|
| 1218 | + case JSON_ERROR_STATE_MISMATCH: |
|
| 1219 | + $message = 'underflow or the modes mismatch'; |
|
| 1220 | + break; |
|
| 1221 | + case JSON_ERROR_CTRL_CHAR: |
|
| 1222 | + $message = 'unexpected control character found'; |
|
| 1223 | + break; |
|
| 1224 | + case JSON_ERROR_SYNTAX: |
|
| 1225 | + $message = 'malformed JSON'; |
|
| 1226 | + break; |
|
| 1227 | + case JSON_ERROR_UTF8: |
|
| 1228 | + $message = 'malformed UTF-8 characters, '. |
|
| 1229 | + 'possibly incorrectly encoded'; |
|
| 1230 | + break; |
|
| 1231 | + default: |
|
| 1232 | + $message = 'unknown error'; |
|
| 1233 | + break; |
|
| 1234 | + } |
|
| 1235 | + throw new RestException(400, 'Error parsing JSON, ' . $message); |
|
| 1236 | + } else if (strlen($data) && $decoded === null || $decoded === $data) { |
|
| 1237 | + throw new RestException(400, 'Error parsing JSON'); |
|
| 1238 | + } |
|
| 1239 | + return object_to_array($decoded); |
|
| 1240 | + } |
|
| 1241 | + |
|
| 1242 | + |
|
| 1243 | + /** |
|
| 1244 | + * Pretty print JSON string |
|
| 1245 | + * @param string $json |
|
| 1246 | + * @return string formated json |
|
| 1247 | + */ |
|
| 1248 | + private function json_format($json) |
|
| 1249 | + { |
|
| 1250 | + $tab = " "; |
|
| 1251 | + $new_json = ""; |
|
| 1252 | + $indent_level = 0; |
|
| 1253 | + $in_string = false; |
|
| 1254 | + $len = strlen($json); |
|
| 1255 | + |
|
| 1256 | + for ($c = 0; $c < $len; $c++) { |
|
| 1257 | + $char = $json[$c]; |
|
| 1258 | + switch ($char) { |
|
| 1259 | + case '{': |
|
| 1260 | + case '[': |
|
| 1261 | + if (!$in_string) { |
|
| 1262 | + $new_json .= $char . "\n" . |
|
| 1263 | + str_repeat($tab, $indent_level + 1); |
|
| 1264 | + $indent_level++; |
|
| 1265 | + } else { |
|
| 1266 | + $new_json .= $char; |
|
| 1267 | + } |
|
| 1268 | + break; |
|
| 1269 | + case '}': |
|
| 1270 | + case ']': |
|
| 1271 | + if (!$in_string) { |
|
| 1272 | + $indent_level--; |
|
| 1273 | + $new_json .= "\n" . str_repeat($tab, $indent_level) |
|
| 1274 | + . $char; |
|
| 1275 | + } else { |
|
| 1276 | + $new_json .= $char; |
|
| 1277 | + } |
|
| 1278 | + break; |
|
| 1279 | + case ',': |
|
| 1280 | + if (!$in_string) { |
|
| 1281 | + $new_json .= ",\n" . str_repeat($tab, $indent_level); |
|
| 1282 | + } else { |
|
| 1283 | + $new_json .= $char; |
|
| 1284 | + } |
|
| 1285 | + break; |
|
| 1286 | + case ':': |
|
| 1287 | + if (!$in_string) { |
|
| 1288 | + $new_json .= ": "; |
|
| 1289 | + } else { |
|
| 1290 | + $new_json .= $char; |
|
| 1291 | + } |
|
| 1292 | + break; |
|
| 1293 | + case '"': |
|
| 1294 | + if ($c == 0) { |
|
| 1295 | + $in_string = true; |
|
| 1296 | + } else if ($c > 0 && $json[$c - 1] != '\\') { |
|
| 1297 | + $in_string = !$in_string; |
|
| 1298 | + } |
|
| 1299 | + default: |
|
| 1300 | + $new_json .= $char; |
|
| 1301 | + break; |
|
| 1302 | + } |
|
| 1303 | + } |
|
| 1304 | + |
|
| 1305 | + return $new_json; |
|
| 1306 | + } |
|
| 1307 | + |
|
| 1308 | + |
|
| 1309 | + public function __toString() |
|
| 1310 | + { |
|
| 1311 | + return $this->getExtension(); |
|
| 1312 | + } |
|
| 1313 | 1313 | |
| 1314 | 1314 | } |
| 1315 | 1315 | |
@@ -1327,123 +1327,123 @@ discard block |
||
| 1327 | 1327 | class DocParser |
| 1328 | 1328 | { |
| 1329 | 1329 | |
| 1330 | - private $params = array(); |
|
| 1331 | - |
|
| 1332 | - |
|
| 1333 | - public function parse($doc = '') |
|
| 1334 | - { |
|
| 1335 | - if ($doc == '') { |
|
| 1336 | - return $this->params; |
|
| 1337 | - } |
|
| 1338 | - //Get the comment |
|
| 1339 | - if (preg_match('#^/\*\*(.*)\*/#s', $doc, $comment) === false) { |
|
| 1340 | - return $this->params; |
|
| 1341 | - } |
|
| 1342 | - $comment = trim($comment[1]); |
|
| 1343 | - //Get all the lines and strip the * from the first character |
|
| 1344 | - if (preg_match_all('#^\s*\*(.*)#m', $comment, $lines) === false) { |
|
| 1345 | - return $this->params; |
|
| 1346 | - } |
|
| 1347 | - $this->parseLines($lines[1]); |
|
| 1348 | - return $this->params; |
|
| 1349 | - } |
|
| 1350 | - |
|
| 1351 | - |
|
| 1352 | - private function parseLines($lines) |
|
| 1353 | - { |
|
| 1354 | - foreach ($lines as $line) { |
|
| 1355 | - $parsedLine = $this->parseLine($line); //Parse the line |
|
| 1356 | - |
|
| 1357 | - if ($parsedLine === false && !isset($this->params['description'])) { |
|
| 1358 | - if (isset($desc)) { |
|
| 1359 | - //Store the first line in the short description |
|
| 1360 | - $this->params['description'] = implode(PHP_EOL, $desc); |
|
| 1361 | - } |
|
| 1362 | - $desc = array(); |
|
| 1363 | - } else if ($parsedLine !== false) { |
|
| 1364 | - $desc[] = $parsedLine; //Store the line in the long description |
|
| 1365 | - } |
|
| 1366 | - } |
|
| 1367 | - $desc = implode(' ', $desc); |
|
| 1368 | - if (!empty($desc)) { |
|
| 1369 | - $this->params['long_description'] = $desc; |
|
| 1370 | - } |
|
| 1371 | - } |
|
| 1372 | - |
|
| 1373 | - |
|
| 1374 | - private function parseLine($line) |
|
| 1375 | - { |
|
| 1376 | - //trim the whitespace from the line |
|
| 1377 | - $line = trim($line); |
|
| 1378 | - |
|
| 1379 | - if (empty($line)) { |
|
| 1380 | - return false; //Empty line |
|
| 1381 | - } |
|
| 1382 | - |
|
| 1383 | - if (strpos($line, '@') === 0) { |
|
| 1384 | - if (strpos($line, ' ') > 0) { |
|
| 1385 | - //Get the parameter name |
|
| 1386 | - $param = substr($line, 1, strpos($line, ' ') - 1); |
|
| 1387 | - $value = substr($line, strlen($param) + 2); //Get the value |
|
| 1388 | - } else { |
|
| 1389 | - $param = substr($line, 1); |
|
| 1390 | - $value = ''; |
|
| 1391 | - } |
|
| 1392 | - //Parse the line and return false if the parameter is valid |
|
| 1393 | - if ($this->setParam($param, $value)) { |
|
| 1394 | - return false; |
|
| 1395 | - } |
|
| 1396 | - } |
|
| 1397 | - return $line; |
|
| 1398 | - } |
|
| 1399 | - |
|
| 1400 | - |
|
| 1401 | - private function setParam($param, $value) |
|
| 1402 | - { |
|
| 1403 | - if ($param == 'param' || $param == 'return') { |
|
| 1404 | - $value = $this->formatParamOrReturn($value); |
|
| 1405 | - } |
|
| 1406 | - if ($param == 'class') { |
|
| 1407 | - list($param, $value) = $this->formatClass($value); |
|
| 1408 | - } |
|
| 1409 | - |
|
| 1410 | - if (empty($this->params[$param])) { |
|
| 1411 | - $this->params[$param] = $value; |
|
| 1412 | - } else if ($param == 'param') { |
|
| 1413 | - $arr = array($this->params[$param], $value); |
|
| 1414 | - $this->params[$param] = $arr; |
|
| 1415 | - } else { |
|
| 1416 | - $this->params[$param] = $value + $this->params[$param]; |
|
| 1417 | - } |
|
| 1418 | - return true; |
|
| 1419 | - } |
|
| 1420 | - |
|
| 1421 | - |
|
| 1422 | - private function formatClass($value) |
|
| 1423 | - { |
|
| 1424 | - $r = preg_split("[\(|\)]", $value); |
|
| 1425 | - if (count($r) > 1) { |
|
| 1426 | - $param = $r[0]; |
|
| 1427 | - parse_str($r[1], $value); |
|
| 1428 | - foreach ($value as $key => $val) { |
|
| 1429 | - $val = explode(',', $val); |
|
| 1430 | - if (count($val) > 1) { |
|
| 1431 | - $value[$key] = $val; |
|
| 1432 | - } |
|
| 1433 | - } |
|
| 1434 | - } else { |
|
| 1435 | - $param = 'Unknown'; |
|
| 1436 | - } |
|
| 1437 | - return array($param, $value); |
|
| 1438 | - } |
|
| 1439 | - |
|
| 1440 | - |
|
| 1441 | - private function formatParamOrReturn($string) |
|
| 1442 | - { |
|
| 1443 | - $pos = strpos($string, ' '); |
|
| 1444 | - $type = substr($string, 0, $pos); |
|
| 1445 | - return '(' . $type . ')' . substr($string, $pos + 1); |
|
| 1446 | - } |
|
| 1330 | + private $params = array(); |
|
| 1331 | + |
|
| 1332 | + |
|
| 1333 | + public function parse($doc = '') |
|
| 1334 | + { |
|
| 1335 | + if ($doc == '') { |
|
| 1336 | + return $this->params; |
|
| 1337 | + } |
|
| 1338 | + //Get the comment |
|
| 1339 | + if (preg_match('#^/\*\*(.*)\*/#s', $doc, $comment) === false) { |
|
| 1340 | + return $this->params; |
|
| 1341 | + } |
|
| 1342 | + $comment = trim($comment[1]); |
|
| 1343 | + //Get all the lines and strip the * from the first character |
|
| 1344 | + if (preg_match_all('#^\s*\*(.*)#m', $comment, $lines) === false) { |
|
| 1345 | + return $this->params; |
|
| 1346 | + } |
|
| 1347 | + $this->parseLines($lines[1]); |
|
| 1348 | + return $this->params; |
|
| 1349 | + } |
|
| 1350 | + |
|
| 1351 | + |
|
| 1352 | + private function parseLines($lines) |
|
| 1353 | + { |
|
| 1354 | + foreach ($lines as $line) { |
|
| 1355 | + $parsedLine = $this->parseLine($line); //Parse the line |
|
| 1356 | + |
|
| 1357 | + if ($parsedLine === false && !isset($this->params['description'])) { |
|
| 1358 | + if (isset($desc)) { |
|
| 1359 | + //Store the first line in the short description |
|
| 1360 | + $this->params['description'] = implode(PHP_EOL, $desc); |
|
| 1361 | + } |
|
| 1362 | + $desc = array(); |
|
| 1363 | + } else if ($parsedLine !== false) { |
|
| 1364 | + $desc[] = $parsedLine; //Store the line in the long description |
|
| 1365 | + } |
|
| 1366 | + } |
|
| 1367 | + $desc = implode(' ', $desc); |
|
| 1368 | + if (!empty($desc)) { |
|
| 1369 | + $this->params['long_description'] = $desc; |
|
| 1370 | + } |
|
| 1371 | + } |
|
| 1372 | + |
|
| 1373 | + |
|
| 1374 | + private function parseLine($line) |
|
| 1375 | + { |
|
| 1376 | + //trim the whitespace from the line |
|
| 1377 | + $line = trim($line); |
|
| 1378 | + |
|
| 1379 | + if (empty($line)) { |
|
| 1380 | + return false; //Empty line |
|
| 1381 | + } |
|
| 1382 | + |
|
| 1383 | + if (strpos($line, '@') === 0) { |
|
| 1384 | + if (strpos($line, ' ') > 0) { |
|
| 1385 | + //Get the parameter name |
|
| 1386 | + $param = substr($line, 1, strpos($line, ' ') - 1); |
|
| 1387 | + $value = substr($line, strlen($param) + 2); //Get the value |
|
| 1388 | + } else { |
|
| 1389 | + $param = substr($line, 1); |
|
| 1390 | + $value = ''; |
|
| 1391 | + } |
|
| 1392 | + //Parse the line and return false if the parameter is valid |
|
| 1393 | + if ($this->setParam($param, $value)) { |
|
| 1394 | + return false; |
|
| 1395 | + } |
|
| 1396 | + } |
|
| 1397 | + return $line; |
|
| 1398 | + } |
|
| 1399 | + |
|
| 1400 | + |
|
| 1401 | + private function setParam($param, $value) |
|
| 1402 | + { |
|
| 1403 | + if ($param == 'param' || $param == 'return') { |
|
| 1404 | + $value = $this->formatParamOrReturn($value); |
|
| 1405 | + } |
|
| 1406 | + if ($param == 'class') { |
|
| 1407 | + list($param, $value) = $this->formatClass($value); |
|
| 1408 | + } |
|
| 1409 | + |
|
| 1410 | + if (empty($this->params[$param])) { |
|
| 1411 | + $this->params[$param] = $value; |
|
| 1412 | + } else if ($param == 'param') { |
|
| 1413 | + $arr = array($this->params[$param], $value); |
|
| 1414 | + $this->params[$param] = $arr; |
|
| 1415 | + } else { |
|
| 1416 | + $this->params[$param] = $value + $this->params[$param]; |
|
| 1417 | + } |
|
| 1418 | + return true; |
|
| 1419 | + } |
|
| 1420 | + |
|
| 1421 | + |
|
| 1422 | + private function formatClass($value) |
|
| 1423 | + { |
|
| 1424 | + $r = preg_split("[\(|\)]", $value); |
|
| 1425 | + if (count($r) > 1) { |
|
| 1426 | + $param = $r[0]; |
|
| 1427 | + parse_str($r[1], $value); |
|
| 1428 | + foreach ($value as $key => $val) { |
|
| 1429 | + $val = explode(',', $val); |
|
| 1430 | + if (count($val) > 1) { |
|
| 1431 | + $value[$key] = $val; |
|
| 1432 | + } |
|
| 1433 | + } |
|
| 1434 | + } else { |
|
| 1435 | + $param = 'Unknown'; |
|
| 1436 | + } |
|
| 1437 | + return array($param, $value); |
|
| 1438 | + } |
|
| 1439 | + |
|
| 1440 | + |
|
| 1441 | + private function formatParamOrReturn($string) |
|
| 1442 | + { |
|
| 1443 | + $pos = strpos($string, ' '); |
|
| 1444 | + $type = substr($string, 0, $pos); |
|
| 1445 | + return '(' . $type . ')' . substr($string, $pos + 1); |
|
| 1446 | + } |
|
| 1447 | 1447 | |
| 1448 | 1448 | } |
| 1449 | 1449 | |
@@ -1456,30 +1456,30 @@ discard block |
||
| 1456 | 1456 | |
| 1457 | 1457 | function parse_doc($php_doc_comment) |
| 1458 | 1458 | { |
| 1459 | - $p = new DocParser(); |
|
| 1460 | - return $p->parse($php_doc_comment); |
|
| 1459 | + $p = new DocParser(); |
|
| 1460 | + return $p->parse($php_doc_comment); |
|
| 1461 | 1461 | |
| 1462 | - $p = new Parser($php_doc_comment); |
|
| 1463 | - return $p; |
|
| 1462 | + $p = new Parser($php_doc_comment); |
|
| 1463 | + return $p; |
|
| 1464 | 1464 | |
| 1465 | - $php_doc_comment = preg_replace( |
|
| 1466 | - "/(^[\\s]*\\/\\*\\*) |
|
| 1465 | + $php_doc_comment = preg_replace( |
|
| 1466 | + "/(^[\\s]*\\/\\*\\*) |
|
| 1467 | 1467 | |(^[\\s]\\*\\/) |
| 1468 | 1468 | |(^[\\s]*\\*?\\s) |
| 1469 | 1469 | |(^[\\s]*) |
| 1470 | 1470 | |(^[\\t]*)/ixm", |
| 1471 | - "", $php_doc_comment); |
|
| 1472 | - $php_doc_comment = str_replace("\r", "", $php_doc_comment); |
|
| 1473 | - $php_doc_comment = preg_replace("/([\\t])+/", "\t", $php_doc_comment); |
|
| 1474 | - return explode("\n", $php_doc_comment); |
|
| 1475 | - |
|
| 1476 | - $php_doc_comment = trim(preg_replace('/\r?\n *\* */', ' ', |
|
| 1477 | - $php_doc_comment)); |
|
| 1478 | - return $php_doc_comment; |
|
| 1479 | - |
|
| 1480 | - preg_match_all('/@([a-z]+)\s+(.*?)\s*(?=$|@[a-z]+\s)/s', $php_doc_comment, |
|
| 1481 | - $matches); |
|
| 1482 | - return array_combine($matches[1], $matches[2]); |
|
| 1471 | + "", $php_doc_comment); |
|
| 1472 | + $php_doc_comment = str_replace("\r", "", $php_doc_comment); |
|
| 1473 | + $php_doc_comment = preg_replace("/([\\t])+/", "\t", $php_doc_comment); |
|
| 1474 | + return explode("\n", $php_doc_comment); |
|
| 1475 | + |
|
| 1476 | + $php_doc_comment = trim(preg_replace('/\r?\n *\* */', ' ', |
|
| 1477 | + $php_doc_comment)); |
|
| 1478 | + return $php_doc_comment; |
|
| 1479 | + |
|
| 1480 | + preg_match_all('/@([a-z]+)\s+(.*?)\s*(?=$|@[a-z]+\s)/s', $php_doc_comment, |
|
| 1481 | + $matches); |
|
| 1482 | + return array_combine($matches[1], $matches[2]); |
|
| 1483 | 1483 | } |
| 1484 | 1484 | |
| 1485 | 1485 | |
@@ -1498,21 +1498,21 @@ discard block |
||
| 1498 | 1498 | */ |
| 1499 | 1499 | function object_to_array($object, $utf_encode = false) |
| 1500 | 1500 | { |
| 1501 | - if (is_array($object) |
|
| 1502 | - || (is_object($object) |
|
| 1503 | - && !($object instanceof JsonSerializable)) |
|
| 1504 | - ) { |
|
| 1505 | - $array = array(); |
|
| 1506 | - foreach ($object as $key => $value) { |
|
| 1507 | - $value = object_to_array($value, $utf_encode); |
|
| 1508 | - if ($utf_encode && is_string($value)) { |
|
| 1509 | - $value = utf8_encode($value); |
|
| 1510 | - } |
|
| 1511 | - $array[$key] = $value; |
|
| 1512 | - } |
|
| 1513 | - return $array; |
|
| 1514 | - } |
|
| 1515 | - return $object; |
|
| 1501 | + if (is_array($object) |
|
| 1502 | + || (is_object($object) |
|
| 1503 | + && !($object instanceof JsonSerializable)) |
|
| 1504 | + ) { |
|
| 1505 | + $array = array(); |
|
| 1506 | + foreach ($object as $key => $value) { |
|
| 1507 | + $value = object_to_array($value, $utf_encode); |
|
| 1508 | + if ($utf_encode && is_string($value)) { |
|
| 1509 | + $value = utf8_encode($value); |
|
| 1510 | + } |
|
| 1511 | + $array[$key] = $value; |
|
| 1512 | + } |
|
| 1513 | + return $array; |
|
| 1514 | + } |
|
| 1515 | + return $object; |
|
| 1516 | 1516 | } |
| 1517 | 1517 | |
| 1518 | 1518 | |
@@ -1522,21 +1522,21 @@ discard block |
||
| 1522 | 1522 | */ |
| 1523 | 1523 | function autoload_formats($class_name) |
| 1524 | 1524 | { |
| 1525 | - $class_name = strtolower($class_name); |
|
| 1525 | + $class_name = strtolower($class_name); |
|
| 1526 | 1526 | |
| 1527 | - $file = RESTLER_PATH . "../../../api/mobile_services/$class_name.php"; |
|
| 1528 | - if (file_exists($file)) { |
|
| 1529 | - require_once ($file); |
|
| 1530 | - } else { |
|
| 1527 | + $file = RESTLER_PATH . "../../../api/mobile_services/$class_name.php"; |
|
| 1528 | + if (file_exists($file)) { |
|
| 1529 | + require_once ($file); |
|
| 1530 | + } else { |
|
| 1531 | 1531 | $file = RESTLER_PATH . "/../../api/mobile_services/$class_name.php"; |
| 1532 | - if (file_exists($file)) { |
|
| 1533 | - require_once ($file); |
|
| 1534 | - } elseif (file_exists(RESTLER_PATH . "/../api/mobile_services/$class_name.php")) { |
|
| 1535 | - require_once ("/../api/mobile_services/$class_name.php"); |
|
| 1536 | - } elseif (file_exists("$class_name.php")) { |
|
| 1537 | - require_once ("$class_name.php"); |
|
| 1538 | - } |
|
| 1539 | - } |
|
| 1532 | + if (file_exists($file)) { |
|
| 1533 | + require_once ($file); |
|
| 1534 | + } elseif (file_exists(RESTLER_PATH . "/../api/mobile_services/$class_name.php")) { |
|
| 1535 | + require_once ("/../api/mobile_services/$class_name.php"); |
|
| 1536 | + } elseif (file_exists("$class_name.php")) { |
|
| 1537 | + require_once ("$class_name.php"); |
|
| 1538 | + } |
|
| 1539 | + } |
|
| 1540 | 1540 | } |
| 1541 | 1541 | |
| 1542 | 1542 | // ================================================================== |
@@ -1553,10 +1553,10 @@ discard block |
||
| 1553 | 1553 | if (!function_exists('isRestlerCompatibilityModeEnabled')) { |
| 1554 | 1554 | |
| 1555 | 1555 | |
| 1556 | - public function isRestlerCompatibilityModeEnabled() |
|
| 1557 | - { |
|
| 1558 | - return false; |
|
| 1559 | - } |
|
| 1556 | + public function isRestlerCompatibilityModeEnabled() |
|
| 1557 | + { |
|
| 1558 | + return false; |
|
| 1559 | + } |
|
| 1560 | 1560 | |
| 1561 | 1561 | } |
| 1562 | 1562 | define('RESTLER_PATH', dirname(__FILE__)); |
| 1563 | 1563 | \ No newline at end of file |
@@ -14,218 +14,218 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class RequestDecorator implements RequestInterface { |
| 16 | 16 | |
| 17 | - use MessageDecoratorTrait; |
|
| 17 | + use MessageDecoratorTrait; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param RequestInterface $inner |
|
| 23 | - */ |
|
| 24 | - public function __construct(RequestInterface $inner) { |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param RequestInterface $inner |
|
| 23 | + */ |
|
| 24 | + public function __construct(RequestInterface $inner) { |
|
| 25 | 25 | |
| 26 | - $this->inner = $inner; |
|
| 26 | + $this->inner = $inner; |
|
| 27 | 27 | |
| 28 | - } |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Returns the current HTTP method |
|
| 32 | - * |
|
| 33 | - * @return string |
|
| 34 | - */ |
|
| 35 | - public function getMethod() { |
|
| 36 | - |
|
| 37 | - return $this->inner->getMethod(); |
|
| 38 | - |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Sets the HTTP method |
|
| 43 | - * |
|
| 44 | - * @param string $method |
|
| 45 | - * @return void |
|
| 46 | - */ |
|
| 47 | - public function setMethod($method) { |
|
| 48 | - |
|
| 49 | - $this->inner->setMethod($method); |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Returns the request url. |
|
| 55 | - * |
|
| 56 | - * @return string |
|
| 57 | - */ |
|
| 58 | - public function getUrl() { |
|
| 59 | - |
|
| 60 | - return $this->inner->getUrl(); |
|
| 61 | - |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Sets the request url. |
|
| 66 | - * |
|
| 67 | - * @param string $url |
|
| 68 | - * @return void |
|
| 69 | - */ |
|
| 70 | - public function setUrl($url) { |
|
| 71 | - |
|
| 72 | - $this->inner->setUrl($url); |
|
| 73 | - |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Returns the absolute url. |
|
| 78 | - * |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - public function getAbsoluteUrl() { |
|
| 82 | - |
|
| 83 | - return $this->inner->getAbsoluteUrl(); |
|
| 84 | - |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Sets the absolute url. |
|
| 89 | - * |
|
| 90 | - * @param string $url |
|
| 91 | - * @return void |
|
| 92 | - */ |
|
| 93 | - public function setAbsoluteUrl($url) { |
|
| 94 | - |
|
| 95 | - $this->inner->setAbsoluteUrl($url); |
|
| 96 | - |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Returns the current base url. |
|
| 101 | - * |
|
| 102 | - * @return string |
|
| 103 | - */ |
|
| 104 | - public function getBaseUrl() { |
|
| 105 | - |
|
| 106 | - return $this->inner->getBaseUrl(); |
|
| 107 | - |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Sets a base url. |
|
| 112 | - * |
|
| 113 | - * This url is used for relative path calculations. |
|
| 114 | - * |
|
| 115 | - * The base url should default to / |
|
| 116 | - * |
|
| 117 | - * @param string $url |
|
| 118 | - * @return void |
|
| 119 | - */ |
|
| 120 | - public function setBaseUrl($url) { |
|
| 121 | - |
|
| 122 | - $this->inner->setBaseUrl($url); |
|
| 123 | - |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Returns the relative path. |
|
| 128 | - * |
|
| 129 | - * This is being calculated using the base url. This path will not start |
|
| 130 | - * with a slash, so it will always return something like |
|
| 131 | - * 'example/path.html'. |
|
| 132 | - * |
|
| 133 | - * If the full path is equal to the base url, this method will return an |
|
| 134 | - * empty string. |
|
| 135 | - * |
|
| 136 | - * This method will also urldecode the path, and if the url was incoded as |
|
| 137 | - * ISO-8859-1, it will convert it to UTF-8. |
|
| 138 | - * |
|
| 139 | - * If the path is outside of the base url, a LogicException will be thrown. |
|
| 140 | - * |
|
| 141 | - * @return string |
|
| 142 | - */ |
|
| 143 | - public function getPath() { |
|
| 144 | - |
|
| 145 | - return $this->inner->getPath(); |
|
| 146 | - |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Returns the list of query parameters. |
|
| 151 | - * |
|
| 152 | - * This is equivalent to PHP's $_GET superglobal. |
|
| 153 | - * |
|
| 154 | - * @return array |
|
| 155 | - */ |
|
| 156 | - public function getQueryParameters() { |
|
| 157 | - |
|
| 158 | - return $this->inner->getQueryParameters(); |
|
| 159 | - |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns the POST data. |
|
| 164 | - * |
|
| 165 | - * This is equivalent to PHP's $_POST superglobal. |
|
| 166 | - * |
|
| 167 | - * @return array |
|
| 168 | - */ |
|
| 169 | - public function getPostData() { |
|
| 170 | - |
|
| 171 | - return $this->inner->getPostData(); |
|
| 172 | - |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Sets the post data. |
|
| 177 | - * |
|
| 178 | - * This is equivalent to PHP's $_POST superglobal. |
|
| 179 | - * |
|
| 180 | - * This would not have been needed, if POST data was accessible as |
|
| 181 | - * php://input, but unfortunately we need to special case it. |
|
| 182 | - * |
|
| 183 | - * @param array $postData |
|
| 184 | - * @return void |
|
| 185 | - */ |
|
| 186 | - public function setPostData(array $postData) { |
|
| 187 | - |
|
| 188 | - $this->inner->setPostData($postData); |
|
| 189 | - |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Returns an item from the _SERVER array. |
|
| 195 | - * |
|
| 196 | - * If the value does not exist in the array, null is returned. |
|
| 197 | - * |
|
| 198 | - * @param string $valueName |
|
| 199 | - * @return string|null |
|
| 200 | - */ |
|
| 201 | - public function getRawServerValue($valueName) { |
|
| 202 | - |
|
| 203 | - return $this->inner->getRawServerValue($valueName); |
|
| 204 | - |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Sets the _SERVER array. |
|
| 209 | - * |
|
| 210 | - * @param array $data |
|
| 211 | - * @return void |
|
| 212 | - */ |
|
| 213 | - public function setRawServerData(array $data) { |
|
| 214 | - |
|
| 215 | - $this->inner->setRawServerData($data); |
|
| 216 | - |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Serializes the request object as a string. |
|
| 221 | - * |
|
| 222 | - * This is useful for debugging purposes. |
|
| 223 | - * |
|
| 224 | - * @return string |
|
| 225 | - */ |
|
| 226 | - public function __toString() { |
|
| 227 | - |
|
| 228 | - return $this->inner->__toString(); |
|
| 229 | - |
|
| 230 | - } |
|
| 30 | + /** |
|
| 31 | + * Returns the current HTTP method |
|
| 32 | + * |
|
| 33 | + * @return string |
|
| 34 | + */ |
|
| 35 | + public function getMethod() { |
|
| 36 | + |
|
| 37 | + return $this->inner->getMethod(); |
|
| 38 | + |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Sets the HTTP method |
|
| 43 | + * |
|
| 44 | + * @param string $method |
|
| 45 | + * @return void |
|
| 46 | + */ |
|
| 47 | + public function setMethod($method) { |
|
| 48 | + |
|
| 49 | + $this->inner->setMethod($method); |
|
| 50 | + |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Returns the request url. |
|
| 55 | + * |
|
| 56 | + * @return string |
|
| 57 | + */ |
|
| 58 | + public function getUrl() { |
|
| 59 | + |
|
| 60 | + return $this->inner->getUrl(); |
|
| 61 | + |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Sets the request url. |
|
| 66 | + * |
|
| 67 | + * @param string $url |
|
| 68 | + * @return void |
|
| 69 | + */ |
|
| 70 | + public function setUrl($url) { |
|
| 71 | + |
|
| 72 | + $this->inner->setUrl($url); |
|
| 73 | + |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Returns the absolute url. |
|
| 78 | + * |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + public function getAbsoluteUrl() { |
|
| 82 | + |
|
| 83 | + return $this->inner->getAbsoluteUrl(); |
|
| 84 | + |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Sets the absolute url. |
|
| 89 | + * |
|
| 90 | + * @param string $url |
|
| 91 | + * @return void |
|
| 92 | + */ |
|
| 93 | + public function setAbsoluteUrl($url) { |
|
| 94 | + |
|
| 95 | + $this->inner->setAbsoluteUrl($url); |
|
| 96 | + |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Returns the current base url. |
|
| 101 | + * |
|
| 102 | + * @return string |
|
| 103 | + */ |
|
| 104 | + public function getBaseUrl() { |
|
| 105 | + |
|
| 106 | + return $this->inner->getBaseUrl(); |
|
| 107 | + |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Sets a base url. |
|
| 112 | + * |
|
| 113 | + * This url is used for relative path calculations. |
|
| 114 | + * |
|
| 115 | + * The base url should default to / |
|
| 116 | + * |
|
| 117 | + * @param string $url |
|
| 118 | + * @return void |
|
| 119 | + */ |
|
| 120 | + public function setBaseUrl($url) { |
|
| 121 | + |
|
| 122 | + $this->inner->setBaseUrl($url); |
|
| 123 | + |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Returns the relative path. |
|
| 128 | + * |
|
| 129 | + * This is being calculated using the base url. This path will not start |
|
| 130 | + * with a slash, so it will always return something like |
|
| 131 | + * 'example/path.html'. |
|
| 132 | + * |
|
| 133 | + * If the full path is equal to the base url, this method will return an |
|
| 134 | + * empty string. |
|
| 135 | + * |
|
| 136 | + * This method will also urldecode the path, and if the url was incoded as |
|
| 137 | + * ISO-8859-1, it will convert it to UTF-8. |
|
| 138 | + * |
|
| 139 | + * If the path is outside of the base url, a LogicException will be thrown. |
|
| 140 | + * |
|
| 141 | + * @return string |
|
| 142 | + */ |
|
| 143 | + public function getPath() { |
|
| 144 | + |
|
| 145 | + return $this->inner->getPath(); |
|
| 146 | + |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Returns the list of query parameters. |
|
| 151 | + * |
|
| 152 | + * This is equivalent to PHP's $_GET superglobal. |
|
| 153 | + * |
|
| 154 | + * @return array |
|
| 155 | + */ |
|
| 156 | + public function getQueryParameters() { |
|
| 157 | + |
|
| 158 | + return $this->inner->getQueryParameters(); |
|
| 159 | + |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns the POST data. |
|
| 164 | + * |
|
| 165 | + * This is equivalent to PHP's $_POST superglobal. |
|
| 166 | + * |
|
| 167 | + * @return array |
|
| 168 | + */ |
|
| 169 | + public function getPostData() { |
|
| 170 | + |
|
| 171 | + return $this->inner->getPostData(); |
|
| 172 | + |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Sets the post data. |
|
| 177 | + * |
|
| 178 | + * This is equivalent to PHP's $_POST superglobal. |
|
| 179 | + * |
|
| 180 | + * This would not have been needed, if POST data was accessible as |
|
| 181 | + * php://input, but unfortunately we need to special case it. |
|
| 182 | + * |
|
| 183 | + * @param array $postData |
|
| 184 | + * @return void |
|
| 185 | + */ |
|
| 186 | + public function setPostData(array $postData) { |
|
| 187 | + |
|
| 188 | + $this->inner->setPostData($postData); |
|
| 189 | + |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Returns an item from the _SERVER array. |
|
| 195 | + * |
|
| 196 | + * If the value does not exist in the array, null is returned. |
|
| 197 | + * |
|
| 198 | + * @param string $valueName |
|
| 199 | + * @return string|null |
|
| 200 | + */ |
|
| 201 | + public function getRawServerValue($valueName) { |
|
| 202 | + |
|
| 203 | + return $this->inner->getRawServerValue($valueName); |
|
| 204 | + |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Sets the _SERVER array. |
|
| 209 | + * |
|
| 210 | + * @param array $data |
|
| 211 | + * @return void |
|
| 212 | + */ |
|
| 213 | + public function setRawServerData(array $data) { |
|
| 214 | + |
|
| 215 | + $this->inner->setRawServerData($data); |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Serializes the request object as a string. |
|
| 221 | + * |
|
| 222 | + * This is useful for debugging purposes. |
|
| 223 | + * |
|
| 224 | + * @return string |
|
| 225 | + */ |
|
| 226 | + public function __toString() { |
|
| 227 | + |
|
| 228 | + return $this->inner->__toString(); |
|
| 229 | + |
|
| 230 | + } |
|
| 231 | 231 | } |
@@ -11,184 +11,184 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Response extends Message implements ResponseInterface { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * This is the list of currently registered HTTP status codes. |
|
| 16 | - * |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - static $statusCodes = [ |
|
| 20 | - 100 => 'Continue', |
|
| 21 | - 101 => 'Switching Protocols', |
|
| 22 | - 102 => 'Processing', |
|
| 23 | - 200 => 'OK', |
|
| 24 | - 201 => 'Created', |
|
| 25 | - 202 => 'Accepted', |
|
| 26 | - 203 => 'Non-Authorative Information', |
|
| 27 | - 204 => 'No Content', |
|
| 28 | - 205 => 'Reset Content', |
|
| 29 | - 206 => 'Partial Content', |
|
| 30 | - 207 => 'Multi-Status', // RFC 4918 |
|
| 31 | - 208 => 'Already Reported', // RFC 5842 |
|
| 32 | - 226 => 'IM Used', // RFC 3229 |
|
| 33 | - 300 => 'Multiple Choices', |
|
| 34 | - 301 => 'Moved Permanently', |
|
| 35 | - 302 => 'Found', |
|
| 36 | - 303 => 'See Other', |
|
| 37 | - 304 => 'Not Modified', |
|
| 38 | - 305 => 'Use Proxy', |
|
| 39 | - 307 => 'Temporary Redirect', |
|
| 40 | - 308 => 'Permanent Redirect', |
|
| 41 | - 400 => 'Bad Request', |
|
| 42 | - 401 => 'Unauthorized', |
|
| 43 | - 402 => 'Payment Required', |
|
| 44 | - 403 => 'Forbidden', |
|
| 45 | - 404 => 'Not Found', |
|
| 46 | - 405 => 'Method Not Allowed', |
|
| 47 | - 406 => 'Not Acceptable', |
|
| 48 | - 407 => 'Proxy Authentication Required', |
|
| 49 | - 408 => 'Request Timeout', |
|
| 50 | - 409 => 'Conflict', |
|
| 51 | - 410 => 'Gone', |
|
| 52 | - 411 => 'Length Required', |
|
| 53 | - 412 => 'Precondition failed', |
|
| 54 | - 413 => 'Request Entity Too Large', |
|
| 55 | - 414 => 'Request-URI Too Long', |
|
| 56 | - 415 => 'Unsupported Media Type', |
|
| 57 | - 416 => 'Requested Range Not Satisfiable', |
|
| 58 | - 417 => 'Expectation Failed', |
|
| 59 | - 418 => 'I\'m a teapot', // RFC 2324 |
|
| 60 | - 421 => 'Misdirected Request', // RFC7540 (HTTP/2) |
|
| 61 | - 422 => 'Unprocessable Entity', // RFC 4918 |
|
| 62 | - 423 => 'Locked', // RFC 4918 |
|
| 63 | - 424 => 'Failed Dependency', // RFC 4918 |
|
| 64 | - 426 => 'Upgrade Required', |
|
| 65 | - 428 => 'Precondition Required', // RFC 6585 |
|
| 66 | - 429 => 'Too Many Requests', // RFC 6585 |
|
| 67 | - 431 => 'Request Header Fields Too Large', // RFC 6585 |
|
| 68 | - 451 => 'Unavailable For Legal Reasons', // draft-tbray-http-legally-restricted-status |
|
| 69 | - 500 => 'Internal Server Error', |
|
| 70 | - 501 => 'Not Implemented', |
|
| 71 | - 502 => 'Bad Gateway', |
|
| 72 | - 503 => 'Service Unavailable', |
|
| 73 | - 504 => 'Gateway Timeout', |
|
| 74 | - 505 => 'HTTP Version not supported', |
|
| 75 | - 506 => 'Variant Also Negotiates', |
|
| 76 | - 507 => 'Insufficient Storage', // RFC 4918 |
|
| 77 | - 508 => 'Loop Detected', // RFC 5842 |
|
| 78 | - 509 => 'Bandwidth Limit Exceeded', // non-standard |
|
| 79 | - 510 => 'Not extended', |
|
| 80 | - 511 => 'Network Authentication Required', // RFC 6585 |
|
| 81 | - ]; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * HTTP status code |
|
| 85 | - * |
|
| 86 | - * @var int |
|
| 87 | - */ |
|
| 88 | - protected $status; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * HTTP status text |
|
| 92 | - * |
|
| 93 | - * @var string |
|
| 94 | - */ |
|
| 95 | - protected $statusText; |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Creates the response object |
|
| 99 | - * |
|
| 100 | - * @param string|int $status |
|
| 101 | - * @param array $headers |
|
| 102 | - * @param resource $body |
|
| 103 | - * @return void |
|
| 104 | - */ |
|
| 105 | - public function __construct($status = null, array $headers = null, $body = null) { |
|
| 106 | - |
|
| 107 | - if (!is_null($status)) $this->setStatus($status); |
|
| 108 | - if (!is_null($headers)) $this->setHeaders($headers); |
|
| 109 | - if (!is_null($body)) $this->setBody($body); |
|
| 110 | - |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Returns the current HTTP status code. |
|
| 116 | - * |
|
| 117 | - * @return int |
|
| 118 | - */ |
|
| 119 | - public function getStatus() { |
|
| 120 | - |
|
| 121 | - return $this->status; |
|
| 122 | - |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Returns the human-readable status string. |
|
| 127 | - * |
|
| 128 | - * In the case of a 200, this may for example be 'OK'. |
|
| 129 | - * |
|
| 130 | - * @return string |
|
| 131 | - */ |
|
| 132 | - public function getStatusText() { |
|
| 133 | - |
|
| 134 | - return $this->statusText; |
|
| 135 | - |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Sets the HTTP status code. |
|
| 140 | - * |
|
| 141 | - * This can be either the full HTTP status code with human readable string, |
|
| 142 | - * for example: "403 I can't let you do that, Dave". |
|
| 143 | - * |
|
| 144 | - * Or just the code, in which case the appropriate default message will be |
|
| 145 | - * added. |
|
| 146 | - * |
|
| 147 | - * @param string|int $status |
|
| 148 | - * @throws \InvalidArgumentExeption |
|
| 149 | - * @return void |
|
| 150 | - */ |
|
| 151 | - public function setStatus($status) { |
|
| 152 | - |
|
| 153 | - if (ctype_digit($status) || is_int($status)) { |
|
| 154 | - |
|
| 155 | - $statusCode = $status; |
|
| 156 | - $statusText = isset(self::$statusCodes[$status]) ? self::$statusCodes[$status] : 'Unknown'; |
|
| 157 | - |
|
| 158 | - } else { |
|
| 159 | - list( |
|
| 160 | - $statusCode, |
|
| 161 | - $statusText |
|
| 162 | - ) = explode(' ', $status, 2); |
|
| 163 | - } |
|
| 164 | - if ($statusCode < 100 || $statusCode > 999) { |
|
| 165 | - throw new \InvalidArgumentException('The HTTP status code must be exactly 3 digits'); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $this->status = $statusCode; |
|
| 169 | - $this->statusText = $statusText; |
|
| 170 | - |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Serializes the response object as a string. |
|
| 175 | - * |
|
| 176 | - * This is useful for debugging purposes. |
|
| 177 | - * |
|
| 178 | - * @return string |
|
| 179 | - */ |
|
| 180 | - public function __toString() { |
|
| 181 | - |
|
| 182 | - $str = 'HTTP/' . $this->httpVersion . ' ' . $this->getStatus() . ' ' . $this->getStatusText() . "\r\n"; |
|
| 183 | - foreach ($this->getHeaders() as $key => $value) { |
|
| 184 | - foreach ($value as $v) { |
|
| 185 | - $str .= $key . ": " . $v . "\r\n"; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - $str .= "\r\n"; |
|
| 189 | - $str .= $this->getBodyAsString(); |
|
| 190 | - return $str; |
|
| 191 | - |
|
| 192 | - } |
|
| 14 | + /** |
|
| 15 | + * This is the list of currently registered HTTP status codes. |
|
| 16 | + * |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + static $statusCodes = [ |
|
| 20 | + 100 => 'Continue', |
|
| 21 | + 101 => 'Switching Protocols', |
|
| 22 | + 102 => 'Processing', |
|
| 23 | + 200 => 'OK', |
|
| 24 | + 201 => 'Created', |
|
| 25 | + 202 => 'Accepted', |
|
| 26 | + 203 => 'Non-Authorative Information', |
|
| 27 | + 204 => 'No Content', |
|
| 28 | + 205 => 'Reset Content', |
|
| 29 | + 206 => 'Partial Content', |
|
| 30 | + 207 => 'Multi-Status', // RFC 4918 |
|
| 31 | + 208 => 'Already Reported', // RFC 5842 |
|
| 32 | + 226 => 'IM Used', // RFC 3229 |
|
| 33 | + 300 => 'Multiple Choices', |
|
| 34 | + 301 => 'Moved Permanently', |
|
| 35 | + 302 => 'Found', |
|
| 36 | + 303 => 'See Other', |
|
| 37 | + 304 => 'Not Modified', |
|
| 38 | + 305 => 'Use Proxy', |
|
| 39 | + 307 => 'Temporary Redirect', |
|
| 40 | + 308 => 'Permanent Redirect', |
|
| 41 | + 400 => 'Bad Request', |
|
| 42 | + 401 => 'Unauthorized', |
|
| 43 | + 402 => 'Payment Required', |
|
| 44 | + 403 => 'Forbidden', |
|
| 45 | + 404 => 'Not Found', |
|
| 46 | + 405 => 'Method Not Allowed', |
|
| 47 | + 406 => 'Not Acceptable', |
|
| 48 | + 407 => 'Proxy Authentication Required', |
|
| 49 | + 408 => 'Request Timeout', |
|
| 50 | + 409 => 'Conflict', |
|
| 51 | + 410 => 'Gone', |
|
| 52 | + 411 => 'Length Required', |
|
| 53 | + 412 => 'Precondition failed', |
|
| 54 | + 413 => 'Request Entity Too Large', |
|
| 55 | + 414 => 'Request-URI Too Long', |
|
| 56 | + 415 => 'Unsupported Media Type', |
|
| 57 | + 416 => 'Requested Range Not Satisfiable', |
|
| 58 | + 417 => 'Expectation Failed', |
|
| 59 | + 418 => 'I\'m a teapot', // RFC 2324 |
|
| 60 | + 421 => 'Misdirected Request', // RFC7540 (HTTP/2) |
|
| 61 | + 422 => 'Unprocessable Entity', // RFC 4918 |
|
| 62 | + 423 => 'Locked', // RFC 4918 |
|
| 63 | + 424 => 'Failed Dependency', // RFC 4918 |
|
| 64 | + 426 => 'Upgrade Required', |
|
| 65 | + 428 => 'Precondition Required', // RFC 6585 |
|
| 66 | + 429 => 'Too Many Requests', // RFC 6585 |
|
| 67 | + 431 => 'Request Header Fields Too Large', // RFC 6585 |
|
| 68 | + 451 => 'Unavailable For Legal Reasons', // draft-tbray-http-legally-restricted-status |
|
| 69 | + 500 => 'Internal Server Error', |
|
| 70 | + 501 => 'Not Implemented', |
|
| 71 | + 502 => 'Bad Gateway', |
|
| 72 | + 503 => 'Service Unavailable', |
|
| 73 | + 504 => 'Gateway Timeout', |
|
| 74 | + 505 => 'HTTP Version not supported', |
|
| 75 | + 506 => 'Variant Also Negotiates', |
|
| 76 | + 507 => 'Insufficient Storage', // RFC 4918 |
|
| 77 | + 508 => 'Loop Detected', // RFC 5842 |
|
| 78 | + 509 => 'Bandwidth Limit Exceeded', // non-standard |
|
| 79 | + 510 => 'Not extended', |
|
| 80 | + 511 => 'Network Authentication Required', // RFC 6585 |
|
| 81 | + ]; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * HTTP status code |
|
| 85 | + * |
|
| 86 | + * @var int |
|
| 87 | + */ |
|
| 88 | + protected $status; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * HTTP status text |
|
| 92 | + * |
|
| 93 | + * @var string |
|
| 94 | + */ |
|
| 95 | + protected $statusText; |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Creates the response object |
|
| 99 | + * |
|
| 100 | + * @param string|int $status |
|
| 101 | + * @param array $headers |
|
| 102 | + * @param resource $body |
|
| 103 | + * @return void |
|
| 104 | + */ |
|
| 105 | + public function __construct($status = null, array $headers = null, $body = null) { |
|
| 106 | + |
|
| 107 | + if (!is_null($status)) $this->setStatus($status); |
|
| 108 | + if (!is_null($headers)) $this->setHeaders($headers); |
|
| 109 | + if (!is_null($body)) $this->setBody($body); |
|
| 110 | + |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Returns the current HTTP status code. |
|
| 116 | + * |
|
| 117 | + * @return int |
|
| 118 | + */ |
|
| 119 | + public function getStatus() { |
|
| 120 | + |
|
| 121 | + return $this->status; |
|
| 122 | + |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Returns the human-readable status string. |
|
| 127 | + * |
|
| 128 | + * In the case of a 200, this may for example be 'OK'. |
|
| 129 | + * |
|
| 130 | + * @return string |
|
| 131 | + */ |
|
| 132 | + public function getStatusText() { |
|
| 133 | + |
|
| 134 | + return $this->statusText; |
|
| 135 | + |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Sets the HTTP status code. |
|
| 140 | + * |
|
| 141 | + * This can be either the full HTTP status code with human readable string, |
|
| 142 | + * for example: "403 I can't let you do that, Dave". |
|
| 143 | + * |
|
| 144 | + * Or just the code, in which case the appropriate default message will be |
|
| 145 | + * added. |
|
| 146 | + * |
|
| 147 | + * @param string|int $status |
|
| 148 | + * @throws \InvalidArgumentExeption |
|
| 149 | + * @return void |
|
| 150 | + */ |
|
| 151 | + public function setStatus($status) { |
|
| 152 | + |
|
| 153 | + if (ctype_digit($status) || is_int($status)) { |
|
| 154 | + |
|
| 155 | + $statusCode = $status; |
|
| 156 | + $statusText = isset(self::$statusCodes[$status]) ? self::$statusCodes[$status] : 'Unknown'; |
|
| 157 | + |
|
| 158 | + } else { |
|
| 159 | + list( |
|
| 160 | + $statusCode, |
|
| 161 | + $statusText |
|
| 162 | + ) = explode(' ', $status, 2); |
|
| 163 | + } |
|
| 164 | + if ($statusCode < 100 || $statusCode > 999) { |
|
| 165 | + throw new \InvalidArgumentException('The HTTP status code must be exactly 3 digits'); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $this->status = $statusCode; |
|
| 169 | + $this->statusText = $statusText; |
|
| 170 | + |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Serializes the response object as a string. |
|
| 175 | + * |
|
| 176 | + * This is useful for debugging purposes. |
|
| 177 | + * |
|
| 178 | + * @return string |
|
| 179 | + */ |
|
| 180 | + public function __toString() { |
|
| 181 | + |
|
| 182 | + $str = 'HTTP/' . $this->httpVersion . ' ' . $this->getStatus() . ' ' . $this->getStatusText() . "\r\n"; |
|
| 183 | + foreach ($this->getHeaders() as $key => $value) { |
|
| 184 | + foreach ($value as $v) { |
|
| 185 | + $str .= $key . ": " . $v . "\r\n"; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + $str .= "\r\n"; |
|
| 189 | + $str .= $this->getBodyAsString(); |
|
| 190 | + return $str; |
|
| 191 | + |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | 194 | } |
@@ -14,45 +14,45 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ClientHttpException extends \Exception implements HttpException { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Response object |
|
| 19 | - * |
|
| 20 | - * @var ResponseInterface |
|
| 21 | - */ |
|
| 22 | - protected $response; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Constructor |
|
| 26 | - * |
|
| 27 | - * @param ResponseInterface $response |
|
| 28 | - */ |
|
| 29 | - public function __construct(ResponseInterface $response) { |
|
| 30 | - |
|
| 31 | - $this->response = $response; |
|
| 32 | - parent::__construct($response->getStatusText(), $response->getStatus()); |
|
| 33 | - |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The http status code for the error. |
|
| 38 | - * |
|
| 39 | - * @return int |
|
| 40 | - */ |
|
| 41 | - public function getHttpStatus() { |
|
| 42 | - |
|
| 43 | - return $this->response->getStatus(); |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Returns the full response object. |
|
| 49 | - * |
|
| 50 | - * @return ResponseInterface |
|
| 51 | - */ |
|
| 52 | - public function getResponse() { |
|
| 53 | - |
|
| 54 | - return $this->response; |
|
| 55 | - |
|
| 56 | - } |
|
| 17 | + /** |
|
| 18 | + * Response object |
|
| 19 | + * |
|
| 20 | + * @var ResponseInterface |
|
| 21 | + */ |
|
| 22 | + protected $response; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Constructor |
|
| 26 | + * |
|
| 27 | + * @param ResponseInterface $response |
|
| 28 | + */ |
|
| 29 | + public function __construct(ResponseInterface $response) { |
|
| 30 | + |
|
| 31 | + $this->response = $response; |
|
| 32 | + parent::__construct($response->getStatusText(), $response->getStatus()); |
|
| 33 | + |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The http status code for the error. |
|
| 38 | + * |
|
| 39 | + * @return int |
|
| 40 | + */ |
|
| 41 | + public function getHttpStatus() { |
|
| 42 | + |
|
| 43 | + return $this->response->getStatus(); |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Returns the full response object. |
|
| 49 | + * |
|
| 50 | + * @return ResponseInterface |
|
| 51 | + */ |
|
| 52 | + public function getResponse() { |
|
| 53 | + |
|
| 54 | + return $this->response; |
|
| 55 | + |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
@@ -14,71 +14,71 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ResponseDecorator implements ResponseInterface { |
| 16 | 16 | |
| 17 | - use MessageDecoratorTrait; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param ResponseInterface $inner |
|
| 23 | - */ |
|
| 24 | - public function __construct(ResponseInterface $inner) { |
|
| 25 | - |
|
| 26 | - $this->inner = $inner; |
|
| 27 | - |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Returns the current HTTP status code. |
|
| 32 | - * |
|
| 33 | - * @return int |
|
| 34 | - */ |
|
| 35 | - public function getStatus() { |
|
| 36 | - |
|
| 37 | - return $this->inner->getStatus(); |
|
| 38 | - |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Returns the human-readable status string. |
|
| 44 | - * |
|
| 45 | - * In the case of a 200, this may for example be 'OK'. |
|
| 46 | - * |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - public function getStatusText() { |
|
| 50 | - |
|
| 51 | - return $this->inner->getStatusText(); |
|
| 52 | - |
|
| 53 | - } |
|
| 54 | - /** |
|
| 55 | - * Sets the HTTP status code. |
|
| 56 | - * |
|
| 57 | - * This can be either the full HTTP status code with human readable string, |
|
| 58 | - * for example: "403 I can't let you do that, Dave". |
|
| 59 | - * |
|
| 60 | - * Or just the code, in which case the appropriate default message will be |
|
| 61 | - * added. |
|
| 62 | - * |
|
| 63 | - * @param string|int $status |
|
| 64 | - * @return void |
|
| 65 | - */ |
|
| 66 | - public function setStatus($status) { |
|
| 67 | - |
|
| 68 | - $this->inner->setStatus($status); |
|
| 69 | - |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Serializes the request object as a string. |
|
| 74 | - * |
|
| 75 | - * This is useful for debugging purposes. |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function __toString() { |
|
| 80 | - |
|
| 81 | - return $this->inner->__toString(); |
|
| 82 | - |
|
| 83 | - } |
|
| 17 | + use MessageDecoratorTrait; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param ResponseInterface $inner |
|
| 23 | + */ |
|
| 24 | + public function __construct(ResponseInterface $inner) { |
|
| 25 | + |
|
| 26 | + $this->inner = $inner; |
|
| 27 | + |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Returns the current HTTP status code. |
|
| 32 | + * |
|
| 33 | + * @return int |
|
| 34 | + */ |
|
| 35 | + public function getStatus() { |
|
| 36 | + |
|
| 37 | + return $this->inner->getStatus(); |
|
| 38 | + |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Returns the human-readable status string. |
|
| 44 | + * |
|
| 45 | + * In the case of a 200, this may for example be 'OK'. |
|
| 46 | + * |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + public function getStatusText() { |
|
| 50 | + |
|
| 51 | + return $this->inner->getStatusText(); |
|
| 52 | + |
|
| 53 | + } |
|
| 54 | + /** |
|
| 55 | + * Sets the HTTP status code. |
|
| 56 | + * |
|
| 57 | + * This can be either the full HTTP status code with human readable string, |
|
| 58 | + * for example: "403 I can't let you do that, Dave". |
|
| 59 | + * |
|
| 60 | + * Or just the code, in which case the appropriate default message will be |
|
| 61 | + * added. |
|
| 62 | + * |
|
| 63 | + * @param string|int $status |
|
| 64 | + * @return void |
|
| 65 | + */ |
|
| 66 | + public function setStatus($status) { |
|
| 67 | + |
|
| 68 | + $this->inner->setStatus($status); |
|
| 69 | + |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Serializes the request object as a string. |
|
| 74 | + * |
|
| 75 | + * This is useful for debugging purposes. |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function __toString() { |
|
| 80 | + |
|
| 81 | + return $this->inner->__toString(); |
|
| 82 | + |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -17,14 +17,14 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | interface HttpException { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * The http status code for the error. |
|
| 22 | - * |
|
| 23 | - * This may either be just the number, or a number and a human-readable |
|
| 24 | - * message, separated by a space. |
|
| 25 | - * |
|
| 26 | - * @return string|null |
|
| 27 | - */ |
|
| 28 | - public function getHttpStatus(); |
|
| 20 | + /** |
|
| 21 | + * The http status code for the error. |
|
| 22 | + * |
|
| 23 | + * This may either be just the number, or a number and a human-readable |
|
| 24 | + * message, separated by a space. |
|
| 25 | + * |
|
| 26 | + * @return string|null |
|
| 27 | + */ |
|
| 28 | + public function getHttpStatus(); |
|
| 29 | 29 | |
| 30 | 30 | } |
@@ -15,236 +15,236 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | trait MessageDecoratorTrait { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The inner request object. |
|
| 20 | - * |
|
| 21 | - * All method calls will be forwarded here. |
|
| 22 | - * |
|
| 23 | - * @var MessageInterface |
|
| 24 | - */ |
|
| 25 | - protected $inner; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Returns the body as a readable stream resource. |
|
| 29 | - * |
|
| 30 | - * Note that the stream may not be rewindable, and therefore may only be |
|
| 31 | - * read once. |
|
| 32 | - * |
|
| 33 | - * @return resource |
|
| 34 | - */ |
|
| 35 | - public function getBodyAsStream() { |
|
| 36 | - |
|
| 37 | - return $this->inner->getBodyAsStream(); |
|
| 38 | - |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Returns the body as a string. |
|
| 43 | - * |
|
| 44 | - * Note that because the underlying data may be based on a stream, this |
|
| 45 | - * method could only work correctly the first time. |
|
| 46 | - * |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - public function getBodyAsString() { |
|
| 50 | - |
|
| 51 | - return $this->inner->getBodyAsString(); |
|
| 52 | - |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Returns the message body, as it's internal representation. |
|
| 57 | - * |
|
| 58 | - * This could be either a string or a stream. |
|
| 59 | - * |
|
| 60 | - * @return resource|string |
|
| 61 | - */ |
|
| 62 | - public function getBody() { |
|
| 63 | - |
|
| 64 | - return $this->inner->getBody(); |
|
| 65 | - |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Updates the body resource with a new stream. |
|
| 70 | - * |
|
| 71 | - * @param resource $body |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - public function setBody($body) { |
|
| 75 | - |
|
| 76 | - $this->inner->setBody($body); |
|
| 77 | - |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Returns all the HTTP headers as an array. |
|
| 82 | - * |
|
| 83 | - * Every header is returned as an array, with one or more values. |
|
| 84 | - * |
|
| 85 | - * @return array |
|
| 86 | - */ |
|
| 87 | - public function getHeaders() { |
|
| 88 | - |
|
| 89 | - return $this->inner->getHeaders(); |
|
| 90 | - |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Will return true or false, depending on if a HTTP header exists. |
|
| 95 | - * |
|
| 96 | - * @param string $name |
|
| 97 | - * @return bool |
|
| 98 | - */ |
|
| 99 | - public function hasHeader($name) { |
|
| 100 | - |
|
| 101 | - return $this->inner->hasHeader($name); |
|
| 102 | - |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Returns a specific HTTP header, based on it's name. |
|
| 107 | - * |
|
| 108 | - * The name must be treated as case-insensitive. |
|
| 109 | - * If the header does not exist, this method must return null. |
|
| 110 | - * |
|
| 111 | - * If a header appeared more than once in a HTTP request, this method will |
|
| 112 | - * concatenate all the values with a comma. |
|
| 113 | - * |
|
| 114 | - * Note that this not make sense for all headers. Some, such as |
|
| 115 | - * `Set-Cookie` cannot be logically combined with a comma. In those cases |
|
| 116 | - * you *should* use getHeaderAsArray(). |
|
| 117 | - * |
|
| 118 | - * @param string $name |
|
| 119 | - * @return string|null |
|
| 120 | - */ |
|
| 121 | - public function getHeader($name) { |
|
| 122 | - |
|
| 123 | - return $this->inner->getHeader($name); |
|
| 124 | - |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Returns a HTTP header as an array. |
|
| 129 | - * |
|
| 130 | - * For every time the HTTP header appeared in the request or response, an |
|
| 131 | - * item will appear in the array. |
|
| 132 | - * |
|
| 133 | - * If the header did not exists, this method will return an empty array. |
|
| 134 | - * |
|
| 135 | - * @param string $name |
|
| 136 | - * @return string[] |
|
| 137 | - */ |
|
| 138 | - public function getHeaderAsArray($name) { |
|
| 139 | - |
|
| 140 | - return $this->inner->getHeaderAsArray($name); |
|
| 141 | - |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Updates a HTTP header. |
|
| 146 | - * |
|
| 147 | - * The case-sensitity of the name value must be retained as-is. |
|
| 148 | - * |
|
| 149 | - * If the header already existed, it will be overwritten. |
|
| 150 | - * |
|
| 151 | - * @param string $name |
|
| 152 | - * @param string|string[] $value |
|
| 153 | - * @return void |
|
| 154 | - */ |
|
| 155 | - public function setHeader($name, $value) { |
|
| 156 | - |
|
| 157 | - $this->inner->setHeader($name, $value); |
|
| 158 | - |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Sets a new set of HTTP headers. |
|
| 163 | - * |
|
| 164 | - * The headers array should contain headernames for keys, and their value |
|
| 165 | - * should be specified as either a string or an array. |
|
| 166 | - * |
|
| 167 | - * Any header that already existed will be overwritten. |
|
| 168 | - * |
|
| 169 | - * @param array $headers |
|
| 170 | - * @return void |
|
| 171 | - */ |
|
| 172 | - public function setHeaders(array $headers) { |
|
| 173 | - |
|
| 174 | - $this->inner->setHeaders($headers); |
|
| 175 | - |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Adds a HTTP header. |
|
| 180 | - * |
|
| 181 | - * This method will not overwrite any existing HTTP header, but instead add |
|
| 182 | - * another value. Individual values can be retrieved with |
|
| 183 | - * getHeadersAsArray. |
|
| 184 | - * |
|
| 185 | - * @param string $name |
|
| 186 | - * @param string $value |
|
| 187 | - * @return void |
|
| 188 | - */ |
|
| 189 | - public function addHeader($name, $value) { |
|
| 190 | - |
|
| 191 | - $this->inner->addHeader($name, $value); |
|
| 192 | - |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Adds a new set of HTTP headers. |
|
| 197 | - * |
|
| 198 | - * Any existing headers will not be overwritten. |
|
| 199 | - * |
|
| 200 | - * @param array $headers |
|
| 201 | - * @return void |
|
| 202 | - */ |
|
| 203 | - public function addHeaders(array $headers) { |
|
| 204 | - |
|
| 205 | - $this->inner->addHeaders($headers); |
|
| 206 | - |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Removes a HTTP header. |
|
| 212 | - * |
|
| 213 | - * The specified header name must be treated as case-insenstive. |
|
| 214 | - * This method should return true if the header was successfully deleted, |
|
| 215 | - * and false if the header did not exist. |
|
| 216 | - * |
|
| 217 | - * @return bool |
|
| 218 | - */ |
|
| 219 | - public function removeHeader($name) { |
|
| 220 | - |
|
| 221 | - $this->inner->removeHeader($name); |
|
| 222 | - |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Sets the HTTP version. |
|
| 227 | - * |
|
| 228 | - * Should be 1.0 or 1.1. |
|
| 229 | - * |
|
| 230 | - * @param string $version |
|
| 231 | - * @return void |
|
| 232 | - */ |
|
| 233 | - public function setHttpVersion($version) { |
|
| 234 | - |
|
| 235 | - $this->inner->setHttpVersion($version); |
|
| 236 | - |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Returns the HTTP version. |
|
| 241 | - * |
|
| 242 | - * @return string |
|
| 243 | - */ |
|
| 244 | - public function getHttpVersion() { |
|
| 245 | - |
|
| 246 | - return $this->inner->getHttpVersion(); |
|
| 247 | - |
|
| 248 | - } |
|
| 18 | + /** |
|
| 19 | + * The inner request object. |
|
| 20 | + * |
|
| 21 | + * All method calls will be forwarded here. |
|
| 22 | + * |
|
| 23 | + * @var MessageInterface |
|
| 24 | + */ |
|
| 25 | + protected $inner; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Returns the body as a readable stream resource. |
|
| 29 | + * |
|
| 30 | + * Note that the stream may not be rewindable, and therefore may only be |
|
| 31 | + * read once. |
|
| 32 | + * |
|
| 33 | + * @return resource |
|
| 34 | + */ |
|
| 35 | + public function getBodyAsStream() { |
|
| 36 | + |
|
| 37 | + return $this->inner->getBodyAsStream(); |
|
| 38 | + |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Returns the body as a string. |
|
| 43 | + * |
|
| 44 | + * Note that because the underlying data may be based on a stream, this |
|
| 45 | + * method could only work correctly the first time. |
|
| 46 | + * |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + public function getBodyAsString() { |
|
| 50 | + |
|
| 51 | + return $this->inner->getBodyAsString(); |
|
| 52 | + |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Returns the message body, as it's internal representation. |
|
| 57 | + * |
|
| 58 | + * This could be either a string or a stream. |
|
| 59 | + * |
|
| 60 | + * @return resource|string |
|
| 61 | + */ |
|
| 62 | + public function getBody() { |
|
| 63 | + |
|
| 64 | + return $this->inner->getBody(); |
|
| 65 | + |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Updates the body resource with a new stream. |
|
| 70 | + * |
|
| 71 | + * @param resource $body |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + public function setBody($body) { |
|
| 75 | + |
|
| 76 | + $this->inner->setBody($body); |
|
| 77 | + |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Returns all the HTTP headers as an array. |
|
| 82 | + * |
|
| 83 | + * Every header is returned as an array, with one or more values. |
|
| 84 | + * |
|
| 85 | + * @return array |
|
| 86 | + */ |
|
| 87 | + public function getHeaders() { |
|
| 88 | + |
|
| 89 | + return $this->inner->getHeaders(); |
|
| 90 | + |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Will return true or false, depending on if a HTTP header exists. |
|
| 95 | + * |
|
| 96 | + * @param string $name |
|
| 97 | + * @return bool |
|
| 98 | + */ |
|
| 99 | + public function hasHeader($name) { |
|
| 100 | + |
|
| 101 | + return $this->inner->hasHeader($name); |
|
| 102 | + |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Returns a specific HTTP header, based on it's name. |
|
| 107 | + * |
|
| 108 | + * The name must be treated as case-insensitive. |
|
| 109 | + * If the header does not exist, this method must return null. |
|
| 110 | + * |
|
| 111 | + * If a header appeared more than once in a HTTP request, this method will |
|
| 112 | + * concatenate all the values with a comma. |
|
| 113 | + * |
|
| 114 | + * Note that this not make sense for all headers. Some, such as |
|
| 115 | + * `Set-Cookie` cannot be logically combined with a comma. In those cases |
|
| 116 | + * you *should* use getHeaderAsArray(). |
|
| 117 | + * |
|
| 118 | + * @param string $name |
|
| 119 | + * @return string|null |
|
| 120 | + */ |
|
| 121 | + public function getHeader($name) { |
|
| 122 | + |
|
| 123 | + return $this->inner->getHeader($name); |
|
| 124 | + |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Returns a HTTP header as an array. |
|
| 129 | + * |
|
| 130 | + * For every time the HTTP header appeared in the request or response, an |
|
| 131 | + * item will appear in the array. |
|
| 132 | + * |
|
| 133 | + * If the header did not exists, this method will return an empty array. |
|
| 134 | + * |
|
| 135 | + * @param string $name |
|
| 136 | + * @return string[] |
|
| 137 | + */ |
|
| 138 | + public function getHeaderAsArray($name) { |
|
| 139 | + |
|
| 140 | + return $this->inner->getHeaderAsArray($name); |
|
| 141 | + |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Updates a HTTP header. |
|
| 146 | + * |
|
| 147 | + * The case-sensitity of the name value must be retained as-is. |
|
| 148 | + * |
|
| 149 | + * If the header already existed, it will be overwritten. |
|
| 150 | + * |
|
| 151 | + * @param string $name |
|
| 152 | + * @param string|string[] $value |
|
| 153 | + * @return void |
|
| 154 | + */ |
|
| 155 | + public function setHeader($name, $value) { |
|
| 156 | + |
|
| 157 | + $this->inner->setHeader($name, $value); |
|
| 158 | + |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Sets a new set of HTTP headers. |
|
| 163 | + * |
|
| 164 | + * The headers array should contain headernames for keys, and their value |
|
| 165 | + * should be specified as either a string or an array. |
|
| 166 | + * |
|
| 167 | + * Any header that already existed will be overwritten. |
|
| 168 | + * |
|
| 169 | + * @param array $headers |
|
| 170 | + * @return void |
|
| 171 | + */ |
|
| 172 | + public function setHeaders(array $headers) { |
|
| 173 | + |
|
| 174 | + $this->inner->setHeaders($headers); |
|
| 175 | + |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Adds a HTTP header. |
|
| 180 | + * |
|
| 181 | + * This method will not overwrite any existing HTTP header, but instead add |
|
| 182 | + * another value. Individual values can be retrieved with |
|
| 183 | + * getHeadersAsArray. |
|
| 184 | + * |
|
| 185 | + * @param string $name |
|
| 186 | + * @param string $value |
|
| 187 | + * @return void |
|
| 188 | + */ |
|
| 189 | + public function addHeader($name, $value) { |
|
| 190 | + |
|
| 191 | + $this->inner->addHeader($name, $value); |
|
| 192 | + |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Adds a new set of HTTP headers. |
|
| 197 | + * |
|
| 198 | + * Any existing headers will not be overwritten. |
|
| 199 | + * |
|
| 200 | + * @param array $headers |
|
| 201 | + * @return void |
|
| 202 | + */ |
|
| 203 | + public function addHeaders(array $headers) { |
|
| 204 | + |
|
| 205 | + $this->inner->addHeaders($headers); |
|
| 206 | + |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Removes a HTTP header. |
|
| 212 | + * |
|
| 213 | + * The specified header name must be treated as case-insenstive. |
|
| 214 | + * This method should return true if the header was successfully deleted, |
|
| 215 | + * and false if the header did not exist. |
|
| 216 | + * |
|
| 217 | + * @return bool |
|
| 218 | + */ |
|
| 219 | + public function removeHeader($name) { |
|
| 220 | + |
|
| 221 | + $this->inner->removeHeader($name); |
|
| 222 | + |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Sets the HTTP version. |
|
| 227 | + * |
|
| 228 | + * Should be 1.0 or 1.1. |
|
| 229 | + * |
|
| 230 | + * @param string $version |
|
| 231 | + * @return void |
|
| 232 | + */ |
|
| 233 | + public function setHttpVersion($version) { |
|
| 234 | + |
|
| 235 | + $this->inner->setHttpVersion($version); |
|
| 236 | + |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Returns the HTTP version. |
|
| 241 | + * |
|
| 242 | + * @return string |
|
| 243 | + */ |
|
| 244 | + public function getHttpVersion() { |
|
| 245 | + |
|
| 246 | + return $this->inner->getHttpVersion(); |
|
| 247 | + |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | 250 | } |
@@ -17,300 +17,300 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Request extends Message implements RequestInterface { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * HTTP Method |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - protected $method; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Request Url |
|
| 29 | - * |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - protected $url; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Creates the request object |
|
| 36 | - * |
|
| 37 | - * @param string $method |
|
| 38 | - * @param string $url |
|
| 39 | - * @param array $headers |
|
| 40 | - * @param resource $body |
|
| 41 | - */ |
|
| 42 | - public function __construct($method = null, $url = null, array $headers = null, $body = null) { |
|
| 43 | - |
|
| 44 | - if (is_array($method)) { |
|
| 45 | - throw new InvalidArgumentException('The first argument for this constructor should be a string or null, not an array. Did you upgrade from sabre/http 1.0 to 2.0?'); |
|
| 46 | - } |
|
| 47 | - if (!is_null($method)) $this->setMethod($method); |
|
| 48 | - if (!is_null($url)) $this->setUrl($url); |
|
| 49 | - if (!is_null($headers)) $this->setHeaders($headers); |
|
| 50 | - if (!is_null($body)) $this->setBody($body); |
|
| 51 | - |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Returns the current HTTP method |
|
| 56 | - * |
|
| 57 | - * @return string |
|
| 58 | - */ |
|
| 59 | - public function getMethod() { |
|
| 60 | - |
|
| 61 | - return $this->method; |
|
| 62 | - |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Sets the HTTP method |
|
| 67 | - * |
|
| 68 | - * @param string $method |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - public function setMethod($method) { |
|
| 72 | - |
|
| 73 | - $this->method = $method; |
|
| 74 | - |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Returns the request url. |
|
| 79 | - * |
|
| 80 | - * @return string |
|
| 81 | - */ |
|
| 82 | - public function getUrl() { |
|
| 83 | - |
|
| 84 | - return $this->url; |
|
| 85 | - |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Sets the request url. |
|
| 90 | - * |
|
| 91 | - * @param string $url |
|
| 92 | - * @return void |
|
| 93 | - */ |
|
| 94 | - public function setUrl($url) { |
|
| 95 | - |
|
| 96 | - $this->url = $url; |
|
| 97 | - |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Returns the list of query parameters. |
|
| 102 | - * |
|
| 103 | - * This is equivalent to PHP's $_GET superglobal. |
|
| 104 | - * |
|
| 105 | - * @return array |
|
| 106 | - */ |
|
| 107 | - public function getQueryParameters() { |
|
| 108 | - |
|
| 109 | - $url = $this->getUrl(); |
|
| 110 | - if (($index = strpos($url, '?')) === false) { |
|
| 111 | - return []; |
|
| 112 | - } else { |
|
| 113 | - parse_str(substr($url, $index + 1), $queryParams); |
|
| 114 | - return $queryParams; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Sets the absolute url. |
|
| 121 | - * |
|
| 122 | - * @param string $url |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - public function setAbsoluteUrl($url) { |
|
| 126 | - |
|
| 127 | - $this->absoluteUrl = $url; |
|
| 128 | - |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Returns the absolute url. |
|
| 133 | - * |
|
| 134 | - * @return string |
|
| 135 | - */ |
|
| 136 | - public function getAbsoluteUrl() { |
|
| 137 | - |
|
| 138 | - return $this->absoluteUrl; |
|
| 139 | - |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Base url |
|
| 144 | - * |
|
| 145 | - * @var string |
|
| 146 | - */ |
|
| 147 | - protected $baseUrl = '/'; |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Sets a base url. |
|
| 151 | - * |
|
| 152 | - * This url is used for relative path calculations. |
|
| 153 | - * |
|
| 154 | - * @param string $url |
|
| 155 | - * @return void |
|
| 156 | - */ |
|
| 157 | - public function setBaseUrl($url) { |
|
| 158 | - |
|
| 159 | - $this->baseUrl = $url; |
|
| 160 | - |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Returns the current base url. |
|
| 165 | - * |
|
| 166 | - * @return string |
|
| 167 | - */ |
|
| 168 | - public function getBaseUrl() { |
|
| 169 | - |
|
| 170 | - return $this->baseUrl; |
|
| 171 | - |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Returns the relative path. |
|
| 176 | - * |
|
| 177 | - * This is being calculated using the base url. This path will not start |
|
| 178 | - * with a slash, so it will always return something like |
|
| 179 | - * 'example/path.html'. |
|
| 180 | - * |
|
| 181 | - * If the full path is equal to the base url, this method will return an |
|
| 182 | - * empty string. |
|
| 183 | - * |
|
| 184 | - * This method will also urldecode the path, and if the url was incoded as |
|
| 185 | - * ISO-8859-1, it will convert it to UTF-8. |
|
| 186 | - * |
|
| 187 | - * If the path is outside of the base url, a LogicException will be thrown. |
|
| 188 | - * |
|
| 189 | - * @return string |
|
| 190 | - */ |
|
| 191 | - public function getPath() { |
|
| 192 | - |
|
| 193 | - // Removing duplicated slashes. |
|
| 194 | - $uri = str_replace('//', '/', $this->getUrl()); |
|
| 195 | - |
|
| 196 | - $uri = Uri\normalize($uri); |
|
| 197 | - $baseUri = Uri\normalize($this->getBaseUrl()); |
|
| 198 | - |
|
| 199 | - if (strpos($uri, $baseUri) === 0) { |
|
| 200 | - |
|
| 201 | - // We're not interested in the query part (everything after the ?). |
|
| 202 | - list($uri) = explode('?', $uri); |
|
| 203 | - return trim(URLUtil::decodePath(substr($uri, strlen($baseUri))), '/'); |
|
| 204 | - |
|
| 205 | - } |
|
| 206 | - // A special case, if the baseUri was accessed without a trailing |
|
| 207 | - // slash, we'll accept it as well. |
|
| 208 | - elseif ($uri . '/' === $baseUri) { |
|
| 209 | - |
|
| 210 | - return ''; |
|
| 211 | - |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - throw new \LogicException('Requested uri (' . $this->getUrl() . ') is out of base uri (' . $this->getBaseUrl() . ')'); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Equivalent of PHP's $_POST. |
|
| 219 | - * |
|
| 220 | - * @var array |
|
| 221 | - */ |
|
| 222 | - protected $postData = []; |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Sets the post data. |
|
| 226 | - * |
|
| 227 | - * This is equivalent to PHP's $_POST superglobal. |
|
| 228 | - * |
|
| 229 | - * This would not have been needed, if POST data was accessible as |
|
| 230 | - * php://input, but unfortunately we need to special case it. |
|
| 231 | - * |
|
| 232 | - * @param array $postData |
|
| 233 | - * @return void |
|
| 234 | - */ |
|
| 235 | - public function setPostData(array $postData) { |
|
| 236 | - |
|
| 237 | - $this->postData = $postData; |
|
| 238 | - |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Returns the POST data. |
|
| 243 | - * |
|
| 244 | - * This is equivalent to PHP's $_POST superglobal. |
|
| 245 | - * |
|
| 246 | - * @return array |
|
| 247 | - */ |
|
| 248 | - public function getPostData() { |
|
| 249 | - |
|
| 250 | - return $this->postData; |
|
| 251 | - |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * An array containing the raw _SERVER array. |
|
| 256 | - * |
|
| 257 | - * @var array |
|
| 258 | - */ |
|
| 259 | - protected $rawServerData; |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Returns an item from the _SERVER array. |
|
| 263 | - * |
|
| 264 | - * If the value does not exist in the array, null is returned. |
|
| 265 | - * |
|
| 266 | - * @param string $valueName |
|
| 267 | - * @return string|null |
|
| 268 | - */ |
|
| 269 | - public function getRawServerValue($valueName) { |
|
| 270 | - |
|
| 271 | - if (isset($this->rawServerData[$valueName])) { |
|
| 272 | - return $this->rawServerData[$valueName]; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Sets the _SERVER array. |
|
| 279 | - * |
|
| 280 | - * @param array $data |
|
| 281 | - * @return void |
|
| 282 | - */ |
|
| 283 | - public function setRawServerData(array $data) { |
|
| 284 | - |
|
| 285 | - $this->rawServerData = $data; |
|
| 286 | - |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Serializes the request object as a string. |
|
| 291 | - * |
|
| 292 | - * This is useful for debugging purposes. |
|
| 293 | - * |
|
| 294 | - * @return string |
|
| 295 | - */ |
|
| 296 | - public function __toString() { |
|
| 297 | - |
|
| 298 | - $out = $this->getMethod() . ' ' . $this->getUrl() . ' HTTP/' . $this->getHTTPVersion() . "\r\n"; |
|
| 299 | - |
|
| 300 | - foreach ($this->getHeaders() as $key => $value) { |
|
| 301 | - foreach ($value as $v) { |
|
| 302 | - if ($key === 'Authorization') { |
|
| 303 | - list($v) = explode(' ', $v, 2); |
|
| 304 | - $v .= ' REDACTED'; |
|
| 305 | - } |
|
| 306 | - $out .= $key . ": " . $v . "\r\n"; |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - $out .= "\r\n"; |
|
| 310 | - $out .= $this->getBodyAsString(); |
|
| 311 | - |
|
| 312 | - return $out; |
|
| 313 | - |
|
| 314 | - } |
|
| 20 | + /** |
|
| 21 | + * HTTP Method |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + protected $method; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Request Url |
|
| 29 | + * |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + protected $url; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Creates the request object |
|
| 36 | + * |
|
| 37 | + * @param string $method |
|
| 38 | + * @param string $url |
|
| 39 | + * @param array $headers |
|
| 40 | + * @param resource $body |
|
| 41 | + */ |
|
| 42 | + public function __construct($method = null, $url = null, array $headers = null, $body = null) { |
|
| 43 | + |
|
| 44 | + if (is_array($method)) { |
|
| 45 | + throw new InvalidArgumentException('The first argument for this constructor should be a string or null, not an array. Did you upgrade from sabre/http 1.0 to 2.0?'); |
|
| 46 | + } |
|
| 47 | + if (!is_null($method)) $this->setMethod($method); |
|
| 48 | + if (!is_null($url)) $this->setUrl($url); |
|
| 49 | + if (!is_null($headers)) $this->setHeaders($headers); |
|
| 50 | + if (!is_null($body)) $this->setBody($body); |
|
| 51 | + |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Returns the current HTTP method |
|
| 56 | + * |
|
| 57 | + * @return string |
|
| 58 | + */ |
|
| 59 | + public function getMethod() { |
|
| 60 | + |
|
| 61 | + return $this->method; |
|
| 62 | + |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Sets the HTTP method |
|
| 67 | + * |
|
| 68 | + * @param string $method |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + public function setMethod($method) { |
|
| 72 | + |
|
| 73 | + $this->method = $method; |
|
| 74 | + |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Returns the request url. |
|
| 79 | + * |
|
| 80 | + * @return string |
|
| 81 | + */ |
|
| 82 | + public function getUrl() { |
|
| 83 | + |
|
| 84 | + return $this->url; |
|
| 85 | + |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Sets the request url. |
|
| 90 | + * |
|
| 91 | + * @param string $url |
|
| 92 | + * @return void |
|
| 93 | + */ |
|
| 94 | + public function setUrl($url) { |
|
| 95 | + |
|
| 96 | + $this->url = $url; |
|
| 97 | + |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Returns the list of query parameters. |
|
| 102 | + * |
|
| 103 | + * This is equivalent to PHP's $_GET superglobal. |
|
| 104 | + * |
|
| 105 | + * @return array |
|
| 106 | + */ |
|
| 107 | + public function getQueryParameters() { |
|
| 108 | + |
|
| 109 | + $url = $this->getUrl(); |
|
| 110 | + if (($index = strpos($url, '?')) === false) { |
|
| 111 | + return []; |
|
| 112 | + } else { |
|
| 113 | + parse_str(substr($url, $index + 1), $queryParams); |
|
| 114 | + return $queryParams; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Sets the absolute url. |
|
| 121 | + * |
|
| 122 | + * @param string $url |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + public function setAbsoluteUrl($url) { |
|
| 126 | + |
|
| 127 | + $this->absoluteUrl = $url; |
|
| 128 | + |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Returns the absolute url. |
|
| 133 | + * |
|
| 134 | + * @return string |
|
| 135 | + */ |
|
| 136 | + public function getAbsoluteUrl() { |
|
| 137 | + |
|
| 138 | + return $this->absoluteUrl; |
|
| 139 | + |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Base url |
|
| 144 | + * |
|
| 145 | + * @var string |
|
| 146 | + */ |
|
| 147 | + protected $baseUrl = '/'; |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Sets a base url. |
|
| 151 | + * |
|
| 152 | + * This url is used for relative path calculations. |
|
| 153 | + * |
|
| 154 | + * @param string $url |
|
| 155 | + * @return void |
|
| 156 | + */ |
|
| 157 | + public function setBaseUrl($url) { |
|
| 158 | + |
|
| 159 | + $this->baseUrl = $url; |
|
| 160 | + |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Returns the current base url. |
|
| 165 | + * |
|
| 166 | + * @return string |
|
| 167 | + */ |
|
| 168 | + public function getBaseUrl() { |
|
| 169 | + |
|
| 170 | + return $this->baseUrl; |
|
| 171 | + |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Returns the relative path. |
|
| 176 | + * |
|
| 177 | + * This is being calculated using the base url. This path will not start |
|
| 178 | + * with a slash, so it will always return something like |
|
| 179 | + * 'example/path.html'. |
|
| 180 | + * |
|
| 181 | + * If the full path is equal to the base url, this method will return an |
|
| 182 | + * empty string. |
|
| 183 | + * |
|
| 184 | + * This method will also urldecode the path, and if the url was incoded as |
|
| 185 | + * ISO-8859-1, it will convert it to UTF-8. |
|
| 186 | + * |
|
| 187 | + * If the path is outside of the base url, a LogicException will be thrown. |
|
| 188 | + * |
|
| 189 | + * @return string |
|
| 190 | + */ |
|
| 191 | + public function getPath() { |
|
| 192 | + |
|
| 193 | + // Removing duplicated slashes. |
|
| 194 | + $uri = str_replace('//', '/', $this->getUrl()); |
|
| 195 | + |
|
| 196 | + $uri = Uri\normalize($uri); |
|
| 197 | + $baseUri = Uri\normalize($this->getBaseUrl()); |
|
| 198 | + |
|
| 199 | + if (strpos($uri, $baseUri) === 0) { |
|
| 200 | + |
|
| 201 | + // We're not interested in the query part (everything after the ?). |
|
| 202 | + list($uri) = explode('?', $uri); |
|
| 203 | + return trim(URLUtil::decodePath(substr($uri, strlen($baseUri))), '/'); |
|
| 204 | + |
|
| 205 | + } |
|
| 206 | + // A special case, if the baseUri was accessed without a trailing |
|
| 207 | + // slash, we'll accept it as well. |
|
| 208 | + elseif ($uri . '/' === $baseUri) { |
|
| 209 | + |
|
| 210 | + return ''; |
|
| 211 | + |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + throw new \LogicException('Requested uri (' . $this->getUrl() . ') is out of base uri (' . $this->getBaseUrl() . ')'); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Equivalent of PHP's $_POST. |
|
| 219 | + * |
|
| 220 | + * @var array |
|
| 221 | + */ |
|
| 222 | + protected $postData = []; |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Sets the post data. |
|
| 226 | + * |
|
| 227 | + * This is equivalent to PHP's $_POST superglobal. |
|
| 228 | + * |
|
| 229 | + * This would not have been needed, if POST data was accessible as |
|
| 230 | + * php://input, but unfortunately we need to special case it. |
|
| 231 | + * |
|
| 232 | + * @param array $postData |
|
| 233 | + * @return void |
|
| 234 | + */ |
|
| 235 | + public function setPostData(array $postData) { |
|
| 236 | + |
|
| 237 | + $this->postData = $postData; |
|
| 238 | + |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Returns the POST data. |
|
| 243 | + * |
|
| 244 | + * This is equivalent to PHP's $_POST superglobal. |
|
| 245 | + * |
|
| 246 | + * @return array |
|
| 247 | + */ |
|
| 248 | + public function getPostData() { |
|
| 249 | + |
|
| 250 | + return $this->postData; |
|
| 251 | + |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * An array containing the raw _SERVER array. |
|
| 256 | + * |
|
| 257 | + * @var array |
|
| 258 | + */ |
|
| 259 | + protected $rawServerData; |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Returns an item from the _SERVER array. |
|
| 263 | + * |
|
| 264 | + * If the value does not exist in the array, null is returned. |
|
| 265 | + * |
|
| 266 | + * @param string $valueName |
|
| 267 | + * @return string|null |
|
| 268 | + */ |
|
| 269 | + public function getRawServerValue($valueName) { |
|
| 270 | + |
|
| 271 | + if (isset($this->rawServerData[$valueName])) { |
|
| 272 | + return $this->rawServerData[$valueName]; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Sets the _SERVER array. |
|
| 279 | + * |
|
| 280 | + * @param array $data |
|
| 281 | + * @return void |
|
| 282 | + */ |
|
| 283 | + public function setRawServerData(array $data) { |
|
| 284 | + |
|
| 285 | + $this->rawServerData = $data; |
|
| 286 | + |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Serializes the request object as a string. |
|
| 291 | + * |
|
| 292 | + * This is useful for debugging purposes. |
|
| 293 | + * |
|
| 294 | + * @return string |
|
| 295 | + */ |
|
| 296 | + public function __toString() { |
|
| 297 | + |
|
| 298 | + $out = $this->getMethod() . ' ' . $this->getUrl() . ' HTTP/' . $this->getHTTPVersion() . "\r\n"; |
|
| 299 | + |
|
| 300 | + foreach ($this->getHeaders() as $key => $value) { |
|
| 301 | + foreach ($value as $v) { |
|
| 302 | + if ($key === 'Authorization') { |
|
| 303 | + list($v) = explode(' ', $v, 2); |
|
| 304 | + $v .= ' REDACTED'; |
|
| 305 | + } |
|
| 306 | + $out .= $key . ": " . $v . "\r\n"; |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + $out .= "\r\n"; |
|
| 310 | + $out .= $this->getBodyAsString(); |
|
| 311 | + |
|
| 312 | + return $out; |
|
| 313 | + |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | 316 | } |
@@ -11,35 +11,35 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | interface ResponseInterface extends MessageInterface { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Returns the current HTTP status code. |
|
| 16 | - * |
|
| 17 | - * @return int |
|
| 18 | - */ |
|
| 19 | - public function getStatus(); |
|
| 14 | + /** |
|
| 15 | + * Returns the current HTTP status code. |
|
| 16 | + * |
|
| 17 | + * @return int |
|
| 18 | + */ |
|
| 19 | + public function getStatus(); |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Returns the human-readable status string. |
|
| 23 | - * |
|
| 24 | - * In the case of a 200, this may for example be 'OK'. |
|
| 25 | - * |
|
| 26 | - * @return string |
|
| 27 | - */ |
|
| 28 | - public function getStatusText(); |
|
| 21 | + /** |
|
| 22 | + * Returns the human-readable status string. |
|
| 23 | + * |
|
| 24 | + * In the case of a 200, this may for example be 'OK'. |
|
| 25 | + * |
|
| 26 | + * @return string |
|
| 27 | + */ |
|
| 28 | + public function getStatusText(); |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Sets the HTTP status code. |
|
| 32 | - * |
|
| 33 | - * This can be either the full HTTP status code with human readable string, |
|
| 34 | - * for example: "403 I can't let you do that, Dave". |
|
| 35 | - * |
|
| 36 | - * Or just the code, in which case the appropriate default message will be |
|
| 37 | - * added. |
|
| 38 | - * |
|
| 39 | - * @param string|int $status |
|
| 40 | - * @throws \InvalidArgumentExeption |
|
| 41 | - * @return void |
|
| 42 | - */ |
|
| 43 | - public function setStatus($status); |
|
| 30 | + /** |
|
| 31 | + * Sets the HTTP status code. |
|
| 32 | + * |
|
| 33 | + * This can be either the full HTTP status code with human readable string, |
|
| 34 | + * for example: "403 I can't let you do that, Dave". |
|
| 35 | + * |
|
| 36 | + * Or just the code, in which case the appropriate default message will be |
|
| 37 | + * added. |
|
| 38 | + * |
|
| 39 | + * @param string|int $status |
|
| 40 | + * @throws \InvalidArgumentExeption |
|
| 41 | + * @return void |
|
| 42 | + */ |
|
| 43 | + public function setStatus($status); |
|
| 44 | 44 | |
| 45 | 45 | } |