- Latest work on route base fallbacks
This commit is contained in:
parent
0fcff83eac
commit
124bddb238
|
@ -6,6 +6,7 @@ namespace Notes\Route\Attribute\Object;
|
||||||
class Route implements \Notes\Attribute {
|
class Route implements \Notes\Attribute {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string|array $method = [ "GET", "POST" ],
|
public string|array $method = [ "GET", "POST" ],
|
||||||
public string $base = "",
|
# NULL will fallback on nearest base
|
||||||
|
public null|string $base = null,
|
||||||
){}
|
){}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,9 @@ class RouteFetcher {
|
||||||
|
|
||||||
public array $defaultMethods = [ 'GET', 'POST' ];
|
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;
|
$this->cache = $cache;
|
||||||
|
|
||||||
|
@ -34,11 +34,11 @@ class RouteFetcher {
|
||||||
$this->folderList = $folderList;
|
$this->folderList = $folderList;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($annotations !== null) {
|
if ($attributes !== null) {
|
||||||
$this->annotations = $annotations;
|
$this->attributes = $attributes;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$this->annotations = [
|
$this->attributes = [
|
||||||
'object' => [ Attribute\Object\Route::class ],
|
'object' => [ Attribute\Object\Route::class ],
|
||||||
'method' => [ Attribute\Method\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 = [];
|
$list = [];
|
||||||
|
|
||||||
foreach($this->scan() as $namespace => $file) {
|
foreach($this->scan() as $namespace => $file) {
|
||||||
|
@ -89,39 +89,40 @@ class RouteFetcher {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$base = "";
|
$base = null;
|
||||||
$class = $this->generateClassname($file->getBasename(".php"), $namespace);
|
$class = $this->generateClassname($file->getBasename(".php"), $namespace);
|
||||||
$methods = $this->defaultMethods;
|
$methods = null;
|
||||||
|
|
||||||
$objectResolver = new ObjectResolver($class, true, true, false, true, $this->cache);
|
$objectResolver = new ObjectResolver($class, true, true, false, true, $this->cache);
|
||||||
|
|
||||||
if ( isset($annotations['object']) ) {
|
if ( isset($attributes['object']) ) {
|
||||||
$object = $objectResolver->getAttributeFromClassname($annotations['object'], false) ?: $objectResolver->getAnnotationFromClassname($annotations['object'], false);
|
|
||||||
|
|
||||||
if ($object) {
|
$objects = $objectResolver->getAttributeListFromClassname($attributes['object'], false);
|
||||||
if ( $object->methods ?? false ) {
|
|
||||||
$methods = $object->methods;
|
foreach(array_reverse($objects) as $object) {
|
||||||
}
|
if ($object->method ?? false ) {
|
||||||
elseif ($object->method ?? false ) {
|
$methods ??= (array) $object->method;
|
||||||
$methods = (array) $object->method;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$base = $object->base ?? "";
|
$base ??= $object->base ?? null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( isset($annotations['method']) ) {
|
if ( isset($attributes['method']) ) {
|
||||||
$routeList = $objectResolver->getAttributeListFromClassname( $annotations['method'], false );
|
$routeList = $objectResolver->getAttributeListFromClassname( $attributes['method'], false );
|
||||||
|
|
||||||
foreach($routeList as $func => $routes) {
|
foreach($routeList as $func => $routes) {
|
||||||
if (is_array($routes)) {
|
if (is_array($routes)) {
|
||||||
|
|
||||||
foreach ($routes as $route) {
|
foreach ($routes as $route) {
|
||||||
$route->base = $base;
|
if ($route instanceof \Notes\Route\Attribute\Method\Route) {
|
||||||
|
$route->base = $route->base ?: $base;
|
||||||
|
}
|
||||||
$route->class = $class;
|
$route->class = $class;
|
||||||
$route->classMethod = $func;
|
$route->classMethod = $func;
|
||||||
|
|
||||||
if (false === ($route->methods ?? false)) {
|
if (false === ($route->methods ?? false)) {
|
||||||
$route->methods = $methods;
|
$route->methods = $methods ?? $this->defaultMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (false !== ($this->callback ?? false)) {
|
if (false !== ($this->callback ?? false)) {
|
||||||
|
|
Loading…
Reference in New Issue