public function register() { $this->forge->singleton(['routing', Routing::class], function () { $routing = new Routing(new HttpRequest(ServerRequestFactory::fromGlobals()), new HttpResponse(new Response())); $routing->makeDispatcher(HTTP . "routes.php"); return $routing; }); $this->provides += [Routing::class]; }
/** * Routing Middleware entry point. * * Route determination and execution is handled entirely by this object. * * @param Request $request * @param Response $response * @param callable|NULL $next * * @return Response */ public function __invoke(Request $request, Response $response, callable $next = NULL) { // we need to get the routing object here due to the strict // middleware __invoke method signature. $this->routing = $this->forge->make(Routing::class); // evaluate the route and return state list($status, $action, $input) = $this->routing->evaluate_route($this->routing->match()); // verify that the route is valid $error_response = $this->routing->getRouteError($response, $status); // has an error been detected? if (is_null($error_response)) { // No, so execute the route return $this->routing->executeRouteAction($input, $action) ? parent::__invoke($request, $response, $next) : $response; } else { // otherwise return the error response return $error_response; } }
public function test_Routing() { //$request = new Request('/controller/amiguchi','GET'); //die_dump($request); //$this->routing->makeRoutes(); $route = $this->routing->makeDispatcher(TEST_PATH . '/tests/core/app/routes.php')->match(); # status will be in the following: # NOT_FOUND = 0; # FOUND = 1; # METHOD_NOT_ALLOWED = 2; $status = array_key_exists(0, $route) ? $route[0] : NULL; # get the target callable $target = array_key_exists(1, $route) ? $route[1] : NULL; # get the request parameters $params = array_key_exists(2, $route) ? $route[2] : NULL; # should be found with correct parameters //$this->assertTrue($status === Dispatcher::FOUND); //$this->assertTrue($params === ['name' => 'greg']); # simulate routing middleware for this test case //$result = call_user_func_array($target, [new Input($route[2]), new Response()]); //$this->assertEquals("Test Route [greg]", $result, // 'Test route should return `Test Route [greg]`.'); }