public function run($callback = null)
{
// Define which method we need to handle
$this->requestedMethod = $this->getRequestMethod();
// Handle all before middlewares
if (isset($this->beforeRoutes[$this->requestedMethod])) {
$this->handle($this->beforeRoutes[$this->requestedMethod]);
}
// Handle all routes
$numHandled = 0;
if (isset($this->afterRoutes[$this->requestedMethod])) {
$numHandled = $this->handle($this->afterRoutes[$this->requestedMethod], true);
}
// If no route was handled, trigger the 404 (if any)
if ($numHandled === 0) {
if ($this->notFoundCallback && is_callable($this->notFoundCallback)) {
call_user_func($this->notFoundCallback);
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
}
} else {
if ($callback) {
$callback();
}
}
// If it originally was a HEAD request, clean up after ourselves by emptying the output buffer
if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
ob_end_clean();
}
// Return true if a route was handled, false otherwise
if ($numHandled === 0) {
return false;
}
return true;
}