Extends the Request definition to add methods for accessing incoming data, specifically server parameters, cookies, matched path parameters, query string arguments, body parameters, and upload file information. "Attributes" are discovered via decomposing the request (and usually specifically the URI path), and typically will be injected by the application. Requests are considered immutable; all methods that might change state are implemented such that they retain the internal state of the current message and return a new instance that contains the changed state.
Inheritance: implements Psr\Http\Message\ServerRequestInterface, use trait MessageTrait, use trait RequestTrait
コード例 #1
0
ファイル: GoHandler.php プロジェクト: somos/framework
 public function __invoke(Go $message)
 {
     $actions = $this->actions;
     $dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) use($actions) {
         foreach ($actions as $action) {
             if ($action->getMatcher() instanceof Route == false) {
                 continue;
             }
             $route = $action->getMatcher();
             $r->addRoute($route->method, $route->uriTemplate, function ($variables = []) use($action) {
                 // TODO: Extract (post) variables from request and insert into action some way easy to consume
                 // URI Variables = parameter
                 // Get Variables = parameter
                 // POST Variables = entity object added to parameters
                 $action->handle($variables);
             });
         }
     });
     $uri = (string) $this->request->getUri()->getPath();
     $method = $this->request->getMethod();
     $routeInfo = $dispatcher->dispatch($method, $uri);
     switch ($routeInfo[0]) {
         case \FastRoute\Dispatcher::NOT_FOUND:
             // TODO: Use a more specialized exception
             throw new \Exception('No action could be found to deal with uri "' . $uri . '" and method "' . $method . '"');
         case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             $allowedMethods = $routeInfo[1];
             // TODO: Use a more specialized exception
             throw new \Exception('The method "' . $method . '" is not allowed with uri "' . $uri . '". The following are allowed ' . 'methods: ' . implode($allowedMethods));
         case \FastRoute\Dispatcher::FOUND:
             list(, $handler, $vars) = $routeInfo;
             $handler($vars);
             break;
     }
 }
コード例 #2
0
ファイル: ServerRequestFactory.php プロジェクト: phly/http
 /**
  * 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);
 }
コード例 #3
0
ファイル: RequestTest.php プロジェクト: phly/conduit
 public function testDecoratorProxiesToAllMethods()
 {
     $stream = $this->getMock('Psr\\Http\\Message\\StreamInterface');
     $psrRequest = new PsrRequest([], [], 'http://example.com', 'POST', $stream, ['Accept' => 'application/xml', 'X-URL' => 'http://example.com/foo']);
     $request = new Request($psrRequest);
     $this->assertEquals('1.1', $request->getProtocolVersion());
     $this->assertSame($stream, $request->getBody());
     $this->assertSame($psrRequest->getHeaders(), $request->getHeaders());
     $this->assertEquals($psrRequest->getRequestTarget(), $request->getRequestTarget());
 }
コード例 #4
0
 public function patchProcessWithRequest(ServerRequest $request)
 {
     return array_merge($request->getQueryParams(), $request->getParsedBody());
 }
コード例 #5
0
ファイル: ServerRequestTest.php プロジェクト: phly/http
 public function testUsesProvidedConstructorArguments()
 {
     $server = ['foo' => 'bar', 'baz' => 'bat'];
     $server['server'] = true;
     $files = ['files' => new UploadedFile('php://temp', 0, 0)];
     $uri = new Uri('http://example.com');
     $method = 'POST';
     $headers = ['host' => ['example.com']];
     $request = new ServerRequest($server, $files, $uri, $method, 'php://memory', $headers);
     $this->assertEquals($server, $request->getServerParams());
     $this->assertEquals($files, $request->getUploadedFiles());
     $this->assertSame($uri, $request->getUri());
     $this->assertEquals($method, $request->getMethod());
     $this->assertEquals($headers, $request->getHeaders());
     $body = $request->getBody();
     $r = new ReflectionProperty($body, 'stream');
     $r->setAccessible(true);
     $stream = $r->getValue($body);
     $this->assertEquals('php://memory', $stream);
 }