- WIP on Attributes conversion from annotations

This commit is contained in:
Dave M. 2023-01-21 18:38:39 +00:00
parent 77c8fb1dd2
commit eed3c693c7
4 changed files with 87 additions and 38 deletions

View File

@ -10,12 +10,17 @@
} }
], ],
"require": { "require": {
"mcnd/notes": "master-dev" "mcnd/notes": "master-dev",
"mcnd/notes-route": "master-dev"
}, },
"repositories": [ "repositories": [
{ {
"type": "vcs", "type": "vcs",
"url": "https://github.com/mcNdave/notes.git" "url": "https://github.com/mcNdave/notes.git"
},
{
"type": "vcs",
"url": "https://github.com/mcNdave/notes-route.git"
} }
], ],
"autoload": { "autoload": {

View File

@ -2,6 +2,8 @@
namespace Notes\Breadcrumb\Annotation\Method; namespace Notes\Breadcrumb\Annotation\Method;
use Notes\Route\Annotation\Method\Route;
class Breadcrumb implements \Notes\Annotation { class Breadcrumb implements \Notes\Annotation {
public string $parent; public string $parent;
@ -9,4 +11,16 @@ class Breadcrumb implements \Notes\Annotation {
public string $icon; public string $icon;
public string $lang; public string $lang;
public string $route;
public array $methods;
public string $class;
public string $classMethod;
public Route $routeAnnotation;
public bool $current = false;
} }

View File

@ -0,0 +1,22 @@
<?php
namespace Notes\Breadcrumb\Attribute\Method;
#[\Attribute]
class Breadcrumb implements \Notes\Attribute {
public Route $routeAnnotation;
public bool $current = false;
public function __construct(
public string $parent = "",
public string $icon = "",
public string $lang = "",
public string $route = "",
public array $methods = [],
public string $class = "",
public string $classMethod = "",
) {}
}

View File

@ -3,59 +3,67 @@
use Notes\ObjectReflection, use Notes\ObjectReflection,
Notes\ObjectResolver; Notes\ObjectResolver;
use Notes\Route\RouteFetcher;
use Psr\Http\Message\ServerRequestInterface;
use Psr\SimpleCache\CacheInterface;
use RuntimeException, DirectoryIterator, Generator; use RuntimeException, DirectoryIterator, Generator;
class Breadcrumb { class Breadcrumb extends RouteFetcher {
protected array $folderList; public function getRouteTree(\Notes\Route\Annotation\Method\Route $route) : array
protected array $fileList;
public function __construct(array $folderList = []) {
$this->folderList = $folderList;
}
public function addFolder($folder) : void
{ {
$this->folderList[] = $folder; $tree = [];
$routes = $this->compile([
'object' => [ Annotation\Object\Route::class, Attribute\Object\Route::class ],
'method' => [ Annotation\Method\Route::class ],
]);
$crumbs = $this->compile([
'method' => [ Annotation\Method\Breadcrumb::class, Attribute\Method\Breadcrumb::class ],
]);
while( $route ) {
$crumb = $this->getBreadcrumbAnnotation($crumbs, $route);
if ($crumb) {
empty($tree) && $crumb->current = true;
$tree[$route->name] = $crumb;
$route = $this->getRouteAnnotation($routes, $crumb);
}
else {
$route = null;
}
};
return array_reverse($tree);
} }
public function setFolderList(array $list) : void protected function getRouteAnnotation(array $list, Annotation\Method\Breadcrumb $crumb) : null|\Notes\Route\Annotation\Method\Route
{ {
$this->folderList = $list; if ($crumb->parent ?? null) {
foreach ($list as $route) {
if ($crumb->parent === $route->name) {
return $route;
}
}
} }
public function scan() : Generator return null;
}
protected function getBreadcrumbAnnotation(array $list, \Notes\Route\Annotation\Method\Route $route) : null|Annotation\Method\Breadcrumb
{ {
foreach($this->folderList as $namespace => $folder) { foreach($list as $crumb) {
if ( ! file_exists($folder) ) { if ($crumb->class === $route->class && $crumb->classMethod === $route->classMethod) {
throw new RuntimeException(sprintf("Folder `%s` can not be found or scanned", $folder)); $crumb->routeAnnotation = $route;
}
foreach (new DirectoryIterator($folder) as $fileinfo) { return $crumb;
if ( ! $fileinfo->isDot() ) {
yield $namespace => $fileinfo;
}
}
} }
} }
public function compile() : array return null;
{
foreach($this->scan() as $namespace => $file) {
$class = $this->generateClassname($file->getBasename(".php"), $namespace);
# Should generate an equivalent of Ulmus's object reflection here !
# var_dump( "<pre>", , "</pre>" );
$objectResolver = new ObjectResolver($class);
#if ( null === $class = $this->getAnnotationFromClassname( Table::class ) ) {
# }
} }
return [];
}
} }