swoole-framwork/core/annotationhandlers/RequestMapping.php
2024-05-20 11:51:07 +08:00

41 lines
1.6 KiB
PHP

<?php
namespace Core\annotationhandlers;
use Core\annotations\RequestMapping;
use Core\BeanFactory;
return [
//$methodAnnoSelf 注解本身
RequestMapping::class => function (\ReflectionMethod $method, $instance, $methodAnnoSelf) {
$path = $methodAnnoSelf->value;//uri;
$requestMethod = count($methodAnnoSelf->method) > 0 ? $methodAnnoSelf->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){
//$refParam->getClass 获取参数Request $request的类型
if($refParam->getClass()->isInstance($extParam)){
$inputParams[] = $extParam;
//continue;
goto abc;
}
}
$inputParams[] = false;
}
abc:
}
return $method->invokeArgs($instance,$inputParams);
//return $method->invoke($instance);//执行反射方法
});
return $instance;
}
];