- WIP on Notes v2.x
This commit is contained in:
1 parent
1dd9da6eb6
commit
75231f32b3
7 files changed
+104
-156
No files matched your search
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Ulmus\Common;
|
||||
|
||||
use Notes\Common\ReflectedClass;
|
||||
use Notes\Common\ReflectedProperty;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use Ulmus\Ulmus,
|
||||
Ulmus\Annotation\Classes\Table,
|
||||
@@ -24,13 +26,7 @@ class EntityResolver {
|
||||
|
||||
public string $entityClass;
|
||||
|
||||
public array $uses;
|
||||
|
||||
public array $class;
|
||||
|
||||
public array $properties;
|
||||
|
||||
public array $methods;
|
||||
public ReflectedClass $reflectedClass;
|
||||
|
||||
protected array $fieldList = [];
|
||||
|
||||
@@ -38,14 +34,10 @@ class EntityResolver {
|
||||
{
|
||||
$this->entityClass = $entityClass;
|
||||
|
||||
list($this->uses, $this->class, $this->methods, $this->properties) = array_values(
|
||||
ObjectReflection::fromClass($entityClass, $cache)->read()
|
||||
);
|
||||
|
||||
$this->resolveAnnotations();
|
||||
$this->reflectedClass = ObjectReflection::fromClass($entityClass, $cache)->reflectClass();
|
||||
}
|
||||
|
||||
public function field($name, $fieldKey = self::KEY_ENTITY_NAME, $throwException = true) : ? array
|
||||
public function field($name, $fieldKey = self::KEY_ENTITY_NAME, $throwException = true) : ? ReflectedProperty
|
||||
{
|
||||
try{
|
||||
return $this->fieldList($fieldKey)[$name] ?? null;
|
||||
@@ -68,25 +60,25 @@ class EntityResolver {
|
||||
{
|
||||
$fieldList = [];
|
||||
|
||||
foreach($this->properties as $item) {
|
||||
foreach($item['tags'] ?? [] as $tag) {
|
||||
if ( $tag['object'] instanceof Field or $tag['object'] instanceof Attribute\Property\Field ) {
|
||||
if ( $skipVirtual && ($tag['object'] instanceof Virtual or $tag['object'] instanceof Attribute\Property\Virtual )) {
|
||||
foreach($this->reflectedClass->getProperties(true) as $item) {
|
||||
foreach($item->getAttributes() as $tag) {
|
||||
if ( $tag->object instanceof Attribute\Property\Field ) {
|
||||
if ( $skipVirtual && $tag->object instanceof Attribute\Property\Virtual ) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch($fieldKey) {
|
||||
case static::KEY_LC_ENTITY_NAME:
|
||||
$key = strtolower($item['name']);
|
||||
$key = strtolower($item->name);
|
||||
break;
|
||||
|
||||
|
||||
case static::KEY_ENTITY_NAME:
|
||||
$key = $item['name'];
|
||||
$key = $item->name;
|
||||
break;
|
||||
|
||||
case static::KEY_COLUMN_NAME:
|
||||
$key = strtolower($tag['object']->name ?? $item['name']);
|
||||
$key = strtolower($tag->object->name ?? $item->name);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -103,26 +95,24 @@ class EntityResolver {
|
||||
return $fieldList;
|
||||
}
|
||||
|
||||
public function relation(string $name) : ? array
|
||||
public function relation(string $name) : array
|
||||
{
|
||||
$property = $this->reflectedClass->getProperties(true)[$name] ?? false;
|
||||
|
||||
try{
|
||||
if ( null !== ( $this->properties[$name] ?? null ) ) {
|
||||
foreach($this->properties[$name]['tags'] ?? [] as $tag) {
|
||||
if ( $tag['object'] instanceof Relation or $tag['object'] instanceof Attribute\Property\Relation ) {
|
||||
return $this->properties[$name];
|
||||
if ( $property ) {
|
||||
foreach($property->getAttributes() as $tag) {
|
||||
if ( $tag->object instanceof Relation or $tag->object instanceof Attribute\Property\Relation ) {
|
||||
return $property;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return [];
|
||||
}
|
||||
catch(\Throwable $e) {
|
||||
# if ( $throwException) {
|
||||
throw new \InvalidArgumentException("Can't find entity relation's column named `$name` from entity {$this->entityClass}");
|
||||
# }
|
||||
throw new \InvalidArgumentException("Can't find entity relation's column named `$name` from entity {$this->entityClass}");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function searchFieldAnnotation(string $field, array|object|string $annotationType, bool $caseSensitive = true) : ? object
|
||||
@@ -134,15 +124,17 @@ class EntityResolver {
|
||||
{
|
||||
$list = [];
|
||||
|
||||
$search = $caseSensitive ? $this->properties : array_change_key_case($this->properties, \CASE_LOWER);
|
||||
$properties = $this->reflectedClass->getProperties(true);
|
||||
|
||||
$search = $caseSensitive ? $properties : array_change_key_case($properties, \CASE_LOWER);
|
||||
|
||||
$annotations = is_array($annotationType) ? $annotationType : [ $annotationType ];
|
||||
|
||||
if ( null !== ( $search[$field] ?? null ) ) {
|
||||
foreach($search[$field]['tags'] ?? [] as $tag) {
|
||||
foreach($search[$field]->getAttributes() as $tag) {
|
||||
foreach($annotations as $annotation) {
|
||||
if ( $tag['object'] instanceof $annotation ) {
|
||||
$list[] = $tag['object'];
|
||||
if ( $tag->object instanceof $annotation ) {
|
||||
$list[] = $tag->object;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -256,6 +248,7 @@ class EntityResolver {
|
||||
*/
|
||||
public function getAnnotationFromClassname(string $className, bool $throwError = true) : ? object
|
||||
{
|
||||
exit(__FILE__);
|
||||
if ( $name = $this->uses[$className] ?? false ) {
|
||||
foreach(array_reverse($this->class['tags']) as $item) {
|
||||
if ( $item['tag'] === $name ) {
|
||||
@@ -292,9 +285,9 @@ class EntityResolver {
|
||||
|
||||
public function getAttributeImplementing(string $interface) : ? object
|
||||
{
|
||||
foreach (array_reverse($this->class['tags']) as $item) {
|
||||
if ($item['object'] instanceof $interface) {
|
||||
return $item['object'];
|
||||
foreach (array_reverse($this->reflectedClass->getAttributes(true)) as $item) {
|
||||
if ($item->object instanceof $interface) {
|
||||
return $item->object;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,61 +296,11 @@ class EntityResolver {
|
||||
|
||||
public function instanciateAnnotationObject(array|\ReflectionAttribute $tagDefinition) : object
|
||||
{
|
||||
|
||||
if ($tagDefinition instanceof \ReflectionAttribute) {
|
||||
$obj = $tagDefinition->newInstance();
|
||||
}
|
||||
else {
|
||||
$arguments = $this->extractArguments($tagDefinition['arguments']);
|
||||
|
||||
if (false === $class = array_search($tagDefinition['tag'], $this->uses)) {
|
||||
throw new \InvalidArgumentException("Annotation class `{$tagDefinition['tag']}` was not found within {$this->entityClass} uses statement (or it's children / traits)");
|
||||
}
|
||||
|
||||
$obj = new $class(... $arguments['constructor']);
|
||||
|
||||
foreach ($arguments['setter'] as $key => $value) {
|
||||
$obj->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
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 &$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user