I’d like to merge both of these methods into a single one:
public function addRoute( string $httpMethod, string $route, callable|array $handler ): self
{
$this->routes[$httpMethod][$route] = $handler;
return $this;
}
public function addRouteByAttribute( array $controllers )
{
foreach( $controllers as $controller ) {
$reflector = new \ReflectionClass($controller);
foreach($reflector->getMethods() as $method) {
$attributes = $method -> getAttributes( \Core\Attributes\Router::class, \ReflectionAttribute::IS_INSTANCEOF );
foreach( $attributes as $attribute ) {
$route = $attribute -> newInstance();
$this -> addRoute( $route -> httpMethod, $route -> route, [ $controller, $method -> getName() ] );
}
}
}
}
Bsically the functionality of addRoute()
should be inside addRouteByAttribute()
.
How could I achieve this?