Files
notes/src/Route/RouteFetcher.php
T
2024-06-04 13:33:11 +00:00

148 lines
4.6 KiB
PHP

<?php namespace Notes\Route;
use Kash\HandleCacheTrait;
use Notes\ObjectReflection,
Notes\ObjectResolver;
use Psr\SimpleCache\CacheInterface;
use RuntimeException, DirectoryIterator, Generator, Closure;
class RouteFetcher {
use HandleCacheTrait;
public array $list = [];
protected bool $debug;
protected array $folderList;
protected Closure $callback;
public array $defaultMethods = [ 'GET', 'POST' ];
protected array $attributes;
public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $attributes = null, ? CacheInterface $cache = null)
{
$this->cache = $cache;
if ($callback !== null) {
$this->callback = $callback;
}
if ($folderList !== null) {
$this->folderList = $folderList;
}
if ($attributes !== null) {
$this->attributes = $attributes;
}
else {
$this->attributes = [
'object' => [ Attribute\Object\Route::class ],
'method' => [ Attribute\Method\Route::class ],
];
}
}
public function addFolder($folder) : void
{
$this->folderList[] = $folder;
}
public function setFolderList(array $list) : void
{
$this->folderList = $list;
}
public function scan(? array $folders = null) : Generator
{
foreach($folders ?: $this->folderList as $namespace => $folder) {
if ( ! file_exists($folder) ) {
throw new RuntimeException(sprintf("Folder `%s` can not be found or scanned", $folder));
}
foreach (new DirectoryIterator($folder) as $fileinfo) {
if ( ! $fileinfo->isDot() ) {
if ( $fileinfo->isDir() ) {
foreach($this->scan([ "{$namespace}\\" . $fileinfo->getBasename() => $fileinfo->getPathname() ]) as $ns2 => $fi2) {
yield $ns2 => $fi2;
}
}
else {
yield $namespace => $fileinfo;
}
}
}
}
}
public function compile(null|array $attributes = null) : array
{
$attributes ??= $this->attributes;
return $this->handleCaching(substr(md5(serialize($attributes)), 0, 7), function() use ($attributes) : array {
$list = [];
foreach($this->scan() as $namespace => $file) {
if ( $file->getExtension() !== "php" ) {
continue;
}
$base = null;
$class = $this->generateClassname($file->getBasename(".php"), $namespace);
$methods = null;
$objectResolver = new ObjectResolver($class, $this->cache);
if ( isset($attributes['object']) ) {
$objects = $objectResolver->reflectedClass->getClassAttributeListFromClassname($attributes['object'], true, false);
foreach($objects as $object) {
if ($object->method ?? false ) {
$methods ??= (array) $object->method;
}
$base ??= $object->base ?? null;
}
}
if ( isset($attributes['method']) ) {
$routeList = $objectResolver->reflectedClass->getMethodAttributeListFromClassname( $attributes['method'], false, false );
foreach($routeList as $func => $routes) {
if (is_array($routes)) {
foreach ($routes as $route) {
if ($route instanceof \Notes\Route\Attribute\Method\Route) {
$route->base = $route->base ?: $base;
}
$route->class = $class;
$route->classMethod = $func;
if (false === ($route->methods ?? false)) {
$route->methods = $methods ?? $this->defaultMethods;
}
if (false !== ($this->callback ?? false)) {
call_user_func_array($this->callback, [$route]);
}
$list[] = $route;
}
}
}
}
}
return $list;
});
}
protected function generateClassname($file, $namespace)
{
return "\\$namespace\\$file";
}
}