Skip to content

Commit

Permalink
change to use function return value for avoid __invoke object
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Nov 25, 2021
1 parent 9c541aa commit 76b1fde
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions annotation.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
<?php
namespace PMVC\PlugIn\annotation;

\PMVC\l(__DIR__.'/src/AnnotationReader.php');
\PMVC\l(__DIR__.'/src/AnnotationParser.php');
\PMVC\l(__DIR__ . '/src/AnnotationReader.php');
\PMVC\l(__DIR__ . '/src/AnnotationParser.php');

${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\annotation';
${_INIT_CONFIG}[_CLASS] = __NAMESPACE__ . '\annotation';
use ReflectionClass;

class annotation extends \PMVC\PlugIn
{
public function getAttrs($any)
{
$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, $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)
Expand All @@ -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;
Expand Down

0 comments on commit 76b1fde

Please sign in to comment.