- WIP on attributes ; on testing phase

This commit is contained in:
Dave M. 2023-01-26 13:29:59 +00:00
parent a876ea9045
commit 87b9ea2e2a
3 changed files with 21 additions and 10 deletions

View File

@ -87,7 +87,7 @@ foreach ($iterator as $info) {
$renderedArgs = ""; $renderedArgs = "";
} }
$attr[] = $attribute = sprintf(" #[%s%s]%s", $name, $renderedArgs ? "($renderedArgs)" : "", $renderedArgs ? " # migrated from: $args" : ""); $attr[] = $attribute = sprintf("%s#[%s%s]%s", str_repeat(" ", strlen($line) - strlen(ltrim($line)) >= 4 ? 4 : 0), $name, $renderedArgs ? "($renderedArgs)" : "", $renderedArgs ? " # migrated from: $args" : "");
$newContent[] = $attribute; $newContent[] = $attribute;
} }

View File

@ -2,33 +2,42 @@
namespace Notes; namespace Notes;
use Kash\HandleCacheTrait;
use Psr\SimpleCache\CacheInterface;
use Reflector, ReflectionClass, ReflectionProperty, ReflectionMethod, ReflectionUnionType, ReflectionNamedType, ReflectionParameter; use Reflector, ReflectionClass, ReflectionProperty, ReflectionMethod, ReflectionUnionType, ReflectionNamedType, ReflectionParameter;
class ObjectReflection { class ObjectReflection {
use HandleCacheTrait;
protected string $classname; protected string $classname;
protected ReflectionClass $classReflection;
public AnnotationReader $annotationReader; public AnnotationReader $annotationReader;
public function __construct($class, AnnotationReader $annotationReader = null) { public function __construct($class, AnnotationReader $annotationReader = null, ? CacheInterface $cache = null) {
$this->classname = ltrim($class, '\\'); $this->classname = ltrim($class, '\\');
$this->classReflection = $class instanceof ReflectionClass ? $class : new ReflectionClass($class); $this->cache = $cache;
$this->annotationReader = $annotationReader ?: AnnotationReader::fromClass($class);
#if ( ! $this->cache || ! $this->cache->has($class) ) {
$this->classReflection = $class instanceof ReflectionClass ? $class : new ReflectionClass($class);
$this->annotationReader = $annotationReader ?: AnnotationReader::fromClass($class);
# }
} }
public static function fromClass($class) : self public static function fromClass($class, ? CacheInterface $cache = null) : self
{ {
return new static($class); return new static($class, null, $cache);
} }
public function read(bool $fullUses = true, bool $fullObject = true, $fullMethod = true, $fullProperty = true) : array public function read(bool $fullUses = true, bool $fullObject = true, $fullMethod = true, $fullProperty = true) : array
{ {
return [ return $this->handleCaching(implode('', [ $this->classname, (int)$fullObject, (int) $fullMethod, (int) $fullProperty ]), fn() => [
'uses' => $this->gatherUses($fullUses), 'uses' => $this->gatherUses($fullUses),
'class' => $this->gatherClass($fullObject), 'class' => $this->gatherClass($fullObject),
'method' => $this->gatherMethods($fullMethod), 'method' => $this->gatherMethods($fullMethod),
'property' => $this->gatherProperties($fullProperty), 'property' => $this->gatherProperties($fullProperty),
]; ]);
} }
public function gatherUses(bool $full = true) : array public function gatherUses(bool $full = true) : array

View File

@ -2,6 +2,8 @@
namespace Notes; namespace Notes;
use Psr\SimpleCache\CacheInterface;
class ObjectResolver { class ObjectResolver {
const KEY_ENTITY_NAME = 01; const KEY_ENTITY_NAME = 01;
@ -17,12 +19,12 @@ class ObjectResolver {
public array $methods; public array $methods;
public function __construct(string $objectClass, bool $fullUses = true, bool $fullObject = true, $fullMethod = true, $fullProperty = true) public function __construct(string $objectClass, bool $fullUses = true, bool $fullObject = true, $fullMethod = true, $fullProperty = true, ? CacheInterface $cache = null)
{ {
$this->objectClass = $objectClass; $this->objectClass = $objectClass;
list($this->uses, $this->class, $this->methods, $this->properties) = array_values( list($this->uses, $this->class, $this->methods, $this->properties) = array_values(
ObjectReflection::fromClass($objectClass)->read($fullUses, $fullObject, $fullMethod, $fullProperty) ObjectReflection::fromClass($objectClass, $cache)->read($fullUses, $fullObject, $fullMethod, $fullProperty)
); );
$this->resolveAnnotations(); $this->resolveAnnotations();