Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持枚举和联合类型的控制器方法参数 #676

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ parameters:
- dev/PHPStan/FileFinder.php
- '#Class UnitEnum not found#'
- '#Class BackedEnum not found#'
- '#Class ReflectionEnum not found#i'
- '#unknown class UnitEnum#'
- '#unknown class BackedEnum#'
- '#unknown class ReflectionEnum#'
- '#Call to static method .+\(\) on an unknown class (Backed|Unit)Enum#'
- '#Method Imi\\Util\\EnumUtil::.+\(\) has invalid return type (Backed|Unit)Enum#'
- '#unknown class Imi\\Test\\Component\\Bean\\EnumBean#'
- '#unknown class Imi\\Test\\Component\\Enum\\TestEnumBean#'
- '#unknown class Imi\\Test\\Component\\Enum\\TestEnumBeanBacked#'
- '#unknown class Imi\\Test\\Component\\Enum\\TestEnumBeanBackedInt#'
- '#Class Imi\\Test\\Component\\Bean\\EnumBean not found#'
- '#Class Imi\\Test\\Component\\Enum\\TestEnumBean not found#'
- '#Class Imi\\Test\\Component\\Enum\\TestEnumBeanBacked not found#'
- '#Class Imi\\Test\\Component\\Enum\\TestEnumBeanBackedInt not found#'
services:
64 changes: 52 additions & 12 deletions src/Components/grpc/src/Middleware/ActionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Imi\Server\Session\Session;
use Imi\Server\View\View;
use Imi\Util\DelayServerBeanCallable;
use Imi\Util\EnumUtil;
use Imi\Util\Http\Consts\MediaType;
use Imi\Util\Http\Consts\RequestHeader;
use Imi\Util\Http\Consts\ResponseHeader;
Expand Down Expand Up @@ -262,9 +263,10 @@
}
elseif ($actionMethodCacheItem->hasDefault())
{
$value = $actionMethodCacheItem->getDefault();
$result[] = $actionMethodCacheItem->getDefault();
continue;

Check warning on line 267 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L266-L267

Added lines #L266 - L267 were not covered by tests
}
elseif (null !== ($type = $actionMethodCacheItem->getType()) && class_exists($type))
elseif (($types = $actionMethodCacheItem->getTypes()) && ActionMethodItem::TYPE_COMMON === $types[0]['type'] && class_exists($type = $types[0]['name']))
{
if (is_subclass_of($type, \Google\Protobuf\Internal\Message::class))
{
Expand All @@ -290,17 +292,55 @@
}
if (null !== $value)
{
switch ($actionMethodCacheItem->getType())
foreach ($actionMethodCacheItem->getTypes() as $type)
{
case 'int':
$value = (int) $value;
break;
case 'float':
$value = (float) $value;
break;
case 'bool':
$value = (bool) $value;
break;
switch ($type['name'])
{
case 'int':
$value = (int) $value;
break 2;

Check warning on line 301 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L300-L301

Added lines #L300 - L301 were not covered by tests
case 'float':
$value = (float) $value;
break 2;

Check warning on line 304 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L303-L304

Added lines #L303 - L304 were not covered by tests
case 'bool':
$value = (bool) $value;
break 2;

Check warning on line 307 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L306-L307

Added lines #L306 - L307 were not covered by tests
default:
switch ($type['type'])
{
case ActionMethodItem::TYPE_UNIT_ENUM:
$newValue = EnumUtil::tryFromName($type['name'], $value);
if (null !== $newValue)

Check warning on line 313 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L311-L313

Added lines #L311 - L313 were not covered by tests
{
$value = $newValue;
break 3;

Check warning on line 316 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L315-L316

Added lines #L315 - L316 were not covered by tests
}
break;
case ActionMethodItem::TYPE_BACKED_ENUM:
if ('int' === $type['enumBackingType'])

Check warning on line 320 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L318-L320

Added lines #L318 - L320 were not covered by tests
{
if (filter_var($value, \FILTER_VALIDATE_INT))

Check warning on line 322 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L322

Added line #L322 was not covered by tests
{
$newValue = $type['name']::tryFrom((int) $value);
if (null !== $newValue)

Check warning on line 325 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L324-L325

Added lines #L324 - L325 were not covered by tests
{
$value = $newValue;
break 3;

Check warning on line 328 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L327-L328

Added lines #L327 - L328 were not covered by tests
}
}
}
else
{
$newValue = $type['name']::tryFrom($value);
if (null !== $newValue)

Check warning on line 335 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L334-L335

Added lines #L334 - L335 were not covered by tests
{
$value = $newValue;
break 3;

Check warning on line 338 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L337-L338

Added lines #L337 - L338 were not covered by tests
}
}
break;

Check warning on line 341 in src/Components/grpc/src/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Components/grpc/src/Middleware/ActionMiddleware.php#L341

Added line #L341 was not covered by tests
}
}
}
}
$result[] = $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Test\Component\Enum\TestEnumBean;
use Imi\Test\Component\Enum\TestEnumBeanBacked;
use Imi\Test\Component\Enum\TestEnumBeanBackedInt;

#[Controller(prefix: '/enum/')]
class EnumController extends HttpController
{
#[Action]
public function test1(TestEnumBean $enum, TestEnumBeanBacked $enumBacked): array
public function test1(TestEnumBean $enum, TestEnumBeanBacked $enumBacked, TestEnumBeanBackedInt $enumBackedInt): array
{
return [
'enum' => $enum,
'enumBacked' => $enumBacked,
'enum' => $enum,
'enumBacked' => $enumBacked,
'enumBackedInt' => $enumBackedInt,
];
}

#[Action]
public function test2(TestEnumBean|string $enum = '', TestEnumBeanBacked|string $enumBacked = ''): array
public function test2(TestEnumBean|string $enum = '', TestEnumBeanBacked|string $enumBacked = '', TestEnumBeanBackedInt|int $enumBackedInt = 0): array
{
return [
'enum' => $enum,
'enumBacked' => $enumBacked,
'enum' => $enum,
'enumBacked' => $enumBacked,
'enumBackedInt' => $enumBackedInt,
];
}
}
Expand Down
26 changes: 15 additions & 11 deletions src/Components/swoole/tests/unit/HttpServer/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,25 +392,29 @@ public function testEnum(): void
$this->markTestSkipped();
}
$http = new HttpRequest();
$response = $http->get($this->host . 'enum/test1?enum=A&enumBacked=imi');
$response = $http->get($this->host . 'enum/test1?enum=A&enumBacked=imi&enumBackedInt=1');
$this->assertEquals([
'enum' => 'A',
'enumBacked' => 'imi',
'enum' => 'A',
'enumBacked' => 'imi',
'enumBackedInt' => 1,
], $response->json(true));
$response = $http->get($this->host . 'enum/test2');
$this->assertEquals([
'enum' => '',
'enumBacked' => '',
'enum' => '',
'enumBacked' => '',
'enumBackedInt' => 0,
], $response->json(true));
$response = $http->get($this->host . 'enum/test2?enum=A&enumBacked=imi');
$response = $http->get($this->host . 'enum/test2?enum=A&enumBacked=imi&enumBackedInt=1');
$this->assertEquals([
'enum' => 'A',
'enumBacked' => 'imi',
'enum' => 'A',
'enumBacked' => 'imi',
'enumBackedInt' => 1,
], $response->json(true));
$response = $http->get($this->host . 'enum/test2?enum=x&enumBacked=x');
$response = $http->get($this->host . 'enum/test2?enum=x&enumBacked=x&enumBackedInt=0');
$this->assertEquals([
'enum' => 'x',
'enumBacked' => 'x',
'enum' => 'x',
'enumBacked' => 'x',
'enumBackedInt' => 0,
], $response->json(true));
}
}
79 changes: 53 additions & 26 deletions src/Server/Http/Middleware/ActionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,14 @@
}
elseif ($actionMethodCacheItem->hasDefault())
{
$value = $actionMethodCacheItem->getDefault();
$result[] = $actionMethodCacheItem->getDefault();
continue;
}
elseif ($actionMethodCacheItem->allowNull())
{
$value = null;
}
elseif (($type = $actionMethodCacheItem->getType()) && (UploadedFileInterface::class === $type || is_subclass_of($type, UploadedFileInterface::class)))
elseif (($types = $actionMethodCacheItem->getTypes()) && ActionMethodItem::TYPE_UPLOADED_FILE === $types[0]['type'])
{
$uploadedFiles ??= $request->getUploadedFiles();
if (!isset($uploadedFiles[$paramName]))
Expand All @@ -268,38 +269,64 @@
{
throw new \RuntimeException(sprintf('Upload file failed. error:%d', $value->getError()));
}
$result[] = $value;
continue;
}
else
{
throw new \InvalidArgumentException(sprintf('Missing parameter: %s', $paramName));
}
if (null !== $value)
{
switch ($actionMethodCacheItem->getType())
foreach ($actionMethodCacheItem->getTypes() as $type)
{
case 'int':
$value = (int) $value;
break;
case 'float':
$value = (float) $value;
break;
case 'bool':
$value = (bool) $value;
break;
case \UnitEnum::class:
$newValue = EnumUtil::tryFromName($actionMethodCacheItem->getTypeClass(), $value);
if (null !== $newValue)
{
$value = $newValue;
}
break;
case \BackedEnum::class:
$newValue = $actionMethodCacheItem->getTypeClass()::tryFrom($value);
if (null !== $newValue)
{
$value = $newValue;
}
break;
switch ($type['name'])
{
case 'int':
$value = (int) $value;
break 2;
case 'float':
$value = (float) $value;
break 2;

Check warning on line 290 in src/Server/Http/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Server/Http/Middleware/ActionMiddleware.php#L289-L290

Added lines #L289 - L290 were not covered by tests
case 'bool':
$value = (bool) $value;
break 2;

Check warning on line 293 in src/Server/Http/Middleware/ActionMiddleware.php

View check run for this annotation

Codecov / codecov/patch

src/Server/Http/Middleware/ActionMiddleware.php#L292-L293

Added lines #L292 - L293 were not covered by tests
default:
switch ($type['type'])
{
case ActionMethodItem::TYPE_UNIT_ENUM:
$newValue = EnumUtil::tryFromName($type['name'], $value);
if (null !== $newValue)
{
$value = $newValue;
break 3;
}
break;
case ActionMethodItem::TYPE_BACKED_ENUM:
if ('int' === $type['enumBackingType'])
{
if (filter_var($value, \FILTER_VALIDATE_INT))
{
$newValue = $type['name']::tryFrom((int) $value);
if (null !== $newValue)
{
$value = $newValue;
break 3;
}
}
}
else
{
$newValue = $type['name']::tryFrom($value);
if (null !== $newValue)
{
$value = $newValue;
break 3;
}
}
break;
}
}
}
}
$result[] = $value;
Expand Down