- WIP on attributes ; on testing phase

This commit is contained in:
Dave M. 2023-01-26 13:34:20 +00:00
parent b9aadd41ee
commit 02c0b2d432
3 changed files with 16 additions and 35 deletions

View File

@ -2,11 +2,11 @@
namespace Notes\Route\Attribute\Method;
#[\Attribute]
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
class Route implements \Notes\Attribute {
public function __construct(
public string $name,
public string $route,
public string $name,
public string|array $method = [ "GET", "POST" ],
public string $base = "",
public ? string $class = null,

View File

@ -2,7 +2,7 @@
namespace Notes\Route\Attribute\Object;
#[\Attribute]
#[\Attribute(\Attribute::TARGET_CLASS)]
class Route implements \Notes\Annotation {
public function __construct(
public string|array $method = [ "GET", "POST" ],

View File

@ -1,5 +1,6 @@
<?php namespace Notes\Route;
use Kash\HandleCacheTrait;
use Notes\ObjectReflection,
Notes\ObjectResolver;
@ -7,6 +8,7 @@ use Psr\SimpleCache\CacheInterface;
use RuntimeException, DirectoryIterator, Generator, Closure;
class RouteFetcher {
use HandleCacheTrait;
public array $list = [];
@ -20,11 +22,7 @@ class RouteFetcher {
protected array $annotations;
protected CacheInterface $cache;
protected bool $forceCacheWrite;
public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $annotations = null, ? CacheInterface $cache = null, bool $forceCacheWrite = false)
public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $annotations = null, ? CacheInterface $cache = null)
{
$this->cache = $cache;
@ -45,8 +43,6 @@ class RouteFetcher {
'method' => [ Annotation\Method\Route::class ],
];
}
$this->forceCacheWrite = $forceCacheWrite;
}
public function addFolder($folder) : void
@ -97,19 +93,21 @@ class RouteFetcher {
$class = $this->generateClassname($file->getBasename(".php"), $namespace);
$methods = $this->defaultMethods;
$objectResolver = new ObjectResolver($class, true, true, false, true);
$objectResolver = new ObjectResolver($class, true, true, false, true, $this->cache);
if ( isset($annotations['object']) ) {
$object = $objectResolver->getAttributeFromClassname($annotations['object'], false) ?: $objectResolver->getAnnotationFromClassname($annotations['object'], false);
if ( $object->methods ?? false ) {
$methods = $object->methods;
}
elseif ($object->method ?? false ) {
$methods = (array) $object->method;
}
if ($object) {
if ( $object->methods ?? false ) {
$methods = $object->methods;
}
elseif ($object->method ?? false ) {
$methods = (array) $object->method;
}
$base = $object->base ?? "";
$base = $object->base ?? "";
}
}
if ( isset($annotations['method']) ) {
@ -145,21 +143,4 @@ class RouteFetcher {
{
return "\\$namespace\\$file";
}
protected function handleCaching(string $key, callable $callback, null|int|\DateInterval $ttl = null) : mixed
{
static $internalCache = [];
if ( isset($internalCache[$key]) ) {
return $internalCache[$key];
}
if ($this->forceCacheWrite || null === ( $internalCache[$key] = $this->cache->get($key) )) {
$internalCache[$key] = call_user_func($callback);
$this->cache->set($key, $internalCache[$key], $ttl);
}
return $internalCache[$key];
}
}