Identation

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