Skip to content

hackphp/http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HackPHP Http

HackPHP Http Message. Compatible with PSR-7, PSR-15, and PSR-17

Installation

composer require hackphp/http

Usage

Middleware Request Handler

/** @var \Psr\Http\Message\ServerRequestInterface $request */
$request = (new ServerRequestFactory)->createFromSwoole($swooleRequest);

$middlewares = [
    new FirstMiddleware(),
    new SecondMiddleware(),
    function (ServerRequestInterface $request, RequestHandlerInterface $next) {
        return $next($request);
    }
];

$handler = new RequestHandler(
    $middlewares,
    $swooleResponse
);

$response = $handler->handle($request);

$response->send();

Response

// create normal response
$statusCode = 200;
$reasonPhrase = "Ok";
$response = (new ResponseFactory)->createResponse(
    $statusCode,
    $reasonPhrase
);

// create from swoole response
$response = (new ResponseFactory)->createFromSwoole($swooleResponse);

// Change the response body
$stream = (new StreamFactory)->createStream("Hack PHP");
$response = $response->withBody($stream);

// Change the status code
$response = $response->withStatus(400);

Request

// Create Request
$request = (new RequestFactory)->createRequest("GET", "/");

// Create Server Request
$request = (new ServerRequestFactory)->createServerRequest(
    "GET",
    "/",
    $_SERVER // server params
);

// Create Server Request from swoole request
$request = (new ServerRequestFactory)->createFromSwoole($swooleRequest);

Stream

// Create from string
$stream = (new StreamFactory)->createStream("Hack PHP");

// Create from file
$mode = 'r';
$stream = (new StreamFactory)->createStreamFromFile($filename, $mode);

// Create from resource
$stream = (new StreamFactory)->createStreamFromResource($resource);

Uploaded File

$file = (new UploadedFileFactory)->createUploadedFile(
    StreamInterface $stream,
    ?int $size = null,
    int $error = \UPLOAD_ERR_OK,
    ?string $clientFilename = null,
    ?string $clientMediaType = null
);

URI

// Create URI
$uri = (new UriFactory)->createUri("https://github.com/hackphp");

// Create From Swoole
$uri = (new UriFactory)->createFromSwoole($swooleRequest);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages