Exemplo n.º 1
0
 public function testRemoveQueryStringFromUrl()
 {
     $request = new Request("/test/?query=true", "POST");
     $this->assertEquals("/test", $request->getUrl());
     $this->assertEquals("POST", $request->getMethod());
     $this->assertEquals([], $request->getHeaders());
 }
Exemplo n.º 2
0
 /**
  * We try to find a matching route for the given request. If we find one
  * we return a response object with the correct response body. In case if
  * there's no matching route we return a 404 response.
  *
  * @return \Kuro\Core\Http\Response
  */
 public function dispatch(\Kuro\Http\Request $request) : \Kuro\Http\Response
 {
     $response = new \Kuro\Http\Response();
     //Try to find a matching route
     foreach ($this->getRoutes() as $route) {
         //Check if there are any matching methods
         $matchMethod = false;
         foreach ($route["methods"] as $method) {
             if (strcasecmp($method, $request->getMethod()) === 0) {
                 $matchMethod = true;
                 break;
             }
         }
         if (!$matchMethod) {
             continue;
         }
         //Check if the a route matches the current route
         //TODO: Add posibility of multiple parameter wildcardds
         $matchRoute = false;
         $routePattern = preg_replace("/{[A-Za-z0-9]+}/", "(?P<parameter>[0-9]+)", $route["route"]);
         $routePattern = str_replace("/", '\\/', $routePattern);
         $routePattern = "/^" . $routePattern . "\$/";
         //Check the route and get extra parameter if available
         $routeParameter = [];
         if (preg_match($routePattern, $request->getUrl(), $routeParameter) === 1) {
             $matchRoute = true;
         }
         //Try to call the callback function
         if ($matchMethod && $matchRoute) {
             $returnArray = [];
             $returnArray["parameter"] = isset($routeParameter["parameter"]) ? $routeParameter["parameter"] : null;
             if ($route["callback"] instanceof Closure) {
                 $returnArray["type"] = "Closure";
                 $returnArray["function"] = $route["callback"];
                 $response->setStatusCode(200);
                 $response->setBody($route["callback"]());
                 return $response;
             }
             //If the callback is a string try to call the right method
             if (is_string($route["callback"])) {
                 $returnArray["type"] = "Controller";
                 $controllerCallback = explode("@", $route["callback"]);
                 if (count($controllerCallback) !== 2) {
                     return ["type" => "error"];
                 }
                 //Get controller name and method from callback string
                 $controllerName = "Core\\Controller\\" . $controllerCallback[0];
                 $controllerMethod = $controllerCallback[1];
                 //Check if the controller exists
                 if (!class_exists($controllerName)) {
                     return ["type" => "error"];
                 }
                 //Check if the method exists
                 if (!method_exists($controllerName, $controllerMethod)) {
                     return ["type" => "error"];
                 }
                 $response->setStatusCode(200);
                 $response->setBody((new $controllerName())->{$controllerMethod}());
                 return $response;
             }
         }
     }
     $response->setStatusCode(404);
     $response->setBody("<h1>404 - Not found</h1>");
     return $response;
 }