mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
Development + bugfixes
This commit is contained in:
@@ -11,16 +11,64 @@ class RouterBase {
|
||||
|
||||
protected static $instance;
|
||||
|
||||
/**
|
||||
* Current request
|
||||
* @var Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Response
|
||||
* @var Response
|
||||
*/
|
||||
protected $response;
|
||||
|
||||
/**
|
||||
* Used to keep track of whether to add routes to stack or not.
|
||||
* @var RouterEntry
|
||||
*/
|
||||
protected $currentRoute;
|
||||
|
||||
/**
|
||||
* All added routes
|
||||
* @var array
|
||||
*/
|
||||
protected $routes;
|
||||
protected $processedRoutes;
|
||||
|
||||
/**
|
||||
* List of
|
||||
* @var array
|
||||
*/
|
||||
protected $controllerUrlMap;
|
||||
|
||||
/**
|
||||
* Backstack array used to keep track of sub-routes
|
||||
* @var array
|
||||
*/
|
||||
protected $backStack;
|
||||
|
||||
/**
|
||||
* The default namespace that all routes will inherit
|
||||
* @var string
|
||||
*/
|
||||
protected $defaultNamespace;
|
||||
|
||||
/**
|
||||
* List of added bootmanagers
|
||||
* @var array
|
||||
*/
|
||||
protected $bootManagers;
|
||||
protected $baseCsrfVerifier;
|
||||
|
||||
/**
|
||||
* Csrf verifier class
|
||||
* @var BaseCsrfVerifier
|
||||
*/
|
||||
protected $csrfVerifier;
|
||||
|
||||
/**
|
||||
* Get exception handlers
|
||||
* @var array
|
||||
*/
|
||||
protected $exceptionHandlers;
|
||||
|
||||
public function __construct() {
|
||||
@@ -37,32 +85,41 @@ class RouterBase {
|
||||
$this->exceptionHandlers = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add route
|
||||
* @param RouterEntry $route
|
||||
* @return RouterEntry
|
||||
*/
|
||||
public function addRoute(RouterEntry $route) {
|
||||
if($this->currentRoute !== null) {
|
||||
$this->backStack[] = $route;
|
||||
} else {
|
||||
$this->routes[] = $route;
|
||||
}
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
||||
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backStack = false, $group = null) {
|
||||
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backStack = false, RouterGroup $group = null) {
|
||||
// Loop through each route-request
|
||||
|
||||
$routesCount = count($routes);
|
||||
$mergedSettings = array();
|
||||
|
||||
/* @var $route RouterEntry */
|
||||
for($i = 0; $i < $routesCount; $i++) {
|
||||
/* @var $group RouterGroup */
|
||||
for($i = 0; $i < count($routes); $i++) {
|
||||
|
||||
$route = $routes[$i];
|
||||
|
||||
$route->addSettings($settings);
|
||||
if(count($settings)) {
|
||||
$route->addSettings($settings);
|
||||
}
|
||||
|
||||
if($backStack) {
|
||||
if($backStack && $group !== null) {
|
||||
$route->setGroup($group);
|
||||
}
|
||||
|
||||
if($this->defaultNamespace && !$route->getNamespace()) {
|
||||
if($route->getNamespace() === null && $this->defaultNamespace !== null) {
|
||||
$namespace = $this->defaultNamespace;
|
||||
if ($route->getNamespace()) {
|
||||
$namespace .= '\\' . $route->getNamespace();
|
||||
@@ -71,42 +128,37 @@ class RouterBase {
|
||||
$route->setNamespace($namespace);
|
||||
}
|
||||
|
||||
$newPrefixes = $prefixes;
|
||||
|
||||
if($route->getPrefix() && trim($route->getPrefix(), '/') !== '') {
|
||||
array_push($newPrefixes, trim($route->getPrefix(), '/'));
|
||||
if($group !== null && $group->getPrefix() !== null && trim($group->getPrefix(), '/') !== '') {
|
||||
$prefixes[] = trim($group->getPrefix(), '/');
|
||||
}
|
||||
|
||||
/* @var $group RouterGroup */
|
||||
$group = null;
|
||||
$this->currentRoute = $route;
|
||||
|
||||
if(!($route instanceof RouterGroup)) {
|
||||
if(is_array($newPrefixes) && count($newPrefixes) && $backStack) {
|
||||
$route->setUrl( '/' . join('/', $newPrefixes) . $route->getUrl() );
|
||||
if($route instanceof ILoadableRoute) {
|
||||
if(is_array($prefixes) && count($prefixes) && $backStack) {
|
||||
$route->setUrl( '/' . join('/', $prefixes) . $route->getUrl() );
|
||||
}
|
||||
|
||||
$this->controllerUrlMap[] = $route;
|
||||
}
|
||||
} else {
|
||||
if(is_callable($route->getCallback())) {
|
||||
|
||||
$this->currentRoute = $route;
|
||||
$route->renderRoute($this->request);
|
||||
|
||||
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
|
||||
if ($route->matchRoute($this->request)) {
|
||||
|
||||
$route->renderRoute($this->request);
|
||||
$group = $route;
|
||||
|
||||
if($route->matchRoute($this->request)) {
|
||||
$mergedSettings = array_merge($settings, $group->getMergeableSettings());
|
||||
|
||||
$group = $route;
|
||||
// Add ExceptionHandler
|
||||
if ($group->getExceptionHandler() !== null) {
|
||||
$this->exceptionHandlers[] = $route;
|
||||
}
|
||||
|
||||
$mergedSettings = array_merge($settings, $group->getMergeableSettings());
|
||||
|
||||
// Add ExceptionHandler
|
||||
if ($group->getExceptionHandler() !== null) {
|
||||
$this->exceptionHandlers[] = $route;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$this->currentRoute = null;
|
||||
@@ -116,7 +168,7 @@ class RouterBase {
|
||||
$this->backStack = array();
|
||||
|
||||
// Route any routes added to the backstack
|
||||
$this->processRoutes($backStack, $mergedSettings, $newPrefixes, true, $group);
|
||||
$this->processRoutes($backStack, $mergedSettings, $prefixes, true, $group);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -124,7 +176,6 @@ class RouterBase {
|
||||
public function routeRequest($original = true) {
|
||||
|
||||
$originalUri = $this->request->getUri();
|
||||
|
||||
$routeNotAllowed = false;
|
||||
|
||||
try {
|
||||
@@ -144,23 +195,17 @@ class RouterBase {
|
||||
// Loop through each route-request
|
||||
$this->processRoutes($this->routes);
|
||||
|
||||
if($original === true) {
|
||||
if($original === true && $this->csrfVerifier !== null) {
|
||||
// Verify csrf token for request
|
||||
if ($this->baseCsrfVerifier !== null) {
|
||||
$this->baseCsrfVerifier->handle($this->request);
|
||||
}
|
||||
$this->csrfVerifier->handle($this->request);
|
||||
}
|
||||
|
||||
$max = count($this->controllerUrlMap);
|
||||
|
||||
/* @var $route RouterEntry */
|
||||
for ($i = 0; $i < $max; $i++) {
|
||||
for ($i = 0; $i < count($this->controllerUrlMap); $i++) {
|
||||
|
||||
$route = $this->controllerUrlMap[$i];
|
||||
|
||||
$routeMatch = $route->matchRoute($this->request);
|
||||
|
||||
if ($routeMatch) {
|
||||
if ($route->matchRoute($this->request)) {
|
||||
|
||||
if (count($route->getRequestMethods()) && !in_array($this->request->getMethod(), $route->getRequestMethods())) {
|
||||
$routeNotAllowed = true;
|
||||
@@ -221,6 +266,7 @@ class RouterBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default namespace
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultNamespace(){
|
||||
@@ -228,6 +274,7 @@ class RouterBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the main default namespace that all routes will inherit
|
||||
* @param string $defaultNamespace
|
||||
* @return static
|
||||
*/
|
||||
@@ -237,6 +284,7 @@ class RouterBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get bootmanagers
|
||||
* @return array
|
||||
*/
|
||||
public function getBootManagers() {
|
||||
@@ -244,38 +292,27 @@ class RouterBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set bootmanagers
|
||||
* @param array $bootManagers
|
||||
*/
|
||||
public function setBootManagers(array $bootManagers) {
|
||||
$this->bootManagers = $bootManagers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add bootmanager
|
||||
* @param RouterBootManager $bootManager
|
||||
*/
|
||||
public function addBootManager(RouterBootManager $bootManager) {
|
||||
$this->bootManagers[] = $bootManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the loaded route
|
||||
* @return RouterEntry
|
||||
*/
|
||||
public function getLoadedRoute() {
|
||||
if(!($this->request->loadedRoute instanceof RouterGroup)) {
|
||||
return $this->request->loadedRoute;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBackstack() {
|
||||
return $this->backStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RouterEntry
|
||||
*/
|
||||
public function getCurrentRoute(){
|
||||
return $this->currentRoute;
|
||||
return $this->request->loadedRoute;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -303,21 +340,21 @@ class RouterBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get base csrf verifier class
|
||||
* Get csrf verifier class
|
||||
* @return BaseCsrfVerifier
|
||||
*/
|
||||
public function getBaseCsrfVerifier() {
|
||||
return $this->baseCsrfVerifier;
|
||||
public function getCsrfVerifier() {
|
||||
return $this->csrfVerifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set base csrf verifier class
|
||||
* Set csrf verifier class
|
||||
*
|
||||
* @param BaseCsrfVerifier $baseCsrfVerifier
|
||||
* @return self
|
||||
* @param BaseCsrfVerifier $csrfVerifier
|
||||
* @return static
|
||||
*/
|
||||
public function setBaseCsrfVerifier(BaseCsrfVerifier $baseCsrfVerifier) {
|
||||
$this->baseCsrfVerifier = $baseCsrfVerifier;
|
||||
public function setCsrfVerifier(BaseCsrfVerifier $csrfVerifier) {
|
||||
$this->csrfVerifier = $csrfVerifier;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -353,25 +390,22 @@ class RouterBase {
|
||||
|
||||
$url = $domain . '/' . trim($route->getUrl(), '/');
|
||||
|
||||
if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) {
|
||||
if($method !== null) {
|
||||
$url .= $method;
|
||||
}
|
||||
if($route instanceof IControllerRoute && $method !== null) {
|
||||
$url .= $method;
|
||||
|
||||
if(count($parameters)) {
|
||||
$url .= join('/', $parameters);
|
||||
}
|
||||
} else {
|
||||
/* @var $route RouterEntry */
|
||||
if(is_array($parameters)) {
|
||||
if($parameters !== null && is_array($parameters)) {
|
||||
$params = array_merge($route->getParameters(), $parameters);
|
||||
} else {
|
||||
$params = $route->getParameters();
|
||||
}
|
||||
|
||||
$otherParams = [];
|
||||
|
||||
$otherParams = array();
|
||||
$i = 0;
|
||||
|
||||
foreach($params as $param => $value) {
|
||||
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
||||
if(stripos($url, '{' . $param. '}') !== false || stripos($url, '{' . $param . '?}') !== false) {
|
||||
@@ -406,7 +440,7 @@ class RouterBase {
|
||||
|
||||
// Return current route if no options has been specified
|
||||
if($controller === null && $parameters === null) {
|
||||
$getParams = (is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET;
|
||||
$getParams = ($getParams !== null && is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET;
|
||||
|
||||
$url = parse_url($this->request->getUri(), PHP_URL_PATH);
|
||||
|
||||
@@ -423,7 +457,6 @@ class RouterBase {
|
||||
|
||||
$c = '';
|
||||
$method = null;
|
||||
|
||||
$max = count($this->controllerUrlMap);
|
||||
|
||||
/* @var $route RouterRoute */
|
||||
@@ -432,8 +465,9 @@ class RouterBase {
|
||||
$route = $this->controllerUrlMap[$i];
|
||||
|
||||
// Check an alias exist, if the matches - use it
|
||||
if($route instanceof RouterRoute) {
|
||||
|
||||
if($route instanceof IControllerRoute) {
|
||||
$c = $route->getController();
|
||||
} else {
|
||||
if($route->hasAlias($controller)) {
|
||||
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
|
||||
}
|
||||
@@ -441,8 +475,6 @@ class RouterBase {
|
||||
if(!is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
||||
$c = $route->getCallback();
|
||||
}
|
||||
} else if($route instanceof RouterController || $route instanceof RouterResource) {
|
||||
$c = $route->getController();
|
||||
}
|
||||
|
||||
if($c === $controller || strpos($c, $controller) === 0) {
|
||||
@@ -457,10 +489,10 @@ class RouterBase {
|
||||
|
||||
$route = $this->controllerUrlMap[$i];
|
||||
|
||||
if($route instanceof RouterRoute && !is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
||||
$c = $route->getClass();
|
||||
} else if($route instanceof RouterController || $route instanceof RouterResource) {
|
||||
if($route instanceof IControllerRoute) {
|
||||
$c = $route->getController();
|
||||
} else if(!is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
||||
$c = $route->getClass();
|
||||
}
|
||||
|
||||
if(stripos($controller, '@') !== false) {
|
||||
@@ -477,10 +509,8 @@ class RouterBase {
|
||||
$controller = ($controller === null) ? '/' : $controller;
|
||||
$url = array($controller);
|
||||
|
||||
if(is_array($parameters)) {
|
||||
foreach($parameters as $key => $value) {
|
||||
array_push($url,$value);
|
||||
}
|
||||
if($parameters !== null && is_array($parameters) && count($parameters)) {
|
||||
$url = array_merge($url, $parameters);
|
||||
}
|
||||
|
||||
$url = '/' . trim(join('/', $url), '/') . '/';
|
||||
@@ -500,6 +530,7 @@ class RouterBase {
|
||||
if(static::$instance === null) {
|
||||
static::$instance = new static();
|
||||
}
|
||||
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user