Ejemplo n.º 1
0
 /**
  * Instantiate request from php _SERVER variable
  * @param array server
  */
 public static function createFromGlobals()
 {
     $server = $_SERVER;
     $uriParts = parse_url($server['REQUEST_URI']);
     $uriParts['host'] = $server['SERVER_NAME'];
     $uriParts['port'] = $server['SERVER_PORT'];
     $uriParts['scheme'] = isset($server['REQUEST_SCHEME']) ? $server['REQUEST_SCHEME'] : (isset($server['HTTPS']) && $server['HTTPS'] == 'on' ? 'https' : 'http');
     if (function_exists('getallheaders')) {
         // a correct case already
         $apacheHeaders = getallheaders();
         foreach ($apacheHeaders as $header => $value) {
             $headers[$header] = array_map('trim', explode(',', $value));
         }
     } else {
         $headers = array();
         // normalize the header key
         foreach ($server as $key => $value) {
             if (substr($key, 0, 5) != 'HTTP_') {
                 continue;
             }
             $name = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
             $headers[$name] = array_map('trim', explode(',', $value));
         }
     }
     $request = new static($server['REQUEST_METHOD'], new Uri($uriParts), $headers, Stream::createFromContents(file_get_contents('php://input')), $server, $_COOKIE, UploadedFile::createFromGlobals($_FILES));
     if ($server['REQUEST_METHOD'] == 'POST' && in_array($request->getMediaType(), array('application/x-www-form-urlencoded', 'multipart/form-data'))) {
         $request->setParsedBody($_POST);
     }
     return $request;
 }