- WIP on Attributes conversion from annotations
This commit is contained in:
parent
77c8fb1dd2
commit
eed3c693c7
@ -10,12 +10,17 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"mcnd/notes": "master-dev"
|
||||
"mcnd/notes": "master-dev",
|
||||
"mcnd/notes-route": "master-dev"
|
||||
},
|
||||
"repositories": [
|
||||
{
|
||||
"type": "vcs",
|
||||
"url": "https://github.com/mcNdave/notes.git"
|
||||
},
|
||||
{
|
||||
"type": "vcs",
|
||||
"url": "https://github.com/mcNdave/notes-route.git"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace Notes\Breadcrumb\Annotation\Method;
|
||||
|
||||
use Notes\Route\Annotation\Method\Route;
|
||||
|
||||
class Breadcrumb implements \Notes\Annotation {
|
||||
|
||||
public string $parent;
|
||||
@ -9,4 +11,16 @@ class Breadcrumb implements \Notes\Annotation {
|
||||
public string $icon;
|
||||
|
||||
public string $lang;
|
||||
|
||||
public string $route;
|
||||
|
||||
public array $methods;
|
||||
|
||||
public string $class;
|
||||
|
||||
public string $classMethod;
|
||||
|
||||
public Route $routeAnnotation;
|
||||
|
||||
public bool $current = false;
|
||||
}
|
||||
|
22
src/Attribute/Method/Breadcrumb.php
Normal file
22
src/Attribute/Method/Breadcrumb.php
Normal 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 = "",
|
||||
) {}
|
||||
|
||||
}
|
@ -3,59 +3,67 @@
|
||||
use Notes\ObjectReflection,
|
||||
Notes\ObjectResolver;
|
||||
|
||||
use Notes\Route\RouteFetcher;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use RuntimeException, DirectoryIterator, Generator;
|
||||
|
||||
class Breadcrumb {
|
||||
class Breadcrumb extends RouteFetcher {
|
||||
|
||||
protected array $folderList;
|
||||
|
||||
protected array $fileList;
|
||||
|
||||
public function __construct(array $folderList = []) {
|
||||
$this->folderList = $folderList;
|
||||
}
|
||||
|
||||
public function addFolder($folder) : void
|
||||
public function getRouteTree(\Notes\Route\Annotation\Method\Route $route) : array
|
||||
{
|
||||
$this->folderList[] = $folder;
|
||||
}
|
||||
$tree = [];
|
||||
|
||||
public function setFolderList(array $list) : void
|
||||
{
|
||||
$this->folderList = $list;
|
||||
}
|
||||
$routes = $this->compile([
|
||||
'object' => [ Annotation\Object\Route::class, Attribute\Object\Route::class ],
|
||||
'method' => [ Annotation\Method\Route::class ],
|
||||
]);
|
||||
|
||||
public function scan() : Generator
|
||||
{
|
||||
foreach($this->folderList as $namespace => $folder) {
|
||||
if ( ! file_exists($folder) ) {
|
||||
throw new RuntimeException(sprintf("Folder `%s` can not be found or scanned", $folder));
|
||||
$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;
|
||||
}
|
||||
};
|
||||
|
||||
foreach (new DirectoryIterator($folder) as $fileinfo) {
|
||||
if ( ! $fileinfo->isDot() ) {
|
||||
yield $namespace => $fileinfo;
|
||||
return array_reverse($tree);
|
||||
}
|
||||
|
||||
protected function getRouteAnnotation(array $list, Annotation\Method\Breadcrumb $crumb) : null|\Notes\Route\Annotation\Method\Route
|
||||
{
|
||||
if ($crumb->parent ?? null) {
|
||||
foreach ($list as $route) {
|
||||
if ($crumb->parent === $route->name) {
|
||||
return $route;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function compile() : array
|
||||
protected function getBreadcrumbAnnotation(array $list, \Notes\Route\Annotation\Method\Route $route) : null|Annotation\Method\Breadcrumb
|
||||
{
|
||||
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 ) ) {
|
||||
|
||||
# }
|
||||
foreach($list as $crumb) {
|
||||
if ($crumb->class === $route->class && $crumb->classMethod === $route->classMethod) {
|
||||
$crumb->routeAnnotation = $route;
|
||||
|
||||
return $crumb;
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user