Tags functionality

This commit is contained in:
2023-04-25 17:49:49 +00:00
parent 5ac437d7c1
commit 05a330d65e
3 changed files with 73 additions and 4 deletions
+45
View File
@@ -6,6 +6,9 @@ namespace Routes;
* *
*/ */
/**
* Summary of Route
*/
class Route class Route
{ {
/* /*
@@ -19,6 +22,12 @@ class Route
*/ */
public $_source; public $_source;
/**
* Tag Name
* @var string
*/
public $_name;
/* /*
* URI String * URI String
*/ */
@@ -87,6 +96,10 @@ class Route
* From Klein * From Klein
* HTTP status List * HTTP status List
*/ */
public $_tags = [];
protected static $httpMessages = array( protected static $httpMessages = array(
// Informational 1xx // Informational 1xx
100 => 'Continue', 100 => 'Continue',
@@ -309,4 +322,36 @@ class Route
return false; 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){
//$pattern = '/\[a:(\w+)\]/';
//$replacement = '/{{$1}}/';
//$route = preg_replace($pattern, $replacement, $this->_uri) );
$route = str_replace(['[i:', '[d:'], '{{', $this->_uri);
$route = str_replace([']', ']'], '}}', $route);
return [
'name' => $this->_name,
'group' => $group,
'tag' => $tag,
'verbs' => $this->_verb,
//'route' => preg_replace($pattern, $replacement, $this->_uri)
'route' => $route
];
}
} }
+20 -2
View File
@@ -20,6 +20,7 @@ class RouteCollection
public $_groupIn = false; public $_groupIn = false;
public $_groupBase = ''; public $_groupBase = '';
public $_groupList = []; public $_groupList = [];
public $_groups = [];
//SINGLETON============================================== //SINGLETON==============================================
@@ -197,7 +198,7 @@ class RouteCollection
* *
* @ctag RouteCollection::group('base',function(){}) * @ctag RouteCollection::group('base',function(){})
*/ */
static function group($base = '', $callback) static function group($base = '', $callback, $name = '')
{ {
$collection = self::getInstance(); $collection = self::getInstance();
$collection->_groupList = []; $collection->_groupList = [];
@@ -206,7 +207,9 @@ class RouteCollection
call_user_func($callback); call_user_func($callback);
$collection->_groupBase = ''; $collection->_groupBase = '';
$collection->_groupIn = false; $collection->_groupIn = false;
return new RouteGroup($collection->_groupList); $group = new RouteGroup($collection->_groupList);
$collection->_groups[$name] = $group;
return $group;
} }
//DEFINITORS============================================= //DEFINITORS=============================================
@@ -355,4 +358,19 @@ class RouteCollection
} }
} }
public static function getGroupRoutesWithTags($group, $tag = '*')
{
$instance = self::getInstance();
if (!isset($instance->_groups[$group])) {
return [];
}
if (!isset($tag)) {
return $instance->_groups[$group];
}
return $instance->_groups[$group]->getRoutesWithTag($group, $tag);
}
} }
+8 -2
View File
@@ -58,10 +58,10 @@ class RouteGroup
return $this; return $this;
} }
function middlewareAdd($name = '') function middlewareAdd($name = '', $function)
{ {
foreach ($this->_routeGroup as $route) { foreach ($this->_routeGroup as $route) {
$route->_before[$name] = $function; $route->_before[] = $name;
} }
return $this; return $this;
} }
@@ -98,4 +98,10 @@ class RouteGroup
return $this; return $this;
} }
function getRoutesWithTag($group, $tag){
return array_filter(array_map(function($route) use($group, $tag){
return $route->hasTag($tag) ? $route->compress($group, $tag) : null;
}, $this->_routeGroup));
}
} }