- WIP on Attributes conversion from annotations
This commit is contained in:
		
							parent
							
								
									77c8fb1dd2
								
							
						
					
					
						commit
						eed3c693c7
					
				@ -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": {
 | 
				
			||||||
 | 
				
			|||||||
@ -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;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										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,
 | 
					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 = [];
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function setFolderList(array $list) : void
 | 
					        $routes = $this->compile([
 | 
				
			||||||
    {
 | 
					            'object' => [ Annotation\Object\Route::class, Attribute\Object\Route::class ],
 | 
				
			||||||
        $this->folderList = $list;
 | 
					            'method' => [ Annotation\Method\Route::class ],
 | 
				
			||||||
    }
 | 
					        ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function scan() : Generator
 | 
					        $crumbs = $this->compile([
 | 
				
			||||||
    {
 | 
					            'method' => [ Annotation\Method\Breadcrumb::class, Attribute\Method\Breadcrumb::class ],
 | 
				
			||||||
        foreach($this->folderList as $namespace => $folder) {
 | 
					        ]);
 | 
				
			||||||
            if ( ! file_exists($folder) ) {
 | 
					
 | 
				
			||||||
                throw new RuntimeException(sprintf("Folder `%s` can not be found or scanned", $folder));
 | 
					        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) {
 | 
					        return array_reverse($tree);
 | 
				
			||||||
                if ( ! $fileinfo->isDot() ) {
 | 
					    }
 | 
				
			||||||
                    yield $namespace => $fileinfo;
 | 
					
 | 
				
			||||||
 | 
					    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) {
 | 
					        foreach($list as $crumb) {
 | 
				
			||||||
            $class = $this->generateClassname($file->getBasename(".php"), $namespace);
 | 
					            if ($crumb->class === $route->class && $crumb->classMethod === $route->classMethod) {
 | 
				
			||||||
 | 
					                $crumb->routeAnnotation = $route;
 | 
				
			||||||
            # 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 $crumb;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return [];
 | 
					        return null;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user