* Add ActionController
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 11 Jun 2011 18:37:37 +0000 (14:37 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 11 Jun 2011 18:37:37 +0000 (14:37 -0400)
* Split out ResponseCode into its own file

http/action_controller.php [new file with mode: 0644]
http/request.php
http/response.php
http/response_code.php [new file with mode: 0644]
http/rest_action.php

diff --git a/http/action_controller.php b/http/action_controller.php
new file mode 100644 (file)
index 0000000..3da7039
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+// Hoplite
+// Copyright (c) 2011 Blue Static
+// 
+// This program is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation, either version 3 of the License, or any later version.
+// 
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program.  If not, see <http://www.gnu.org/licenses/>.
+
+namespace hoplite\http;
+
+require_once HOPLITE_ROOT . '/http/action.php';
+require_once HOPLITE_ROOT . '/http/response_code.php';
+
+/*!
+  An ActionController is an Action that operates like a typical MVC Controller.
+  It will look at the Request's data for a key named 'action', which should
+  correspond to a method named 'Action$action'.
+*/
+class ActionController extends Action
+{
+  /*!
+    Forwards the request/response pair to an internal method based on a key in
+    the Request object.
+  */
+  public function Invoke(Request $request, Response $response)
+  {
+    $method = $this->_GetActionMethod($request);
+    if (!method_exists($this, $method)) {
+      $response->response_code = ResponseCode::NOT_FOUND;
+      $this->controller()->Stop();
+      return;
+    }
+
+    $this->$method($request, $response);
+  }
+
+  /*!
+    Returns the method name to invoke based on the Request.
+    @return string|NULL
+  */
+  protected function _GetActionMethod(Request $request)
+  {
+    if (!isset($request->data['action']))
+      return NULL;
+    return 'Action' . $request->data['action'];
+  }
+}
index aaa7ac56c9dd6fd33bde7b05547f85c7af5a7846..7f0590e01e6768dbdd4104f2ab9e0f63d102f889 100644 (file)
@@ -31,5 +31,5 @@ class Request extends \hoplite\base\StrictObject
   public $url = '';
 
   /*! @var array HTTP request data. */
-  public $data = NULL;
+  public $data = array();
 }
index ebebf1322886633aba8bd9b303f1361541c5f703..86f2d02ec16558d50f4c18990d353f91c508345a 100644 (file)
@@ -17,6 +17,7 @@
 namespace hoplite\http;
 
 require_once HOPLITE_ROOT . 'base/strict_object.php';
+require_once HOPLITE_ROOT . 'http/response_code.php';
 
 /*!
   A Response holds data processed by Action objects. When the RootController is
@@ -43,50 +44,3 @@ class Response extends \hoplite\base\StrictObject
   /*! @var array Model data. */
   public $data = array();
 }
-
-/*!
-  An enumeration of all the HTTP status codes as constants. This is the complete
-  list of codes. Not all will be usable by an application.
-  @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
-*/
-class ResponseCode
-{
-  const 100 = CONTINUE;
-  const 101 = SWITCHING_PROTOCOLS;
-  const 200 = OK;
-  const 201 = CREATED;
-  const 202 = ACCEPTED;
-  const 204 = NO_CONTENT;
-  const 205 = RESET_CONTENT;
-  const 206 = PARTIAL_CONTENT;
-  const 300 = MULTIPLE_CHOICES;
-  const 301 = MOVED_PERMANENTLY;
-  const 302 = FOUND;
-  const 303 = SEE_OTHER;
-  const 304 = NOT_MODIFIED;
-  const 305 = USE_PROXY;
-  const 307 = TEMPORARY_REDIRECT;
-  const 400 = BAD_REQUEST;
-  const 401 = UNAUTHORIZED;
-  const 402 = PAYMENT_REQUIRED;
-  const 403 = FORBIDDEN;
-  const 404 = NOT_FOUND;
-  const 405 = METHOD_NOT_ALLOWED;
-  const 406 = NOT_ACCEPTABLE;
-  const 407 = PROXY_AUTHENTICATION_REQUIRED;
-  const 408 = REQUEST_TIMEOUT;
-  const 409 = CONFLICT;
-  const 410 = GONE;
-  const 411 = LENGTH_REQUIRED;
-  const 412 = PRECONDITION_FAILED;
-  const 413 = REQUEST_ENTITY_TOO_LARGE;
-  const 415 = UNSUPPORTED_MEDIA_TYPE;
-  const 416 = REQUESTED_RANGE_NOT_SATISFIABLE;
-  const 417 = EXPECTATION_FAILED;
-  const 500 = INTERNAL_SERVER_ERROR;
-  const 501 = NOT_IMPLEMENTED;
-  const 502 = BAD_GATEWAY;
-  const 503 = SERVICE_UNAVAILABLE;
-  const 504 = GATEWAY_TIMEOUT;
-  const 505 = HTTP_VERSION_NOT_SUPPORTED;
-}
diff --git a/http/response_code.php b/http/response_code.php
new file mode 100644 (file)
index 0000000..ab8cd54
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+// Hoplite
+// Copyright (c) 2011 Blue Static
+// 
+// This program is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation, either version 3 of the License, or any later version.
+// 
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program.  If not, see <http://www.gnu.org/licenses/>.
+
+namespace hoplite\http;
+
+/*!
+  An enumeration of all the HTTP status codes as constants. This is the complete
+  list of codes. Not all will be usable by an application.
+  @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
+*/
+class ResponseCode
+{
+  const CONTINUE                         = 100;
+  const SWITCHING_PROTOCOLS              = 101;
+  const OK                               = 200;
+  const CREATED                          = 201;
+  const ACCEPTED                         = 202;
+  const NO_CONTENT                       = 204;
+  const RESET_CONTENT                    = 205;
+  const PARTIAL_CONTENT                  = 206;
+  const MULTIPLE_CHOICES                 = 300;
+  const MOVED_PERMANENTLY                = 301;
+  const FOUND                            = 302;
+  const SEE_OTHER                        = 303;
+  const NOT_MODIFIED                     = 304;
+  const USE_PROXY                        = 305;
+  const TEMPORARY_REDIRECT               = 307;
+  const BAD_REQUEST                      = 400;
+  const UNAUTHORIZED                     = 401;
+  const PAYMENT_REQUIRED                 = 402;
+  const FORBIDDEN                        = 403;
+  const NOT_FOUND                        = 404;
+  const METHOD_NOT_ALLOWED               = 405;
+  const NOT_ACCEPTABLE                   = 406;
+  const PROXY_AUTHENTICATION_REQUIRED    = 407;
+  const REQUEST_TIMEOUT                  = 408;
+  const CONFLICT                         = 409;
+  const GONE                             = 410;
+  const LENGTH_REQUIRED                  = 411;
+  const PRECONDITION_FAILED              = 412;
+  const REQUEST_ENTITY_TOO_LARGE         = 413;
+  const UNSUPPORTED_MEDIA_TYPE           = 415;
+  const REQUESTED_RANGE_NOT_SATISFIABLE  = 416;
+  const EXPECTATION_FAILED               = 417;
+  const INTERNAL_SERVER_ERROR            = 500;
+  const NOT_IMPLEMENTED                  = 501;
+  const BAD_GATEWAY                      = 502;
+  const SERVICE_UNAVAILABLE              = 503;
+  const GATEWAY_TIMEOUT                  = 504;
+  const HTTP_VERSION_NOT_SUPPORTED       = 505;
+}
index 1d6c20d31345c466d98663e2964dbb018573167a..9300d4c8c00da13ec483932d3ad17cc90c55dd66 100644 (file)
@@ -16,6 +16,9 @@
 
 namespace hoplite\http;
 
+require_once HOPLITE_ROOT . '/http/action.php';
+require_once HOPLITE_ROOT . '/http/response_code.php';
+
 /*!
   This abstract class is the base for handling all requests and filling out
   response objects.
@@ -30,7 +33,7 @@ class RestAction extends Action
     $valid_methods = array('get', 'post', 'delete', 'put');
     $method = strtolower($request->http_method);
     if (!in_array($method, $valid_methods)) {
-      $response->http_code = 405 /* METHOD_NOT_ALLOWED */;
+      $response->http_code = ResponseCode::METHOD_NOT_ALLOWED;
       $this->controller()->Stop();
       return;
     }