Update Router and Route to require autoloading
Update samples to use new Router and Route Refactor ApiSample namespace
This commit is contained in:
parent
8597b9be06
commit
298bdab35f
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace apisample;
|
namespace ApiSample;
|
||||||
|
|
||||||
use pjdietz\WellRESTed\Router;
|
use pjdietz\WellRESTed\Router;
|
||||||
use pjdietz\WellRESTed\Route;
|
use pjdietz\WellRESTed\Route;
|
||||||
|
|
@ -18,38 +18,30 @@ class ApiSampleRouter extends Router
|
||||||
|
|
||||||
$this->addTemplate(
|
$this->addTemplate(
|
||||||
'/articles/',
|
'/articles/',
|
||||||
'ArticleCollectionHandler',
|
'ArticleCollectionHandler'
|
||||||
'ArticleCollectionHandler.php'
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->addTemplate(
|
$this->addTemplate(
|
||||||
'/articles/{id}',
|
'/articles/{id}',
|
||||||
'ArticleItemHandler',
|
'ArticleItemHandler',
|
||||||
'ArticleItemHandler.inc.php',
|
|
||||||
array('id' => Route::RE_NUM)
|
array('id' => Route::RE_NUM)
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->addTemplate(
|
$this->addTemplate(
|
||||||
'/articles/{slug}',
|
'/articles/{slug}',
|
||||||
'ArticleItemHandler',
|
'ArticleItemHandler',
|
||||||
'ArticleItemHandler.inc.php',
|
|
||||||
array('slug' => Route::RE_SLUG)
|
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.
|
// Customize as needed based on your server.
|
||||||
$template = '/wellrested/samples/apisample' . $template;
|
$template = '/wellrested/samples/apisample' . $template;
|
||||||
$handlerClassName = '\apisample\handlers\\' . $handlerClassName;
|
$handlerClassName = '\apisample\handlers\\' . $handlerClassName;
|
||||||
$handlerFilePath = dirname(__FILE__) . '/handlers/' . $handlerFilePath;
|
|
||||||
|
|
||||||
$this->addRoute(
|
$this->addRoute(
|
||||||
Route::newFromUriTemplate(
|
Route::newFromUriTemplate(
|
||||||
$template,
|
$template,
|
||||||
$handlerClassName,
|
$handlerClassName,
|
||||||
$handlerFilePath,
|
|
||||||
$variables
|
$variables
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace apisample;
|
namespace ApiSample;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple class for reading and writing articles to a text file.
|
* Simple class for reading and writing articles to a text file.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace apisample\handlers;
|
namespace ApiSample\Handlers;
|
||||||
|
|
||||||
use \pjdietz\WellRESTed\Handler;
|
use \pjdietz\WellRESTed\Handler;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace apisample\handlers;
|
namespace ApiSample\Handlers;
|
||||||
|
|
||||||
use \pjdietz\WellRESTed\Handler;
|
use \pjdietz\WellRESTed\Handler;
|
||||||
|
|
||||||
|
|
@ -2,7 +2,14 @@
|
||||||
|
|
||||||
require_once('../../vendor/autoload.php');
|
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 = $router->getResponse();
|
||||||
$response->respond();
|
$response->respond();
|
||||||
exit;
|
exit;
|
||||||
|
|
|
||||||
|
|
@ -39,23 +39,14 @@ class Route
|
||||||
*/
|
*/
|
||||||
public $handler;
|
public $handler;
|
||||||
|
|
||||||
/**
|
|
||||||
* The path to the source file defing the handler class.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
public $handlerPath;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $pattern
|
* @param $pattern
|
||||||
* @param $handler
|
* @param $handler
|
||||||
* @param $handlerPath
|
|
||||||
*/
|
*/
|
||||||
public function __construct($pattern, $handler, $handlerPath = null)
|
public function __construct($pattern, $handler)
|
||||||
{
|
{
|
||||||
$this->pattern = $pattern;
|
$this->pattern = $pattern;
|
||||||
$this->handler = $handler;
|
$this->handler = $handler;
|
||||||
$this->handlerPath = $handlerPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -63,7 +54,6 @@ class Route
|
||||||
*
|
*
|
||||||
* @param string $uriTemplate
|
* @param string $uriTemplate
|
||||||
* @param string $handler
|
* @param string $handler
|
||||||
* @param string $handlerPath
|
|
||||||
* @param array $variables
|
* @param array $variables
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
* @return Route
|
* @return Route
|
||||||
|
|
@ -71,7 +61,6 @@ class Route
|
||||||
static public function newFromUriTemplate(
|
static public function newFromUriTemplate(
|
||||||
$uriTemplate,
|
$uriTemplate,
|
||||||
$handler,
|
$handler,
|
||||||
$handlerPath = null,
|
|
||||||
$variables = null
|
$variables = null
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
|
@ -133,7 +122,7 @@ class Route
|
||||||
$pattern = '/^' . $pattern . '$/';
|
$pattern = '/^' . $pattern . '$/';
|
||||||
|
|
||||||
$klass = __CLASS__;
|
$klass = __CLASS__;
|
||||||
$route = new $klass($pattern, $handler, $handlerPath);
|
$route = new $klass($pattern, $handler);
|
||||||
return $route;
|
return $route;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,12 +55,6 @@ class Router
|
||||||
if (preg_match($route->pattern, $path, $matches)) {
|
if (preg_match($route->pattern, $path, $matches)) {
|
||||||
|
|
||||||
$klass = $route->handler;
|
$klass = $route->handler;
|
||||||
|
|
||||||
// Autoload, if needed.
|
|
||||||
if (is_string($route->handlerPath) && !class_exists($klass)) {
|
|
||||||
require_once($route->handlerPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
$handler = new $klass($request, $matches);
|
$handler = new $klass($request, $matches);
|
||||||
return $handler->response;
|
return $handler->response;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue