Created
December 31, 2016 17:54
-
-
Save ikariiin/70bf1e3f01d48320117428dec182eeaf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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