2017 © Pedro Peláez
 

library heredity

Middleware Dispatcher For Hack

image

ytake/heredity

Middleware Dispatcher For Hack

  • Friday, December 22, 2017
  • by ytake
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • Hack
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

heredity

Middleware Dispatcher For Hack., (*1)

Build Status, (*2)

PSR-7 HTTP message library Not Supported.
Supported Only Hack library.
Required HHVM >= 4.20.0, (*3)

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, (*4)


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), ); } }

The Versions