2017 © Pedro Peláez
 

library hakudo

A PSR7 / PSR-15 compatible HTTP router.

image

n1215/hakudo

A PSR7 / PSR-15 compatible HTTP router.

  • Wednesday, February 21, 2018
  • by n1215
  • Repository
  • 0 Watchers
  • 2 Stars
  • 80 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Hakudo(白道)🌕

Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality, (*1)

A PSR7 / PSR-15 compatible HTTP router using n1215/http-router, n1215/http-request-matcher and n1215/jugoya., (*2)

Usage

<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use N1215\Http\Router\Exception\RouteNotFoundException;
use N1215\Http\Router\Exception\RoutingException;
use N1215\Http\Router\Handler\RoutingErrorResponderInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;

// 1. Implement RoutingErrorResponderInterface.
class YourNotFoundErrorResponder implements RoutingErrorResponderInterface
{
    public function supports(RoutingException $exception): bool
    {
        // use this responder only when Route not found exception is thrown by the router.
        return $exception instanceof RouteNotFoundException;
    }

    public function respond(RoutingException $exception, ServerRequestInterface $request): ResponseInterface
    {
        // implement
    }
}

// 2. Implement PSR-11 Container
class YourContainer implements Psr\Container\ContainerInterface
{
    public function get($id)
    {
        // implement
    }

    public function has($id) 
    {
        // implement
    }
}

// 3. Implement PSR-15 RequestHandler
class YourHandler implements RequestHandlerInterface
{
    public function handle(ServerRequestInterface $request): ResponseInterface 
    {
        $id = $request->getAttribute('id'); // matched route parameters becomes attributes.
        // implement
    }
}

// 4. Implement PSR-15 Middleware
class YourMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // implement
    }
}

// 5. register handler and middleware to the container
$container = new YourContainer();
$container->bind(YourHandler::class, function () { return new YourHandler(); });
$container->bind(YourMiddleware::class, function () { return new YourMiddleware(); });

// 6. create Router instance
$router = new N1215\Hakudo\Router(
    N1215\Jugoya\LazyRequestHandlerBuilder::fromContainer($container),
    new N1215\Http\Router\Result\RoutingResultFactory()
);

// 7. add routes
use N1215\Hakudo\RequestMatcher\Path;
$router->add(
    Path::get('|/resources/(?<resource_name>[a-z_]+)/(?<id>[0-9]+)|'), // you can use any other RequestMatcher instance.
    YourHandler::class, // you can use same signature closure or RequestHandler instance instead of RequestHandler's container entry name.
    [YourMiddleware::class] // you can use same signature closure or Middleware instance instead of Middleware's container entry name.
)->name('route_name');

// 8. create RoutingHandler
$routingHandler = new N1215\Http\Router\Handler\RoutingHandler(
    $router,
    new YourNotFoundErrorResponder()
);

// 9. Use RoutingHandler as an implementation of PSR-15 server request handler.
/** @var ServerRequestInterface $request */
/** @var ResponseInterface $response */
$response = $routingHandler->handle($request);

Class diagrams

Hakudo

hakudo, (*3)

RouterInterface

n1215/http-router, (*4)

RequestMatcherInterface

n1215/http-request-matcher, (*5)

Jugoya

n1215/jugoya, (*6)

License

The MIT License (MIT). Please see LICENSE for more information., (*7)

The Versions

21/02 2018

dev-master

9999999-dev https://github.com/n1215/hakudo

A PSR7 / PSR-15 compatible HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar n1215

psr-7 router psr-15

21/02 2018

v0.6.0

0.6.0.0 https://github.com/n1215/hakudo

A PSR7 / PSR-15 compatible HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar n1215

psr-7 router psr-15

18/12 2017

v0.5.0

0.5.0.0 https://github.com/n1215/hakudo

A PSR7 / PSR-15 compatible HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar n1215

psr-7 router psr-15

08/12 2017

v0.4.0

0.4.0.0 https://github.com/n1215/hakudo

A PSR7 / PSR-15 compatible HTTP router.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar n1215

psr-7 router psr-15

07/12 2017
06/12 2017
04/12 2017