Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Cleaned up Zepto\Router
Browse files Browse the repository at this point in the history
  • Loading branch information
hassankhan committed Feb 19, 2014
1 parent 054a2ca commit 497058e
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions library/Zepto/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Router
*
* @var Route[]
*/
protected $routes = array();
protected $routes;

/**
* Callback function to execute when no matching URL is found
Expand Down Expand Up @@ -242,7 +242,6 @@ public function run()
}
}
catch (\Exception $e) {
$this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR;
$this->error($e);
return FALSE;
}
Expand All @@ -255,35 +254,33 @@ public function run()

// Call not found handler if no match was found
if ($route === null) {
$this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND;
$this->not_found();
return FALSE;
}

// If route is a valid Route object, then try and execute its callback
else {

// Set current route
$this->current_route = $route;
// Set current route
$this->current_route = $route;

// Get parameters from request
$params = $this->parse_parameters($route);
// Get parameters from request
$params = $this->parse_parameters($route);

// Try to execute callback for route, if it fails, catch the exception
// and generate a HTTP 500 error
try {
$this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_OK;
// Try to execute callback for route, if it fails, catch the exception
// and generate a HTTP 500 error
try {
$this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_OK;

// Set response content
$this->response->setContent($route->execute($params));
// Set response content
$this->response->setContent($route->execute($params));

// Send response
$this->response->send();
return TRUE;
}
catch (\Exception $e) {
$this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR;
$this->error($e);
return FALSE;
// Send response
$this->response->send();
return TRUE;
}
catch (\Exception $e) {
$this->error($e);
return FALSE;
}
}
}
Expand Down Expand Up @@ -343,6 +340,9 @@ public function error($arg = null)
$this->error_handler = $arg;
}
else {
// Set HTTP status on router
$this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR;

// Execute error handler and set result as response content
if (is_callable($this->error_handler)) {
$this->response->setContent(call_user_func($this->error_handler, $arg));
Expand All @@ -352,7 +352,7 @@ public function error($arg = null)
}

// Set response's status code
$this->response->setStatusCode(\Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR);
$this->response->setStatusCode($this->current_http_status);

// Send response
$this->response->send();
Expand All @@ -375,6 +375,9 @@ public function not_found($callback = null)
$this->not_found_handler = $callback;
}
else {
// Set HTTP status on router
$this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND;

// Execute not found handler and set result as response content
if (is_callable($this->not_found_handler)) {
$this->response->setContent(call_user_func($this->not_found_handler));
Expand Down

0 comments on commit 497058e

Please sign in to comment.