public function route($uri) { $fspath = PathResolver::getInstance()->getPath("{PUBLIC}" . $uri); if (_IS_CLI_SERVER && is_file($fspath)) { $r = new \Cherry\Web\Response(); return $r->sendFile($fspath); } // Loop through every route and compare it with the URI foreach ($this->routes as $route) { // Create a route with all identifiers replaced with ([^/]+) regex syntax // E.g. $route_regex = shop-please/([^/]+)/moo (originally shop-please/:some_identifier/moo) $route_regex = preg_replace('@:[^/]+@', '([^/]+)', $route[0]); // Check if URI path matches regex pattern, if so create an array of values from the URI if (!preg_match('@^' . $route_regex . '$@', $uri, $matches)) { continue; } // Create an array of identifiers from the route preg_match('@^' . $route_regex . '$@', $route[0], $identifiers); // Decode the matches $matches = array_map(function ($str) { return urldecode($str); }, $matches); $identifiers = array_map(function ($str) { return substr($str, 1); }, $identifiers); // Combine the identifiers with the values $params = array_combine($identifiers, $matches); array_shift($params); $action = $route->action; $this->dispatch($action, $params); return 200; } return 404; }
function logHit(Request $req, Response $resp) { $log = sprintf("%15s [%s (%s)] %d %s %s %s", date("d-M-y h:i:s", $req->getTimestamp()), $req->getRemoteIp(), $req->getRemoteHost(), $resp->getStatus(), $req->getRequestMethod(), $req->getRequestURL(), $resp->contentLength ? "(" . $resp->contentLength . " bytes)" : ""); fprintf(STDERR, $log . "\n"); }