Commits

Anonymous committed 643debe

initial add of the files from my toy RESTful example

Comments (0)

Files changed (6)

controllers/MyController.php

+<?php
+
+class MyController
+{
+}

controllers/UsersController.php

+<?php
+
+class UsersController extends MyController
+{
+    public function getAction($request) {
+        if(isset($request->url_elements[2])) {
+            $user_id = (int)$request->url_elements[2];
+            if(isset($request->url_elements[3])) {
+                switch($request->url_elements[3]) {
+                case 'friends':
+                    $data["message"] = "user " . $user_id . "has many friends";
+                    break;
+                default:
+                    // do nothing, this is not a supported action
+                    break;
+                }
+            } else {
+                $data["message"] = "here is the info for user " . $user_id;
+            }
+        } else {
+            $data["message"] = "you want a list of users";
+        }
+        return $data;
+    }
+
+    public function postAction($request) {
+        $data = $request->parameters;
+        $data['message'] = "This data was submitted";
+        return $data;
+    }
+}
+<?php
+
+spl_autoload_register('apiAutoload');
+function apiAutoload($classname)
+{
+    if (preg_match('/[a-zA-Z]+Controller$/', $classname)) {
+        include __DIR__ . '/controllers/' . $classname . '.php';
+        return true;
+    } elseif (preg_match('/[a-zA-Z]+Model$/', $classname)) {
+        include __DIR__ . '/models/' . $classname . '.php';
+        return true;
+    } elseif (preg_match('/[a-zA-Z]+View$/', $classname)) {
+        include __DIR__ . '/views/' . $classname . '.php';
+        return true;
+    } else {
+        include __DIR__ . '/library/' . str_replace('_', DIRECTORY_SEPARATOR, $classname) . '.php';
+        return true;
+    }
+    return false;
+}
+
+$request = new Request();
+
+// route the request to the right place
+$controller_name = ucfirst($request->url_elements[1]) . 'Controller';
+if (class_exists($controller_name)) {
+    $controller = new $controller_name();
+    $action_name = strtolower($request->verb) . 'Action';
+    $result = $controller->$action_name($request);
+
+    $view_name = ucfirst($request->format) . 'View';
+    if(class_exists($view_name)) {
+        $view = new $view_name();
+        $view->render($result);
+    }
+}
+
+<?php
+
+class Request {
+    public $url_elements;
+    public $verb;
+    public $parameters;
+
+    public function __construct() {
+        $this->verb = $_SERVER['REQUEST_METHOD'];
+        $this->url_elements = explode('/', $_SERVER['PATH_INFO']);
+
+        $this->parseIncomingParams();
+        // initialise json as default format
+        $this->format = 'json';
+        if(isset($this->parameters['format'])) {
+            $this->format = $this->parameters['format'];
+        }
+        return true;
+    }
+
+    public function parseIncomingParams() {
+        $parameters = array();
+
+        // first of all, pull the GET vars
+        if (isset($_SERVER['QUERY_STRING'])) {
+            parse_str($_SERVER['QUERY_STRING'], $parameters);
+        }
+
+        // now how about PUT/POST bodies? These override what we got from GET
+        $body = file_get_contents("php://input");
+        $content_type = false;
+        if(isset($_SERVER['CONTENT_TYPE'])) {
+            $content_type = $_SERVER['CONTENT_TYPE'];
+        }
+        switch($content_type) {
+            case "application/json":
+                $body_params = json_decode($body);
+                if($body_params) {
+                    foreach($body_params as $param_name => $param_value) {
+                        $parameters[$param_name] = $param_value;
+                    }
+                }
+                $this->format = "json";
+                break;
+            case "application/x-www-form-urlencoded":
+                parse_str($body, $postvars);
+                foreach($postvars as $field => $value) {
+                    $parameters[$field] = $value;
+
+                }
+                $this->format = "html";
+                break;
+            default:
+                // we could parse other supported formats here
+                break;
+        }
+        $this->parameters = $parameters;
+    }
+}
+<?php
+
+class ApiView {
+    protected function addCount($data) {
+        if(!empty($data)) {
+            // do nothing, this is added earlier
+        } else {
+            $data['meta']['count'] = 0;
+        }
+        return $data;
+    }
+}
+<?php
+
+class JsonView extends ApiView {
+    public function render($content) {
+        header('Content-Type: application/json; charset=utf8');
+        echo json_encode($content);
+        return true;
+    }
+}