Beispiel #1
0
 public static function deriveFromRequest(Request $request)
 {
     //build an empty header
     $headers = new Headers();
     //analyze each possible acceptable encoding
     foreach ($request->getHeader('Accept') as $acceptable) {
         if (in_array($acceptable, ['text/yaml', 'text/x-yaml', 'application/yaml', 'application/x-yaml', 'application/xml', 'text/xml', 'application/json'])) {
             //select (and store) the content type
             $headers->set('Content-Type', $acceptable);
             //the content-type has been selected
             break;
         }
     }
     //build and return the response
     return new self(200, $headers);
 }
Beispiel #2
0
 /**
  * Create new HTTP request with data extracted from the application
  * Environment object.
  *
  * @param Environment $environment The Slim application Environment
  *
  * @return self
  */
 public static function createFromEnvironment(Environment $environment)
 {
     $method = $environment['REQUEST_METHOD'];
     $uri = Uri::createFromEnvironment($environment);
     $headers = Headers::createFromEnvironment($environment);
     $cookies = Cookies::parseHeader($headers->get('Cookie', []));
     $serverParams = $environment->all();
     $body = new RequestBody();
     $uploadedFiles = UploadedFile::createFromEnvironment($environment);
     $request = new static($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
     if ($method === 'POST' && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])) {
         // parsed body must be $_POST filter_input_array(INPUT_POST) won't work!
         $request = $request->withParsedBody($_POST);
     }
     return $request;
 }