From e97df353ae5c99c435e7e2ad4932828a1a79f37b Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 18 Jul 2022 16:18:29 +0000 Subject: [PATCH] - Prepping 8.1 migration --- src/Annotation/Annotation.php | 5 - src/Annotation/AnnotationReader.php | 103 -------- src/Annotation/Classes/Collation.php | 2 +- src/Annotation/Classes/Method.php | 2 +- src/Annotation/Classes/Table.php | 2 +- src/Annotation/Property/Field.php | 2 +- src/Annotation/Property/Filter.php | 2 +- src/Annotation/Property/FilterJoin.php | 2 +- src/Annotation/Property/GroupBy.php | 2 +- src/Annotation/Property/Join.php | 2 +- src/Annotation/Property/OrderBy.php | 2 +- src/Annotation/Property/Relation.php | 2 +- src/Annotation/Property/Relation/Ignore.php | 2 +- src/Annotation/Property/Where.php | 2 +- src/Annotation/Property/WithJoin.php | 2 +- src/Common/EntityResolver.php | 5 +- src/Common/ObjectReflection.php | 269 -------------------- src/EntityTrait.php | 2 +- 18 files changed, 18 insertions(+), 392 deletions(-) delete mode 100644 src/Annotation/Annotation.php delete mode 100644 src/Annotation/AnnotationReader.php delete mode 100644 src/Common/ObjectReflection.php diff --git a/src/Annotation/Annotation.php b/src/Annotation/Annotation.php deleted file mode 100644 index ac55c70..0000000 --- a/src/Annotation/Annotation.php +++ /dev/null @@ -1,5 +0,0 @@ -class = $class; - } - - public static function fromClass($class) : self - { - return new static($class); - } - - public function getProperty(ReflectionProperty $property) - { - return $this->parseDocComment($property); - } - - public function getClass(ReflectionClass $class) - { - return $this->parseDocComment($class); - } - - public function getMethod(ReflectionMethod $method) - { - return $this->parseDocComment($method); - } - - protected function parseDocComment(Reflector $reflect) - { - $namespace = $this->getObjectNamespace($reflect); - $tags = []; - - foreach(preg_split("/\r\n|\n|\r/", $reflect->getDocComment()) as $line) { - $line = ltrim($line, "* \t\/"); - $line = rtrim($line, "\t "); - - if ( substr($line, 0, 1) === '@' ) { - $line = ltrim($line, '@'); - - $open = strpos($line, "("); - $close = strrpos($line, ")"); - - if ( ! in_array(false, [ $open, $close ], true) && ( ++$open !== $close ) ) { - $arguments = substr($line, $open, $close - $open); - - try { - $tags[] = [ - 'tag' => substr($line, 0, $open - 1), - 'arguments' => eval("namespace $namespace; return [ $arguments ];" ), - ]; - } - catch(\Throwable $error) { - throw new \InvalidArgumentException("An error occured while parsing annotation from '" . $this->getObjectName($reflect) . "' : @$line -- " . $error->getMessage()); - } - } - else { - $tags[] = [ - 'tag' => $line, - 'arguments' => [], - ]; - } - } - } - - return $tags; - } - - protected function getObjectName(Reflector $reflect) : string - { - switch(true) { - case $reflect instanceof ReflectionMethod : - case $reflect instanceof ReflectionProperty : - return $reflect->class . "::" . $reflect->name; - - case $reflect instanceof ReflectionClass : - return $reflect->name; - } - } - - protected function getObjectNamespace(Reflector $reflect) : string - { - switch(true) { - case $reflect instanceof ReflectionMethod : - case $reflect instanceof ReflectionProperty : - return $reflect->getDeclaringClass()->getNamespaceName(); - - case $reflect instanceof ReflectionClass : - return $reflect->getNamespaceName(); - } - } -} diff --git a/src/Annotation/Classes/Collation.php b/src/Annotation/Classes/Collation.php index 225c646..acb3fb4 100644 --- a/src/Annotation/Classes/Collation.php +++ b/src/Annotation/Classes/Collation.php @@ -2,6 +2,6 @@ namespace Ulmus\Annotation\Classes; -class Collation implements \Ulmus\Annotation\Annotation { +class Collation implements \Notes\Annotation { } diff --git a/src/Annotation/Classes/Method.php b/src/Annotation/Classes/Method.php index 523d2bb..a267bed 100644 --- a/src/Annotation/Classes/Method.php +++ b/src/Annotation/Classes/Method.php @@ -2,6 +2,6 @@ namespace Ulmus\Annotation\Classes; -class Method implements \Ulmus\Annotation\Annotation { +class Method implements \Notes\Annotation { } diff --git a/src/Annotation/Classes/Table.php b/src/Annotation/Classes/Table.php index 0e1af18..9309160 100644 --- a/src/Annotation/Classes/Table.php +++ b/src/Annotation/Classes/Table.php @@ -2,7 +2,7 @@ namespace Ulmus\Annotation\Classes; -class Table implements \Ulmus\Annotation\Annotation { +class Table implements \Notes\Annotation { public string $name; diff --git a/src/Annotation/Property/Field.php b/src/Annotation/Property/Field.php index 25e4df6..cfbf886 100644 --- a/src/Annotation/Property/Field.php +++ b/src/Annotation/Property/Field.php @@ -2,7 +2,7 @@ namespace Ulmus\Annotation\Property; -class Field implements \Ulmus\Annotation\Annotation { +class Field implements \Notes\Annotation { public string $type; diff --git a/src/Annotation/Property/Filter.php b/src/Annotation/Property/Filter.php index e25f831..ee8c048 100644 --- a/src/Annotation/Property/Filter.php +++ b/src/Annotation/Property/Filter.php @@ -2,7 +2,7 @@ namespace Ulmus\Annotation\Property; -class Filter implements \Ulmus\Annotation\Annotation { +class Filter implements \Notes\Annotation { public string $method; diff --git a/src/Annotation/Property/FilterJoin.php b/src/Annotation/Property/FilterJoin.php index d46129a..2f7bac9 100644 --- a/src/Annotation/Property/FilterJoin.php +++ b/src/Annotation/Property/FilterJoin.php @@ -2,7 +2,7 @@ namespace Ulmus\Annotation\Property; -class FilterJoin implements \Ulmus\Annotation\Annotation { +class FilterJoin implements \Notes\Annotation { public string $method; diff --git a/src/Annotation/Property/GroupBy.php b/src/Annotation/Property/GroupBy.php index 6d3eca9..e556a1d 100644 --- a/src/Annotation/Property/GroupBy.php +++ b/src/Annotation/Property/GroupBy.php @@ -2,7 +2,7 @@ namespace Ulmus\Annotation\Property; -class GroupBy implements \Ulmus\Annotation\Annotation { +class GroupBy implements \Notes\Annotation { public array $fields = []; diff --git a/src/Annotation/Property/Join.php b/src/Annotation/Property/Join.php index 2709dda..185fcc7 100644 --- a/src/Annotation/Property/Join.php +++ b/src/Annotation/Property/Join.php @@ -2,7 +2,7 @@ namespace Ulmus\Annotation\Property; -class Join implements \Ulmus\Annotation\Annotation { +class Join implements \Notes\Annotation { public string $type; diff --git a/src/Annotation/Property/OrderBy.php b/src/Annotation/Property/OrderBy.php index a2ad525..1c181e7 100644 --- a/src/Annotation/Property/OrderBy.php +++ b/src/Annotation/Property/OrderBy.php @@ -2,7 +2,7 @@ namespace Ulmus\Annotation\Property; -class OrderBy implements \Ulmus\Annotation\Annotation { +class OrderBy implements \Notes\Annotation { public string $field; diff --git a/src/Annotation/Property/Relation.php b/src/Annotation/Property/Relation.php index 15fadf7..1749670 100644 --- a/src/Annotation/Property/Relation.php +++ b/src/Annotation/Property/Relation.php @@ -2,7 +2,7 @@ namespace Ulmus\Annotation\Property; -class Relation implements \Ulmus\Annotation\Annotation { +class Relation implements \Notes\Annotation { public string $type; diff --git a/src/Annotation/Property/Relation/Ignore.php b/src/Annotation/Property/Relation/Ignore.php index 0b6ef93..541fe3c 100644 --- a/src/Annotation/Property/Relation/Ignore.php +++ b/src/Annotation/Property/Relation/Ignore.php @@ -2,4 +2,4 @@ namespace Ulmus\Annotation\Property\Relation; -class Ignore implements \Ulmus\Annotation\Annotation {} +class Ignore implements \Notes\Annotation {} diff --git a/src/Annotation/Property/Where.php b/src/Annotation/Property/Where.php index 7affbd7..67085e4 100644 --- a/src/Annotation/Property/Where.php +++ b/src/Annotation/Property/Where.php @@ -4,7 +4,7 @@ namespace Ulmus\Annotation\Property; use Ulmus\Query; -class Where implements \Ulmus\Annotation\Annotation { +class Where implements \Notes\Annotation { public /* stringable */ $field; diff --git a/src/Annotation/Property/WithJoin.php b/src/Annotation/Property/WithJoin.php index ff582b0..035fea6 100644 --- a/src/Annotation/Property/WithJoin.php +++ b/src/Annotation/Property/WithJoin.php @@ -2,7 +2,7 @@ namespace Ulmus\Annotation\Property; -class WithJoin implements \Ulmus\Annotation\Annotation { +class WithJoin implements \Notes\Annotation { protected array $joins; diff --git a/src/Common/EntityResolver.php b/src/Common/EntityResolver.php index 2b8923d..6b4c6f2 100644 --- a/src/Common/EntityResolver.php +++ b/src/Common/EntityResolver.php @@ -3,12 +3,15 @@ namespace Ulmus\Common; use Ulmus\Ulmus, - Ulmus\Annotation\Annotation, Ulmus\Annotation\Classes\Table, Ulmus\Annotation\Property\Field, Ulmus\Annotation\Property\Virtual, Ulmus\Annotation\Property\Relation; +use Notes\Annotation; + +use Notes\ObjectReflection; + class EntityResolver { const KEY_ENTITY_NAME = 01; diff --git a/src/Common/ObjectReflection.php b/src/Common/ObjectReflection.php deleted file mode 100644 index bda150c..0000000 --- a/src/Common/ObjectReflection.php +++ /dev/null @@ -1,269 +0,0 @@ -classReflection = $class instanceof ReflectionClass ? $class : new ReflectionClass($class); - $this->annotationReader = $annotationReader ?: AnnotationReader::fromClass($class); - } - - public static function fromClass($class) : self - { - return new static($class); - } - - public function read() : array - { - return [ - 'uses' => $this->gatherUses(true), - 'class' => $this->gatherClass(true), - 'method' => $this->gatherMethods(true), - 'property' => $this->gatherProperties(true), - ]; - } - - public function gatherUses(bool $full = true) : array - { - $list = []; - - if ( $full ) { - if ( $parentClass = $this->classReflection->getParentClass() ) { - $list = static::fromClass($parentClass)->gatherUses(true); - } - - foreach($this->classReflection->getTraits() as $trait) { - $list = array_merge($list, static::fromClass($trait)->gatherUses(true)); - } - } - - return array_merge($this->getUsesStatements(), $list); - } - - public function gatherClass(bool $full = true) : array - { - $class = []; - - if ( $full ) { - if ( $parentClass = $this->classReflection->getParentClass() ) { - $class = static::fromClass($parentClass)->gatherClass(true); - } - - $itemName = function($item) { - return $item->getName(); - }; - } - - return array_merge_recursive($class, [ - 'tags' => $this->annotationReader->getClass($this->classReflection) - ] + ( ! $full ? [] : [ - 'traits' => array_map($itemName, $this->classReflection->getTraits()), - 'interfaces' => array_map($itemName, $this->classReflection->getInterfaces()), - ])); - } - - public function gatherProperties(bool $full = true, int $filter = - ReflectionProperty::IS_PUBLIC | - ReflectionProperty::IS_PROTECTED | - ReflectionProperty::IS_PRIVATE - ) : array - { - $properties = []; - $defaultValues = $this->classReflection->getDefaultProperties(); - - if ( $full ) { - if ( $parentClass = $this->classReflection->getParentClass() ) { - $properties = static::fromClass($parentClass)->gatherProperties($full, $filter); - } - } - - $list = []; - - foreach($this->classReflection->getProperties($filter) as $property) { - $current = [ - 'name' => $property->getName() - ]; - - # Default value can be 'null', so isset() it not suitable here - if ( array_key_exists($current['name'], $defaultValues) ) { - $current['value'] = $defaultValues[ $current['name'] ]; - } - - if ( $property->hasType() ) { - $current['type'] = $property->getType()->getName(); - $current['builtin'] = $property->getType()->isBuiltIn(); - $current['nullable'] = $property->getType()->allowsNull(); - } - - $current['tags'] = $this->annotationReader->getProperty($property); - - if ( $this->ignoreElementAnnotation($current['tags']) ) { - continue; - } - - $list[ $current['name'] ] = $current; - } - - return array_merge($properties, $list); - } - - public function gatherMethods(bool $full = true, int $filter = - ReflectionMethod::IS_PUBLIC | - ReflectionMethod::IS_PROTECTED | - ReflectionMethod::IS_PRIVATE | - ReflectionMethod::IS_STATIC - ) : array - { - $list = $methods = []; - - if ( $full ) { - if ( $parentClass = $this->classReflection->getParentClass() ) { - $methods = static::fromClass($parentClass)->gatherMethods($full, $filter); - } - } - - foreach($this->classReflection->getMethods($filter) as $method) { - $parameters = []; - - foreach($method->getParameters() as $parameter) { - $parameters[$parameter->getName()] = [ - 'null' => $parameter->allowsNull(), - 'position' => $parameter->getPosition(), - 'type' => $parameter->hasType() ? $parameter->getType()->getName() : false, - 'array' => \Notes\ObjectReflection::isType('array', $parameter), - 'callable' => \Notes\ObjectReflection::isType('callable', $parameter), - 'optional' => $parameter->isOptional(), - 'byReference' => $parameter->isPassedByReference(), - ]; - } - - $current = [ - 'name' => $method->getName(), - 'type' => $method->hasReturnType() ? $method->getReturnType()->getName() : false, - 'constructor' => $method->isConstructor(), - 'destructor' => $method->isDestructor(), - 'parameters' => $parameters, - ]; - - $current['tags'] = $this->annotationReader->getMethod($method); - - if ( $this->ignoreElementAnnotation($current['tags']) ) { - continue; - } - - $list[ $current['name'] ] = $current; - } - - return array_merge($methods, $list); - } - - protected function ignoreElementAnnotation($tags) : bool - { - return in_array('IGNORE', array_map('strtoupper', array_column($tags, 'tag') )); - } - - - protected function readCode() : string - { - static $code = []; - $fileName = $this->classReflection->getFilename(); - return $code[$fileName] ?? $code[$fileName] = file_get_contents($fileName); - } - - protected function getUsesStatements() : array - { - $uses = []; - $tokens = token_get_all( $c = $this->readCode() ); - - while ( $token = array_shift($tokens) ) { - - if ( is_array($token) ) { - list($token, $value) = $token; - } - - switch ($token) { - case T_CLASS: - case T_TRAIT: - case T_INTERFACE: - break 2; - - case T_USE: - $isUse = true; - break; - - case T_NS_SEPARATOR: - $isNamespace = $isUse; - break; - - case T_STRING: - case T_NAME_QUALIFIED: - if ( $isNamespace && $latestString ) { - $statement[] = $latestString; - } - - $latestString = $value; - break; - - case T_AS: - # My\Name\Space\aClassHere `as` ClassAlias; - $replacedClass = implode("\\", array_merge($statement, [ $latestString ])); - $latestString = null; - break; - - case T_WHITESPACE: - case T_COMMENT: - case T_DOC_COMMENT: - break; - - case '{': - # opening a sub-namespace -> \My\Name\Space\`{`OneItem, AnotherItem} - if ( $isNamespace ) { - $inNamespace = true; - } - break; - - case ';'; - case ',': - case '}': - if ( $isUse ) { - if ( $replacedClass ) { - $uses[$replacedClass] = $latestString; - $replacedClass = ""; - } - elseif ( $latestString ) { - $uses[implode("\\", array_merge($statement, [ $latestString ]))] = $latestString; - } - } - - if ( $inNamespace ) { - $latestString = ""; - - # \My\Name\Space\{OneItem, AnotherItem`}` <- closing a sub-namespace - if ( $token !== "}" ) { - break; - } - } - - case T_OPEN_TAG: - default: - $statement = []; - $latestString = ""; - $replacedClass = null; - $isNamespace = $inNamespace = false; - $isUse = ( $isUse ?? false ) && ( $token === ',' ); - break; - } - } - - return $uses; - } -} diff --git a/src/EntityTrait.php b/src/EntityTrait.php index a999a9e..942686a 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -273,7 +273,7 @@ trait EntityTrait { /** * @Ignore */ - public function jsonSerialize() + public function jsonSerialize() : mixed { return $this->entityGetDataset(); }