diff --git a/src/Route/Attribute/Object/Route.php b/src/Route/Attribute/Object/Route.php index 508ae23..b800c0b 100644 --- a/src/Route/Attribute/Object/Route.php +++ b/src/Route/Attribute/Object/Route.php @@ -6,6 +6,7 @@ namespace Notes\Route\Attribute\Object; class Route implements \Notes\Attribute { public function __construct( public string|array $method = [ "GET", "POST" ], - public string $base = "", + # NULL will fallback on nearest base + public null|string $base = null, ){} } diff --git a/src/Route/RouteFetcher.php b/src/Route/RouteFetcher.php index 21eb1e6..8a5adff 100644 --- a/src/Route/RouteFetcher.php +++ b/src/Route/RouteFetcher.php @@ -20,9 +20,9 @@ class RouteFetcher { public array $defaultMethods = [ 'GET', 'POST' ]; - protected array $annotations; + protected array $attributes; - public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $annotations = null, ? CacheInterface $cache = null) + public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $attributes = null, ? CacheInterface $cache = null) { $this->cache = $cache; @@ -34,11 +34,11 @@ class RouteFetcher { $this->folderList = $folderList; } - if ($annotations !== null) { - $this->annotations = $annotations; + if ($attributes !== null) { + $this->attributes = $attributes; } else { - $this->annotations = [ + $this->attributes = [ 'object' => [ Attribute\Object\Route::class ], 'method' => [ Attribute\Method\Route::class ], ]; @@ -77,11 +77,11 @@ class RouteFetcher { } } - public function compile(null|array $annotations = null) : array + public function compile(null|array $attributes = null) : array { - $annotations ??= $this->annotations; + $attributes ??= $this->attributes; - return $this->handleCaching(substr(md5(serialize($annotations)), 0, 7), function() use ($annotations) : array { + return $this->handleCaching(substr(md5(serialize($attributes)), 0, 7), function() use ($attributes) : array { $list = []; foreach($this->scan() as $namespace => $file) { @@ -89,39 +89,40 @@ class RouteFetcher { continue; } - $base = ""; + $base = null; $class = $this->generateClassname($file->getBasename(".php"), $namespace); - $methods = $this->defaultMethods; + $methods = null; $objectResolver = new ObjectResolver($class, true, true, false, true, $this->cache); - if ( isset($annotations['object']) ) { - $object = $objectResolver->getAttributeFromClassname($annotations['object'], false) ?: $objectResolver->getAnnotationFromClassname($annotations['object'], false); + if ( isset($attributes['object']) ) { - if ($object) { - if ( $object->methods ?? false ) { - $methods = $object->methods; - } - elseif ($object->method ?? false ) { - $methods = (array) $object->method; + $objects = $objectResolver->getAttributeListFromClassname($attributes['object'], false); + + foreach(array_reverse($objects) as $object) { + if ($object->method ?? false ) { + $methods ??= (array) $object->method; } - $base = $object->base ?? ""; + $base ??= $object->base ?? null; } } - if ( isset($annotations['method']) ) { - $routeList = $objectResolver->getAttributeListFromClassname( $annotations['method'], false ); + if ( isset($attributes['method']) ) { + $routeList = $objectResolver->getAttributeListFromClassname( $attributes['method'], false ); foreach($routeList as $func => $routes) { if (is_array($routes)) { + foreach ($routes as $route) { - $route->base = $base; + if ($route instanceof \Notes\Route\Attribute\Method\Route) { + $route->base = $route->base ?: $base; + } $route->class = $class; $route->classMethod = $func; if (false === ($route->methods ?? false)) { - $route->methods = $methods; + $route->methods = $methods ?? $this->defaultMethods; } if (false !== ($this->callback ?? false)) {