/** * 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 * @param boolean $quitAfterRun Does the handle function need to quit after one route was matched? * @return int The number of routes handled */ private function handle($routes, $quitAfterRun = false) { // Counter to keep track of the number of routes we've handled $numHandled = 0; // The current page URL $uri = Headers::GetRequestUri(); // Variables in the URL $urlvars = array(); // Loop all routes foreach ($routes as $route) { // we have a match! if (preg_match_all('#^' . $route['pattern'] . '$#', $uri, $matches, PREG_OFFSET_CAPTURE)) { // Extract the matched URL parameters (and only the parameters) $params = Headers::GetRegExParams(array_slice($matches, 1)); // call the handling function with the URL parameters call_user_func_array($route['fn'], $params); // yay! $numHandled++; // If we need to quit, then quit if ($quitAfterRun) { break; } } } // Return the number of routes handled return $numHandled; }