Skip to content

Instantly share code, notes, and snippets.

@ikariiin
Created December 31, 2016 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikariiin/70bf1e3f01d48320117428dec182eeaf to your computer and use it in GitHub Desktop.
Save ikariiin/70bf1e3f01d48320117428dec182eeaf to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: saitama
* Date: 28/12/16
* Time: 11:39 PM
*/
namespace GourabMe\Router;
use Aerys\Request;
use Aerys\Response;
class Router {
private $jsonRoutes;
private $router;
private $routeHandlerPair;
public function __construct(string $rawJsonRoutes) {
$this->jsonRoutes = json_decode($rawJsonRoutes, true);
$this->router = \Aerys\router();
return $this;
}
public function process(): self {
array_walk($this->jsonRoutes, function($val, $key) {
$this->walker($val);
});
return $this;
}
private function walker(array $route): self {
$this->createRouteHandlerPair($route['baseRoute'], $route['baseClass']);
$this->router->route($route["method"], $route["baseRoute"], [$this, "routeReceiver"]);
return $this;
}
public function routeReceiver(
Request $request,
Response $response,
array $args
) {
$response->end($this->getRouteHandler($request->getUri()));
}
public function getRouter() {
return $this->router;
}
private function createRouteHandlerPair(
string $route,
string $handler
) {
$this->routeHandlerPair[$route] = $handler;
}
private function getRouteHandler(string $route) {
return $this->routeHandlerPair[$route];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment