/** * Adds Database queries, URI, Method and Controller. Also finalizes * the Timeline. */ public function resolve(Request $request) { $request->timelineData = $this->timeline->finalize($request->time); $request->databaseQueries = $this->databaseQueries; $routeInfo = Router::getInstance()->getInfo(); $request->controller = $routeInfo['controller']; $request->method = $routeInfo['method']; $request->getData = pRequest::getInstance()->get(); $request->postData = pRequest::getInstance()->getPost(); return $request; }
public function getRequest() { return Request::getInstance(); }
/** * Handle a a set of routes: if a match is found, execute the relating handling function. * * @param array $routes Collection of route patterns and their handling functions * * @return int The number of routes handled */ protected function handle($routes) { // The current page URL $currentUri = Request::getInstance()->getUrlPath(); // Loop all routes foreach ($routes as $route) { // we have a match! if (preg_match_all('#^' . $route['pattern'] . '$#', $currentUri, $matches, PREG_OFFSET_CAPTURE)) { // Rework matches to only contain the matches, not the orig string $matches = array_slice($matches, 1); // Extract the matched URL parameters (and only the parameters) $params = array_map(function ($match, $index) use($matches) { // We have a following parameter: take the substring from the current param position until the next one's position (thank you PREG_OFFSET_CAPTURE) if (isset($matches[$index + 1]) && isset($matches[$index + 1][0]) && is_array($matches[$index + 1][0])) { return trim(substr($match[0][0], 0, $matches[$index + 1][0][1] - $match[0][1]), '/'); } else { // We have no following parameters: return the whole lot return isset($match[0][0]) ? trim($match[0][0], '/') : null; } }, $matches, array_keys($matches)); $params = array_merge([Request::getInstance()], $params); // call the handling function with the URL parameters if ($route['as']) { $this->routeName = $route['as']; } if ($route['middleware']) { $before = $route['middleware']; if (is_string($before) && strstr($before, '@')) { $before = explode('@', $before); $before[0] = $before[0]::getInstance(); } call_user_func_array($before, $params); } if (is_array($route['fn'])) { $this->routeInfo = ['controller' => $route['fn'][0], 'method' => $route['fn'][1]]; $route['fn'][0] = new $route['fn'][0](); } else { $this->routeInfo['controller'] = 'Anonymous'; $this->routeInfo['method'] = is_string($route['fn']) ? $route['fn'] : 'Anonymous'; } call_user_func_array($route['fn'], $params); // check if quit after deal with the route if (isset($route['final']) && $route['final'] === false) { continue; } return true; } } return false; }