Exemple #1
0
 public static function createFromGlobals()
 {
     if (!isset($_SERVER['REQUEST_METHOD'], $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'])) {
         throw new HttpException('Missing required fields in global $_SERVER - [REQUEST_METHOD, HTTP_HOST, REQUEST_URI]');
     }
     $method = strtolower($_SERVER['REQUEST_METHOD']);
     if (!in_array($method, self::METHODS)) {
         throw new HttpException('Unsupported request method ' . $method);
     }
     if ($method === 'get') {
         $data = $_GET;
     } else {
         if ($method === 'post') {
             $data = $_POST;
         } else {
             // PHP does not automatically populate $_PUT and $_DELETE variables
             $body = file_get_contents('php://input');
             // HTTP_CONTENT_TYPE - PHP built-in server; CONTENT_TYPE - everything else
             $contentType = $_SERVER['HTTP_CONTENT_TYPE'] ?? $_SERVER['CONTENT_TYPE'] ?? '';
             $contentType = strtolower($contentType);
             if (strpos($contentType, 'application/json') !== false) {
                 $data = json_decode($body, true);
             } else {
                 parse_str($body, $data);
             }
         }
     }
     return new static($method, Uri::createFromGlobals(), $data, $_FILES ?? [], $_SERVER['HTTP_REFERER'] ?? '');
 }