[Phphtmllib-devel] SF.net SVN: phphtmllib:[3318] trunk/phphtmllib/src
Status: Beta
Brought to you by:
hemna
From: <he...@us...> - 2010-01-07 20:37:39
|
Revision: 3318 http://phphtmllib.svn.sourceforge.net/phphtmllib/?rev=3318&view=rev Author: hemna Date: 2010-01-07 20:37:29 +0000 (Thu, 07 Jan 2010) Log Message: ----------- started working on REST server interfaces Added Paths: ----------- trunk/phphtmllib/src/rest/ trunk/phphtmllib/src/rest/RESTController.inc trunk/phphtmllib/src/rest/RESTInterface.inc trunk/phphtmllib/src/rest/RESTTarget.inc Added: trunk/phphtmllib/src/rest/RESTController.inc =================================================================== --- trunk/phphtmllib/src/rest/RESTController.inc (rev 0) +++ trunk/phphtmllib/src/rest/RESTController.inc 2010-01-07 20:37:29 UTC (rev 3318) @@ -0,0 +1,91 @@ +<?php +/** + * The Base Controller + * + * @author Walter A. Boring IV + * @package phpHtmlLib-framework + * @subpackage Controller + */ + + +/** + * This class handles dealing with REST requests. + * It makes sure the target can handle a REST specific request + * for each of the REST type of requests + * + * GET/PUT/DELETE/POST + * + */ +class RESTController extends Controller { + + //TODO create mechanism to show + //list of REST services. + //which will become the default + //target. + + protected $default_target = "TODO"; + + + function __construct($prefix) { + + if ($prefix) { + // set directory prefix to use + // all our requests will go here + RequestBuilder::set_file($prefix); + } + + if (!($this->target = Request::singleton()->get_target())) { + // no target specified + // use default target + $this->target = $this->default_target; + } + + // instantiate the object + $this->obj = new $this->target; + + // register request variables + // Request::singleton()->register($this->obj->request_vars()); + + if ($this->obj instanceof FormContent) { + // this is a form content + // let's wrap it into FormProcessor + $this->obj = $this->get_form_processor_object($this->obj); + } + } + + function execute() { + + //make sure the target object implements the REST interface + $class = new ReflectionObject($this->obj); + + $request = Request::Singleton(); + + $request->register($this->obj->request_vars()); + + switch ($request->get_request_method()) { + case Request::METHOD_GET: + $request->register($this->obj->request_get_vars()); + $this->obj->do_GET(); + break; + + case Request::METHOD_POST: + $request->register($this->obj->request_post_vars()); + $this->obj->do_POST(); + break; + + case Request::METHOD_PUT: + $request->register($this->obj->request_put_vars()); + $this->obj->do_PUT(); + break; + + case Request::METHOD_DELETE: + $request->register($this->obj->request_delete_vars()); + $this->obj->do_DELETE(); + break; + } + + } + + +} +?> \ No newline at end of file Added: trunk/phphtmllib/src/rest/RESTInterface.inc =================================================================== --- trunk/phphtmllib/src/rest/RESTInterface.inc (rev 0) +++ trunk/phphtmllib/src/rest/RESTInterface.inc 2010-01-07 20:37:29 UTC (rev 3318) @@ -0,0 +1,46 @@ +<?php +/** + * This file contains the REST Interface class + * + * @author Walter A. Boring IV + * @package phpHtmlLib-framework + * @subpackage Controller + */ + + +/** + * This interface describes methods that must be + * implemented to handle REST requests. + * + */ +interface RESTInterface { + + /** + * This method is called to handle a GET + * query. + * + */ + public function do_GET(); + + /** + * This method is called to handle a POST + * request. + * + */ + public function do_POST(); + + /** + * This method is called to handle a PUT + * request + * + */ + public function do_PUT(); + + + /** + * This method is called to handle a DELETE + * + */ + public function do_DELETE(); +} +?> \ No newline at end of file Added: trunk/phphtmllib/src/rest/RESTTarget.inc =================================================================== --- trunk/phphtmllib/src/rest/RESTTarget.inc (rev 0) +++ trunk/phphtmllib/src/rest/RESTTarget.inc 2010-01-07 20:37:29 UTC (rev 3318) @@ -0,0 +1,160 @@ +<?php + +/** + * This class is designed to handle returning proper + * http result headers for a REST target. + * + * + * @author waboring + * + */ +class RESTTarget implements RESTInterface { + + const HTTP_400 = "400 Bad Request"; + const HTTP_404 = "404 Not Found"; + const HTTP_415 = "415 Unsupported Media Type"; + + + + /** + * This contains an array of vars used by all + * REST requests. + * + * The child class defines this to register + * these vars as requred for EVERY request. + */ + protected $request_vars = array(); + + /** + * request variables required by a POST + * @var array + */ + protected $post_vars = array(); + + /** + * request variables required by a GET + * + * @var array + */ + protected $get_vars = array(); + + /** + * request variables required by a PUT + * @var array + */ + protected $put_vars = array(); + + /** + * request variables required by a DELETE + * @var array + */ + protected $delete_vars = array(); + + /** + * If we are called here, + * that means the child doesn't support GET + * + * @see lib/external/phphtmllib/src/rest/RESTInterface#do_GET() + */ + public function do_GET() { + $this->error(RESTTarget::HTTP_404); + } + + /** + * If we are called here, + * that means the child doesn't support PUT + * + * @see lib/external/phphtmllib/src/rest/RESTInterface#do_PUT() + */ + public function do_PUT() { + $this->error(RESTTarget::HTTP_404); + } + + /** + * If we are called here, + * that means the child doesn't support POST + * + * @see lib/external/phphtmllib/src/rest/RESTInterface#do_POST() + */ + public function do_POST() { + $this->error(RESTTarget::HTTP_404); + } + + /** + * If we are called here, + * that means the child doesn't support DELETE + * + * @see lib/external/phphtmllib/src/rest/RESTInterface#do_DELETE() + */ + public function do_DELETE() { + $this->error(RESTTarget::HTTP_404); + } + + + /** + * This method returns an array of + * request variable names that + * are required for any REST request + * + * @return unknown_type + */ + public function request_vars() { + return $this->request_vars; + } + + /** + * This method is called by the RESTController + * to get the list of required variables + * for a GET + * + * @return unknown_type + */ + public function request_get_vars() { + return $this->get_vars; + } + + /** + * This method is called by the RESTController + * to get the list of required variables + * for a POST + * + * @return unknown_type + */ + public function request_post_vars() { + return $this->post_vars; + } + + /** + * This method is called by the RESTController + * to get the list of required variables + * for a PUT + * + * @return unknown_type + */ + public function request_put_vars() { + return $this->put_vars; + } + + /** + * This method is called by the RESTController + * to get the list of required variables + * for a DELETE + * + * @return unknown_type + */ + public function request_delete_vars() { + return $this->delete_vars; + } + + /** + * Throw out an http error code and exit + * + * @param String $code + * @return none + */ + protected function error($code) { + header("HTTP/1.0 ".$code); + exit; + } +} +?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |