mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
N°5655 - Router: Add method to dump available routes
This commit is contained in:
@@ -327,7 +327,6 @@ try
|
|||||||
|
|
||||||
// Response is a \WebPage, let's handle it like legacy operations
|
// Response is a \WebPage, let's handle it like legacy operations
|
||||||
$oP = $mResponse;
|
$oP = $mResponse;
|
||||||
// TODO 3.1: If no route match, die instead of fallback to legacy operation dispatch and dump available routes if in dev env.
|
|
||||||
}
|
}
|
||||||
// Otherwise, use legacy operation
|
// Otherwise, use legacy operation
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
namespace Combodo\iTop\Router;
|
namespace Combodo\iTop\Router;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionMethod;
|
||||||
use utils;
|
use utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,6 +37,44 @@ class Router
|
|||||||
return static::$oSingleton;
|
return static::$oSingleton;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{0: string, 1: string} Array of available routes namespaces and their corresponding controllers (eg. ['object' => '\Combodo\iTop\Controller\Base\Layout\ObjectController', ...])
|
||||||
|
*/
|
||||||
|
public static function GetRoutesNamespaces(): array
|
||||||
|
{
|
||||||
|
$aRoutesNamespaces = [];
|
||||||
|
foreach (utils::GetClassesForInterface('Combodo\iTop\Controller\iController', '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]']) as $sControllerFQCN) {
|
||||||
|
$aRoutesNamespaces[$sControllerFQCN::ROUTE_NAMESPACE] = $sControllerFQCN;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $aRoutesNamespaces;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{0: string, 1: string} Array of available routes and their corresponding controllers (eg. ['object.modify' => '\Combodo\iTop\Controller\Base\Layout\ObjectController::OperationModify', ...])
|
||||||
|
* @throws \ReflectionException
|
||||||
|
*/
|
||||||
|
public static function GetRoutes(): array
|
||||||
|
{
|
||||||
|
$aRoutes = [];
|
||||||
|
foreach (static::GetRoutesNamespaces() as $sRouteNamespace => $sRouteControllerFQCN) {
|
||||||
|
$oReflectionClass = new ReflectionClass($sRouteControllerFQCN);
|
||||||
|
foreach ($oReflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $oReflectionMethod) {
|
||||||
|
// Ignore non "operation" methods
|
||||||
|
$sPrefix = 'Operation';
|
||||||
|
$iPos = stripos($oReflectionMethod->name, $sPrefix);
|
||||||
|
if ($iPos !== 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sOperationName = substr($oReflectionMethod->name, $iPos + strlen($sPrefix));
|
||||||
|
$aRoutes[$sRouteNamespace.'.'.utils::ToSnakeCase($sOperationName)] = $sRouteControllerFQCN.'::'.$oReflectionMethod->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $aRoutes;
|
||||||
|
}
|
||||||
|
|
||||||
/**********************/
|
/**********************/
|
||||||
/* Non-static methods */
|
/* Non-static methods */
|
||||||
/**********************/
|
/**********************/
|
||||||
@@ -161,8 +201,8 @@ class Router
|
|||||||
*/
|
*/
|
||||||
protected function FindControllerFromRouteNamespace(string $sRouteNamespace): ?string
|
protected function FindControllerFromRouteNamespace(string $sRouteNamespace): ?string
|
||||||
{
|
{
|
||||||
foreach (utils::GetClassesForInterface('Combodo\iTop\Controller\iController', '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]']) as $sControllerFQCN) {
|
foreach (static::GetRoutesNamespaces() as $sControllerRouteNamespace => $sControllerFQCN) {
|
||||||
if ($sControllerFQCN::ROUTE_NAMESPACE === $sRouteNamespace) {
|
if ($sControllerRouteNamespace === $sRouteNamespace) {
|
||||||
return $sControllerFQCN;
|
return $sControllerFQCN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user