79 lines
2.6 KiB
PHP
79 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use MyCodebox\MariaCore\Http\Request;
|
|
use MyCodebox\MariaCore\Http\Response;
|
|
|
|
/**
|
|
* Controller für die Anzeige und Ausgabe der registrierten Routen.
|
|
*/
|
|
class RoutesController
|
|
{
|
|
/**
|
|
* @var mixed Router-Instanz (typischerweise MyCodebox\MariaCore\Router)
|
|
*/
|
|
protected $router;
|
|
|
|
/**
|
|
* Konstruktor.
|
|
*
|
|
* @param mixed $router
|
|
*/
|
|
public function __construct($router)
|
|
{
|
|
$this->router = $router;
|
|
}
|
|
|
|
/**
|
|
* Gibt alle registrierten Routen als JSON zurück.
|
|
*
|
|
* @param Request $request
|
|
* @param array $params
|
|
* @return Response
|
|
*/
|
|
public function json(Request $request, array $params): Response
|
|
{
|
|
$routes = [];
|
|
foreach ($this->router->getRoutes() as $method => $paths) {
|
|
foreach ($paths as $path => $route) {
|
|
$routes[] = [
|
|
'method' => $method,
|
|
'path' => $path,
|
|
'name' => $route['name'] ?? null,
|
|
'middleware_count' => isset($route['middleware']) ? count($route['middleware']) : 0,
|
|
];
|
|
}
|
|
}
|
|
return (new Response())->withJson($routes, 200, JSON_PRETTY_PRINT);
|
|
}
|
|
|
|
/**
|
|
* Gibt alle registrierten Routen als HTML-Tabelle zurück.
|
|
*
|
|
* @param Request $request
|
|
* @param array $params
|
|
* @return Response
|
|
*/
|
|
public function html(Request $request, array $params): Response
|
|
{
|
|
$routes = [];
|
|
foreach ($this->router->getRoutes() as $method => $paths) {
|
|
foreach ($paths as $path => $route) {
|
|
$routes[] = [
|
|
'method' => $method,
|
|
'path' => $path,
|
|
'name' => $route['name'] ?? '',
|
|
'middleware_count' => isset($route['middleware']) ? count($route['middleware']) : 0,
|
|
];
|
|
}
|
|
}
|
|
$html = '<h1>Routen-Übersicht</h1><table border="1" cellpadding="5" style="border-collapse:collapse"><tr><th>Method</th><th>Path</th><th>Name</th><th>Middleware</th></tr>';
|
|
foreach ($routes as $route) {
|
|
$link = '<a href="' . htmlspecialchars($route['path'], ENT_QUOTES, 'UTF-8') . '">' . htmlspecialchars($route['path'], ENT_QUOTES, 'UTF-8') . '</a>';
|
|
$html .= '<tr><td>' . htmlspecialchars($route['method'], ENT_QUOTES, 'UTF-8') . '</td><td>' . $link . '</td><td>' . htmlspecialchars($route['name'], ENT_QUOTES, 'UTF-8') . '</td><td>' . (int)$route['middleware_count'] . '</td></tr>';
|
|
}
|
|
$html .= '</table>';
|
|
return (new Response())->withHtml($html);
|
|
}
|
|
}
|