Identation
This commit is contained in:
+45
-30
@@ -6,12 +6,13 @@ namespace Routes;
|
||||
*
|
||||
*/
|
||||
|
||||
class Route {
|
||||
class Route
|
||||
{
|
||||
/*
|
||||
* HTTP VERB
|
||||
*/
|
||||
|
||||
public $_verb = Array();
|
||||
public $_verb = array();
|
||||
/*
|
||||
* Execution Source
|
||||
* HTTP | Cli | SOAP |
|
||||
@@ -31,32 +32,32 @@ class Route {
|
||||
/*
|
||||
* URI Segments
|
||||
*/
|
||||
public $_uriSegments = Array();
|
||||
public $_uriSegments = array();
|
||||
|
||||
/*
|
||||
* Extracted params
|
||||
*/
|
||||
public $_segments = Array();
|
||||
public $_segments = array();
|
||||
|
||||
/*
|
||||
* Prepared params
|
||||
*/
|
||||
public $_params = Array();
|
||||
public $_params = array();
|
||||
|
||||
/*
|
||||
* Execute before dispatch route
|
||||
*/
|
||||
public $_before = Array();
|
||||
public $_before = array();
|
||||
|
||||
/*
|
||||
* Route callback
|
||||
*/
|
||||
public $_callback = Array();
|
||||
public $_callback = array();
|
||||
|
||||
/*
|
||||
* Execute after route dispatched
|
||||
*/
|
||||
public $_after = Array();
|
||||
public $_after = array();
|
||||
|
||||
/*
|
||||
* Prepared RegEx
|
||||
@@ -81,7 +82,7 @@ class Route {
|
||||
/*
|
||||
* Middlewares to remove from the list
|
||||
*/
|
||||
public $_middlewaresToIgnore = Array();
|
||||
public $_middlewaresToIgnore = array();
|
||||
/*
|
||||
* From Klein
|
||||
* HTTP status List
|
||||
@@ -136,7 +137,8 @@ class Route {
|
||||
);
|
||||
|
||||
//=======================================================
|
||||
function execute() {
|
||||
function execute()
|
||||
{
|
||||
//Middlewares with function or class method
|
||||
foreach ($this->_before as $key => $before) {
|
||||
if (!in_array($key, $this->_middlewaresToIgnore) && !$this->_ignore) {
|
||||
@@ -144,9 +146,9 @@ class Route {
|
||||
$before = explode('@', $before);
|
||||
$class = new $before[0]();
|
||||
$class->{$before[1]}();
|
||||
}else if(is_array($before)){
|
||||
} else if (is_array($before)) {
|
||||
call_user_func($before['callback'], $before['params']);
|
||||
}else{
|
||||
} else {
|
||||
call_user_func($before);
|
||||
}
|
||||
}
|
||||
@@ -162,7 +164,7 @@ class Route {
|
||||
if (sizeof($r->getParameters()) > 0 && $r->getParameters()[0]->getClass() != NULL) {
|
||||
$element = $r->getParameters()[0]->getClass()->name;
|
||||
$element = new $element;
|
||||
if(isset($this->_params['id'])){
|
||||
if (isset($this->_params['id'])) {
|
||||
$element->load($this->_params['id']);
|
||||
}
|
||||
$this->_params['id'] = $element;
|
||||
@@ -170,10 +172,10 @@ class Route {
|
||||
$class->{$segments[1]}(...array_values($this->_params));
|
||||
} else {
|
||||
$r = new \ReflectionFunction($callback);
|
||||
if(sizeof($r->getParameters()) > 0 && $r->getParameters()[0]->getClass() != NULL){
|
||||
if (sizeof($r->getParameters()) > 0 && $r->getParameters()[0]->getClass() != NULL) {
|
||||
$element = $r->getParameters()[0]->getClass()->name;
|
||||
$element = new $element;
|
||||
if(isset($this->_params['id'])){
|
||||
if (isset($this->_params['id'])) {
|
||||
$element->load($this->_params['id']);
|
||||
}
|
||||
$this->_params['id'] = $element;
|
||||
@@ -187,7 +189,8 @@ class Route {
|
||||
}
|
||||
}
|
||||
|
||||
function prepare() {
|
||||
function prepare()
|
||||
{
|
||||
$this->_segments = explode('/', $this->_uri);
|
||||
|
||||
foreach ($this->_segments as $segment) {
|
||||
@@ -207,7 +210,8 @@ class Route {
|
||||
$this->_regex = "/^\/" . $this->_regex . "$/";
|
||||
}
|
||||
|
||||
function createIndexes() {
|
||||
function createIndexes()
|
||||
{
|
||||
foreach ($this->_segments as $key => $segment) {
|
||||
if (preg_match('/\[.*?\]$/', $segment)) {
|
||||
$segment = trim(trim($segment, '['), ']');
|
||||
@@ -217,55 +221,66 @@ class Route {
|
||||
}
|
||||
}
|
||||
|
||||
function setUri($uri) {
|
||||
function setUri($uri)
|
||||
{
|
||||
$this->_uri = $uri;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function getSegments() {
|
||||
function getSegments()
|
||||
{
|
||||
return $this->_segments;
|
||||
}
|
||||
|
||||
function doBlock() {
|
||||
function doBlock()
|
||||
{
|
||||
$this->_block = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function notBlock() {
|
||||
function notBlock()
|
||||
{
|
||||
$this->_block = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function doIgnore() {
|
||||
function doIgnore()
|
||||
{
|
||||
$this->_ignore = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function notIgnore() {
|
||||
function notIgnore()
|
||||
{
|
||||
$this->_ignore = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareIgnore($name = '') {
|
||||
function middlewareIgnore($name = '')
|
||||
{
|
||||
$this->_middlewaresToIgnore[] = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareAdd($name = '', $params) {
|
||||
function middlewareAdd($name = '', $params = [])
|
||||
{
|
||||
$this->_before[$name] = $params;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareAppend($name = '', $function) {
|
||||
function middlewareAppend($name = '', $function)
|
||||
{
|
||||
$this->_before[$name] = $function;
|
||||
}
|
||||
|
||||
function setWeight($weigth = 0) {
|
||||
function setWeight($weigth = 0)
|
||||
{
|
||||
$this->_weight = $weigth;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function setHttpError($code = 400, $message = null) {
|
||||
function setHttpError($code = 400, $message = null)
|
||||
{
|
||||
if ($message == null) {
|
||||
$message = self::$httpMessages[$code];
|
||||
}
|
||||
@@ -278,7 +293,8 @@ class Route {
|
||||
return $this;
|
||||
}
|
||||
|
||||
function match($uri) {
|
||||
function match($uri)
|
||||
{
|
||||
//Wildcards allways match
|
||||
if (in_array('*', $this->_verb) || $this->_uri == '*') {
|
||||
return true;
|
||||
@@ -294,4 +310,3 @@ class Route {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,19 +2,20 @@
|
||||
|
||||
namespace Routes;
|
||||
|
||||
class RouteCollection {
|
||||
class RouteCollection
|
||||
{
|
||||
|
||||
private static $_routeCollection;
|
||||
|
||||
public $_uri = '/';
|
||||
public $_routes = Array();
|
||||
public $_routes = array();
|
||||
private $_verb = '';
|
||||
private $_loadedFiles = Array();
|
||||
private $_errors = Array();
|
||||
private $_defaultMiddlewares = Array();
|
||||
private $_middlewareSet = Array();
|
||||
private static $_verbsWhitelist = Array('*', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'CLI');
|
||||
private static $_verbsWeb = Array('GET', 'POST', 'PUT', 'PATCH', 'DELETE');
|
||||
private $_loadedFiles = array();
|
||||
private $_errors = array();
|
||||
private $_defaultMiddlewares = array();
|
||||
private $_middlewareSet = array();
|
||||
private static $_verbsWhitelist = array('*', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'CLI');
|
||||
private static $_verbsWeb = array('GET', 'POST', 'PUT', 'PATCH', 'DELETE');
|
||||
|
||||
public $_groupIn = false;
|
||||
public $_groupBase = '';
|
||||
@@ -23,11 +24,13 @@ class RouteCollection {
|
||||
|
||||
//SINGLETON==============================================
|
||||
|
||||
private function __construct() {
|
||||
private function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private static function newObj() {
|
||||
private static function newObj()
|
||||
{
|
||||
if (!isset(self::$_routeCollection)) {
|
||||
self::$_routeCollection = new RouteCollection();
|
||||
if (php_sapi_name() == "cli") {
|
||||
@@ -44,7 +47,8 @@ class RouteCollection {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function getInstance() {
|
||||
public static function getInstance()
|
||||
{
|
||||
if (!isset(self::$_routeCollection)) {
|
||||
return self::newObj();
|
||||
}
|
||||
@@ -58,7 +62,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag\t RouteCollection->crawl()
|
||||
*/
|
||||
public function crawl($basepath = __DIR__, $filenames = Array('Routes.php', 'routes.php')) {
|
||||
public function crawl($basepath = __DIR__, $filenames = array('Routes.php', 'routes.php'))
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
|
||||
$rdi = new \RecursiveDirectoryIterator($basepath);
|
||||
@@ -66,7 +71,7 @@ class RouteCollection {
|
||||
foreach (new \RecursiveIteratorIterator($rdi) as $file) {
|
||||
foreach ($filenames as $filename) {
|
||||
// if (strpos($file, $filename) && $file[0] != '.') {
|
||||
if (strpos($file, $filename) && !strpos($file, '.swp') ) {
|
||||
if (strpos($file, $filename) && !strpos($file, '.swp')) {
|
||||
$instance->_loadedFiles[] = $file;
|
||||
}
|
||||
}
|
||||
@@ -79,7 +84,8 @@ class RouteCollection {
|
||||
* Load the files with the routes
|
||||
* @ctag\t RouteCollection->loadRoutes()
|
||||
*/
|
||||
function loadRoutes() {
|
||||
function loadRoutes()
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
|
||||
foreach ($instance->_loadedFiles as $loadedFile) {
|
||||
@@ -93,7 +99,8 @@ class RouteCollection {
|
||||
* GET | POST | PUT | PATCH | DELETE | CLI
|
||||
*
|
||||
*/
|
||||
private function defineVerb() {
|
||||
private function defineVerb()
|
||||
{
|
||||
|
||||
$verb = '';
|
||||
|
||||
@@ -115,8 +122,9 @@ class RouteCollection {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function sort_routes() {
|
||||
usort($this->_routes, function($a, $b) {
|
||||
private function sort_routes()
|
||||
{
|
||||
usort($this->_routes, function ($a, $b) {
|
||||
if ($a->_weight == $b->_weight) {
|
||||
return 0;
|
||||
}
|
||||
@@ -127,7 +135,8 @@ class RouteCollection {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function submit() {
|
||||
function submit()
|
||||
{
|
||||
global $ROUTE;
|
||||
|
||||
self::$_routeCollection->sort_routes();
|
||||
@@ -149,10 +158,11 @@ class RouteCollection {
|
||||
|
||||
$ROUTE = $route;
|
||||
|
||||
foreach ($route->_before as $key => $requestedMiddleware){
|
||||
$route->_before[$key] = Array(
|
||||
foreach ($route->_before as $key => $requestedMiddleware) {
|
||||
$route->_before[$key] = array(
|
||||
'callback' => $this->_middlewareSet[$key],
|
||||
'params' => $requestedMiddleware);
|
||||
'params' => $requestedMiddleware
|
||||
);
|
||||
}
|
||||
|
||||
$route->_before = array_merge($this->_defaultMiddlewares, $route->_before);
|
||||
@@ -187,7 +197,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::group('base',function(){})
|
||||
*/
|
||||
static function group($base = '', $callback){
|
||||
static function group($base = '', $callback)
|
||||
{
|
||||
$collection = self::getInstance();
|
||||
$collection->_groupList = [];
|
||||
$collection->_groupIn = true;
|
||||
@@ -204,7 +215,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::get('/url',function(){})
|
||||
*/
|
||||
static function get($uri, $callback, $weight = 0) {
|
||||
static function get($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('GET', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
@@ -212,7 +224,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::cli('/url',function(){})
|
||||
*/
|
||||
static function cli($uri, $callback, $weight = 0) {
|
||||
static function cli($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('CLI', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
@@ -220,7 +233,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::post('/url',function(){})
|
||||
*/
|
||||
static function post($uri, $callback, $weight = 0) {
|
||||
static function post($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('POST', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
@@ -228,7 +242,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::put('/url',function(){})
|
||||
*/
|
||||
static function put($uri, $callback, $weight = 0) {
|
||||
static function put($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('PUT', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
@@ -236,7 +251,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::patch('/url',function(){})
|
||||
*/
|
||||
static function patch($uri, $callback, $weight = 0) {
|
||||
static function patch($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('PATCH', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
@@ -245,7 +261,8 @@ class RouteCollection {
|
||||
* @ctag RouteCollection::delete('/url',Controller@Method)
|
||||
* @ctag RouteCollection::delete('/url',function(){})
|
||||
*/
|
||||
static function delete($uri, $callback, $weight = 0) {
|
||||
static function delete($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('DELETE', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
@@ -253,7 +270,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::resource('/url',function(){})
|
||||
*/
|
||||
static function resource($uri) {
|
||||
static function resource($uri)
|
||||
{
|
||||
throw new Exception('Not implemented');
|
||||
}
|
||||
|
||||
@@ -262,10 +280,11 @@ class RouteCollection {
|
||||
* @ctag RouteCollection::add('VERB','/url',function(){})
|
||||
* @ctag RouteCollection::add('VERB','/url',Controller@Method)
|
||||
*/
|
||||
static function add($verb, $uri, $callback, $weight = 0) {
|
||||
static function add($verb, $uri, $callback, $weight = 0)
|
||||
{
|
||||
$route = new Route();
|
||||
|
||||
if(self::getInstance()->_groupIn){
|
||||
if (self::getInstance()->_groupIn) {
|
||||
$uri = self::getInstance()->_groupBase . $uri;
|
||||
self::getInstance()->_groupList[] = &$route;
|
||||
}
|
||||
@@ -291,7 +310,8 @@ class RouteCollection {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function addRoute(Route $route) {
|
||||
function addRoute(Route $route)
|
||||
{
|
||||
$this->_routes[] = $route;
|
||||
}
|
||||
|
||||
@@ -299,7 +319,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::addDefaultMiddleware('name',function(){})
|
||||
*/
|
||||
static function addDefaultMiddleware($name = '', $function) {
|
||||
static function addDefaultMiddleware($name = '', $function)
|
||||
{
|
||||
self::getInstance()->_defaultMiddlewares[$name] = $function;
|
||||
return self::getInstance();
|
||||
}
|
||||
@@ -308,7 +329,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::addDefaultMiddleware('name',function(){})
|
||||
*/
|
||||
static function registerMiddleware($name = '', $function) {
|
||||
static function registerMiddleware($name = '', $function)
|
||||
{
|
||||
return self::getInstance()->_middlewareSet[$name] = $function;
|
||||
}
|
||||
|
||||
@@ -316,7 +338,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::onHttpError(function(){})
|
||||
*/
|
||||
static function onHttpError($code, $function) {
|
||||
static function onHttpError($code, $function)
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
}
|
||||
|
||||
@@ -324,7 +347,8 @@ class RouteCollection {
|
||||
*
|
||||
* @ctag RouteCollection::httpError($class)
|
||||
*/
|
||||
private function httpError(\stdClass $info) {
|
||||
private function httpError(\stdClass $info)
|
||||
{
|
||||
$info = (array) $info;
|
||||
foreach ($this->_errors as $error) {
|
||||
call_user_func_array($error, $info);
|
||||
|
||||
+33
-21
@@ -2,77 +2,89 @@
|
||||
|
||||
namespace Routes;
|
||||
|
||||
class RouteGroup{
|
||||
class RouteGroup
|
||||
{
|
||||
|
||||
private $_routeGroup = [];
|
||||
|
||||
function __construct($group){
|
||||
function __construct($group)
|
||||
{
|
||||
$this->setGroup($group);
|
||||
|
||||
}
|
||||
|
||||
function setGroup($group){
|
||||
function setGroup($group)
|
||||
{
|
||||
$this->_routeGroup = $group;
|
||||
}
|
||||
|
||||
function doBlock() {
|
||||
foreach($this->_routeGroup as $route){
|
||||
function doBlock()
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_block = true;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function notBlock() {
|
||||
foreach($this->_routeGroup as $route){
|
||||
function notBlock()
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_block = false;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function doIgnore() {
|
||||
foreach($this->_routeGroup as $route){
|
||||
function doIgnore()
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_ignore = true;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function notIgnore() {
|
||||
foreach($this->_routeGroup as $route){
|
||||
function notIgnore()
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_ignore = false;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareIgnore($name = '') {
|
||||
foreach($this->_routeGroup as $route){
|
||||
function middlewareIgnore($name = '')
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_middlewaresToIgnore[] = $name;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareAdd($name = '') {
|
||||
foreach($this->_routeGroup as $route){
|
||||
function middlewareAdd($name = '')
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_before[$name] = $function;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareAppend($name = '', $function) {
|
||||
foreach($this->_routeGroup as $route){
|
||||
function middlewareAppend($name = '', $function)
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_before[$name] = $function;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function setWeight($weigth = 0) {
|
||||
foreach($this->_routeGroup as $route){
|
||||
function setWeight($weigth = 0)
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_weight = $weigth;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
function setHttpError($code = 400, $message = null) {
|
||||
foreach($this->_routeGroup as $route){
|
||||
function setHttpError($code = 400, $message = null)
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
if ($message == null) {
|
||||
$message = self::$httpMessages[$code];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user