ds/router

This package is abandoned and no longer maintained. No replacement package was suggested.

Router Decorator with Cached FastRoute Controller Handler

v1.0.0 2017-11-27 14:12 UTC

This package is not auto-updated.

Last update: 2018-01-20 10:32:59 UTC


README

Code Climate Test Coverage

Build Status

Scrutinizer Code Quality Code Coverage Build Status

Rs\Router

PHP Router

Adaptor Based Routing Component.

Includes the following Adaptors:

  • Fast Route (with cached routes for String|Closure)
  • Symfony Routing

Creating the Router

$router = new Rs\Router\Router(
    Ds\Router\Interfaces\AdaptorInterface,
    Ds\Router\Interfaces\RouteCollectionInterface
)

//Fast Router with Serialized Routes.
$router = new Router(
    new FastRouteAdaptor(
        new SuperClosure(
            new Serializer()
        ),
        (array) $options
    ),
    new RouteCollection()
);

//Use the Factory methods for easy access
$router = \Ds\Router\RouterFactory::createFastRouter($options);

Router Adaptors

Adaptors must implement 'Ds\Router\Interfaces\AdaptorInterface

Provided Adaptors:

  • FastRoute - Ds\Adaptors\FastRouteAdaptor
  • Symfony - Ds\Adaptors\SymfonyAdaptor
    Ds\Router\Adaptor\FastRouteAdaptor(
        SerializerInterface $serializer,
        array $options = []
    );


    $fastRoute = new FastRouteAdaptor(
         new SuperClosure(
            new Serializer()
         ),
         [
             'cacheDisabled' => FALSE,
             'cacheFile' => __DIR__ . '/routes.cache',
             'cacheExpires' => 60 * 60 * 24 * 7,
             'errorHandlers' => [
                 'default' => [
                     'handler' => '\Site\Controllers\Error\ErrorController::error404',
                     'name' => ['error']
                 ]
             ]
         ]
    )

$router = $router->withAdaptor($fastRoute)

Route Handlers

 //PSR Based.
 function (
    RequestInterface $request, 
    ResponseInterface $response, 
    callable $next = null
  ) {
        if ($next){
            $response = $next($request, $response);
        }
        
        return $response;
  }

Route Example

    $router->addRoute(
        'get', 
        '/simple', 
        function ($request, $response){
          return $response;
        },
        ['routeName']
     );

Using class dispatcher

  //Load Dispatcher to convert handler intro response.
  $dispatcher = new \Ds\Router\Dispatcher\Dispatcher([
    'whitelist' => ['ControllerInterface'],
    'blacklist' => ['ControllerInterface2'],
    'autoWire' => true
  ]);
  
  
  //$ontainer some DI-Container.  
  //Dispatch the request with params to pass to controller.
  $dispatched = $dispatcher->dispatch(
    $serverRequest,
    $response->getHandler(),
    ['container' => $container]  
  );
  
  var_dump($dispatched->getBody()->getContents());
  

Route Collections

Routes Collections must implement \Ds\Router\Interfaces\RouteCollectionInterface.

$routeCollection = new \Ds\Router\RouteCollection();

$routeCollection->group('/sub-dir', function() use ($routeCollection){

    $routeCollection->addRoute(
            (string)$httpMethod,
            (string)$path,
            (string)$handler,
            (array)$names
    );
});

$router = $router->withCollection($routeCollection)

Loaders

Collection Loaders must implement \Ds\Router\Interfaces\LoaderInterface.


//Or routes can be added via seperate files via the FileLoader
//Variables can be made accessible via 'vars' option.
$fileLoader = new Ds\Router\Loaders\FileLoader($router, [
    'vars' => [
        'varName' => $varName,
    ]
]);

//load routes from Routes/routes.php and Routes/routes2.php
$router = $fileLoader->loadFiles([
    __DIR__ . '/path/to/file.php',
]);

####Routes Example


Using the router.

Once the adaptor and routes have been added Routes can be matched to the PSR7 request and a RS\Router\RouterResponse returned

Dispatching Routes.

Use Router::getRouterResponse() to return the RouterResponse for that request.

Dispatching Handlers.

Handlers must be resolved from the in order to get the response from the controller/closure. This is useful when dealing with expensive calls or to areas of the site than might not be accessible before/after middleware e.g. Authentication. Trying RouterResponse::getResponse() without resolving will cause an Exception to be thrown.