Hi.
Hab eben ein Routing-System programmiert und versucht es ziemlich schnell zu halten. Es gibt eig. keine Rekursionen etc.
Download: http://www.file-upload.net/download-10825263/Routing.rar.html
Oder hier:
Spoiler anzeigen
Route.php
PHP
<?php
namespace iExit;
class Route {
private $route;
private $callback;
private $post = false;
private $matches = 0;
private $integers = 0;
private $strings = 0;
private $params = [];
public function __construct($route, $callback, $post = false) {
$this->route = $route;
$this->post = $post;
if(is_callable($callback))
$this->callback = $callback;
}
public function matches($i) {
$this->matches += $i;
}
public function addInteger($i) {
$this->integers += $i;
}
public function getMatches() {
return $this->matches;
}
public function addString($i) {
$this->strings += $i;
}
public function getRoute() {
return $this->route;
}
public function isPostable() {
return $this->post;
}
public function getCallback() {
return $this->callback;
}
public function addParam($name, $value) {
$this->params[$name] = $value;
}
public function getParams() {
return $this->params;
}
}
Alles anzeigen
RouteHandler.php
PHP
<?php
namespace iExit;
class RouteHandler {
private $routes = [];
public function add(Route $route) {
if(!in_array($route->getRoute(), $this->routes))
$this->routes[] = $route;
}
public function get($path, $callback) {
$routeHandlerResult = new RouteHandlerResult();
foreach($this->routes as $route) {
if($route->getRoute() == $path)
return $this->prepareRoute($route, $callback, $routeHandlerResult);
$givenRoute = $this->explodeRoute($path);
$currentRoute = $this->explodeRoute($route->getRoute());
if(count($givenRoute) == count($currentRoute)) {
for($i = 0; $i < count($givenRoute); $i++) {
if($currentRoute[$i] != '') {
if(substr($currentRoute[$i], 0, 1) == '@') {
$explodedArea = explode(':', $currentRoute[$i]);
switch(strtolower(str_replace('@', '', $explodedArea[0]))) {
case 'int':
case 'integer':
if(ctype_digit($givenRoute[$i])) {
$route->addParam($explodedArea[1], $givenRoute[$i]);
$route->addInteger(1);
$route->matches(1);
}
break;
case 'str':
case 'string':
if(!ctype_digit($givenRoute[$i])) {
$route->addParam($explodedArea[1], $givenRoute[$i]);
$route->addString(1);
$route->matches(1);
}
break;
}
} else {
if($givenRoute[$i] == $currentRoute[$i])
$route->matches(1);
}
}
}
if($route->getMatches() == (count($givenRoute) - 1))
$this->prepareRoute($route, $callback, $routeHandlerResult);
}
}
}
private function explodeRoute($path) {
return explode('/', $path);
}
public function prepareRoute(Route $route, $callback, RouteHandlerResult $rhr) {
if($route->isPostable() == false && count($_POST) > 0)
$rhr->setResult(RouteHandlerResult::ACCESS_DENIED);
else
$rhr->setResult(RouteHandlerResult::ACCESSABLE);
$rhr->setRoute($route);
return $callback($rhr);
}
}
Alles anzeigen
RouteHandlerResult.php
PHP
<?php
namespace iExit;
class RouteHandlerResult {
const ACCESSABLE = 'ACCESSABLE';
const NOT_FOUND = 'NOT_FOUND';
const ACCESS_DENIED = 'ACCESS_DENIED';
private $result = 'ACCESS_DENIED';
private $route;
public function setResult($result) {
if($result == RouteHandlerResult::ACCESSABLE || $result == RouteHandlerResult::NOT_FOUND || $result == RouteHandlerResult::ACCESS_DENIED)
$this->result = $result;
}
public function getRouteResult() {
return $this->result;
}
public function setRoute(Route $route) {
$this->route = $route;
}
public function getRoute() {
return $this->route;
}
}
Alles anzeigen
Test.php
PHP
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'lib/RouteHandler.php';
require_once 'lib/Route.php';
require_once 'lib/RouteHandlerResult.php';
$routeHandler = new iExit\RouteHandler();
$routeHandler->add(new iExit\Route('/test', function() {
echo 'testRoute';
}, true));
$routeHandler->add(new iExit\Route('/test/@int:Zahl1/@int:Zahl2', function($param) {
echo $param['Zahl1'].' plus '.$param['Zahl2']. ' ergibt: '.($param['Zahl1']+$param['Zahl2']);
}, true));
$routeHandler->get(rtrim($_GET['p'], '/'), (function(iExit\RouteHandlerResult $rhr) {
switch($rhr->getRouteResult()) {
case iExit\RouteHandlerResult::ACCESSABLE:
$callback = $rhr->getRoute()->getCallback();
$callback($rhr->getRoute()->getParams());
break;
case iExit\RouteHandlerResult::NOT_FOUND:
break;
case iExit\RouteHandlerResult::ACCESS_DENIED:
break;
}
}));
Alles anzeigen
Viel Spaß