Addin a lot of changes
This commit is contained in:
+161
-43
@@ -2,56 +2,174 @@
|
||||
|
||||
namespace Routes;
|
||||
|
||||
enum HTTPCodes: string
|
||||
/**
|
||||
* HTTP Status Codes Enum
|
||||
*
|
||||
* Represents standard HTTP response status codes with descriptive messages.
|
||||
* Supports querying code categories and retrieving status information.
|
||||
*/
|
||||
enum HTTPCodes: int
|
||||
{
|
||||
// Informational 1xx
|
||||
case 100 = 'Continue';
|
||||
case 101 = 'Switching Protocols';
|
||||
case CONTINUE = 100;
|
||||
case SWITCHING_PROTOCOLS = 101;
|
||||
|
||||
// Successful 2xx
|
||||
case 200 = 'OK';
|
||||
case 201 = 'Created';
|
||||
case 202 = 'Accepted';
|
||||
case 203 = 'Non-Authoritative Information';
|
||||
case 204 = 'No Content';
|
||||
case 205 = 'Reset Content';
|
||||
case 206 = 'Partial Content';
|
||||
case OK = 200;
|
||||
case CREATED = 201;
|
||||
case ACCEPTED = 202;
|
||||
case NON_AUTHORITATIVE_INFORMATION = 203;
|
||||
case NO_CONTENT = 204;
|
||||
case RESET_CONTENT = 205;
|
||||
case PARTIAL_CONTENT = 206;
|
||||
|
||||
// Redirection 3xx
|
||||
case 300 = 'Multiple Choices';
|
||||
case 301 = 'Moved Permanently';
|
||||
case 302 = 'Found';
|
||||
case 303 = 'See Other';
|
||||
case 304 = 'Not Modified';
|
||||
case 305 = 'Use Proxy';
|
||||
case 306 = '(Unused)';
|
||||
case 307 = 'Temporary Redirect';
|
||||
case MULTIPLE_CHOICES = 300;
|
||||
case MOVED_PERMANENTLY = 301;
|
||||
case FOUND = 302;
|
||||
case SEE_OTHER = 303;
|
||||
case NOT_MODIFIED = 304;
|
||||
case USE_PROXY = 305;
|
||||
case TEMPORARY_REDIRECT = 307;
|
||||
|
||||
// Client Error 4xx
|
||||
case 400 = 'Bad Request';
|
||||
case 401 = 'Unauthorized';
|
||||
case 402 = 'Payment Required';
|
||||
case 403 = 'Forbidden';
|
||||
case 404 = 'Not Found';
|
||||
case 405 = 'Method Not Allowed';
|
||||
case 406 = 'Not Acceptable';
|
||||
case 407 = 'Proxy Authentication Required';
|
||||
case 408 = 'Request Timeout';
|
||||
case 409 = 'Conflict';
|
||||
case 410 = 'Gone';
|
||||
case 411 = 'Length Required';
|
||||
case 412 = 'Precondition Failed';
|
||||
case 413 = 'Request Entity Too Large';
|
||||
case 414 = 'Request-URI Too Long';
|
||||
case 415 = 'Unsupported Media Type';
|
||||
case 416 = 'Requested Range Not Satisfiable';
|
||||
case 417 = 'Expectation Failed';
|
||||
case BAD_REQUEST = 400;
|
||||
case UNAUTHORIZED = 401;
|
||||
case PAYMENT_REQUIRED = 402;
|
||||
case FORBIDDEN = 403;
|
||||
case NOT_FOUND = 404;
|
||||
case METHOD_NOT_ALLOWED = 405;
|
||||
case NOT_ACCEPTABLE = 406;
|
||||
case PROXY_AUTHENTICATION_REQUIRED = 407;
|
||||
case REQUEST_TIMEOUT = 408;
|
||||
case CONFLICT = 409;
|
||||
case GONE = 410;
|
||||
case LENGTH_REQUIRED = 411;
|
||||
case PRECONDITION_FAILED = 412;
|
||||
case REQUEST_ENTITY_TOO_LARGE = 413;
|
||||
case REQUEST_URI_TOO_LONG = 414;
|
||||
case UNSUPPORTED_MEDIA_TYPE = 415;
|
||||
case REQUESTED_RANGE_NOT_SATISFIABLE = 416;
|
||||
case EXPECTATION_FAILED = 417;
|
||||
|
||||
// Server Error 5xx
|
||||
case 500 = 'Internal Server Error';
|
||||
case 501 = 'Not Implemented';
|
||||
case 502 = 'Bad Gateway';
|
||||
case 503 = 'Service Unavailable';
|
||||
case 504 = 'Gateway Timeout';
|
||||
case 505 = 'HTTP Version Not Supported';
|
||||
}
|
||||
case INTERNAL_SERVER_ERROR = 500;
|
||||
case NOT_IMPLEMENTED = 501;
|
||||
case BAD_GATEWAY = 502;
|
||||
case SERVICE_UNAVAILABLE = 503;
|
||||
case GATEWAY_TIMEOUT = 504;
|
||||
case HTTP_VERSION_NOT_SUPPORTED = 505;
|
||||
|
||||
/**
|
||||
* Get the human-readable message for this status code
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::CONTINUE => 'Continue',
|
||||
self::SWITCHING_PROTOCOLS => 'Switching Protocols',
|
||||
self::OK => 'OK',
|
||||
self::CREATED => 'Created',
|
||||
self::ACCEPTED => 'Accepted',
|
||||
self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information',
|
||||
self::NO_CONTENT => 'No Content',
|
||||
self::RESET_CONTENT => 'Reset Content',
|
||||
self::PARTIAL_CONTENT => 'Partial Content',
|
||||
self::MULTIPLE_CHOICES => 'Multiple Choices',
|
||||
self::MOVED_PERMANENTLY => 'Moved Permanently',
|
||||
self::FOUND => 'Found',
|
||||
self::SEE_OTHER => 'See Other',
|
||||
self::NOT_MODIFIED => 'Not Modified',
|
||||
self::USE_PROXY => 'Use Proxy',
|
||||
self::TEMPORARY_REDIRECT => 'Temporary Redirect',
|
||||
self::BAD_REQUEST => 'Bad Request',
|
||||
self::UNAUTHORIZED => 'Unauthorized',
|
||||
self::PAYMENT_REQUIRED => 'Payment Required',
|
||||
self::FORBIDDEN => 'Forbidden',
|
||||
self::NOT_FOUND => 'Not Found',
|
||||
self::METHOD_NOT_ALLOWED => 'Method Not Allowed',
|
||||
self::NOT_ACCEPTABLE => 'Not Acceptable',
|
||||
self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
|
||||
self::REQUEST_TIMEOUT => 'Request Timeout',
|
||||
self::CONFLICT => 'Conflict',
|
||||
self::GONE => 'Gone',
|
||||
self::LENGTH_REQUIRED => 'Length Required',
|
||||
self::PRECONDITION_FAILED => 'Precondition Failed',
|
||||
self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large',
|
||||
self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long',
|
||||
self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
|
||||
self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
|
||||
self::EXPECTATION_FAILED => 'Expectation Failed',
|
||||
self::INTERNAL_SERVER_ERROR => 'Internal Server Error',
|
||||
self::NOT_IMPLEMENTED => 'Not Implemented',
|
||||
self::BAD_GATEWAY => 'Bad Gateway',
|
||||
self::SERVICE_UNAVAILABLE => 'Service Unavailable',
|
||||
self::GATEWAY_TIMEOUT => 'Gateway Timeout',
|
||||
self::HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this is a successful response (2xx)
|
||||
*/
|
||||
public function isSuccess(): bool
|
||||
{
|
||||
return $this->value >= 200 && $this->value < 300;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this is a client error (4xx)
|
||||
*/
|
||||
public function isClientError(): bool
|
||||
{
|
||||
return $this->value >= 400 && $this->value < 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this is a server error (5xx)
|
||||
*/
|
||||
public function isServerError(): bool
|
||||
{
|
||||
return $this->value >= 500 && $this->value < 600;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this is a redirect (3xx)
|
||||
*/
|
||||
public function isRedirect(): bool
|
||||
{
|
||||
return $this->value >= 300 && $this->value < 400;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this is informational (1xx)
|
||||
*/
|
||||
public function isInformational(): bool
|
||||
{
|
||||
return $this->value >= 100 && $this->value < 200;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the category name for this status code
|
||||
*/
|
||||
public function category(): string
|
||||
{
|
||||
return match (true) {
|
||||
$this->isInformational() => 'Informational',
|
||||
$this->isSuccess() => 'Success',
|
||||
$this->isRedirect() => 'Redirection',
|
||||
$this->isClientError() => 'Client Error',
|
||||
$this->isServerError() => 'Server Error',
|
||||
default => 'Unknown',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTTPCodes enum from an integer code
|
||||
* Returns null if code doesn't exist
|
||||
*/
|
||||
public static function tryFromCode(int $code): ?self
|
||||
{
|
||||
return self::tryFrom($code);
|
||||
}
|
||||
}
|
||||
+168
-138
@@ -2,105 +2,162 @@
|
||||
|
||||
namespace Routes;
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Summary of Route
|
||||
*/
|
||||
class Route
|
||||
{
|
||||
/*
|
||||
* HTTP VERB
|
||||
*/
|
||||
public Array $_verb = Array(); // Route Verbs to match
|
||||
|
||||
public $_verb = Array();
|
||||
/*
|
||||
* Execution Source
|
||||
* HTTP | Cli | SOAP |
|
||||
*/
|
||||
public $_source;
|
||||
public string $_source; // Execution Source (HTTP | Cli | SOAP)
|
||||
|
||||
/**
|
||||
* Tag Name
|
||||
* @var string
|
||||
*/
|
||||
public $_name;
|
||||
public string $_name; // Route Name
|
||||
|
||||
/*
|
||||
* URI String
|
||||
*/
|
||||
public $_uri;
|
||||
public string $_uri; // Route URI
|
||||
|
||||
/*
|
||||
* Routes dispatch priority
|
||||
*/
|
||||
public $_weight = 0;
|
||||
public int $_weight = 0; // Wight of the route, for sorting
|
||||
|
||||
/*
|
||||
* URI Segments
|
||||
*/
|
||||
public $_uriSegments = Array();
|
||||
public Array $_uriSegments = Array(); // Route Full URI Segments
|
||||
|
||||
/*
|
||||
* Extracted params
|
||||
*/
|
||||
public $_segments = Array();
|
||||
public Array $_segments = Array(); // Route parsed URI Segments
|
||||
|
||||
/*
|
||||
* Prepared params
|
||||
*/
|
||||
public $_params = Array();
|
||||
public Array $_params = Array(); // Prepared Routes parameters
|
||||
|
||||
/*
|
||||
* Execute before dispatch route
|
||||
*/
|
||||
public $_before = Array();
|
||||
public Array $_before = Array(); // Functions to execute Before the Route
|
||||
|
||||
/*
|
||||
* Route callback
|
||||
*/
|
||||
public $_callback = Array();
|
||||
public Array|string $_callback = Array(); // Route Callback
|
||||
|
||||
/*
|
||||
* Execute after route dispatched
|
||||
*/
|
||||
public $_after = Array();
|
||||
public Array $_after = Array(); // Functions to execute After the Route
|
||||
|
||||
/*
|
||||
* Prepared RegEx
|
||||
*/
|
||||
public $_regex = '';
|
||||
public string $_regex = ''; // The URI regex to match
|
||||
|
||||
/*
|
||||
* Can execute other route after this one?
|
||||
*/
|
||||
public $_block = true;
|
||||
public bool $_block = true; // Will this route block the remaining execution?
|
||||
|
||||
/*
|
||||
* Is there a problem witch one?
|
||||
*/
|
||||
public $_httpError = false;
|
||||
public bool $_httpError = false;
|
||||
|
||||
/*
|
||||
* This rout should be counted?
|
||||
*/
|
||||
public $_ignore = false;
|
||||
public bool $_ignore = false; // Wil the execution of this route be counted. To know 404
|
||||
|
||||
/*
|
||||
* Middlewares to remove from the list
|
||||
public array $_middlewaresToIgnore = array(); // Middlewares this Route will ignore
|
||||
|
||||
public array $_tags = array(); // Tags of the Route
|
||||
|
||||
|
||||
// ========
|
||||
// Getters and Setters
|
||||
//
|
||||
|
||||
/**
|
||||
* Set the name of this Route
|
||||
*
|
||||
* @param string $name
|
||||
* @return Route
|
||||
*/
|
||||
public $_middlewaresToIgnore = Array();
|
||||
|
||||
/*
|
||||
* Arbitrary tag on a route
|
||||
function setName(string $name): Route
|
||||
{
|
||||
$this->_name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the route URI
|
||||
*
|
||||
* @param mixed $uri
|
||||
*
|
||||
* @return Route
|
||||
*/
|
||||
public $_tags = Array();
|
||||
function setUri($uri): Route
|
||||
{
|
||||
$this->_uri = $uri;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of segments of the route
|
||||
* @return array
|
||||
*/
|
||||
function getSegments(): array
|
||||
{
|
||||
return $this->_segments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define that this route will not block the execution
|
||||
*
|
||||
* @return Route
|
||||
*/
|
||||
function notBlock(): Route
|
||||
{
|
||||
$this->_block = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define that this route will be ignored on the counting
|
||||
* if no routes match, we will throw a Excepton
|
||||
*
|
||||
* @return Route
|
||||
*/
|
||||
function doIgnore(): Route
|
||||
{
|
||||
$this->_ignore = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a tag for this Route
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @return Route
|
||||
*/
|
||||
function setTag(string $key, string $value): Route
|
||||
{
|
||||
$this->_tags[$key][] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this Route has a specific tag
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
function hasTag(string $key): bool
|
||||
{
|
||||
return array_key_exists($key, $this->_tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Route weight
|
||||
*
|
||||
* @param int $weigth
|
||||
* @return Route
|
||||
*/
|
||||
function setWeight(int $weigth = 0): Route
|
||||
{
|
||||
$this->_weight = $weigth;
|
||||
return $this;
|
||||
}
|
||||
|
||||
//=======================================================
|
||||
function execute()
|
||||
// Route Functionality
|
||||
//
|
||||
|
||||
/**
|
||||
* Execute the route
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function execute(): mixed
|
||||
{
|
||||
|
||||
$routeReturn = "";
|
||||
|
||||
//Middlewares with function or class method
|
||||
foreach ($this->_before as $key => $before) {
|
||||
if (!in_array($key, $this->_middlewaresToIgnore) && !$this->_ignore) {
|
||||
@@ -132,7 +189,7 @@ class Route
|
||||
}
|
||||
$this->_params['id'] = $element;
|
||||
}
|
||||
$class->{$segments[1]}(...array_values($this->_params));
|
||||
$routeReturn = $class->{$segments[1]}(...array_values($this->_params));
|
||||
} else {
|
||||
$r = new \ReflectionFunction($callback);
|
||||
$firstType = count($r->getParameters()) > 0 ? $r->getParameters()[0]->getType() : null;
|
||||
@@ -144,15 +201,22 @@ class Route
|
||||
}
|
||||
$this->_params['id'] = $element;
|
||||
}
|
||||
call_user_func_array($callback, $this->_params);
|
||||
$routeReturn = call_user_func_array($callback, $this->_params);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->_after as $after) {
|
||||
call_user_func($after);
|
||||
}
|
||||
|
||||
return $routeReturn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the route placeholders
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function prepare()
|
||||
{
|
||||
$this->_segments = explode('/', $this->_uri);
|
||||
@@ -176,6 +240,11 @@ class Route
|
||||
$this->_regex = "/^\/" . $this->_regex . "$/";
|
||||
}
|
||||
|
||||
/**
|
||||
* Split the route segments into indexes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function createIndexes()
|
||||
{
|
||||
foreach ($this->_segments as $key => $segment) {
|
||||
@@ -187,61 +256,29 @@ class Route
|
||||
}
|
||||
}
|
||||
|
||||
function setUri($uri)
|
||||
{
|
||||
$this->_uri = $uri;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function getSegments()
|
||||
{
|
||||
return $this->_segments;
|
||||
}
|
||||
|
||||
function doBlock()
|
||||
{
|
||||
$this->_block = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function notBlock()
|
||||
{
|
||||
$this->_block = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function doIgnore()
|
||||
{
|
||||
$this->_ignore = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function notIgnore()
|
||||
{
|
||||
$this->_ignore = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareIgnore($name = '')
|
||||
{
|
||||
$this->_middlewaresToIgnore[] = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareAdd($name = '', $params = [])
|
||||
/**
|
||||
* Add a middleware for this route
|
||||
*
|
||||
* @param string $name Name of the Middleware
|
||||
* @param Array $params Array of parametrs to pass on the function
|
||||
*
|
||||
* @return Route
|
||||
*/
|
||||
function middlewareAdd(string $name, array $params): Route
|
||||
{
|
||||
$this->_before[$name] = $params;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareAppend($name = '', $function)
|
||||
/**
|
||||
* Middlewares this Route will ignore
|
||||
*
|
||||
* @param mixed $name
|
||||
* @return Route
|
||||
*/
|
||||
function middlewareIgnore(string $name): Route
|
||||
{
|
||||
$this->_before[$name] = $function;
|
||||
}
|
||||
|
||||
function setWeight($weigth = 0)
|
||||
{
|
||||
$this->_weight = $weigth;
|
||||
$this->_middlewaresToIgnore[] = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -251,7 +288,7 @@ class Route
|
||||
$message = self::$httpMessages[$code];
|
||||
}
|
||||
|
||||
$info = new \stdClass();
|
||||
$info = new stdClass();
|
||||
$info->code = $code;
|
||||
$info->message = $message;
|
||||
|
||||
@@ -259,7 +296,13 @@ class Route
|
||||
return $this;
|
||||
}
|
||||
|
||||
function match($uri)
|
||||
/**
|
||||
* Check if the URI match the Route Signature
|
||||
*
|
||||
* @param string $uri
|
||||
* @return bool
|
||||
*/
|
||||
function match(string $uri): bool
|
||||
{
|
||||
//Wildcards allways match
|
||||
if (in_array('*', $this->_verb) || $this->_uri == '*') {
|
||||
@@ -275,21 +318,8 @@ class Route
|
||||
return false;
|
||||
}
|
||||
|
||||
function name($name){
|
||||
$this->_name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function setTag($key = '', $value = ''){
|
||||
$this->_tags[$key][] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function hasTag($key){
|
||||
return array_key_exists($key, $this->_tags);
|
||||
}
|
||||
|
||||
function compress($group, $tag){
|
||||
function compress($group, $tag)
|
||||
{
|
||||
//$pattern = '/\[a:(\w+)\]/';
|
||||
//$replacement = '/{{$1}}/';
|
||||
//$route = preg_replace($pattern, $replacement, $this->_uri) );
|
||||
|
||||
+459
-263
@@ -2,35 +2,48 @@
|
||||
|
||||
namespace Routes;
|
||||
|
||||
use stdClass;
|
||||
use Exception;
|
||||
|
||||
class RouteCollection
|
||||
{
|
||||
|
||||
private static $_routeCollection;
|
||||
private static RouteCollection $_routeCollection;
|
||||
|
||||
public $_uri = '/';
|
||||
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');
|
||||
|
||||
public $_groupIn = false;
|
||||
public $_groupBase = '';
|
||||
public $_groupList = [];
|
||||
public $_groups = [];
|
||||
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
|
||||
|
||||
|
||||
//SINGLETON==============================================
|
||||
|
||||
/**
|
||||
* Empty constructor for Singleton
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private static function newObj()
|
||||
/**
|
||||
* Instanciate the Singleton Object
|
||||
*
|
||||
* @return RouteCollection
|
||||
*/
|
||||
private static function newObj(): RouteCollection
|
||||
{
|
||||
if (!isset(self::$_routeCollection)) {
|
||||
self::$_routeCollection = new RouteCollection();
|
||||
@@ -40,15 +53,18 @@ class RouteCollection
|
||||
self::$_routeCollection->_uri = isset($_SERVER['REQUEST_URI']) ? explode('?', $_SERVER['REQUEST_URI'])[0] : '/';
|
||||
}
|
||||
self::$_routeCollection->defineVerb();
|
||||
self::$_routeCollection->setDefaultParsers();
|
||||
}
|
||||
self::$_routeCollection->_uri = str_replace("//", "/", self::$_routeCollection->_uri);
|
||||
return self::$_routeCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Return the Singleton instance
|
||||
*
|
||||
* @return RouteCollection
|
||||
*/
|
||||
public static function getInstance()
|
||||
public static function getInstance(): RouteCollection
|
||||
{
|
||||
if (!isset(self::$_routeCollection)) {
|
||||
return self::newObj();
|
||||
@@ -56,18 +72,22 @@ class RouteCollection
|
||||
return self::$_routeCollection;
|
||||
}
|
||||
|
||||
//HELPERS================================================
|
||||
//=======================================================
|
||||
// HELPERS
|
||||
//
|
||||
|
||||
/*
|
||||
* Find route files with names under pathname
|
||||
*
|
||||
* @ctag\t RouteCollection->crawl()
|
||||
/**
|
||||
* Crawl the filesystem to find the System Routes
|
||||
*
|
||||
* @param string $basePath Path to crawl
|
||||
* @param array $filenames Filenames to Match
|
||||
* @return RouteCollection
|
||||
*/
|
||||
public function crawl($basepath = __DIR__, $filenames = array('Routes.php', 'routes.php'))
|
||||
public function crawl(string $basePath = __DIR__, array $filenames = array('Routes.php', 'routes.php')): RouteCollection
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
|
||||
$rdi = new \RecursiveDirectoryIterator($basepath);
|
||||
$rdi = new \RecursiveDirectoryIterator($basePath);
|
||||
|
||||
foreach (new \RecursiveIteratorIterator($rdi) as $file) {
|
||||
foreach ($filenames as $filename) {
|
||||
@@ -81,11 +101,11 @@ class RouteCollection
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the files
|
||||
*
|
||||
* Load the files with the routes
|
||||
* @ctag\t RouteCollection->loadRoutes()
|
||||
* @return RouteCollection
|
||||
*/
|
||||
function loadRoutes()
|
||||
function loadRoutes(): RouteCollection
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
|
||||
@@ -96,15 +116,14 @@ class RouteCollection
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* GET | POST | PUT | PATCH | DELETE | CLI
|
||||
*
|
||||
* Infer the Request Verb
|
||||
*
|
||||
* @param string $verb DO NOT USE
|
||||
* @throws Exception
|
||||
* @return void
|
||||
*/
|
||||
private function defineVerb()
|
||||
private function defineVerb(string $verb = ""): void
|
||||
{
|
||||
|
||||
$verb = '';
|
||||
|
||||
if (isset($_POST['_method'])) {
|
||||
$verb = strtoupper($_POST['_method']);
|
||||
} else if (php_sapi_name() == "cli") {
|
||||
@@ -116,14 +135,16 @@ class RouteCollection
|
||||
if (in_array($verb, self::$_verbsWhitelist)) {
|
||||
$this->_verb = $verb;
|
||||
} else {
|
||||
echo 'Invalid Verb';
|
||||
throw new Exception("Verb not Allowed");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the route order
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function sort_routes()
|
||||
private function sortRoutes(): void
|
||||
{
|
||||
usort($this->_routes, function ($a, $b) {
|
||||
if ($a->_weight == $b->_weight) {
|
||||
@@ -133,231 +154,6 @@ class RouteCollection
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function submit()
|
||||
{
|
||||
global $ROUTE;
|
||||
|
||||
self::$_routeCollection->sort_routes();
|
||||
|
||||
$one_hit = false;
|
||||
|
||||
foreach (self::$_routeCollection->_routes as $route) {
|
||||
|
||||
if (!in_array(self::$_routeCollection->_verb, $route->_verb) && !in_array('*', $route->_verb)) {
|
||||
//No it is not
|
||||
continue;
|
||||
}
|
||||
|
||||
//Request match a route
|
||||
$match = $route->match(self::$_routeCollection->_uri);
|
||||
|
||||
if ($match) {
|
||||
//Yup. Execute the route
|
||||
|
||||
$ROUTE = $route;
|
||||
|
||||
foreach ($route->_before as $key => $requestedMiddleware) {
|
||||
$route->_before[$key] = array(
|
||||
'callback' => $this->_middlewareSet[$key],
|
||||
'params' => $requestedMiddleware
|
||||
);
|
||||
}
|
||||
|
||||
$route->_before = array_merge($this->_defaultMiddlewares, $route->_before);
|
||||
|
||||
$route->execute();
|
||||
|
||||
if (!$route->_ignore) {
|
||||
$one_hit = true;
|
||||
}
|
||||
|
||||
if ($route->_httpError) {
|
||||
$this->httpError($route->_httpError);
|
||||
return;
|
||||
}
|
||||
|
||||
//If Executed, interrupt route chain?
|
||||
if ($route->_block) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$one_hit) {
|
||||
$info = new \stdClass();
|
||||
$info->code = 404;
|
||||
$info->message = 'Not Found';
|
||||
self::$_routeCollection->httpError($info);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::group('base',function(){})
|
||||
*/
|
||||
static function group($base = '', $callback, $name = '')
|
||||
{
|
||||
$collection = self::getInstance();
|
||||
$collection->_groupList = [];
|
||||
$collection->_groupIn = true;
|
||||
$collection->_groupBase = $base;
|
||||
call_user_func($callback);
|
||||
$collection->_groupBase = '';
|
||||
$collection->_groupIn = false;
|
||||
$group = new RouteGroup($collection->_groupList);
|
||||
$collection->_groups[$name] = $group;
|
||||
return $group;
|
||||
}
|
||||
|
||||
//DEFINITORS=============================================
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::get('/url',function(){})
|
||||
*/
|
||||
static function get($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('GET', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::cli('/url',function(){})
|
||||
*/
|
||||
static function cli($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('CLI', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::post('/url',function(){})
|
||||
*/
|
||||
static function post($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('POST', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::put('/url',function(){})
|
||||
*/
|
||||
static function put($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('PUT', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::patch('/url',function(){})
|
||||
*/
|
||||
static function patch($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('PATCH', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::delete('/url',Controller@Method)
|
||||
* @ctag RouteCollection::delete('/url',function(){})
|
||||
*/
|
||||
static function delete($uri, $callback, $weight = 0)
|
||||
{
|
||||
return self::add('DELETE', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::resource('/url',function(){})
|
||||
*/
|
||||
static function resource($uri)
|
||||
{
|
||||
throw new Exception('Not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::add('VERB','/url',function(){})
|
||||
* @ctag RouteCollection::add('VERB','/url',Controller@Method)
|
||||
*/
|
||||
static function add($verb, $uri, $callback, $weight = 0)
|
||||
{
|
||||
$route = new Route();
|
||||
|
||||
if (self::getInstance()->_groupIn) {
|
||||
$uri = self::getInstance()->_groupBase . $uri;
|
||||
self::getInstance()->_groupList[] = &$route;
|
||||
}
|
||||
$uri = rtrim($uri, '/');
|
||||
if (is_array($verb)) {
|
||||
$route->_verb = array_map('strtoupper', $verb);
|
||||
} else if (strtoupper($verb) == 'WEB') {
|
||||
$route->_verb = array_merge($route->_verb, self::$_verbsWeb);
|
||||
} else {
|
||||
$route->_verb[] = strtoupper($verb);
|
||||
}
|
||||
|
||||
$route->_uri = $uri;
|
||||
$route->_callback[] = $callback;
|
||||
$route->_weight = $weight;
|
||||
$route->prepare();
|
||||
|
||||
self::getInstance()->_routes[] = $route;
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function addRoute(Route $route)
|
||||
{
|
||||
$this->_routes[] = $route;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::addDefaultMiddleware('name',function(){})
|
||||
*/
|
||||
static function addDefaultMiddleware($name = '', $function)
|
||||
{
|
||||
self::getInstance()->_defaultMiddlewares[$name] = $function;
|
||||
return self::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::addDefaultMiddleware('name',function(){})
|
||||
*/
|
||||
static function registerMiddleware($name = '', $function)
|
||||
{
|
||||
return self::getInstance()->_middlewareSet[$name] = $function;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::onHttpError(function(){})
|
||||
*/
|
||||
static function onHttpError($code, $function)
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::httpError($class)
|
||||
*/
|
||||
private function httpError(\stdClass $info)
|
||||
{
|
||||
$info = (array) $info;
|
||||
foreach ($this->_errors as $error) {
|
||||
call_user_func_array($error, $info);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getGroupRoutesWithTags($group, $tag = '*')
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
@@ -373,4 +169,404 @@ class RouteCollection
|
||||
return $instance->_groups[$group]->getRoutesWithTag($group, $tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trace logs
|
||||
*
|
||||
* @param bool $trace Should we trace
|
||||
* @param int $verbosity How verbose 1 - EXECUTION | 2 - CONTENT
|
||||
* @return RouteCollection
|
||||
*/
|
||||
public static function shouldTrace(bool $trace = true, int $verbosity = 1): RouteCollection
|
||||
{
|
||||
self::getInstance()->_trace = $trace;
|
||||
self::getInstance()->_traceVerbosity = $verbosity;
|
||||
return self::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a trace to the log
|
||||
*
|
||||
* @param mixed $trace Content to Trace
|
||||
* @param int $verbosity How verbose 1 - EXECUTION | 2 - CONTENT
|
||||
* @return void
|
||||
*/
|
||||
private static function trace(mixed $trace, int $verbosity = 1): void
|
||||
{
|
||||
if (!self::getInstance()->_trace) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($verbosity <= self::getInstance()->_traceVerbosity) {
|
||||
error_log(print_r($trace, true));
|
||||
}
|
||||
}
|
||||
|
||||
//=======================================================
|
||||
// Content Parsing
|
||||
//
|
||||
|
||||
/**
|
||||
* Register some common return types
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function setDefaultParsers(): void
|
||||
{
|
||||
$this->_contentParsers["string"] = function (string $content) {
|
||||
echo $content;
|
||||
};
|
||||
|
||||
$this->_contentParsers["array"] = function (array $content) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($content);
|
||||
};
|
||||
|
||||
$this->_contentParsers["stdClass"] = function (object $content) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($content);
|
||||
};
|
||||
|
||||
}
|
||||
/**
|
||||
* Register Content Parsers
|
||||
*
|
||||
* @param string $type Classname of the parser
|
||||
* @param callable $parser How to parse the content
|
||||
* @return RouteCollection
|
||||
*/
|
||||
public static function addParser(string $type, callable $parser): RouteCollection
|
||||
{
|
||||
self::getInstance()->_contentParsers[$type] = $parser;
|
||||
return self::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the content parsing
|
||||
*
|
||||
* @param mixed $content Content to be parsed
|
||||
* @return void
|
||||
*/
|
||||
public function parseContent(mixed $content): void
|
||||
{
|
||||
$contentType = gettype($content);
|
||||
|
||||
if ($contentType == "null") { // Route didn't return anything
|
||||
return;
|
||||
}
|
||||
|
||||
if($contentType == "object"){
|
||||
$contentType = get_class($content);
|
||||
}
|
||||
|
||||
foreach (self::getInstance()->_contentParsers as $type => $parser) {
|
||||
if ($contentType == $type) {
|
||||
call_user_func($parser, $content);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//=======================================================
|
||||
// API
|
||||
//
|
||||
|
||||
/**
|
||||
* Create a Route Group with the prefix
|
||||
*
|
||||
* @param string $prefix The name prefix
|
||||
* @param callable $callback The closure that will nest the routes
|
||||
* @param string $name The name of the group
|
||||
* @return RouteGroup
|
||||
*/
|
||||
public static function group(string $prefix, callable $callback, string $name = ""): RouteGroup
|
||||
{
|
||||
$collection = self::getInstance();
|
||||
$collection->_groupList = [];
|
||||
$collection->_groupIn = true;
|
||||
$collection->_groupBase = $prefix;
|
||||
call_user_func($callback);
|
||||
$collection->_groupBase = '';
|
||||
$collection->_groupIn = false;
|
||||
$group = new RouteGroup($collection->_groupList);
|
||||
$collection->_groups[$name] = $group;
|
||||
|
||||
$collection::trace("RouteGroup $prefix Added", 2);
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function get(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('GET', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function cli(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('CLI', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function post(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('POST', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function put(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('PUT', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function patch(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('PATCH', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function delete(string $uri, string|callable $callback, int $weight = 0): Route
|
||||
{
|
||||
return self::add('DELETE', $uri, $callback, $weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a full resource group for the prefix
|
||||
*
|
||||
* @param string $prefix Prefix to add the resource
|
||||
* @param string $resourceNameSpace NameSpace of the class to map
|
||||
* @return RouteGroup
|
||||
*/
|
||||
static function resource(string $prefix, string $resourceNameSpace): RouteGroup
|
||||
{
|
||||
throw new Exception('Not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Route to the Collection
|
||||
*
|
||||
* @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 int $weight Weight of the Route
|
||||
* @return Route
|
||||
*/
|
||||
static function add(array|string $verb, string $uri, string|callable $callback, int $weight = 0): Route
|
||||
{
|
||||
$route = new Route();
|
||||
|
||||
if (self::getInstance()->_groupIn) {
|
||||
$uri = self::getInstance()->_groupBase . $uri;
|
||||
self::getInstance()->_groupList[] = &$route;
|
||||
}
|
||||
|
||||
$uri = rtrim($uri, '/');
|
||||
|
||||
if (is_array($verb)) {
|
||||
$route->_verb = array_map('strtoupper', $verb);
|
||||
} else if (strtoupper($verb) == 'WEB') {
|
||||
$route->_verb = array_merge($route->_verb, self::$_verbsWeb);
|
||||
} else {
|
||||
$route->_verb[] = strtoupper($verb);
|
||||
}
|
||||
|
||||
$route->_uri = $uri;
|
||||
$route->_callback[] = $callback;
|
||||
$route->_weight = $weight;
|
||||
$route->prepare();
|
||||
|
||||
self::getInstance()->_routes[] = $route;
|
||||
|
||||
self::trace("Route:: " . implode("|", $route->_verb) . " - $uri - ADDED");
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Pre Build Route
|
||||
*
|
||||
* @param Route $route The route object
|
||||
* @return RouteCollection
|
||||
*/
|
||||
function addRoute(Route $route): RouteCollection
|
||||
{
|
||||
$this->_routes[] = $route;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register error Pages
|
||||
*
|
||||
* @param int $code URI to Match
|
||||
* @param string|callable $callback Callback to Execute. Might be a class or a callable
|
||||
* @return RouteCollection
|
||||
*/
|
||||
static function error(int $code, string|callable $callback): RouteCollection
|
||||
{
|
||||
self::getInstance()->_errors[$code] = $callback;
|
||||
return self::getInstance();
|
||||
}
|
||||
|
||||
//=======================================================
|
||||
// Middlewares
|
||||
//
|
||||
|
||||
/**
|
||||
* Register a Middleware
|
||||
*
|
||||
* @param string $name Middleware Name
|
||||
* @param callable $function Middleware colsure
|
||||
* @return RouteCollection
|
||||
*/
|
||||
static function registerMiddleware(string $name, callable $function): RouteCollection
|
||||
{
|
||||
self::getInstance()->_middlewareSet[$name] = $function;
|
||||
return self::getInstance();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a middleware to match all Routes
|
||||
*
|
||||
* @param string $name Middleware Name
|
||||
* @param callable $function Middleware colsure
|
||||
* @return RouteCollection
|
||||
*/
|
||||
static function addDefaultMiddleware(string $name, callable $function): RouteCollection
|
||||
{
|
||||
self::getInstance()->_defaultMiddlewares[$name] = $function;
|
||||
return self::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::onHttpError(function(){})
|
||||
*/
|
||||
static function onHttpError($code, $function)
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag RouteCollection::httpError($class)
|
||||
*/
|
||||
private function httpError(stdClass $info)
|
||||
{
|
||||
$info = (array) $info;
|
||||
foreach ($this->_errors as $error) {
|
||||
call_user_func_array($error, $info);
|
||||
}
|
||||
}
|
||||
|
||||
//=======================================================
|
||||
// RUN
|
||||
//
|
||||
|
||||
/**
|
||||
* Submit the request and execute the matching and Routing
|
||||
*
|
||||
* @return RouteCollection
|
||||
*/
|
||||
public function submit(): RouteCollection
|
||||
{
|
||||
self::$_routeCollection->sortRoutes();
|
||||
|
||||
$one_hit = false;
|
||||
|
||||
foreach (self::$_routeCollection->_routes as $route) {
|
||||
|
||||
if (!in_array(self::$_routeCollection->_verb, $route->_verb) && !in_array('*', $route->_verb)) {
|
||||
//No it is not
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if the request match a Route
|
||||
$match = $route->match(self::$_routeCollection->_uri);
|
||||
|
||||
if ($match) { // This Route Matches the pattern
|
||||
$this->_currentRoute = $route;
|
||||
|
||||
foreach ($route->_before as $key => $requestedMiddleware) {
|
||||
$route->_before[$key] = array(
|
||||
'callback' => $this->_middlewareSet[$key],
|
||||
'params' => $requestedMiddleware
|
||||
);
|
||||
}
|
||||
|
||||
$route->_before = array_merge($this->_defaultMiddlewares, $route->_before);
|
||||
|
||||
$routeResult = $route->execute();
|
||||
|
||||
if (!$route->_ignore) {
|
||||
$one_hit = true;
|
||||
}
|
||||
|
||||
if ($route->_httpError) {
|
||||
$this->httpError($route->_httpError);
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->parseContent($routeResult);
|
||||
|
||||
if ($route->_block) { //If Executed, interrupt route chain?
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$one_hit) { // Check if at least on counted Route matched
|
||||
if (isset($this->_errors['404'])) { // Return a defined 404
|
||||
call_user_func($this->_errors['404']);
|
||||
} else { // Return a regular 404
|
||||
http_response_code(404);
|
||||
}
|
||||
}
|
||||
|
||||
return $this; // Do you really want to chain more Stuff?
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ class RouteGroup
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareIgnore($name = '')
|
||||
function middlewareIgnore($name)
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_middlewaresToIgnore[] = $name;
|
||||
@@ -58,7 +58,7 @@ class RouteGroup
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareAdd($name = '', $function)
|
||||
function middlewareAdd($name)
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_before[] = $name;
|
||||
@@ -66,7 +66,7 @@ class RouteGroup
|
||||
return $this;
|
||||
}
|
||||
|
||||
function middlewareAppend($name = '', $function)
|
||||
function middlewareAppend($name, $function)
|
||||
{
|
||||
foreach ($this->_routeGroup as $route) {
|
||||
$route->_before[$name] = $function;
|
||||
@@ -98,10 +98,16 @@ class RouteGroup
|
||||
return $this;
|
||||
}
|
||||
|
||||
function getRoutesWithTag($group, $tag){
|
||||
function getRoutesWithTag($group, $tag): Array
|
||||
{
|
||||
return array_filter(array_map(function($route) use($group, $tag){
|
||||
return $route->hasTag($tag) ? $route->compress($group, $tag) : null;
|
||||
}, $this->_routeGroup));
|
||||
}
|
||||
|
||||
}
|
||||
function getRoutes()
|
||||
{
|
||||
return $this->_routeGroup;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user