Namespace API sample.

Add and update comments.
This commit is contained in:
PJ Dietz 2012-12-05 16:08:30 -05:00
parent ff331935d2
commit a2255bebd9
5 changed files with 44 additions and 9 deletions

View File

@ -1,7 +1,12 @@
<?php <?php
namespace apisample;
require_once(dirname(__FILE__) . '/../../Router.inc.php'); require_once(dirname(__FILE__) . '/../../Router.inc.php');
/**
* Loads and instantiates handlers based on URI.
*/
class ApiSampleRouter extends \wellrested\Router { class ApiSampleRouter extends \wellrested\Router {
public function __construct() { public function __construct() {
@ -9,16 +14,16 @@ class ApiSampleRouter extends \wellrested\Router {
parent::__construct(); parent::__construct();
$this->addTemplate('/articles/', $this->addTemplate('/articles/',
'\handlers\ArticleCollectionHandler', 'ArticleCollectionHandler',
'ArticleCollectionHandler.inc.php'); 'ArticleCollectionHandler.inc.php');
$this->addTemplate('/articles/{id}', $this->addTemplate('/articles/{id}',
'\handlers\ArticleItemHandler', 'ArticleItemHandler',
'ArticleItemHandler.inc.php', 'ArticleItemHandler.inc.php',
array('id' => \wellrested\Route::RE_NUM)); array('id' => \wellrested\Route::RE_NUM));
$this->addTemplate('/articles/{slug}', $this->addTemplate('/articles/{slug}',
'\handlers\ArticleItemHandler', 'ArticleItemHandler',
'ArticleItemHandler.inc.php', 'ArticleItemHandler.inc.php',
array('slug' => \wellrested\Route::RE_SLUG)); array('slug' => \wellrested\Route::RE_SLUG));
@ -28,6 +33,7 @@ class ApiSampleRouter extends \wellrested\Router {
// Customize as needed based on your server. // Customize as needed based on your server.
$template = '/wellrested/samples/apisample' . $template; $template = '/wellrested/samples/apisample' . $template;
$handlerClassName = '\apisample\handlers\\' . $handlerClassName;
$handlerFilePath = dirname(__FILE__) . '/handlers/' . $handlerFilePath; $handlerFilePath = dirname(__FILE__) . '/handlers/' . $handlerFilePath;
$this->addRoute(\wellrested\Route::newFromUriTemplate( $this->addRoute(\wellrested\Route::newFromUriTemplate(

View File

@ -1,5 +1,10 @@
<?php <?php
namespace apisample\data;
/**
* Simple class for reading and writing articles to a text file.
*/
class ArticlesControler { class ArticlesControler {
public $data; public $data;

View File

@ -1,16 +1,22 @@
<?php <?php
namespace handlers; namespace apisample\handlers;
require_once(dirname(__FILE__) . '/../../../Handler.inc.php'); require_once(dirname(__FILE__) . '/../../../Handler.inc.php');
require_once(dirname(__FILE__) . '/../data/ArticlesControler.inc.php'); require_once(dirname(__FILE__) . '/../data/ArticlesControler.inc.php');
/**
* Handler class for a list of articles.
*/
class ArticleCollectionHandler extends \wellrested\Handler { class ArticleCollectionHandler extends \wellrested\Handler {
/**
* Respond to a GET request.
*/
protected function get() { protected function get() {
// Display the list of articles. // Display the list of articles.
$articles = new \ArticlesControler(); $articles = new \apisample\data\ArticlesControler();
if (isset($articles->data)) { if (isset($articles->data)) {
@ -28,6 +34,9 @@ class ArticleCollectionHandler extends \wellrested\Handler {
} }
/**
* Respond to a POST request.
*/
protected function post() { protected function post() {
// Read the request body, and ensure it is in the proper format. // Read the request body, and ensure it is in the proper format.
@ -64,7 +73,7 @@ class ArticleCollectionHandler extends \wellrested\Handler {
} }
// Ensure slug is not a duplicate. // Ensure slug is not a duplicate.
$articles = new \ArticlesControler(); $articles = new \apisample\data\ArticlesControler();
if ($articles->getArticleBySlug($article['slug']) !== false) { if ($articles->getArticleBySlug($article['slug']) !== false) {
$this->response->statusCode = 409; $this->response->statusCode = 409;
$this->response->setHeader('Content-type', 'text/plain'); $this->response->setHeader('Content-type', 'text/plain');

View File

@ -1,16 +1,25 @@
<?php <?php
namespace handlers; namespace apisample\handlers;
require_once(dirname(__FILE__) . '/../../../Handler.inc.php'); require_once(dirname(__FILE__) . '/../../../Handler.inc.php');
require_once(dirname(__FILE__) . '/../data/ArticlesControler.inc.php'); require_once(dirname(__FILE__) . '/../data/ArticlesControler.inc.php');
/**
* Handler class for one specific article.
*
* When instantiated by the Router, this class should receive an id or slug
* argument to identify the article.
*/
class ArticleItemHandler extends \wellrested\Handler { class ArticleItemHandler extends \wellrested\Handler {
/**
* Respond to a GET request.
*/
protected function get() { protected function get() {
// Read the list of articles. // Read the list of articles.
$articles = new \ArticlesControler(); $articles = new \apisample\data\ArticlesControler();
$article = false; $article = false;
@ -41,6 +50,9 @@ class ArticleItemHandler extends \wellrested\Handler {
} }
/**
* Respond to a PUT request.
*/
protected function put() { protected function put() {
// Read the request body, and ensure it is in the proper format. // Read the request body, and ensure it is in the proper format.
@ -141,6 +153,9 @@ class ArticleItemHandler extends \wellrested\Handler {
} }
/**
* Respond to a DELETE request.
*/
protected function delete() { protected function delete() {
// Read the list of articles. // Read the list of articles.

View File

@ -2,7 +2,7 @@
require_once('ApiSampleRouter.inc.php'); require_once('ApiSampleRouter.inc.php');
$router = new ApiSampleRouter(); $router = new \apisample\ApiSampleRouter();
$response = $router->getResponse(); $response = $router->getResponse();
$response->respond(); $response->respond();
exit; exit;