nazg / heredity
Middleware Dispatcher For Hack
Installs: 6 320
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Language:Hack
Requires
- hhvm: ^4.62
- facebook/hack-http-request-response-interfaces: ^0.3
- hhvm/hhvm-autoload: ^3.0
- hhvm/hsl: ^4.0
- hhvm/hsl-experimental: ^4.25
- nazg/http-server-request-handler: ^0.6
Requires (Dev)
- facebook/fbexpect: ^2.7
- hhvm/hacktest: ^2.0
- hhvm/hhast: ^4.0
- nazg/glue: ^1.0
- ytake/hungrr: ^0.13
README
Middleware Dispatcher For Hack.
PSR-7 HTTP message library Not Supported.
Supported Only Hack library.
Required HHVM >= 4.20.0
install
$ composer require nazg/heredity
Usage
Basic
1. Example Simple Request Handler
use type Nazg\Http\Server\RequestHandlerInterface; use type Facebook\Experimental\Http\Message\ServerRequestInterface; use type Facebook\Experimental\Http\Message\ResponseInterface; use type Ytake\Hungrr\Response; use type Ytake\Hungrr\StatusCode; use type NazgHeredityTest\Middleware\MockMiddleware; use namespace HH\Lib\Experimental\IO; use function json_encode; final class SimpleRequestHandler implements RequestHandlerInterface { public function handle( IO\WriteHandle $handle, ServerRequestInterface $request ): ResponseInterface { $header = $request->getHeader(MockMiddleware::MOCK_HEADER); if (count($header)) { $handle->rawWriteBlocking(json_encode($header)); return new Response($handle, StatusCode::OK); } $handle->rawWriteBlocking(json_encode([])); return new Response($handle, StatusCode::OK); } }
2. Creating Middleware
use type Facebook\Experimental\Http\Message\ResponseInterface; use type Facebook\Experimental\Http\Message\ServerRequestInterface; use type Nazg\Http\Server\MiddlewareInterface; use type Nazg\Http\Server\RequestHandlerInterface; use type HH\Lib\Experimental\IO\WriteHandle; class SimpleMiddleware implements MiddlewareInterface { public function process( WriteHandle $writeHandle, ServerRequestInterface $request, RequestHandlerInterface $handler, ): ResponseInterface { // ... do something and return response return $handler->handle($writeHandle, $request); } }
3. Middleware
use type Nazg\Heredity\Heredity; use type Nazg\Heredity\MiddlewareStack; use type Ytake\Hungrr\ServerRequestFactory; use namespace HH\Lib\Experimental\IO; list($read, $write) = IO\pipe_non_disposable(); $heredity = new Heredity( new MiddlewareStack([ SimpleMiddleware::class ]), new SimpleRequestHandler() ); $response = $heredity->handle($write, ServerRequestFactory::fromGlobals());
With Dependency Injection Container
example. nazg-hack/glue
use namespace HH\Lib\Str; use type Nazg\Http\Server\AsyncMiddlewareInterface; use type Nazg\Heredity\Exception\MiddlewareResolvingException; use type Nazg\Heredity\Resolvable; use type Nazg\Glue\Container; class GlueResolver implements Resolvable<AsyncMiddlewareInterface> { public function __construct( protected Container $container ) {} public function resolve( classname<AsyncMiddlewareInterface> $middleware ): AsyncMiddlewareInterface { if ($this->container->has($middleware)) { return $this->container->get($middleware); } throw new MiddlewareResolvingException( Str\format('Identifier "%s" is not binding.', $middleware), ); } }