- First commit

This commit is contained in:
Dave Mc Nicoll committed 2024-10-14 15:28:35 +00:00
commit f5317f906d
9 files changed
+484

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
<?php
namespace Lean\Api;
use Lean\Factory\HttpFactory;
use Notes\ObjectReflection;
use Notes\Route\Attribute\Method\Route;
use function CSSLSJ\Reprise\Api\View\{ _, lang, url, route, form };
class RouteDescriptor
{
public string $routeLine = <<<HTML
<li>
<span><a href='%s' title="%s" style='font-family:monospace;font-size:.85em'>%s</a> - %s</span>
<span style='color:#ac1b1b'>%s</span>
<small style="color:#374300">%s</small>
</li>
HTML;
public function __construct(
public object $controller,
) {}
/**
* @return Returns HTML describing given controller's routes
*/
public function describe() : string
{
$html = "";
$reflector = new ObjectReflection($this->controller::class);
$attribute = $reflector->reflectClass()->getAttribute(\Notes\Route\Attribute\Object\Route::class);
$base = $attribute ? $attribute->object->base : "";
foreach($reflector->reflectMethods() as $method) {
foreach(array_reverse($method->getAttributes(Route::class)) as $routeAttribute) {
$route = $routeAttribute->object;
$path = rtrim($route->route, '/');
$cleaned = $this->cleanRouteFromRegex($base.$path);
$url = url($cleaned);
$html .= sprintf($this->routeLine, $url, $base.$path, $cleaned, $route->description, implode(', ', (array)$route->method), $route->name );
}
}
return sprintf('<ul>%s</ul>', $html);
}
protected function cleanRouteFromRegex(string $route) : string
{
$paths = explode('/', $route);
foreach($paths as &$path) {
list($newPath, ) = explode(':', $path);
$path = $newPath === $path ? $path : "$newPath}";
}
return implode('/', $paths);
}
}