Skip to content

Commit

Permalink
commit by Simon.Ye
Browse files Browse the repository at this point in the history
  • Loading branch information
samlinye committed Sep 1, 2021
1 parent ffe60c1 commit ac97185
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 152 deletions.
2 changes: 1 addition & 1 deletion src/annotation/ClientIp.php
Expand Up @@ -7,7 +7,7 @@

/**
* @Annotation
* @Target({"ANNOTATION", "PROPERTY"})
* @Target("ANNOTATION")
*/
final class ClientIp
{
Expand Down
13 changes: 0 additions & 13 deletions src/annotation/DtoBind.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/annotation/HttpHeader.php
Expand Up @@ -6,7 +6,7 @@

/**
* @Annotation
* @Target({"ANNOTATION", "PROPERTY"})
* @Target("ANNOTATION")
*/
final class HttpHeader
{
Expand Down
3 changes: 1 addition & 2 deletions src/annotation/JwtClaim.php
Expand Up @@ -3,11 +3,10 @@
namespace mgboot\core\annotation;

use Doctrine\Common\Annotations\Annotation\Target;
use mgboot\Cast;

/**
* @Annotation
* @Target({"ANNOTATION", "PROPERTY"})
* @Target("ANNOTATION")
*/
final class JwtClaim
{
Expand Down
2 changes: 1 addition & 1 deletion src/annotation/PathVariable.php
Expand Up @@ -6,7 +6,7 @@

/**
* @Annotation
* @Target({"METHOD","PROPERTY"})
* @Target("ANNOTATION")
*/
final class PathVariable
{
Expand Down
2 changes: 1 addition & 1 deletion src/annotation/RequestBody.php
Expand Up @@ -6,7 +6,7 @@

/**
* @Annotation
* @Target({"ANNOTATION", "PROPERTY"})
* @Target("ANNOTATION")
*/
final class RequestBody
{
Expand Down
2 changes: 1 addition & 1 deletion src/annotation/RequestParam.php
Expand Up @@ -7,7 +7,7 @@

/**
* @Annotation
* @Target("METHOD")
* @Target("ANNOTATION")
*/
final class RequestParam
{
Expand Down
2 changes: 1 addition & 1 deletion src/annotation/UploadedFile.php
Expand Up @@ -6,7 +6,7 @@

/**
* @Annotation
* @Target({"ANNOTATION", "PROPERTY"})
* @Target("ANNOTATION")
*/
final class UploadedFile
{
Expand Down
13 changes: 0 additions & 13 deletions src/mvc/HandlerFuncArgInfo.php
Expand Up @@ -93,11 +93,6 @@ class HandlerFuncArgInfo
*/
private $needRequestBody = false;

/**
* @var string
*/
private $dtoClassName = '';

private function __construct(?array $data = null)
{
if (empty($data)) {
Expand Down Expand Up @@ -251,12 +246,4 @@ public function isNeedRequestBody(): bool
{
return $this->needRequestBody;
}

/**
* @return string
*/
public function getDtoClassName(): string
{
return $this->dtoClassName;
}
}
109 changes: 0 additions & 109 deletions src/mvc/HandlerFuncArgsInjector.php
Expand Up @@ -7,11 +7,7 @@
use mgboot\http\server\UploadedFile;
use mgboot\util\ArrayUtils;
use mgboot\util\JsonUtils;
use mgboot\util\ReflectUtils;
use mgboot\util\StringUtils;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use RuntimeException;
use stdClass;
use Throwable;
Expand Down Expand Up @@ -91,11 +87,6 @@ public static function inject(Request $req): array
continue;
}

if ($info->getDtoClassName() !== '') {
self::injectDto($req, $args, $info);
continue;
}

self::thowException($handler, $info);
}

Expand Down Expand Up @@ -301,106 +292,6 @@ private static function injectRequestBody(Request $req, array &$args, HandlerFun
$args[] = $payload;
}

private static function injectDto(Request $req, array &$args, HandlerFuncArgInfo $info): void
{
$handler = $req->getRouteRule()->getHandler();
$fmt = '@@fmt:' . self::$fmt1 . ', reason: %s';
$isGet = strtoupper($req->getMethod()) === 'GET';
$contentType = $req->getHeader('Content-Type');
$isJsonPayload = stripos($contentType, 'application/json') !== false;

$isXmlPayload = stripos($contentType, 'application/xml') !== false ||
stripos($contentType, 'text/xml') !== false;

if ($isGet) {
$map1 = $req->getQueryParams();
} else if ($isJsonPayload) {
$map1 = JsonUtils::mapFrom($req->getRawBody());
} else if ($isXmlPayload) {
$map1 = StringUtils::xml2assocArray($req->getRawBody());
} else {
$map1 = array_merge($req->getQueryParams(), $req->getFormData());
}

if (!is_array($map1)) {
if ($info->isNullable()) {
$args[] = null;
return;
}

self::thowException($handler, $info, $fmt, 'param map is empty');
}

$className = $info->getDtoClassName();

try {
$bean = new $className();
} catch (Throwable $ex) {
$bean = null;
}

if (!is_object($bean)) {
if ($info->isNullable()) {
$args[] = null;
return;
}

self::thowException($handler, $info, $fmt, '无法实例化 dto 对象');
}

list($success, $errorTips) = self::mapToBean($bean, $map1);

if (!$success) {
if ($info->isNullable()) {
$args[] = null;
return;
}

self::thowException($handler, $info, $fmt, $errorTips);
}

$args[] = $bean;
}

private static function mapToBean(object $bean, array $map1): array
{
try {
$clazz = new ReflectionClass($bean);
} catch (Throwable $ex) {
return [false, $ex->getMessage()];
}

try {
$fields = $clazz->getProperties(ReflectionProperty::IS_PRIVATE);
} catch (Throwable $ex) {
return [false, $ex->getMessage()];
}

try {
$methods = $clazz->getMethods(ReflectionMethod::IS_PUBLIC);
} catch (Throwable $ex) {
return [false, $ex->getMessage()];
}

foreach ($fields as $field) {
$setter = ReflectUtils::getSetter($field, $methods);

if (!($setter instanceof ReflectionMethod)) {
continue;
}

$mapValue = ReflectUtils::getMapValueByProperty($map1, $field);

try {
$setter->invoke($bean, $mapValue);
} catch (Throwable $ex) {
return [false, $ex->getMessage()];
}
}

return [true, ''];
}

/**
* @param string $handler
* @param HandlerFuncArgInfo $info
Expand Down
14 changes: 5 additions & 9 deletions src/mvc/RouteRulesBuilder.php
Expand Up @@ -6,7 +6,6 @@
use Lcobucci\JWT\Token;
use mgboot\core\annotation\ClientIp;
use mgboot\core\annotation\DeleteMapping;
use mgboot\core\annotation\DtoBind;
use mgboot\core\annotation\GetMapping;
use mgboot\core\annotation\HttpHeader;
use mgboot\core\annotation\JwtAuth;
Expand Down Expand Up @@ -350,12 +349,6 @@ private static function buildHandlerFuncArgs(ReflectionMethod $method): array
$params[$i] = HandlerFuncArgInfo::create($map1);
continue;
}

if ($anno instanceof DtoBind) {
$map1['dtoClassName'] = $typeName;
$params[$i] = HandlerFuncArgInfo::create($map1);
continue;
}
}

$params[$i] = HandlerFuncArgInfo::create($map1);
Expand Down Expand Up @@ -400,7 +393,7 @@ private static function buildExtraAnnotations(ReflectionMethod $method): array
$extraAnnotations = [];

foreach ($annos as $anno) {
if (!is_object($anno)) {
if (!is_object($anno) || !method_exists($anno, 'getValue')) {
continue;
}

Expand All @@ -418,7 +411,10 @@ private static function buildExtraAnnotations(ReflectionMethod $method): array
continue;
}

$extraAnnotations[] = $clazz;
$extraAnnotations[] = [
'annoClazz' => $clazz,
'initParams' => $anno->getValue()
];
}

return compact('extraAnnotations');
Expand Down

0 comments on commit ac97185

Please sign in to comment.