mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
[TASK] Updated
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
<?php
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
class Router {
|
||||
use Pecee\Url;
|
||||
|
||||
class RouterBase {
|
||||
|
||||
protected static $instance;
|
||||
|
||||
protected $currentRoute;
|
||||
protected $routes;
|
||||
protected $controllerUrlMap;
|
||||
protected $backstack;
|
||||
protected $requestUri;
|
||||
protected $requestMethod;
|
||||
@@ -16,16 +19,12 @@ class Router {
|
||||
public function __construct() {
|
||||
$this->routes = array();
|
||||
$this->backstack = array();
|
||||
$this->controllerUrlMap = array();
|
||||
$this->requestUri = rtrim($_SERVER['REQUEST_URI'], '/');
|
||||
$this->requestMethod = strtolower(isset($_GET['_method']) ? $_GET['_method'] : $_SERVER['REQUEST_METHOD']);
|
||||
}
|
||||
|
||||
public function addRoute(RouterEntry $route) {
|
||||
// Add default namespace
|
||||
if(!$route->getNamespace() && $this->defaultControllerNamespace !== null) {
|
||||
$route->setNamespace($this->defaultControllerNamespace);
|
||||
}
|
||||
|
||||
if($this->currentRoute !== null) {
|
||||
$this->backstack[] = $route;
|
||||
} else {
|
||||
@@ -33,12 +32,6 @@ class Router {
|
||||
}
|
||||
}
|
||||
|
||||
public function route($url, $callback) {
|
||||
$route = new RouterRoute($url, $callback);
|
||||
$this->addRoute($route);
|
||||
return $route;
|
||||
}
|
||||
|
||||
protected function loadClass($name) {
|
||||
if(!class_exists($name)) {
|
||||
throw new RouterException(sprintf('Class %s does not exist', $name));
|
||||
@@ -55,6 +48,11 @@ class Router {
|
||||
$this->loadClass($route->getMiddleware());
|
||||
}
|
||||
|
||||
// Add default namespace
|
||||
if(!$route->getNamespace() && $this->defaultControllerNamespace !== null) {
|
||||
$route->setNamespace($this->defaultControllerNamespace);
|
||||
}
|
||||
|
||||
if(is_object($route->getCallback()) && is_callable($route->getCallback())) {
|
||||
|
||||
// When the callback is a function
|
||||
@@ -64,6 +62,7 @@ class Router {
|
||||
// When the callback is a method
|
||||
|
||||
$controller = explode('@', $route->getCallback());
|
||||
|
||||
$class = $route->getNamespace() . '\\' . $controller[0];
|
||||
|
||||
$class = $this->loadClass($class);
|
||||
@@ -80,7 +79,7 @@ class Router {
|
||||
}
|
||||
}
|
||||
|
||||
protected function renderBackstack(array $routes, &$settings, &$prefixes) {
|
||||
protected function processRoutes(array $routes, array &$settings = array(), array &$prefixes = array()) {
|
||||
// Loop through each route-request
|
||||
/* @var $route RouterEntry */
|
||||
foreach($routes as $route) {
|
||||
@@ -90,10 +89,16 @@ class Router {
|
||||
array_push($prefixes, $route->getPrefix());
|
||||
}
|
||||
|
||||
// If the route is a group
|
||||
$route->setSettings($settings);
|
||||
|
||||
if($route instanceof RouterRoute) {
|
||||
$route->setSettings($settings);
|
||||
$route->setUrl( '/' . join('/', $prefixes) . $route->getUrl() );
|
||||
if(is_array($prefixes) && count($prefixes)) {
|
||||
$route->setUrl( '/' . join('/', $prefixes) . $route->getUrl() );
|
||||
}
|
||||
|
||||
if(stripos($route->getCallback(), '@') !== false) {
|
||||
$this->controllerUrlMap[$route->getCallback()] = $route;
|
||||
}
|
||||
}
|
||||
|
||||
// Stop if the route matches
|
||||
@@ -102,38 +107,20 @@ class Router {
|
||||
$this->renderRoute($route);
|
||||
}
|
||||
|
||||
// Remove itself from backstack
|
||||
array_shift($this->backstack);
|
||||
if(count($this->backstack)) {
|
||||
// Remove itself from backstack
|
||||
array_shift($this->backstack);
|
||||
|
||||
// Route any routes added to the backstack
|
||||
$this->renderBackstack($this->backstack, $settings, $prefixes);
|
||||
// Route any routes added to the backstack
|
||||
$this->processRoutes($this->backstack, $settings, $prefixes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function routeRequest() {
|
||||
// Loop through each route-request
|
||||
/* @var $route RouterEntry */
|
||||
foreach($this->routes as $route) {
|
||||
|
||||
// Reset variables
|
||||
$settings = array();
|
||||
$prefixes = array();
|
||||
|
||||
$settings = array_merge($settings, $route->getMergeableSettings());
|
||||
|
||||
if($route->getPrefix()) {
|
||||
array_push($prefixes, $route->getPrefix());
|
||||
}
|
||||
|
||||
// Stop if the route matches
|
||||
$route = $route->getRoute($this->requestMethod, $this->requestUri);
|
||||
if($route) {
|
||||
$this->renderRoute($route);
|
||||
}
|
||||
|
||||
// Route any routes added to the backstack
|
||||
$this->renderBackstack($this->backstack, $settings, $prefixes);
|
||||
}
|
||||
$this->processRoutes($this->routes);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -192,6 +179,36 @@ class Router {
|
||||
return $this->routes;
|
||||
}
|
||||
|
||||
public function getRoute($controller, $parameters = null, $getParams = null) {
|
||||
/* @var $route RouterRoute */
|
||||
foreach($this->controllerUrlMap as $c => $route) {
|
||||
$params = $route->getParameters();
|
||||
|
||||
if(strtolower($c) === strtolower($controller)) {
|
||||
|
||||
$url = $route->getUrl();
|
||||
|
||||
$i = 0;
|
||||
foreach($params as $param => $value) {
|
||||
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
||||
$url = str_ireplace('{' . $param. '}', $value, $route->getUrl());
|
||||
$i++;
|
||||
}
|
||||
|
||||
$p = '';
|
||||
if($getParams !== null) {
|
||||
$p = '?'.Url::arrayToParams($getParams);
|
||||
}
|
||||
|
||||
$url .= $p;
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
|
||||
return '/';
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
if(self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
@@ -27,18 +27,6 @@ abstract class RouterEntry {
|
||||
$this->parameters = array();
|
||||
}
|
||||
|
||||
protected function parseParameter($path) {
|
||||
$parameters = array();
|
||||
|
||||
preg_match('/{([A-Za-z\-\_]*?)}/is', $path, $parameters);
|
||||
|
||||
if(isset($parameters[1]) && count($parameters[1]) > 0) {
|
||||
return $parameters[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $callback
|
||||
* @return self;
|
||||
|
||||
@@ -11,12 +11,36 @@ class RouterRoute extends RouterEntry {
|
||||
|
||||
public function __construct($url, $callback) {
|
||||
parent::__construct();
|
||||
$this->url = $url;
|
||||
$this->callback = $callback;
|
||||
$this->setUrl($url);
|
||||
$this->setCallback($callback);
|
||||
|
||||
$this->settings['aliases'] = array();
|
||||
}
|
||||
|
||||
protected function parseParameters($url) {
|
||||
$parameters = array();
|
||||
|
||||
preg_match_all('/{([A-Za-z\-\_]*?)}/is', $url, $parameters);
|
||||
|
||||
if(isset($parameters[1]) && count($parameters[1]) > 0) {
|
||||
return $parameters[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function parseParameter($path) {
|
||||
$parameters = array();
|
||||
|
||||
preg_match('/{([A-Za-z\-\_]*?)}/is', $path, $parameters);
|
||||
|
||||
if(isset($parameters[1]) && count($parameters[1]) > 0) {
|
||||
return $parameters[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRoute($requestMethod, &$url) {
|
||||
|
||||
// Check if request method is allowed
|
||||
@@ -73,6 +97,15 @@ class RouterRoute extends RouterEntry {
|
||||
* @return self
|
||||
*/
|
||||
public function setUrl($url) {
|
||||
|
||||
$parameters = $this->parseParameters($url);
|
||||
|
||||
if($parameters !== null) {
|
||||
foreach($parameters as $param) {
|
||||
$this->parameters[$param] = '';
|
||||
}
|
||||
}
|
||||
|
||||
$this->url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ---------------------------
|
||||
* Router helper class
|
||||
* ---------------------------
|
||||
* This class is added so calls can be made staticly like Router::get() making the code look more pretty.
|
||||
*/
|
||||
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
class SimpleRouter {
|
||||
|
||||
public static function start($defaultNamespace = null) {
|
||||
$router = RouterBase::GetInstance();
|
||||
$router->setDefaultControllerNamespace($defaultNamespace);
|
||||
$router->routeRequest();
|
||||
}
|
||||
|
||||
public static function get($url, $callback) {
|
||||
$route = new RouterRoute($url, $callback);
|
||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_GET);
|
||||
|
||||
$router = RouterBase::getInstance();
|
||||
$router->addRoute($route);
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
||||
public static function post($url, $callback) {
|
||||
$route = new RouterRoute($url, $callback);
|
||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_POST);
|
||||
|
||||
$router = RouterBase::getInstance();
|
||||
$router->addRoute($route);
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
||||
public static function put($url, $callback) {
|
||||
$route = new RouterRoute($url, $callback);
|
||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_PUT);
|
||||
|
||||
$router = RouterBase::getInstance();
|
||||
$router->addRoute($route);
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
||||
public static function delete($url, $callback) {
|
||||
$route = new RouterRoute($url, $callback);
|
||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_DELETE);
|
||||
|
||||
$router = RouterBase::getInstance();
|
||||
$router->addRoute($route);
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
||||
public static function group($settings = array(), $callback) {
|
||||
$group = new RouterGroup();
|
||||
$group->setCallback($callback);
|
||||
|
||||
if($settings !== null && is_array($settings)) {
|
||||
$group->setSettings($settings);
|
||||
}
|
||||
|
||||
$router = RouterBase::getInstance();
|
||||
$router->addRoute($group);
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
public static function match(array $requestTypes, $url, $callback) {
|
||||
$route = new RouterRoute($url, $callback);
|
||||
foreach($requestTypes as $requestType) {
|
||||
$route->addRequestType($requestType);
|
||||
}
|
||||
|
||||
$router = RouterBase::getInstance();
|
||||
$router->addRoute($route);
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
||||
public static function ressource($controller, $settings = array()) {
|
||||
// not yet implemented
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user