mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
Development
- Isolated Http classes from router. - Removed getInstance from Request object - current router request should now be obtained through SimpleRouter::request(). - Fixed broken test cases. - Added test for regular expression match. - Updated documentation to reflect changes. - Added more helper examples to documentation. - Added helpers to demo-project. - Optimisations.
This commit is contained in:
@@ -20,7 +20,13 @@ class Input {
|
||||
*/
|
||||
public $file;
|
||||
|
||||
public function __construct() {
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
public function __construct(Request &$request) {
|
||||
$this->request = $request;
|
||||
$this->setGet();
|
||||
$this->setPost();
|
||||
$this->setFile();
|
||||
@@ -58,7 +64,7 @@ class Input {
|
||||
return ($key !== null) ? $element[$key] : $element;
|
||||
}
|
||||
|
||||
if(Request::getInstance()->getMethod() !== 'get') {
|
||||
if($this->request->getMethod() !== 'get') {
|
||||
|
||||
$element = $this->post->findFirst($index);
|
||||
|
||||
|
||||
@@ -5,28 +5,15 @@ use Pecee\Http\Input\Input;
|
||||
|
||||
class Request {
|
||||
|
||||
protected static $instance;
|
||||
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Return new instance
|
||||
* @return static
|
||||
*/
|
||||
public static function getInstance() {
|
||||
if(self::$instance === null) {
|
||||
self::$instance = new static();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function __construct() {
|
||||
$this->data = array();
|
||||
$this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : array();
|
||||
$this->uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : array();
|
||||
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : (isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : array());
|
||||
$this->headers = $this->getAllHeaders();
|
||||
$this->input = new Input();
|
||||
$this->input = new Input($this);
|
||||
}
|
||||
|
||||
protected function getAllHeaders() {
|
||||
|
||||
@@ -4,6 +4,12 @@ namespace Pecee\Http;
|
||||
|
||||
class Response {
|
||||
|
||||
protected $request;
|
||||
|
||||
public function __construct(Request &$request) {
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the http status code
|
||||
*
|
||||
@@ -31,7 +37,7 @@ class Response {
|
||||
}
|
||||
|
||||
public function refresh() {
|
||||
$this->redirect(Request::getInstance()->getUri());
|
||||
$this->redirect($this->request->getUri());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,12 +5,14 @@ use Pecee\Exception\RouterException;
|
||||
use Pecee\Handler\IExceptionHandler;
|
||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\Http\Response;
|
||||
|
||||
class RouterBase {
|
||||
|
||||
protected static $instance;
|
||||
|
||||
protected $request;
|
||||
protected $response;
|
||||
protected $currentRoute;
|
||||
protected $routes;
|
||||
protected $processedRoutes;
|
||||
@@ -21,10 +23,13 @@ class RouterBase {
|
||||
protected $baseCsrfVerifier;
|
||||
protected $exceptionHandlers;
|
||||
|
||||
// TODO: clean up - cut some of the methods down to smaller pieces
|
||||
|
||||
public function __construct() {
|
||||
$this->request = Request::getInstance();
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
public function reset() {
|
||||
$this->request = new Request();
|
||||
$this->response = new Response($this->request);
|
||||
$this->routes = array();
|
||||
$this->backStack = array();
|
||||
$this->controllerUrlMap = array();
|
||||
@@ -289,6 +294,14 @@ class RouterBase {
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get response
|
||||
* @return Response
|
||||
*/
|
||||
public function getResponse() {
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get base csrf verifier class
|
||||
* @return BaseCsrfVerifier
|
||||
@@ -341,10 +354,10 @@ class RouterBase {
|
||||
$url = $domain . '/' . trim($route->getUrl(), '/');
|
||||
|
||||
if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) {
|
||||
$url .= $method;
|
||||
}
|
||||
if($method !== null) {
|
||||
$url .= $method;
|
||||
}
|
||||
|
||||
if($route instanceof RouterController || $route instanceof RouterResource) {
|
||||
if(count($parameters)) {
|
||||
$url .= join('/', $parameters);
|
||||
}
|
||||
@@ -419,12 +432,15 @@ class RouterBase {
|
||||
$route = $this->controllerUrlMap[$i];
|
||||
|
||||
// Check an alias exist, if the matches - use it
|
||||
if($route instanceof RouterRoute && $route->hasAlias($controller)) {
|
||||
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
|
||||
}
|
||||
if($route instanceof RouterRoute) {
|
||||
|
||||
if($route instanceof RouterRoute && !is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
||||
$c = $route->getCallback();
|
||||
if($route->hasAlias($controller)) {
|
||||
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
|
||||
}
|
||||
|
||||
if(!is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
||||
$c = $route->getCallback();
|
||||
}
|
||||
} else if($route instanceof RouterController || $route instanceof RouterResource) {
|
||||
$c = $route->getController();
|
||||
}
|
||||
@@ -476,6 +492,10 @@ class RouterBase {
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current router instance
|
||||
* @return static
|
||||
*/
|
||||
public static function getInstance() {
|
||||
if(static::$instance === null) {
|
||||
static::$instance = new static();
|
||||
|
||||
@@ -13,6 +13,10 @@ use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||
|
||||
class SimpleRouter {
|
||||
|
||||
public function __construct() {
|
||||
die('test');
|
||||
}
|
||||
|
||||
/**
|
||||
* Start/route request
|
||||
* @param null $defaultNamespace
|
||||
@@ -128,4 +132,12 @@ class SimpleRouter {
|
||||
return RouterBase::getInstance()->getRoute($controller, $parameters, $getParams);
|
||||
}
|
||||
|
||||
public static function request() {
|
||||
return RouterBase::getInstance()->getRequest();
|
||||
}
|
||||
|
||||
public static function response() {
|
||||
return RouterBase::getInstance()->getResponse();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user