Reworked error handling.
This commit is contained in:
parent
69b14085b4
commit
d5a2753c20
1 changed files with 67 additions and 109 deletions
|
@ -38,18 +38,10 @@ class Router extends Object
|
|||
{
|
||||
parent::__construct();
|
||||
|
||||
$response = new Response();
|
||||
|
||||
try
|
||||
{
|
||||
// Catches requests that aren't lowercase
|
||||
$lowercase_request = strtolower($_REQUEST['request']);
|
||||
|
||||
if ($_REQUEST['request'] != $lowercase_request)
|
||||
{
|
||||
// @todo Rework the Browser class to handle the 301 (perhaps redirect301()) to not break other code
|
||||
header('Location: ' . substr_replace($_SERVER['REQUEST_URI'], $lowercase_request, 1, strlen($lowercase_request)), true, 301);
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
// Grabs the requested page
|
||||
$request = $_REQUEST['request'];
|
||||
$components = explode('/', $request);
|
||||
|
@ -70,53 +62,34 @@ class Router extends Object
|
|||
}
|
||||
}
|
||||
|
||||
// Creates our class name
|
||||
array_unshift($nouns, $version);
|
||||
|
||||
$class = implode('_', $nouns);
|
||||
|
||||
// Creates our filename
|
||||
array_unshift($nouns, SITE_MODULE_PATH);
|
||||
|
||||
$filename = implode('/', $nouns) . '.php';
|
||||
|
||||
if (file_exists($filename))
|
||||
if (!file_exists($filename))
|
||||
{
|
||||
if (class_exists($class))
|
||||
throw new Exception('Cannot find the file ' . $filename);
|
||||
}
|
||||
|
||||
if (!class_exists($class))
|
||||
{
|
||||
throw new Exception('Cannot find the class ' . $class);
|
||||
}
|
||||
|
||||
$resource = new $class($uids);
|
||||
|
||||
// Determines if we need to serve over HTTP or HTTPS
|
||||
if ($resource->secure == false && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'])
|
||||
{
|
||||
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], true, 301);
|
||||
throw new Exception();
|
||||
throw new Exception('This resource expects HTTPS communication.');
|
||||
}
|
||||
elseif ($resource->secure == true && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == false))
|
||||
{
|
||||
header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], true, 301);
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
// Checks for the PHPSESSID in the query string
|
||||
if (stripos($_SERVER['REQUEST_URI'], '?PHPSESSID=') === false)
|
||||
{
|
||||
// XHTML compliancy stuff
|
||||
// @todo Wonder if this could be yanked now that we're in HTML5 land
|
||||
ini_set('arg_separator.output', '&');
|
||||
ini_set('url_rewriter.tags', 'a=href,area=href,frame=src,input=src,fieldset=');
|
||||
|
||||
// @todo Will want to generate the header based on if we're pushing documentation or API
|
||||
header('Content-type: text/html; charset=UTF-8');
|
||||
// header('Content-type: application/json');
|
||||
//header('Content-type: application/json; charset=UTF-8');
|
||||
}
|
||||
else
|
||||
{
|
||||
// Redirect so Google knows to index the page without the session ID
|
||||
list($request_uri, $phpsessid) = explode('?PHPSESSID=', $_SERVER['REQUEST_URI'], 2);
|
||||
header('HTTP/1.1 301 Moved Permanently');
|
||||
header('Location: ' . $request_uri);
|
||||
|
||||
throw new Exception('Requested URI contains PHPSESSID, redirecting.');
|
||||
throw new Exception('This resource expects HTTP communication.');
|
||||
}
|
||||
|
||||
// Gets the profiler status
|
||||
|
@ -125,16 +98,17 @@ class Router extends Object
|
|||
|
||||
$method = strtolower($_SERVER['REQUEST_METHOD']);
|
||||
|
||||
if (method_exists($resource, $method))
|
||||
if (!method_exists($resource, $method))
|
||||
{
|
||||
throw new Exception('Cannot find the method ' . $class . '::' . $method);
|
||||
}
|
||||
|
||||
// Starts a timer before the resource is executed
|
||||
if ($profiler)
|
||||
{
|
||||
Profiler::timer('resource ' . $method);
|
||||
}
|
||||
|
||||
$response = new Response();
|
||||
|
||||
if ($resource->validate)
|
||||
{
|
||||
$validation_errors = $resource->__validate();
|
||||
|
@ -161,30 +135,14 @@ class Router extends Object
|
|||
{
|
||||
Profiler::timer('resource ' . $method);
|
||||
}
|
||||
|
||||
$response->respond();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('Missing method');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('Missing class');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('Missing file');
|
||||
}
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
// @todo
|
||||
exit('fuuuu');
|
||||
$output = $e->getMessage();
|
||||
$response->status = 500;
|
||||
$response->message = $e->getMessage();
|
||||
}
|
||||
|
||||
$response->respond();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue