Permalink
Browse files

Collection/Resource is checked through mapping

  • Loading branch information...
1 parent 4bf5168 commit 568847f48af32f43ae657b3e1c11cefc2f357488 @jaytaph committed Mar 20, 2011
Showing with 70 additions and 61 deletions.
  1. +16 −0 config/restmap.php
  2. +43 −11 library/ji/request.php
  3. +5 −5 library/ji/restController.php
  4. +6 −45 public/index.php
View
@@ -0,0 +1,16 @@
+<?php
+
+define ("MAP_COLLECTION", true);
+define ("MAP_RESOURCE", false);
+
+$config['mapping'] = array(
+ 'event' => array('event', MAP_RESOURCE),
+ 'events' => array('event', MAP_COLLECTION),
+ 'talk' => array('talk', MAP_RESOURCE),
+ 'talks' => array('talk', MAP_COLLECTION),
+ 'user' => array('user', MAP_RESOURCE),
+ 'users' => array('user', MAP_COLLECTION),
+ 'comment' => array('comment', MAP_RESOURCE),
+ 'comments' => array('comment', MAP_COLLECTION),
+ 'default' => array('default', MAP_RESOURCE),
+ );
@@ -30,31 +30,39 @@ protected function _populate($server_vars) {
$this->setAccept($server_vars['HTTP_ACCEPT']);
$this->setHost($server_vars['HTTP_HOST']);
+ $items = array();
+
// collect URL info and sets the url elements.
if(isset($server_vars['PATH_INFO'])) {
$items = explode('/',$server_vars['PATH_INFO']);
+
+ // Remove first (empty) item
array_shift($items);
+ }
- // Default default to default..
+ // Default defaults to default..
if (count($items) == 0) $items[] = "default";
- /* Resources have an even number of items on the path. Collections don't:
- * Resource: /event/3/talk/5
- * Collection: /event/4/talks
- */
- $this->setResource((count($items) & 1) != 1);
-
// Map serialized url data into key value pairs
$path = "";
while (count($items)) {
$k = array_shift($items);
if (strlen($k) == 0) continue;
- $this->addUrlElement($k, array_shift($items), $path);
- // Holds the "hiearchy" for finding deeper controllers
+ $tmp = $this->_findMapping($k);
+ if ($tmp === false) {
+ throw new Exception("Mapping not found");
+ }
+
+ if ($tmp[1] == MAP_RESOURCE) {
+ $this->addUrlElement($k, array_shift($items), $path);
+ } else {
+ $this->addUrlElement($k, null, $path);
+ }
+
+ // Holds the "hierarchy" for finding deeper controllers
$path .= $k."_";
}
- }
// Parse query string and set (default) values
parse_str($server_vars['QUERY_STRING'], $parameters);
@@ -64,6 +72,20 @@ protected function _populate($server_vars) {
}
+ /**
+ * @param $name
+ * @return bool
+ */
+ protected function _findMapping($name) {
+ global $config;
+ return isset ($config['mapping'][$name]) ? $config['mapping'][$name] : false;
+ }
+
+
+ /**
+ * @param $name
+ * @return bool
+ */
public function addUrlElement($element, $resource, $path) {
$this->_url_elements[] = array(
'element' => $element,
@@ -72,6 +94,11 @@ public function addUrlElement($element, $resource, $path) {
);
}
+
+ /**
+ * @param $name
+ * @return bool
+ */
public function getUrlElements() {
return $this->_url_elements;
}
@@ -137,7 +164,12 @@ public function setResource($resource)
public function isResource()
{
- return $this->_resource;
+ return ($this->getResource() !== null);
+ }
+
+ public function getResource() {
+ $element = $this->getMainElement();
+ return $element['resource'];
}
public function setVerb($verb)
@@ -30,6 +30,9 @@ function handle(ji_request $request) {
$this->setResponse(new ji_response());
}
+ // Check if we need to call resource or collection
+ $entity = $request->isResource() ? "Resource" : "Collection";
+
/**
* HTTP PUT and POST are pretty much interchangeable. Can be configured here
*/
@@ -40,11 +43,11 @@ function handle(ji_request $request) {
break;
case "put" :
// PUT is create or update, depending on resource or collection
- $crudMethod = ($entity == "Resource") ? "Update" : "Create";
+ $crudMethod = ($request->isResource()) ? "Update" : "Create";
break;
case "post" :
// POST is create or update, depending on resource or collection
- $crudMethod = ($entity == "Resource") ? "Update" : "Create";
+ $crudMethod = ($request->isResource()) ? "Update" : "Create";
break;
case "get" :
default :
@@ -53,9 +56,6 @@ function handle(ji_request $request) {
break;
}
- // Check if we need to call resource or collection
- $entity = $request->isResource() ? "Resource" : "Collection";
-
// Call function
$handleFunc = "handle".$crudMethod.$entity;
$this->$handleFunc();
View
@@ -1,23 +1,19 @@
<?php
-print "<pre>";
-
// Full error reporting
error_reporting(-1);
ini_set('display_errors', true);
+// Include rest mapping
+include_once("../config/restmap.php");
+
// Setup
set_exception_handler('handle_exception');
-setupDb();
$request = new ji_request($_SERVER);
// Pass through parent controllers if needed (in case of /event/%d/talk/%d/comment/%d)
foreach ($request->getParentElements() as $element) {
- print "PARENT ELEMENTS! ".print_r($element,true);
- $tmp = findController($element['element']);
- if (! $tmp) die ("Cannot find controller: ".$element['element']);
-
- $controllerClass = $element['path'].$tmp."Controller";
+ $controllerClass = $element['path'].$tmp[0]."Controller";
print "Controller: ".$controllerClass." : ";
// Check if we are allowed to continue
@@ -33,9 +29,7 @@
// Call the actual endpoint controller (we call handle(), not passthrough() and we call without resource)
$element = $request->getMainElement();
-$tmp = findController($element['element']);
-if (! $tmp) die ("Cannot find controller ".$element['element']);
-$controllerClass = $element['path'].$tmp."Controller";
+$controllerClass = $element['path'].$element['element']."Controller";
$controller = new $controllerClass();
$response = $controller->handle($request);
@@ -60,24 +54,7 @@ function output(ji_response $response) {
}
-/**
- * Controller mapper. This makes it possible to map certain uri-elements to controllers.
- * For instance: "/events/%d" to the event-controller, but also "/children" to the
- * child-controller if needed.
- *
- * It means we have to map each URL element for now, but I prefer this over auto-detection
- * for the time being.
- */
-function findController($name) {
- $mappers = array(
- 'event' => 'event', 'events' => 'event',
- 'talk' => 'talk', 'talks' => 'talk',
- 'user' => 'user', 'users' => 'user',
- 'comment' => 'comment', 'comments' => 'comment',
- 'default' => 'default'
- );
- return isset ($mappers[$name]) ? $mappers[$name] : false;
-}
+
/**
@@ -117,19 +94,3 @@ function __autoload($classname) {
return true;
}
}
-
-
-/**
- * @return void
- */
-function setupDb() {
- // config setup
- define('BASEPATH', '.');
-
- // Assumes that here we find CodeIgnited database config...
- require_once('../../system/application/config/database.php');
- $ji_db = new PDO('mysql:host=' . $db['default']['hostname'] .
- ';dbname=' . $db['default']['database'],
- $db['default']['username'],
- $db['default']['password']);
-}

0 comments on commit 568847f

Please sign in to comment.