Dropped response class
Seems pointless to have class that had a single method that basically contained all of the shit that the resource already had and knew. Moved respond() method to the response class and moved all of the response validation logic and errors from the router to the response class.
This commit is contained in:
parent
a749c80d93
commit
141ac693cb
3 changed files with 166 additions and 193 deletions
|
@ -36,15 +36,6 @@ class Resource extends Object
|
|||
*/
|
||||
public $secure = false;
|
||||
|
||||
/**
|
||||
* Required
|
||||
*
|
||||
* Variables that are required.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $required = [];
|
||||
|
||||
/**
|
||||
* Filter
|
||||
*
|
||||
|
@ -63,7 +54,7 @@ class Resource extends Object
|
|||
*/
|
||||
public $validate = [];
|
||||
|
||||
// @todo
|
||||
// @todo Document this
|
||||
public $status = 200;
|
||||
public $message = 'OK';
|
||||
public $echo = false;
|
||||
|
@ -71,8 +62,8 @@ class Resource extends Object
|
|||
public $offset = false;
|
||||
public $errors = [];
|
||||
public $uids = [];
|
||||
|
||||
// @todo if $status != 200 && $message == 'OK' ...
|
||||
public $response = false;
|
||||
public $profiler = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
|
@ -86,7 +77,14 @@ class Resource extends Object
|
|||
{
|
||||
$this->uids = $uids;
|
||||
|
||||
parent::__construct(['cache', 'db']);
|
||||
try
|
||||
{
|
||||
// Determines if we need to serve over HTTP or HTTPS
|
||||
if ($this->secure
|
||||
&& (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == false))
|
||||
{
|
||||
throw new Exception('400 - SSL is required.');
|
||||
}
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$filter = isset($this->filter[$method]);
|
||||
|
@ -170,6 +168,82 @@ class Resource extends Object
|
|||
//
|
||||
// $_PUT = $GLOBALS['_PUT'];
|
||||
}
|
||||
|
||||
if ($this->errors)
|
||||
{
|
||||
throw new Exception('400 - Missing or invalid parameters.');
|
||||
}
|
||||
|
||||
parent::__construct(['cache', 'db']);
|
||||
|
||||
// Checks if the request method has been implemented
|
||||
//if (get_class($this) != 'Resource')
|
||||
{
|
||||
if (!method_exists($this, $method))
|
||||
{
|
||||
throw new Exception('405 - Method not allowed.');
|
||||
}
|
||||
else
|
||||
{
|
||||
// Gets the profiler status
|
||||
// @todo Refactor out that stripos
|
||||
$profiler = $this->config->pickles['profiler'];
|
||||
$profiler = $profiler === true
|
||||
|| stripos($profiler, 'timers') !== false;
|
||||
|
||||
// Starts a timer before the resource is executed
|
||||
if ($profiler)
|
||||
{
|
||||
Profiler::timer('resource ' . $method);
|
||||
}
|
||||
|
||||
$this->response = $this->$method();
|
||||
|
||||
// Stops the resource timer
|
||||
if ($profiler)
|
||||
{
|
||||
Profiler::timer('resource ' . $method);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$this->status = 400;
|
||||
$this->message = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function respond()
|
||||
{
|
||||
header('Content-type: application/json');
|
||||
|
||||
$meta = [
|
||||
'status' => $this->status,
|
||||
'message' => $this->message,
|
||||
];
|
||||
|
||||
foreach (['echo', 'limit', 'offset', 'errors'] as $variable)
|
||||
{
|
||||
if ($this->$variable)
|
||||
{
|
||||
$meta[$variable] = $this->$variable;
|
||||
}
|
||||
}
|
||||
|
||||
$response = ['meta' => $meta];
|
||||
|
||||
foreach (['response', 'profiler'] as $variable)
|
||||
{
|
||||
if ($this->$variable)
|
||||
{
|
||||
$response[$variable] = $this->$variable;
|
||||
}
|
||||
}
|
||||
|
||||
$pretty = isset($_REQUEST['pretty']) ? JSON_PRETTY_PRINT : false;
|
||||
|
||||
echo json_encode($response, $pretty);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
<?php
|
||||
|
||||
class Response extends Object
|
||||
{
|
||||
public $status = 200;
|
||||
public $message = 'OK';
|
||||
public $echo = false;
|
||||
public $limit = false;
|
||||
public $offset = false;
|
||||
public $errors = false;
|
||||
public $response = false;
|
||||
public $profiler = false;
|
||||
|
||||
public function respond()
|
||||
{
|
||||
header('Content-type: application/json');
|
||||
|
||||
$meta = [
|
||||
'status' => $this->status,
|
||||
'message' => $this->message,
|
||||
];
|
||||
|
||||
foreach (['echo', 'limit', 'offset', 'errors'] as $variable)
|
||||
{
|
||||
if ($this->$variable)
|
||||
{
|
||||
$meta[$variable] = $this->$variable;
|
||||
}
|
||||
}
|
||||
|
||||
$response = ['meta' => $meta];
|
||||
|
||||
foreach (['response', 'profiler'] as $variable)
|
||||
{
|
||||
if ($this->$variable)
|
||||
{
|
||||
$response[$variable] = $this->$variable;
|
||||
}
|
||||
}
|
||||
|
||||
$pretty = isset($_REQUEST['pretty']) ? JSON_PRETTY_PRINT : false;
|
||||
|
||||
exit(json_encode($response, $pretty));
|
||||
}
|
||||
}
|
||||
|
|
@ -38,8 +38,6 @@ class Router extends Object
|
|||
{
|
||||
parent::__construct();
|
||||
|
||||
$response = new Response();
|
||||
|
||||
try
|
||||
{
|
||||
// Grabs the requested page
|
||||
|
@ -70,81 +68,28 @@ class Router extends Object
|
|||
array_unshift($nouns, SITE_RESOURCE_PATH);
|
||||
$filename = implode('/', $nouns) . '.php';
|
||||
|
||||
if (!file_exists($filename))
|
||||
// Checks that the file is present and contains our class
|
||||
if (!file_exists($filename) || !class_exists($class))
|
||||
{
|
||||
// @todo Should be a 404, will need to change it up after I add
|
||||
// namespaces and a Pickles\Exception
|
||||
throw new Exception('Cannot find the file ' . $filename);
|
||||
}
|
||||
|
||||
if (!class_exists($class))
|
||||
{
|
||||
throw new Exception('Cannot find the class ' . $class);
|
||||
throw new Exception('404 - Not Found.');
|
||||
}
|
||||
|
||||
// Instantiates our resource with the UIDs
|
||||
$resource = new $class($uids);
|
||||
|
||||
// Determines if we need to serve over HTTP or HTTPS
|
||||
if ($resource->secure == false && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'])
|
||||
{
|
||||
throw new Exception('This resource expects HTTPS communication.');
|
||||
}
|
||||
elseif ($resource->secure == true && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == false))
|
||||
{
|
||||
throw new Exception('This resource expects HTTP communication.');
|
||||
}
|
||||
|
||||
// Gets the profiler status
|
||||
$profiler = $this->config->pickles['profiler'];
|
||||
$profiler = $profiler === true || stripos($profiler, 'timers') !== false;
|
||||
|
||||
$method = strtolower($_SERVER['REQUEST_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);
|
||||
}
|
||||
|
||||
if ($resource->validate)
|
||||
{
|
||||
$validation_errors = $resource->__validate();
|
||||
|
||||
if ($validation_errors)
|
||||
{
|
||||
$response->status = 400;
|
||||
$response->message = implode(' ', $validation_errors);
|
||||
}
|
||||
}
|
||||
|
||||
if ($response->status == 200)
|
||||
{
|
||||
$resource_return = $resource->$method();
|
||||
|
||||
if ($resource_return)
|
||||
{
|
||||
$response->response = $resource_return;
|
||||
}
|
||||
}
|
||||
|
||||
// Stops the resource timer
|
||||
if ($profiler)
|
||||
{
|
||||
Profiler::timer('resource ' . $method);
|
||||
}
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$response->status = 500;
|
||||
$response->message = $e->getMessage();
|
||||
// Creates a resource object if we don't have one
|
||||
if (!isset($resource))
|
||||
{
|
||||
$resource = new Resource();
|
||||
}
|
||||
|
||||
$response->respond();
|
||||
$resource->status = 400;
|
||||
$resource->message = $e->getMessage();
|
||||
}
|
||||
|
||||
$resource->respond();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue