Update Router and Route to require autoloading

Update samples to use new Router and Route
Refactor ApiSample namespace
This commit is contained in:
PJ Dietz 2013-01-24 21:48:44 -05:00
parent 8597b9be06
commit 298bdab35f
7 changed files with 16 additions and 34 deletions

View File

@ -1,6 +1,6 @@
<?php
namespace apisample;
namespace ApiSample;
use pjdietz\WellRESTed\Router;
use pjdietz\WellRESTed\Route;
@ -18,38 +18,30 @@ class ApiSampleRouter extends Router
$this->addTemplate(
'/articles/',
'ArticleCollectionHandler',
'ArticleCollectionHandler.php'
'ArticleCollectionHandler'
);
$this->addTemplate(
'/articles/{id}',
'ArticleItemHandler',
'ArticleItemHandler.inc.php',
array('id' => Route::RE_NUM)
);
$this->addTemplate(
'/articles/{slug}',
'ArticleItemHandler',
'ArticleItemHandler.inc.php',
array('slug' => Route::RE_SLUG)
);
}
public function addTemplate($template, $handlerClassName, $handlerFilePath, $variables = null)
public function addTemplate($template, $handlerClassName, $variables = null)
{
// Customize as needed based on your server.
$template = '/wellrested/samples/apisample' . $template;
$handlerClassName = '\apisample\handlers\\' . $handlerClassName;
$handlerFilePath = dirname(__FILE__) . '/handlers/' . $handlerFilePath;
$this->addRoute(
Route::newFromUriTemplate(
$template,
$handlerClassName,
$handlerFilePath,
$variables
)
);

View File

@ -1,6 +1,6 @@
<?php
namespace apisample;
namespace ApiSample;
/**
* Simple class for reading and writing articles to a text file.

View File

@ -1,6 +1,6 @@
<?php
namespace apisample\handlers;
namespace ApiSample\Handlers;
use \pjdietz\WellRESTed\Handler;

View File

@ -1,6 +1,6 @@
<?php
namespace apisample\handlers;
namespace ApiSample\Handlers;
use \pjdietz\WellRESTed\Handler;

View File

@ -2,7 +2,14 @@
require_once('../../vendor/autoload.php');
$router = new \apisample\ApiSampleRouter();
// Have to add these manually since this isn't part of the normal package.
// Your project won't have to do this.
require_once('ApiSampleRouter.php');
require_once('ArticlesController.php');
require_once('Handlers/ArticleCollectionHandler.php');
require_once('Handlers/ArticleItemHandler.php');
$router = new \ApiSample\ApiSampleRouter();
$response = $router->getResponse();
$response->respond();
exit;

View File

@ -39,23 +39,14 @@ class Route
*/
public $handler;
/**
* The path to the source file defing the handler class.
*
* @var string
*/
public $handlerPath;
/**
* @param $pattern
* @param $handler
* @param $handlerPath
*/
public function __construct($pattern, $handler, $handlerPath = null)
public function __construct($pattern, $handler)
{
$this->pattern = $pattern;
$this->handler = $handler;
$this->handlerPath = $handlerPath;
}
/**
@ -63,7 +54,6 @@ class Route
*
* @param string $uriTemplate
* @param string $handler
* @param string $handlerPath
* @param array $variables
* @throws \Exception
* @return Route
@ -71,7 +61,6 @@ class Route
static public function newFromUriTemplate(
$uriTemplate,
$handler,
$handlerPath = null,
$variables = null
) {
@ -133,7 +122,7 @@ class Route
$pattern = '/^' . $pattern . '$/';
$klass = __CLASS__;
$route = new $klass($pattern, $handler, $handlerPath);
$route = new $klass($pattern, $handler);
return $route;
}

View File

@ -55,12 +55,6 @@ class Router
if (preg_match($route->pattern, $path, $matches)) {
$klass = $route->handler;
// Autoload, if needed.
if (is_string($route->handlerPath) && !class_exists($klass)) {
require_once($route->handlerPath);
}
$handler = new $klass($request, $matches);
return $handler->response;