/** * Create a request from the supplied superglobal values. * * If any argument is not supplied, the corresponding superglobal value will * be used. * * The ServerRequest created is then passed to the fromServer() method in * order to marshal the request URI and headers. * * @see fromServer() * @param array $server $_SERVER superglobal * @param array $query $_GET superglobal * @param array $body $_POST superglobal * @param array $cookies $_COOKIE superglobal * @param array $files $_FILES superglobal * @return ServerRequest * @throws InvalidArgumentException for invalid file values */ public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null) { $server = static::normalizeServer($server ?: $_SERVER); $files = static::normalizeFiles($files ?: $_FILES); $headers = static::marshalHeaders($server); $request = new ServerRequest($server, $files, static::marshalUriFromServer($server, $headers), static::get('REQUEST_METHOD', $server, 'GET'), 'php://input', $headers); return $request->withCookieParams($cookies ?: $_COOKIE)->withQueryParams($query ?: $_GET)->withParsedBody($body ?: $_POST); }
/** * {@inheritdoc} */ public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null) { $server = static::normalizeServer($server ?: $_SERVER); $files = static::normalizeFiles($files ?: $_FILES); $headers = static::marshalHeaders($server); $request = new ServerRequest($server, $files, static::marshalUriFromServer($server, $headers), static::get('REQUEST_METHOD', $server, 'GET'), 'php://input', $headers); $contentType = current($request->getHeader('Content-Type')); $input = file_get_contents('php://input'); // support header like "application/json" and "application/json; charset=utf-8" if ($contentType !== false && stristr($contentType, 'application/json')) { $data = (array) json_decode($input); } else { switch ($request->getMethod()) { case 'POST': $data = $_POST; break; default: parse_str($input, $data); break; } } return $request->withCookieParams($cookies ?: $_COOKIE)->withQueryParams($query ?: $_GET)->withParsedBody($body ?: $data); }
/** * {@inheritdoc} */ public function getServerRequest() { $query = $this->getQuery(); $post = $this->getPost(); $cookies = $this->getCookies(); $server = ServerRequestFactory::normalizeServer($this->params); $headers = ServerRequestFactory::marshalHeaders($server); $uri = ServerRequestFactory::marshalUriFromServer($server, $headers); $method = ServerRequestFactory::get('REQUEST_METHOD', $server, 'GET'); $request = new ServerRequest($server, [], $uri, $method, $this->stdin, $headers); return $request->withCookieParams($cookies)->withQueryParams($query)->withParsedBody($post); }
public function testParamsOrder() { $request = new ServerRequest(); $cycle = new Cycle($request->withParsedBody(['foo' => 'bar'])); $this->assertEquals('bar', $cycle['foo']); $cycle = new Cycle($request->withCookieParams(['foo' => 'cookie'])); $this->assertEquals('cookie', $cycle['foo']); $cycle = new Cycle($request->withCookieParams(['foo' => 'cookie'])->withParsedBody(['foo' => 'post'])); $this->assertEquals('cookie', $cycle['foo']); $cycle = new Cycle($request->withQueryParams(['foo' => 'get'])->withParsedBody(['foo' => 'post'])); $this->assertEquals('post', $cycle['foo']); $cycle = new Cycle($request->withQueryParams(['foo' => 'get'])->withParsedBody(['foo' => 'post'])); $cycle->setPathParameters(['foo' => 'path']); $this->assertEquals('path', $cycle['foo']); }