diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index c39dee5ab5..9dc9793bba 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -5,6 +5,10 @@ - [#2411](https://github.com/hyperf/hyperf/pull/2411) Added method `Hyperf\Database\Query\Builder::forPageBeforeId` for database. - [#2420](https://github.com/hyperf/hyperf/pull/2420) [#2426](https://github.com/hyperf/hyperf/pull/2426) Added option `enable-event-dispatcher` to initialize EventDispatcher for command. +## Optimized + +- [#2429](https://github.com/hyperf/hyperf/pull/2429) Optimized error message when does not set the value of `@var` for `@Inject`. + # v2.0.9 - 2020-08-31 ## Added diff --git a/docs/zh-cn/db/gen.md b/docs/zh-cn/db/gen.md index 913b534887..132c2a4aa3 100644 --- a/docs/zh-cn/db/gen.md +++ b/docs/zh-cn/db/gen.md @@ -244,7 +244,7 @@ class ModelUpdateVisitor extends Visitor } if (Str::startsWith($cast, 'decimal')) { - // 如果 cast 为 decimal,则这是 @property 为 string + // 如果 cast 为 decimal,则 @property 改为 string return 'string'; } diff --git a/docs/zh-cn/db/quick-start.md b/docs/zh-cn/db/quick-start.md index b141ce2234..dc3b994e89 100644 --- a/docs/zh-cn/db/quick-start.md +++ b/docs/zh-cn/db/quick-start.md @@ -25,15 +25,16 @@ composer require hyperf/database 默认配置如下,数据库支持多库配置,默认为 `default`。 | 配置项 | 类型 | 默认值 | 备注 | -|:--------------------:|:------:|:---------------:|:------------------:| +| :------------------: | :----: | :-------------: | :----------------: | | driver | string | 无 | 数据库引擎 | | host | string | 无 | 数据库地址 | -| database | string | 无 | 数据库默认 DB | +| database | string | 无 | 数据库默认 DB | | username | string | 无 | 数据库用户名 | | password | string | null | 数据库密码 | | charset | string | utf8 | 数据库编码 | | collation | string | utf8_unicode_ci | 数据库编码 | | prefix | string | '' | 数据库模型前缀 | +| timezone | string | null | 数据库时区 | | pool.min_connections | int | 1 | 连接池内最少连接数 | | pool.max_connections | int | 10 | 连接池内最大连接数 | | pool.connect_timeout | float | 10.0 | 连接等待超时时间 | diff --git a/src/di/src/Annotation/Inject.php b/src/di/src/Annotation/Inject.php index 2b09cf3284..c1ba2d49bc 100644 --- a/src/di/src/Annotation/Inject.php +++ b/src/di/src/Annotation/Inject.php @@ -74,6 +74,8 @@ public function collectProperty(string $className, ?string $target): void throw $exception; } $this->value = ''; + } catch (\Throwable $exception) { + throw new AnnotationException("The @Inject value is invalid for {$className}->{$target}. Because {$exception->getMessage()}"); } } } diff --git a/src/di/tests/InjectTest.php b/src/di/tests/InjectTest.php index a91865bc97..227aa1365e 100644 --- a/src/di/tests/InjectTest.php +++ b/src/di/tests/InjectTest.php @@ -13,11 +13,13 @@ use Hyperf\Contract\ContainerInterface; use Hyperf\Di\Annotation\AnnotationReader; +use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\ScanConfig; use Hyperf\Di\Annotation\Scanner; use Hyperf\Di\Aop\Ast; use Hyperf\Di\BetterReflectionManager; use Hyperf\Di\ClassLoader; +use Hyperf\Di\Exception\AnnotationException; use Hyperf\Di\Exception\NotFoundException; use Hyperf\Utils\ApplicationContext; use HyperfTest\Di\ExceptionStub\DemoInjectException; @@ -25,6 +27,7 @@ use HyperfTest\Di\Stub\AspectCollector; use HyperfTest\Di\Stub\Demo; use HyperfTest\Di\Stub\DemoInject; +use HyperfTest\Di\Stub\EmptyVarValue; use Mockery; use PHPUnit\Framework\TestCase; @@ -76,6 +79,26 @@ public function testInjectException() } } + public function testInjectNotInitReflector() + { + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage('The @Inject value is invalid for HyperfTest\Di\Stub\EmptyVarValue->demo. Because The class reflector object does not init yet'); + + $inject = new Inject(); + $inject->collectProperty(EmptyVarValue::class, 'demo'); + } + + public function testInjectEmptyVar() + { + $this->expectException(AnnotationException::class); + $this->expectExceptionMessage('The @Inject value is invalid for HyperfTest\Di\Stub\EmptyVarValue->demo. Because Argument 1 passed to Roave\BetterReflection\TypesFinder\FindPropertyType::Roave\BetterReflection\TypesFinder\{closure}() must be an instance of phpDocumentor\Reflection\DocBlock\Tags\Var_, instance of phpDocumentor\Reflection\DocBlock\Tags\InvalidTag given'); + + BetterReflectionManager::initClassReflector([__DIR__ . '/Stub']); + + $inject = new Inject(); + $inject->collectProperty(EmptyVarValue::class, 'demo'); + } + protected function getContainer() { $container = Mockery::mock(ContainerInterface::class); diff --git a/src/di/tests/Stub/EmptyVarValue.php b/src/di/tests/Stub/EmptyVarValue.php new file mode 100644 index 0000000000..e3c6160987 --- /dev/null +++ b/src/di/tests/Stub/EmptyVarValue.php @@ -0,0 +1,23 @@ +