Addin a lot of changes
This commit is contained in:
+102
-50
@@ -10,26 +10,29 @@ class RouteCollection
|
||||
|
||||
private static RouteCollection $_routeCollection;
|
||||
|
||||
public string $_uri = '/';
|
||||
public array $_routes = array();
|
||||
private string $_verb = '';
|
||||
private array $_loadedFiles = array();
|
||||
private array $_errors = array();
|
||||
private array $_defaultMiddlewares = array();
|
||||
private array $_middlewareSet = array();
|
||||
private static array $_verbsWhitelist = array('*', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'CLI');
|
||||
private static array $_verbsWeb = array('GET', 'POST', 'PUT', 'PATCH', 'DELETE');
|
||||
public bool $_groupIn = false;
|
||||
public string $_groupBase = '';
|
||||
public array $_groupList = array();
|
||||
public array $_groups = array();
|
||||
public array $_contentParsers = array(); // Configure parsers for possible Route Returns
|
||||
public Route $_currentRoute; // The Route Being Dispatched
|
||||
public bool $_trace = false; // For debuggin
|
||||
public int $_traceVerbosity = 1; // How Verbose to trace
|
||||
public private(set) string $_uri = '/';
|
||||
public private(set) Array $_routes = Array();
|
||||
public private(set) string $_verb = '';
|
||||
public private(set) Array $_loadedFiles = Array();
|
||||
public private(set) Array $_errors = Array();
|
||||
public private(set) Array $_defaultMiddlewares = Array();
|
||||
public private(set) Array $_middlewareSet = Array();
|
||||
public private(set) bool $_groupIn = false;
|
||||
public private(set) string $_groupBase = '';
|
||||
public private(set) Array $_groupList = Array();
|
||||
public private(set) Array $_groups = Array();
|
||||
public private(set) Array $_contentParsers = Array(); // Configure parsers for possible Route Returns
|
||||
public private(set) Route $_currentRoute; // The Route Being Dispatched
|
||||
public private(set) bool $_trace = false; // For debuggin
|
||||
public private(set) int $_traceVerbosity = 1; // How Verbose to trace
|
||||
public private(set) Array $_objectSpawners = Array();
|
||||
|
||||
private static Array $_verbsWhitelist = Array('*', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'CLI');
|
||||
private static Array $_verbsWeb = Array('GET', 'POST', 'PUT', 'PATCH', 'DELETE');
|
||||
|
||||
//SINGLETON==============================================
|
||||
//=======================================================
|
||||
// SINGLETON
|
||||
//
|
||||
|
||||
/**
|
||||
* Empty constructor for Singleton
|
||||
@@ -52,8 +55,10 @@ class RouteCollection
|
||||
} else {
|
||||
self::$_routeCollection->_uri = isset($_SERVER['REQUEST_URI']) ? explode('?', $_SERVER['REQUEST_URI'])[0] : '/';
|
||||
}
|
||||
|
||||
self::$_routeCollection->defineVerb();
|
||||
self::$_routeCollection->setDefaultParsers();
|
||||
self::$_routeCollection->setDefaultObjectSpawners();
|
||||
}
|
||||
self::$_routeCollection->_uri = str_replace("//", "/", self::$_routeCollection->_uri);
|
||||
return self::$_routeCollection;
|
||||
@@ -212,16 +217,16 @@ class RouteCollection
|
||||
*/
|
||||
private function setDefaultParsers(): void
|
||||
{
|
||||
$this->_contentParsers["string"] = function (string $content) {
|
||||
$this->_contentParsers["string"] = function (string $content):void {
|
||||
echo $content;
|
||||
};
|
||||
|
||||
$this->_contentParsers["array"] = function (array $content) {
|
||||
$this->_contentParsers["array"] = function (array $content):void {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($content);
|
||||
};
|
||||
|
||||
$this->_contentParsers["stdClass"] = function (object $content) {
|
||||
$this->_contentParsers["stdClass"] = function (object $content):void {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($content);
|
||||
};
|
||||
@@ -250,11 +255,11 @@ class RouteCollection
|
||||
{
|
||||
$contentType = gettype($content);
|
||||
|
||||
if ($contentType == "null") { // Route didn't return anything
|
||||
if ($contentType == "NULL") { // Route didn't return anything
|
||||
return;
|
||||
}
|
||||
|
||||
if($contentType == "object"){
|
||||
if ($contentType == "object") {
|
||||
$contentType = get_class($content);
|
||||
}
|
||||
|
||||
@@ -264,6 +269,61 @@ class RouteCollection
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
call_user_func(self::getInstance()->_contentParsers['stdClass'], $content);
|
||||
}
|
||||
|
||||
//=======================================================
|
||||
// Object Spawner
|
||||
//
|
||||
|
||||
/**
|
||||
* Get a Spawner type
|
||||
*
|
||||
* @param string $type
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getObjectSpawner(string $type): mixed
|
||||
{
|
||||
return self::getInstance()->_objectSpawners[$type];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register some Object Spawner Types
|
||||
*
|
||||
* This will serve to be overwritten for each case so this
|
||||
* library can have a default spawner and allow custom spawners
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function setDefaultObjectSpawners(): void
|
||||
{
|
||||
$this->_objectSpawners = ObjectSpawners::getDefaultSpawners();
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to append a Spawner type on the list
|
||||
*
|
||||
* @param string $name
|
||||
* @param callable $callback
|
||||
* @return RouteCollection
|
||||
*/
|
||||
protected function addSpawner(string $name, callable $callback): RouteCollection
|
||||
{
|
||||
$this->_objectSpawners[$name] = $callback;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* API to append a Spawner type on the list
|
||||
*
|
||||
* @param string $name
|
||||
* @param callable $callback
|
||||
* @return RouteCollection
|
||||
*/
|
||||
public static function appendObjectSpawner(string $name, callable $callback): RouteCollection
|
||||
{
|
||||
return self::getInstance()->addSpawner($name, $callback);
|
||||
}
|
||||
|
||||
//=======================================================
|
||||
@@ -299,11 +359,11 @@ class RouteCollection
|
||||
* Add a GET route
|
||||
*
|
||||
* @param string $uri URI to Match
|
||||
* @param string|callable $callback Callback to Execute. Might be a class or a callable
|
||||
* @param string|callable|Array $callback Callback to Execute. Might be a class or a callable
|
||||
* @param int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function get(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
static function get(string $uri, string|callable|array $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('GET', $uri, $callback, $weight);
|
||||
}
|
||||
@@ -312,11 +372,11 @@ class RouteCollection
|
||||
* Add a CLI route
|
||||
*
|
||||
* @param string $uri URI to Match
|
||||
* @param string|callable $callback Callback to Execute. Might be a class or a callable
|
||||
* @param string|callable|array $callback Callback to Execute. Might be a class or a callable
|
||||
* @param int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function cli(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
static function cli(string $uri, string|callable|array $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('CLI', $uri, $callback, $weight);
|
||||
}
|
||||
@@ -325,11 +385,11 @@ class RouteCollection
|
||||
* Add a CLI route
|
||||
*
|
||||
* @param string $uri URI to Match
|
||||
* @param string|callable $callback Callback to Execute. Might be a class or a callable
|
||||
* @param string|callable|Array $callback Callback to Execute. Might be a class or a callable
|
||||
* @param int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function post(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
static function post(string $uri, string|callable|array $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('POST', $uri, $callback, $weight);
|
||||
}
|
||||
@@ -338,11 +398,11 @@ class RouteCollection
|
||||
* Add a CLI route
|
||||
*
|
||||
* @param string $uri URI to Match
|
||||
* @param string|callable $callback Callback to Execute. Might be a class or a callable
|
||||
* @param string|callable|Array $callback Callback to Execute. Might be a class or a callable
|
||||
* @param int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function put(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
static function put(string $uri, string|callable|array $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('PUT', $uri, $callback, $weight);
|
||||
}
|
||||
@@ -351,11 +411,11 @@ class RouteCollection
|
||||
* Add a CLI route
|
||||
*
|
||||
* @param string $uri URI to Match
|
||||
* @param string|callable $callback Callback to Execute. Might be a class or a callable
|
||||
* @param string|callable|Array $callback Callback to Execute. Might be a class or a callable
|
||||
* @param int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function patch(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
static function patch(string $uri, string|callable|array $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('PATCH', $uri, $callback, $weight);
|
||||
}
|
||||
@@ -364,11 +424,11 @@ class RouteCollection
|
||||
* Add a CLI route
|
||||
*
|
||||
* @param string $uri URI to Match
|
||||
* @param string|callable $callback Callback to Execute. Might be a class or a callable
|
||||
* @param string|callable|Array $callback Callback to Execute. Might be a class or a callable
|
||||
* @param int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function delete(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
static function delete(string $uri, string|callable|array $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('DELETE', $uri, $callback, $weight);
|
||||
}
|
||||
@@ -390,11 +450,11 @@ class RouteCollection
|
||||
*
|
||||
* @param Array|string $verb Verbs to match the Route
|
||||
* @param string $uri URI to Match
|
||||
* @param string|callable $callback Callback to Execute. Might be a class or a callable
|
||||
* @param string|callable|Array $callback Callback to Execute. Might be a class or a callable
|
||||
* @param int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function add(array|string $verb, string $uri, string|callable $callback, int $weight = 0): Route
|
||||
static function add(array|string $verb, string $uri, string|callable|array $callback, int $weight = 0): Route
|
||||
{
|
||||
$route = new Route();
|
||||
|
||||
@@ -406,17 +466,14 @@ class RouteCollection
|
||||
$uri = rtrim($uri, '/');
|
||||
|
||||
if (is_array($verb)) {
|
||||
$route->_verb = array_map('strtoupper', $verb);
|
||||
$route->setVerb(array_map('strtoupper', $verb));
|
||||
} else if (strtoupper($verb) == 'WEB') {
|
||||
$route->_verb = array_merge($route->_verb, self::$_verbsWeb);
|
||||
$route->setVerb(array_merge($route->_verb, self::$_verbsWeb));
|
||||
} else {
|
||||
$route->_verb[] = strtoupper($verb);
|
||||
$route->appendVerb(strtoupper($verb));
|
||||
}
|
||||
|
||||
$route->_uri = $uri;
|
||||
$route->_callback[] = $callback;
|
||||
$route->_weight = $weight;
|
||||
$route->prepare();
|
||||
$route->setUri($uri)->setCallback($callback)->setWeight($weight)->prepare();
|
||||
|
||||
self::getInstance()->_routes[] = $route;
|
||||
|
||||
@@ -537,7 +594,7 @@ class RouteCollection
|
||||
);
|
||||
}
|
||||
|
||||
$route->_before = array_merge($this->_defaultMiddlewares, $route->_before);
|
||||
$route->setMiddlewares(array_merge($this->_defaultMiddlewares, $route->_before));
|
||||
|
||||
$routeResult = $route->execute();
|
||||
|
||||
@@ -545,11 +602,6 @@ class RouteCollection
|
||||
$one_hit = true;
|
||||
}
|
||||
|
||||
if ($route->_httpError) {
|
||||
$this->httpError($route->_httpError);
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->parseContent($routeResult);
|
||||
|
||||
if ($route->_block) { //If Executed, interrupt route chain?
|
||||
|
||||
Reference in New Issue
Block a user