--- /dev/null
+<?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'];
+ }
+}
public $url = '';
/*! @var array HTTP request data. */
- public $data = NULL;
+ public $data = array();
}
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
/*! @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;
-}
--- /dev/null
+<?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;
+}
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.
$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;
}