notes/src/Breadcrumb/Breadcrumb.php

76 lines
2.0 KiB
PHP

<?php namespace Notes\Breadcrumb;
use Notes\ObjectReflection,
Notes\ObjectResolver;
use Notes\Route\{ RouteFetcher, Attribute as RouteAttribute };
use Psr\Http\Message\ServerRequestInterface;
use Psr\SimpleCache\CacheInterface;
use RuntimeException, DirectoryIterator, Generator;
class Breadcrumb extends RouteFetcher {
public bool $ignoreSoleRoute = true;
public function getRouteTree(\Notes\Route\Attribute\Method\Route $route) : array
{
$tree = [];
$routes = $this->compile([
'object' => [ RouteAttribute\Object\Route::class ],
'method' => [ RouteAttribute\Method\Route::class ],
]);
$crumbs = $this->compile([
'method' => [ Attribute\Method\Breadcrumb::class ],
]);
while( $route ) {
$crumb = $this->getBreadcrumbAttribute($crumbs, $route);
if ($crumb) {
empty($tree) && $crumb->current = true;
$tree[$route->name] = $crumb;
$route = $this->getRouteAttribute($routes, $crumb);
}
else {
$route = null;
}
}
if ($this->ignoreSoleRoute && count($tree) === 1) {
return [];
}
return array_reverse($tree);
}
protected function getRouteAttribute(array $list, Attribute\Method\Breadcrumb $crumb) : null|RouteAttribute\Method\Route
{
if ($crumb->parent ?? null) {
foreach ($list as $route) {
if ($crumb->parent === $route->name) {
return $route;
}
}
}
return null;
}
protected function getBreadcrumbAttribute(array $list, RouteAttribute\Method\Route $route) : null|Attribute\Method\Breadcrumb
{
foreach($list as $crumb) {
if ($crumb->class === $route->class && $crumb->classMethod === $route->classMethod) {
$crumb->routeAnnotation = $route;
return $crumb;
}
}
return null;
}
}