swoole-framwork/core/annotationhandlers/RequestMapping.php
2024-05-21 14:33:56 +08:00

47 lines
1.9 KiB
PHP

<?php
namespace Core\annotationhandlers;
use Core\annotations\RequestMapping;
use Core\BeanFactory;
use Core\init\DecoratorCollector;
return [
//$methodAnnoSelf 注解本身
RequestMapping::class => function (\ReflectionMethod $method, $instance, $self) {
$path = $self->value;//uri;
$requestMethod = $self->method ?: ['GET'];
$RouterCollector = BeanFactory::getBean("RouterCollector");
//var_dump($RouterCollector);
$RouterCollector->addRouter($requestMethod, $path, function ($params,$extParams) use ($method, $instance) {
$inputParams=[];
$refParams=$method->getParameters();
foreach ($refParams as $refParam){
if(isset($params[$refParam->getName()])){
$inputParams[] = $params[$refParam->getName()];
}else{
foreach($extParams as $extParam){
$type = $refParam->getType();
if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
$className = $type->getName();
if(class_exists($className)){
$reflectionClass = new \ReflectionClass($className);
if($reflectionClass->isInstance($extParam)){
$inputParams[] = $extParam;
goto end;
}
}
}
}
$inputParams[] = false;
}
end:
}
return BeanFactory::getBean(DecoratorCollector::class)->exec($method,$instance,$inputParams);
//return $method->invokeArgs($instance,$inputParams);
//return $method->invoke($instance);//执行反射方法
});
return $instance;
}
];