Compare commits
5 Commits
2a45abae27
...
0ba4fe3a51
Author | SHA1 | Date | |
---|---|---|---|
|
0ba4fe3a51 | ||
|
d92bb527da | ||
87b9ea2e2a | |||
a876ea9045 | |||
f4fffbd480 |
120
bin/migrate.php
Executable file
120
bin/migrate.php
Executable file
@ -0,0 +1,120 @@
|
|||||||
|
#!/usr/bin/php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
array_shift($argv);
|
||||||
|
|
||||||
|
$opt = getopt('c::f:v', [ 'folder:', 'confirm', 'verbose' ]);
|
||||||
|
|
||||||
|
if ( ! $opt ) {
|
||||||
|
exit("Bad arguments provided, you must specify a fullpath using the -f or --folder option\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
$iterator = new RecursiveIteratorIterator(new class(new RecursiveDirectoryIterator($opt['folder'] ?? $opt['f'], FilesystemIterator::SKIP_DOTS)) extends RecursiveFilterIterator {
|
||||||
|
public function accept() : bool {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}, RecursiveIteratorIterator::SELF_FIRST);
|
||||||
|
|
||||||
|
$files = array();
|
||||||
|
|
||||||
|
foreach ($iterator as $info) {
|
||||||
|
if ($info->getExtension() !== "php" || $info->isDir() ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo sprintf("\n### MIGRATING FILE %s\n", $info->getFilename());
|
||||||
|
|
||||||
|
$opened = false;
|
||||||
|
|
||||||
|
$attr = $newContent = [];
|
||||||
|
|
||||||
|
$content = file_get_contents($info->getPathname());
|
||||||
|
|
||||||
|
foreach(explode(PHP_EOL, $content) as $lineIdx => $line) {
|
||||||
|
$tline = trim($line);
|
||||||
|
|
||||||
|
if ($tline === "/**") {
|
||||||
|
$opened = true;
|
||||||
|
}
|
||||||
|
elseif ($tline === '*/') {
|
||||||
|
$opened = false;
|
||||||
|
}
|
||||||
|
elseif (substr($tline, 0, 1) === '*') {
|
||||||
|
$annotation = trim(substr($tline, 1));
|
||||||
|
|
||||||
|
if (strpos($annotation, '@') === 0 ) {
|
||||||
|
$argpos = strpos($annotation, '(');
|
||||||
|
|
||||||
|
if ($argpos !== false) {
|
||||||
|
$name = substr($annotation, 1, $argpos - 1);
|
||||||
|
$args = substr($annotation, $argpos + 1, strrpos($annotation, ')') - $argpos - 1);
|
||||||
|
|
||||||
|
try{
|
||||||
|
$array = eval("return [{$args}];");
|
||||||
|
|
||||||
|
$renderedArgs = [];
|
||||||
|
|
||||||
|
foreach($array as $key => $argument) {
|
||||||
|
|
||||||
|
if ( is_array($argument) ) {
|
||||||
|
$argument = var_export($argument, true);
|
||||||
|
}
|
||||||
|
elseif ( is_bool($argument) ) {
|
||||||
|
$argument = $argument ? 'true' : 'false';
|
||||||
|
}
|
||||||
|
elseif (! is_numeric($argument) ) {
|
||||||
|
$argument = "\"$argument\"";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_numeric($key)) {
|
||||||
|
$renderedArgs[] = $argument;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$renderedArgs[] = "$key: $argument";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$renderedArgs = implode(', ', $renderedArgs);
|
||||||
|
}
|
||||||
|
catch(\Throwable $e) {
|
||||||
|
echo sprintf("!!! WARNING : %s in file (%s) line %d ; using previous notation.\n", $e->getMessage(), $info->getPathname(), $lineIdx + 1);
|
||||||
|
|
||||||
|
$renderedArgs = str_replace(' => ', ': ', $args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$name = substr($annotation, 1);
|
||||||
|
$renderedArgs = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$newContent[] = $line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$newContent = implode(PHP_EOL, $newContent);
|
||||||
|
|
||||||
|
if ($attr) {
|
||||||
|
if ( isset($opt['verbose']) || isset($opt['v']) ) {
|
||||||
|
echo $newContent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( isset($opt['confirm']) || isset($opt['c']) ) {
|
||||||
|
file_put_contents($info->getPathname(), $newContent);
|
||||||
|
|
||||||
|
echo sprintf("\n### FILE CONVERSION COMPLETED %s\n", $info->getFilename());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
echo sprintf("\n### FILE CONVERSION ANALYZED, NOTHING WRITTEN UNTIL --confirm or -c FLAG IS PROVIDED %s\n", $info->getFilename());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
echo sprintf("\n### NOTHING TO DO ON FILE %s\n", $info->getFilename());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -36,37 +36,52 @@ class AnnotationReader
|
|||||||
|
|
||||||
protected function parseDocComment(Reflector $reflect)
|
protected function parseDocComment(Reflector $reflect)
|
||||||
{
|
{
|
||||||
$namespace = $this->getObjectNamespace($reflect);
|
|
||||||
$tags = [];
|
$tags = [];
|
||||||
|
|
||||||
foreach(preg_split("/\r\n|\n|\r/", $reflect->getDocComment()) as $line) {
|
if ( $reflect->getAttributes() ) {
|
||||||
$line = ltrim($line, "* \t\/");
|
foreach($reflect->getAttributes() as $attr) {
|
||||||
$line = rtrim($line, "\t ");
|
try {
|
||||||
|
$tags[] = [
|
||||||
|
'tag' => substr(strrchr($attr->getName(), "\\"), 1),
|
||||||
|
'arguments' => $attr->getArguments(),
|
||||||
|
'object' => $attr->newInstance(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
catch(\Throwable $e) {
|
||||||
|
throw new \Exception(sprintf("%s for %s in file '%s'", $e->getMessage(), $reflect, $this->class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$namespace = $this->getObjectNamespace($reflect);
|
||||||
|
|
||||||
if ( substr($line, 0, 1) === '@' ) {
|
foreach (preg_split("/\r\n|\n|\r/", $reflect->getDocComment()) as $line) {
|
||||||
$line = ltrim($line, '@');
|
$line = ltrim($line, "* \t\/");
|
||||||
|
$line = rtrim($line, "\t ");
|
||||||
|
|
||||||
$open = strpos($line, "(");
|
if (substr($line, 0, 1) === '@') {
|
||||||
$close = strrpos($line, ")");
|
$line = ltrim($line, '@');
|
||||||
|
|
||||||
if ( ! in_array(false, [ $open, $close ], true) && ( ++$open !== $close ) ) {
|
$open = strpos($line, "(");
|
||||||
$arguments = substr($line, $open, $close - $open);
|
$close = strrpos($line, ")");
|
||||||
|
|
||||||
try {
|
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[] = [
|
$tags[] = [
|
||||||
'tag' => substr($line, 0, $open - 1),
|
'tag' => $line,
|
||||||
'arguments' => eval("namespace $namespace; return [ $arguments ];"),
|
'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' => [],
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
5
src/Attribute.php
Normal file
5
src/Attribute.php
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Notes;
|
||||||
|
|
||||||
|
interface Attribute {}
|
@ -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
|
||||||
@ -79,9 +88,9 @@ class ObjectReflection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function gatherProperties(bool $full = true, int $filter =
|
public function gatherProperties(bool $full = true, int $filter =
|
||||||
ReflectionProperty::IS_PUBLIC |
|
ReflectionProperty::IS_PUBLIC |
|
||||||
ReflectionProperty::IS_PROTECTED |
|
ReflectionProperty::IS_PROTECTED |
|
||||||
ReflectionProperty::IS_PRIVATE
|
ReflectionProperty::IS_PRIVATE
|
||||||
) : array
|
) : array
|
||||||
{
|
{
|
||||||
$defaultValues = $this->classReflection->getDefaultProperties();
|
$defaultValues = $this->classReflection->getDefaultProperties();
|
||||||
@ -123,10 +132,10 @@ class ObjectReflection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function gatherMethods(bool $full = true, int $filter =
|
public function gatherMethods(bool $full = true, int $filter =
|
||||||
ReflectionMethod::IS_PUBLIC |
|
ReflectionMethod::IS_PUBLIC |
|
||||||
ReflectionMethod::IS_PROTECTED |
|
ReflectionMethod::IS_PROTECTED |
|
||||||
ReflectionMethod::IS_PRIVATE |
|
ReflectionMethod::IS_PRIVATE |
|
||||||
ReflectionMethod::IS_STATIC
|
ReflectionMethod::IS_STATIC
|
||||||
) : array
|
) : array
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
@ -148,7 +157,7 @@ class ObjectReflection {
|
|||||||
$parameters[$parameter->getName()] = [
|
$parameters[$parameter->getName()] = [
|
||||||
'null' => $parameter->allowsNull(),
|
'null' => $parameter->allowsNull(),
|
||||||
'position' => $parameter->getPosition(),
|
'position' => $parameter->getPosition(),
|
||||||
'type' => $parameter->hasType() ? $parameter->getType()->getName() : false,
|
'type' => $parameter->hasType() && $parameter->getType() instanceof \ReflectionNamedType ? $parameter->getType()->getName() : false,
|
||||||
'array' => $this->isType('array', $parameter),
|
'array' => $this->isType('array', $parameter),
|
||||||
'callable' => $this->isType('callable', $parameter),
|
'callable' => $this->isType('callable', $parameter),
|
||||||
'optional' => $parameter->isOptional(),
|
'optional' => $parameter->isOptional(),
|
||||||
|
@ -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,70 +19,71 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transform an annotation into it's object's counterpart
|
* Transform an annotation into it's object's counterpart
|
||||||
*/
|
*/
|
||||||
public function getAnnotationFromClassname(string $className) : ? object
|
public function getAttributeFromClassname(array|string $className, bool $throwOnError = true) : object|bool
|
||||||
{
|
{
|
||||||
if ( $name = $this->uses[$className] ?? false) {
|
foreach((array) $className as $class) {
|
||||||
foreach(array_reverse($this->class['tags']) as $item) {
|
foreach (array_reverse($this->class['tags']) as $item) {
|
||||||
if ( $item['tag'] === $name ) {
|
if ($item['object'] instanceof $class) {
|
||||||
return $this->instanciateAnnotationObject($item);
|
return $this->instanciateAnnotationObject($item);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($this->properties as $property) {
|
foreach ($this->properties as $property) {
|
||||||
foreach($property['tags'] as $item) {
|
foreach ($property['tags'] as $tag) {
|
||||||
if ( $item['tag'] === $name ) {
|
if ($item['object'] instanceof $class) {
|
||||||
return $this->instanciateAnnotationObject($item);
|
return $this->instanciateAnnotationObject($tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($this->methods as $method) {
|
foreach ($this->methods as $method) {
|
||||||
foreach($method['tags'] as $item) {
|
foreach ($method['tags'] as $tag) {
|
||||||
if ( $item['tag'] === $name ) {
|
if ($item['object'] instanceof $class) {
|
||||||
return $this->instanciateAnnotationObject($item);
|
return $this->instanciateAnnotationObject($tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new \Exception("Annotation `$className` could not be found within your object `{$this->objectClass}`");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new \InvalidArgumentException("Class `$className` was not found within {$this->objectClass} uses statement (or it's children / traits)");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
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
|
* Transform an annotation into it's object's counterpart
|
||||||
*/
|
*/
|
||||||
public function getAnnotationListFromClassname(string $className, bool $throwOnError = true) : array
|
public function getAttributeListFromClassname(array|string $className, bool $throwOnError = true) : array
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
|
|
||||||
if ( $name = $this->uses[$className] ?? false) {
|
foreach((array) $className as $class) {
|
||||||
foreach($this->class['tags'] as $item) {
|
foreach ($this->class['tags'] as $item) {
|
||||||
if ($item['tag'] === $name) {
|
if ($item['object'] instanceof $class) {
|
||||||
$list[] = $this->instanciateAnnotationObject($item);
|
$list[] = $this->instanciateAnnotationObject($item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($this->properties as $property) {
|
foreach ($this->properties as $property) {
|
||||||
foreach($property['tags'] as $item) {
|
foreach ($property['tags'] as $item) {
|
||||||
if ( $item['tag'] === $name ) {
|
if ($item['object'] instanceof $class) {
|
||||||
$list[$property['name']] ??= [];
|
$list[$property['name']] ??= [];
|
||||||
|
|
||||||
$list[$property['name']][] = $this->instanciateAnnotationObject($item);
|
$list[$property['name']][] = $this->instanciateAnnotationObject($item);
|
||||||
@ -88,17 +91,101 @@ class ObjectResolver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($this->methods as $method) {
|
foreach ($this->methods as $method) {
|
||||||
foreach($method['tags'] as $item) {
|
foreach ($method['tags'] as $item) {
|
||||||
if ( $item['tag'] === $name ) {
|
if ($item['object'] instanceof $class) {
|
||||||
$list[$method['name']] ??= [];
|
$list[$method['name']] ??= [];
|
||||||
|
|
||||||
$list[$method['name']][] = $this->instanciateAnnotationObject($item);
|
$list[$method['name']][] = $this->instanciateAnnotationObject($item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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)");
|
if ($throwOnError) throw new \InvalidArgumentException("Class `$className` was not found within {$this->objectClass} uses statement (or it's children / traits)");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,6 +194,10 @@ class ObjectResolver {
|
|||||||
|
|
||||||
public function instanciateAnnotationObject(array $tagDefinition) : Annotation
|
public function instanciateAnnotationObject(array $tagDefinition) : Annotation
|
||||||
{
|
{
|
||||||
|
if (isset($tagDefinition['object']) && $tagDefinition['object'] instanceof \Attribute) {
|
||||||
|
return $tagDefinition['object'];
|
||||||
|
}
|
||||||
|
|
||||||
$arguments = $this->extractArguments($tagDefinition['arguments']);
|
$arguments = $this->extractArguments($tagDefinition['arguments']);
|
||||||
|
|
||||||
if ( false === $class = array_search($tagDefinition['tag'], $this->uses) ) {
|
if ( false === $class = array_search($tagDefinition['tag'], $this->uses) ) {
|
||||||
@ -145,18 +236,18 @@ class ObjectResolver {
|
|||||||
protected function resolveAnnotations()
|
protected function resolveAnnotations()
|
||||||
{
|
{
|
||||||
foreach($this->class['tags'] as $key => &$tag) {
|
foreach($this->class['tags'] as $key => &$tag) {
|
||||||
$tag['object'] = $this->instanciateAnnotationObject($tag);
|
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($this->properties as &$property) {
|
foreach($this->properties as &$property) {
|
||||||
foreach($property['tags'] as &$tag){
|
foreach($property['tags'] as &$tag){
|
||||||
$tag['object'] = $this->instanciateAnnotationObject($tag);
|
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($this->methods as &$method) {
|
foreach($this->methods as &$method) {
|
||||||
foreach($method['tags'] as &$tag){
|
foreach($method['tags'] as &$tag){
|
||||||
$tag['object'] = $this->instanciateAnnotationObject($tag);
|
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user