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.
public static fromGlobals ( array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null ) : |
||
$server | array | $_SERVER superglobal |
$query | array | $_GET superglobal |
$body | array | $_POST superglobal |
$cookies | array | $_COOKIE superglobal |
$files | array | $_FILES superglobal |
Résultat |
/** * Create a Server instance * * Creates a server instance from the callback and the following * PHP environmental values: * * - server; typically this will be the $_SERVER superglobal * - query; typically this will be the $_GET superglobal * - body; typically this will be the $_POST superglobal * - cookies; typically this will be the $_COOKIE superglobal * - files; typically this will be the $_FILES superglobal * * @param callable $callback * @param array $server * @param array $query * @param array $body * @param array $cookies * @param array $files * @return self */ public static function createServer(callable $callback, array $server, array $query, array $body, array $cookies, array $files) { $request = ServerRequestFactory::fromGlobals($server, $query, $body, $cookies, $files); $response = new Response(); return new self($callback, $request, $response); }
public function testCanCreateServerRequestViaFromGlobalsMethod() { $server = ['SERVER_PROTOCOL' => '1.1', 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'application/json', 'REQUEST_METHOD' => 'POST', 'REQUEST_URI' => '/foo/bar', 'QUERY_STRING' => 'bar=baz']; $cookies = $query = $body = $files = ['bar' => 'baz']; $cookies['cookies'] = true; $query['query'] = true; $body['body'] = true; $files = ['files' => ['tmp_name' => 'php://temp', 'size' => 0, 'error' => 0, 'name' => 'foo.bar', 'type' => 'text/plain']]; $expectedFiles = ['files' => new UploadedFile('php://temp', 0, 0, 'foo.bar', 'text/plain')]; $request = ServerRequestFactory::fromGlobals($server, $query, $body, $cookies, $files); $this->assertInstanceOf('Phly\\Http\\ServerRequest', $request); $this->assertEquals($cookies, $request->getCookieParams()); $this->assertEquals($query, $request->getQueryParams()); $this->assertEquals($body, $request->getParsedBody()); $this->assertEquals($expectedFiles, $request->getUploadedFiles()); $this->assertEmpty($request->getAttributes()); }
public function testCanCreateServerRequestViaFromGlobalsMethod() { $server = ['SERVER_PROTOCOL' => '1.1', 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'application/json', 'REQUEST_METHOD' => 'POST', 'REQUEST_URI' => '/foo/bar', 'QUERY_STRING' => 'bar=baz']; $cookies = $query = $body = $files = ['bar' => 'baz']; $cookies['cookies'] = true; $query['query'] = true; $body['body'] = true; $files['files'] = true; $request = ServerRequestFactory::fromGlobals($server, $query, $body, $cookies, $files); $this->assertInstanceOf('Phly\\Http\\ServerRequest', $request); $this->assertEquals($cookies, $request->getCookieParams()); $this->assertEquals($query, $request->getQueryParams()); $this->assertEquals($body, $request->getParsedBody()); $this->assertEquals($files, $request->getFileParams()); $this->assertEmpty($request->getAttributes()); }
public function __invoke() { return [\Phly\Http\ServerRequest::class => \DI\factory(function () { return \Phly\Http\ServerRequestFactory::fromGlobals(); })]; }