From 298bdab35f0d923f2f68d2fbf6567d8ab0d1a42b Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 24 Jan 2013 21:48:44 -0500 Subject: [PATCH] Update Router and Route to require autoloading Update samples to use new Router and Route Refactor ApiSample namespace --- samples/apisample/ApiSampleRouter.php | 14 +++----------- samples/apisample/ArticlesController.php | 2 +- .../handlers/ArticleCollectionHandler.php | 2 +- ...ItemHandler.inc.php => ArticleItemHandler.php} | 2 +- samples/apisample/index.php | 9 ++++++++- src/pjdietz/WellRESTed/Route.php | 15 ++------------- src/pjdietz/WellRESTed/Router.php | 6 ------ 7 files changed, 16 insertions(+), 34 deletions(-) rename samples/apisample/handlers/{ArticleItemHandler.inc.php => ArticleItemHandler.php} (99%) diff --git a/samples/apisample/ApiSampleRouter.php b/samples/apisample/ApiSampleRouter.php index e4e720b..e6dff38 100644 --- a/samples/apisample/ApiSampleRouter.php +++ b/samples/apisample/ApiSampleRouter.php @@ -1,6 +1,6 @@ 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 ) ); diff --git a/samples/apisample/ArticlesController.php b/samples/apisample/ArticlesController.php index b8349da..740a306 100644 --- a/samples/apisample/ArticlesController.php +++ b/samples/apisample/ArticlesController.php @@ -1,6 +1,6 @@ getResponse(); $response->respond(); exit; diff --git a/src/pjdietz/WellRESTed/Route.php b/src/pjdietz/WellRESTed/Route.php index 60e05c4..951cd92 100644 --- a/src/pjdietz/WellRESTed/Route.php +++ b/src/pjdietz/WellRESTed/Route.php @@ -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; } diff --git a/src/pjdietz/WellRESTed/Router.php b/src/pjdietz/WellRESTed/Router.php index 9a1fb89..fccbe67 100644 --- a/src/pjdietz/WellRESTed/Router.php +++ b/src/pjdietz/WellRESTed/Router.php @@ -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;