- WIP on Attributes conversion from annotations

This commit is contained in:
Dave M. 2023-01-21 18:38:18 +00:00
parent 83858164dc
commit b9aadd41ee
3 changed files with 71 additions and 75 deletions

View File

@ -14,7 +14,7 @@ class Route implements \Notes\Annotation {
*/ */
public string $route; public string $route;
public string $method; public /*string*/ $method;
public string $base; public string $base;

View File

@ -4,7 +4,6 @@ namespace Notes\Route\Attribute\Method;
#[\Attribute] #[\Attribute]
class Route implements \Notes\Attribute { class Route implements \Notes\Attribute {
public function __construct( public function __construct(
public string $name, public string $name,
public string $route, public string $route,

View File

@ -3,6 +3,7 @@
use Notes\ObjectReflection, use Notes\ObjectReflection,
Notes\ObjectResolver; Notes\ObjectResolver;
use Psr\SimpleCache\CacheInterface;
use RuntimeException, DirectoryIterator, Generator, Closure; use RuntimeException, DirectoryIterator, Generator, Closure;
class RouteFetcher { class RouteFetcher {
@ -19,10 +20,13 @@ class RouteFetcher {
protected array $annotations; protected array $annotations;
public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $annotations = null, bool $debug = false) protected CacheInterface $cache;
protected bool $forceCacheWrite;
public function __construct(?Closure $callback = null, ? array $folderList = null, ? array $annotations = null, ? CacheInterface $cache = null, bool $forceCacheWrite = false)
{ {
// Handles if we must fetch or store in cache $this->cache = $cache;
$this->debug = $debug;
if ($callback !== null) { if ($callback !== null) {
$this->callback = $callback; $this->callback = $callback;
@ -37,22 +41,12 @@ class RouteFetcher {
} }
else { else {
$this->annotations = [ $this->annotations = [
'object' => Annotation\Object\Route::class, 'object' => [ Annotation\Object\Route::class, Attribute\Object\Route::class ],
'method' => Annotation\Method\Route::class, 'method' => [ Annotation\Method\Route::class ],
]; ];
} }
}
public function __destruct() { $this->forceCacheWrite = $forceCacheWrite;
if ($this->debug) {
foreach ($this->list as $key => $item) {
$cacheKey = sprintf("%s:routes.%s", basename(__CLASS__), $key);
if (function_exists('apcu_enabled') && apcu_enabled()) {
apcu_store($cacheKey, $item, 3600);
}
}
}
} }
public function addFolder($folder) : void public function addFolder($folder) : void
@ -91,9 +85,8 @@ class RouteFetcher {
{ {
$annotations ??= $this->annotations; $annotations ??= $this->annotations;
if ( $cacheValue = $this->fetchFromCache($this->generateListKey($annotations)) ) { return $this->handleCaching(substr(md5(serialize($annotations)), 0, 7), function() use ($annotations) : array {
return $cacheValue; $list = [];
}
foreach($this->scan() as $namespace => $file) { foreach($this->scan() as $namespace => $file) {
if ( $file->getExtension() !== "php" ) { if ( $file->getExtension() !== "php" ) {
@ -106,18 +99,24 @@ class RouteFetcher {
$objectResolver = new ObjectResolver($class, true, true, false, true); $objectResolver = new ObjectResolver($class, true, true, false, true);
if ( isset($annotations['object']) && (null !== ( $object = $objectResolver->getAnnotationFromClassname($annotations['object']) )) ){ if ( isset($annotations['object']) ) {
$object = $objectResolver->getAttributeFromClassname($annotations['object'], false) ?: $objectResolver->getAnnotationFromClassname($annotations['object'], false);
if ( $object->methods ?? false ) { if ( $object->methods ?? false ) {
$methods = $object->methods; $methods = $object->methods;
} }
elseif ($object->method ?? false ) {
$methods = (array) $object->method;
}
$base = $object->base ?? ""; $base = $object->base ?? "";
} }
if ( isset($annotations['method']) ) { if ( isset($annotations['method']) ) {
$routeList = $objectResolver->getAnnotationListFromClassname( $annotations['method'] ); $routeList = $objectResolver->getAnnotationListFromClassname( $annotations['method'], false );
;
foreach($routeList as $func => $routes) { foreach($routeList as $func => $routes) {
if (is_array($routes)) {
foreach ($routes as $route) { foreach ($routes as $route) {
$route->base = $base; $route->base = $base;
$route->class = $class; $route->class = $class;
@ -136,33 +135,31 @@ class RouteFetcher {
} }
} }
} }
return $this->debug ? $this->list[$this->generateListKey($annotations)] = $list : $list;
} }
protected function fetchFromCache(string $key) : array|null return $list;
{ });
if (! $this->debug) {
if (!isset($this->list[$key]) && function_exists('apcu_enabled') && apcu_enabled()) {
$cacheKey = sprintf("%s:routes.%s", basename(__CLASS__), $key);
$fetch = apcu_fetch($cacheKey, $success);
if ($success) {
return $fetch;
}
}
}
return $this->list[$key] ?? null;
}
protected function generateListKey(array $annotations) {
return ($annotations['object'] ?? "") . ($annotations['method'] ?? "");
} }
protected function generateClassname($file, $namespace) protected function generateClassname($file, $namespace)
{ {
return "\\$namespace\\$file"; 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];
}
} }