Implement commands
This commit is contained in:
54
src/Http/RoutesConfig.php
Normal file
54
src/Http/RoutesConfig.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Ardent\Undercurrent\Http;
|
||||
|
||||
use Ardent\Undercurrent\Attribute\Route;
|
||||
use ReflectionAttribute;
|
||||
use ReflectionClass;
|
||||
|
||||
class RoutesConfig
|
||||
{
|
||||
/**
|
||||
* @var array<RouteConfig>
|
||||
*/
|
||||
private array $routes = [];
|
||||
|
||||
private array $controllers = [];
|
||||
|
||||
public function add(string $controllerClass): self
|
||||
{
|
||||
$this->controllers[] = $controllerClass;
|
||||
$reflectionClass = new ReflectionClass($controllerClass);
|
||||
|
||||
foreach ($reflectionClass->getMethods() as $method) {
|
||||
$attributes = $method->getAttributes(Route::class, ReflectionAttribute::IS_INSTANCEOF);
|
||||
|
||||
if (count($attributes) === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$route = $attributes[0]->newInstance();
|
||||
|
||||
$this->routes[] = new RouteConfig(
|
||||
$route,
|
||||
$controllerClass,
|
||||
$method->getName(),
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<RouteConfig>
|
||||
*/
|
||||
public function getRoutes(): array
|
||||
{
|
||||
return $this->routes;
|
||||
}
|
||||
|
||||
public function getControllers(): array
|
||||
{
|
||||
return $this->controllers;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user