From 76b1fde972164ad9cd12285329b9f8ad6b79f2d1 Mon Sep 17 00:00:00 2001 From: Hill Date: Fri, 26 Nov 2021 07:47:40 +0800 Subject: [PATCH] change to use function return value for avoid __invoke object --- annotation.php | 70 ++++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/annotation.php b/annotation.php index f7b1a51..5fcbbf6 100755 --- a/annotation.php +++ b/annotation.php @@ -1,36 +1,44 @@ getRawAnnotation($any); - $attrs = \PMVC\get($attrData, 'attrs'); - $result = [ - "data"=>[], - "obj"=>[], - ]; - if ($attrs) { - foreach($attrs as $a) { - $name = $a->getName(); - $args = $a->getArguments(); - $obj = (new ReflectionClass($name))->newInstanceArgs($args); - $dataArr = [ - "name" => $name, - "args" => $args, - ]; - \PMVC\value($result, ['data', $name], null, $dataArr, true); - \PMVC\value($result, ['obj', $name], null, $obj, true); + $attrData = $this->getRawAnnotation($any); + $attrs = \PMVC\get($attrData, 'attrs'); + $result = [ + 'data' => [], + 'obj' => [], + ]; + if ($attrs) { + foreach ($attrs as $a) { + $name = $a->getName(); + $args = $a->getArguments(); + $obj = (new ReflectionClass($name))->newInstanceArgs($args); + $dataArr = [ + 'name' => $name, + 'args' => $args, + ]; + \PMVC\value($result, ['data', $name], null, $dataArr, true); + \PMVC\value( + $result, + ['obj', $name], + null, + function () use ($obj) { + return $obj; + }, + true + ); + } } - } - return $result; + return $result; } public function get($s, $keepRawData = false, $withoutError = false) @@ -40,38 +48,40 @@ public function get($s, $keepRawData = false, $withoutError = false) if ($withoutError) { return false; } else { - return !trigger_error('Can\'t find annotation. '.print_r($s,true)); + return !trigger_error( + 'Can\'t find annotation. ' . print_r($s, true) + ); } } $parser = new AnnotationParser($doc, $keepRawData); - return $parser; + return $parser; } public function getRawAnnotation($s) { - $reader = new AnnotationReader(); + $reader = new AnnotationReader(); if (is_string($s)) { if (class_exists($s)) { return $reader->getClass($s); } elseif (function_exists($s)) { return $reader->getFunction($s); } - } elseif ( is_array($s) && is_callable($s) ) { - if (method_exists($s[0],$s[1])) { - return $reader->getMethod($s[0],$s[1]); + } elseif (is_array($s) && is_callable($s)) { + if (method_exists($s[0], $s[1])) { + return $reader->getMethod($s[0], $s[1]); } elseif (is_callable([$s[0], 'isCallable'])) { $func = $s[0]->isCallable($s[1]); if ($func) { if (is_object($func)) { - return $this->getRawAnnotation([$func,'__invoke']); + return $this->getRawAnnotation([$func, '__invoke']); } else { return $this->getRawAnnotation($func); } } } } - if (is_a($s,'Closure')) { + if (is_a($s, 'Closure')) { return $reader->getFunction($s); } return null;