- Totally reworked Notes to allievate most conceptions problems. Removed old annotations artifacts too

This commit is contained in:
mcndave committed 2024-05-09 19:43:08 +00:00
1 parent 996c8fa14a
commit 61b84bfd09
18 files changed
+495 -463

No files matched your search

+17 -214
View File
@@ -2,16 +2,13 @@
namespace Notes;
use Notes\Common\ReflectedClass;
use Psr\SimpleCache\CacheInterface;
class ObjectResolver {
const KEY_ENTITY_NAME = 01;
const KEY_COLUMN_NAME = 02;
public string $objectClass;
public array $uses;
public ReflectedClass $reflectedClass;
public array $class;
@@ -19,52 +16,11 @@ class ObjectResolver {
public array $methods;
public function __construct(string $objectClass, bool $fullUses = true, bool $fullObject = true, $fullMethod = true, $fullProperty = true, ? CacheInterface $cache = null)
public function __construct(string $objectClass, ? CacheInterface $cache = null)
{
$this->objectClass = $objectClass;
list($this->uses, $this->class, $this->methods, $this->properties) = array_values(
ObjectReflection::fromClass($objectClass, $cache)->read($fullUses, $fullObject, $fullMethod, $fullProperty)
);
$this->resolveAnnotations();
}
/**
* Transform an annotation into it's object's counterpart
*/
public function getAttributeFromClassname(array|string $className, bool $throwOnError = true) : object|bool
{
foreach((array) $className as $class) {
foreach (array_reverse($this->class['tags']) as $item) {
if ($item['object'] instanceof $class) {
return $this->instanciateAnnotationObject($item);
}
foreach ($this->properties as $property) {
foreach ($property['tags'] as $tag) {
if ($item['object'] instanceof $class) {
return $this->instanciateAnnotationObject($tag);
}
}
}
foreach ($this->methods as $method) {
foreach ($method['tags'] as $tag) {
if ($item['object'] instanceof $class) {
return $this->instanciateAnnotationObject($tag);
}
}
}
}
}
if ($throwOnError) {
throw new \Exception(sprintf("Annotation `%s` could not be found within your object `%s`.", implode(', ', (array)$className), $this->objectClass));
}
return false;
$this->reflectedClass = ObjectReflection::fromClass($objectClass, $cache)->reflectClass();
}
/**
@@ -75,26 +31,26 @@ class ObjectResolver {
$list = [];
foreach((array) $className as $class) {
foreach ($this->class['tags'] as $item) {
if ($item['object'] instanceof $class) {
$list[] = $this->instanciateAnnotationObject($item);
foreach ($this->reflectedClass->getAttributes() as $item) {
if ($item->object instanceof $class) {
$list[] = $item->object;
}
}
foreach ($this->properties as $property) {
foreach ($property['tags'] as $item) {
if ($item['object'] instanceof $class) {
$list[$property['name']] ??= [];
$list[$property['name']][] = $this->instanciateAnnotationObject($item);
foreach ($this->reflectedClass->getProperties(true) as $property) {
foreach ($property->attributes as $item) {
if ($item->object instanceof $class) {
$list[$property->name] ??= [];
$list[$property->name][] = $item->object;
}
}
}
foreach ($this->methods as $method) {
foreach ($method['tags'] as $item) {
if ($item['object'] instanceof $class) {
$list[$method['name']] ??= [];
$list[$method['name']][] = $this->instanciateAnnotationObject($item);
foreach ($this->reflectedClass->getMethods(true) as $method) {
foreach ($method->attributes as $item) {
if ($item->object instanceof $class) {
$list[$method->name] ??= [];
$list[$method->name][] = $item->object;
}
}
}
@@ -106,157 +62,4 @@ class ObjectResolver {
return $list;
}
/**
* @deprecated Will soon be deprecated in favour of PHP's native attributes
* Transform an annotation into it's object's counterpart
*/
public function getAnnotationFromClassname(array|string $className, bool $throwOnError = true) : object|bool
{
foreach((array) $className as $class) {
if ( $name = $this->uses[$class] ?? false) {
foreach (array_reverse($this->class['tags']) as $item) {
if ($item['tag'] === $name) {
return $this->instanciateAnnotationObject($item);
}
foreach ($this->properties as $property) {
foreach ($property['tags'] as $item) {
if ($item['tag'] === $name) {
return $this->instanciateAnnotationObject($item);
}
}
}
foreach ($this->methods as $method) {
foreach ($method['tags'] as $item) {
if ($item['tag'] === $name) {
return $this->instanciateAnnotationObject($item);
}
}
}
}
}
}
if ($throwOnError) {
throw new \Exception(sprintf("Annotation `%s` could not be found within your object `%s`.", implode(', ', (array)$className), $this->objectClass));
}
return false;
}
/**
* Transform an annotation into it's object's counterpart
*/
public function getAnnotationListFromClassname(array|string $className, bool $throwOnError = true) : array
{
$list = [];
foreach((array) $className as $class) {
if ($name = $this->uses[$class] ?? false) {
foreach ($this->class['tags'] as $item) {
if ($item['tag'] === $name) {
$list[] = $this->instanciateAnnotationObject($item);
}
}
foreach ($this->properties as $property) {
foreach ($property['tags'] as $item) {
if ($item['tag'] === $name) {
$list[$property['name']] ??= [];
$list[$property['name']][] = $this->instanciateAnnotationObject($item);
}
}
}
foreach ($this->methods as $method) {
foreach ($method['tags'] as $item) {
if ($item['tag'] === $name) {
$list[$method['name']] ??= [];
$list[$method['name']][] = $this->instanciateAnnotationObject($item);
}
}
}
}
}
if ( empty($list) ) {
if ($throwOnError) throw new \InvalidArgumentException("Class `$className` was not found within {$this->objectClass} uses statement (or it's children / traits)");
}
return $list;
}
public function instanciateAnnotationObject(array $tagDefinition) : Annotation|\Notes\Attribute
{
if (isset($tagDefinition['object']) && $tagDefinition['object'] instanceof \Notes\Attribute) {
return $tagDefinition['object'];
}
$arguments = $this->extractArguments($tagDefinition['arguments']);
if ( false === $class = array_search($tagDefinition['tag'], $this->uses) ) {
return new class() implements Annotation {};
# throw new \InvalidArgumentException("Annotation class `{$tagDefinition['tag']}` was not found within {$this->objectClass} uses statement (or it's children / traits)");
}
try {
$obj = new $class(... $arguments['constructor']);
}
catch (\Throwable $e) {
$err = $e::class;
throw new $err(sprintf("An error occured trying to instanciate class definition '%s' given argument '%s' : %s", var_export($tagDefinition, true), var_export($arguments['constructor'], true), $e->getMessage()));
}
foreach($arguments['setter'] as $key => $value) {
$obj->$key = $value;
}
if ( ! $obj instanceof \Notes\Attribute ) {
throw new \RuntimeException(sprintf("An error occurred trying to retrieve an Attribute def:%s from class %s", json_encode($tagDefinition), $this->objectClass));
}
return $obj;
}
/**
* Extracts arguments from an Annotation definition, easing object's declaration.
*/
protected function extractArguments(array $arguments) : array
{
$list = [
'setter' => [],
'constructor' => [],
];
ksort($arguments);
foreach($arguments as $key => $value) {
$list[ is_int($key) ? 'constructor' : 'setter' ][$key] = $value;
}
return $list;
}
protected function resolveAnnotations()
{
foreach($this->class['tags'] as $key => &$tag) {
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
}
foreach($this->properties as &$property) {
foreach($property['tags'] as &$tag){
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
}
}
foreach($this->methods as &$method) {
foreach($method['tags'] as &$tag){
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
}
}
}
}