103 lines
3.3 KiB
PHP
103 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Lean\Api;
|
|
|
|
use Lean\Factory\HttpFactory;
|
|
use Notes\ObjectReflection;
|
|
use Notes\Route\Attribute\Method\Route;
|
|
use Notes\Security\SecurityHandler;
|
|
use Picea\Extension\UrlExtension;
|
|
use Psr\Container\ContainerInterface;
|
|
use Taxus\Taxus;
|
|
|
|
class RouteDescriptor
|
|
{
|
|
protected SecurityHandler $securityHandler;
|
|
|
|
protected Taxus $taxus;
|
|
|
|
public function __construct(
|
|
public object $controller,
|
|
protected UrlExtension $urlExtension,
|
|
ContainerInterface $container
|
|
) {
|
|
if ($container->has(SecurityHandler::class) && $container->has(Taxus::class)) {
|
|
$this->securityHandler = $container->get(SecurityHandler::class);
|
|
$this->taxus = $container->get(Taxus::class);
|
|
}
|
|
}
|
|
|
|
public function getRoutes() : array
|
|
{
|
|
$routes = [];
|
|
|
|
$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 = $this->urlExtension->buildUrl($cleaned);
|
|
|
|
$routes[] = [
|
|
'name' => $route->name,
|
|
'route' => $url,
|
|
'path' => $base.$path,
|
|
'cleaned' => $cleaned,
|
|
'description'=> $route->description,
|
|
#'methods' =>implode(', ', (array)$route->method),
|
|
'methods' => (array) $route->method,
|
|
'privileges' => $this->getPrivilegeFromRoute($method->name),
|
|
];
|
|
}
|
|
}
|
|
|
|
return $routes;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* @param string $method Which method are we inspecting
|
|
* @return false|array|null Returns NULL when securityHandler is undefined, FALSE when no security applies for this method and else an array of ['admin' => [ 'status' => true, 'description' => "' ], ...].
|
|
*/
|
|
protected function getPrivilegeFromRoute(string $method) : null|false|array
|
|
{
|
|
if ( isset($this->securityHandler) ){
|
|
$list = [];
|
|
|
|
if ( $this->securityHandler->isLocked($this->controller::class, $method) ) {
|
|
foreach($this->taxus->list as $name => $definition) {
|
|
if ($definition[0]->testableArguments !== null) {
|
|
if ( $this->securityHandler->hasGrantPermission($this->controller::class, $method, ...$definition[0]->testableArguments) ) {
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |