Removed $formattedValue from AbstractAnnotation.

This commit is contained in:
李铭昕 2021-06-15 23:13:21 +08:00
parent 70cd22cb17
commit ee156ab50d
12 changed files with 33 additions and 31 deletions

View File

@ -40,6 +40,7 @@
- [#3635](https://github.com/hyperf/hyperf/pull/3635) Added `Hyperf\Utils\CodeGen\PhpParser` which used to generate AST for reflection.
- [#3648](https://github.com/hyperf/hyperf/pull/3648) Added `Hyperf\Utils\CodeGen\PhpDocReaderManager` to manage `PhpDocReader`.
- [#3679](https://github.com/hyperf/hyperf/pull/3679) Added Nacos SDK component.
- [#3698](https://github.com/hyperf/hyperf/pull/3698) Support PHP8 Attribute which can replace doctrine annotations.
## Optimized

View File

@ -60,6 +60,6 @@ class CircuitBreaker extends AbstractAnnotation
public function __construct(...$value)
{
parent::__construct(...$value);
$this->bindMainProperty('value');
$this->bindMainProperty('value', $value);
}
}

View File

@ -29,6 +29,6 @@ class Value extends AbstractAnnotation
public function __construct(...$value)
{
parent::__construct(...$value);
$this->bindMainProperty('key');
$this->bindMainProperty('key', $value);
}
}

View File

@ -77,7 +77,7 @@ class Crontab extends AbstractAnnotation
public function __construct(...$value)
{
parent::__construct(...$value);
$this->bindMainProperty('rule');
$this->bindMainProperty('rule', $value);
if (! empty($this->rule)) {
$this->rule = str_replace('\\', '', $this->rule);
}

View File

@ -17,33 +17,21 @@ use ReflectionProperty;
abstract class AbstractAnnotation implements AnnotationInterface, Arrayable
{
/**
* @var null|array
*/
protected $formattedValue = null;
protected $formattedValue;
public function __construct(...$value)
{
$this->formattedValue = $this->formatParams($value);
foreach ($this->formattedValue as $key => $val) {
$formattedValue = $this->formatParams($value);
foreach ($formattedValue as $key => $val) {
if (property_exists($this, $key)) {
$this->{$key} = $val;
}
}
}
protected function formatParams($value): array
{
if (isset($value[0])) {
$value = $value[0];
}
if (! is_array($value)) {
$value = ['value' => $value];
}
return $value;
}
public function toArray(): array
{
$properties = ReflectionManager::reflectClass(static::class)->getProperties(ReflectionProperty::IS_PUBLIC);
@ -69,10 +57,22 @@ abstract class AbstractAnnotation implements AnnotationInterface, Arrayable
AnnotationCollector::collectProperty($className, $target, static::class, $this);
}
protected function bindMainProperty(string $key)
protected function formatParams($value): array
{
if (isset($this->formattedValue['value'])) {
$this->{$key} = $this->formattedValue['value'];
if (isset($value[0])) {
$value = $value[0];
}
if (! is_array($value)) {
$value = ['value' => $value];
}
return $value;
}
protected function bindMainProperty(string $key, array $value)
{
$formattedValue = $this->formatParams($value);
if (isset($formattedValue['value'])) {
$this->{$key} = $formattedValue['value'];
}
}
}

View File

@ -33,6 +33,6 @@ abstract class Mapping extends AbstractAnnotation
public function __construct(...$value)
{
parent::__construct(...$value);
$this->bindMainProperty('path');
$this->bindMainProperty('path', $value);
}
}

View File

@ -29,6 +29,6 @@ class Middleware extends AbstractAnnotation
public function __construct(...$value)
{
parent::__construct(...$value);
$this->bindMainProperty('middleware');
$this->bindMainProperty('middleware', $value);
}
}

View File

@ -29,6 +29,6 @@ class Middlewares extends AbstractAnnotation
public function __construct(...$value)
{
$this->formatParams($value);
$this->bindMainProperty('middlewares');
$this->bindMainProperty('middlewares', $value);
}
}

View File

@ -43,13 +43,14 @@ class RequestMapping extends Mapping
public function __construct(...$value)
{
parent::__construct(...$value);
if (isset($this->formattedValue['methods'])) {
if (is_string($this->formattedValue['methods'])) {
$formattedValue = $this->formatParams($value);
if (isset($formattedValue['methods'])) {
if (is_string($formattedValue['methods'])) {
// Explode a string to a array
$this->methods = explode(',', Str::upper(str_replace(' ', '', $this->formattedValue['methods'])));
$this->methods = explode(',', Str::upper(str_replace(' ', '', $formattedValue['methods'])));
} else {
$methods = [];
foreach ($this->formattedValue['methods'] as $method) {
foreach ($formattedValue['methods'] as $method) {
$methods[] = Str::upper(str_replace(' ', '', $method));
}
$this->methods = $methods;

View File

@ -32,7 +32,7 @@ class ModelListener extends AbstractAnnotation
{
parent::__construct(...$value);
if ($formattedValue = $this->formattedValue['value'] ?? null) {
if ($formattedValue = $this->formatParams($value)['value'] ?? null) {
if (is_string($formattedValue)) {
$this->models = [$formattedValue];
} elseif (is_array($formattedValue) && ! Arr::isAssoc($formattedValue)) {

View File

@ -28,7 +28,7 @@ class Event extends AbstractAnnotation
public function __construct(...$value)
{
parent::__construct(...$value);
$this->bindMainProperty('event');
$this->bindMainProperty('event', $value);
}
public function collectMethod(string $className, ?string $target): void

View File

@ -27,7 +27,7 @@ class SocketIONamespace extends AbstractAnnotation
public function __construct(...$value)
{
parent::__construct(...$value);
$this->bindMainProperty('namespace');
$this->bindMainProperty('namespace', $value);
}
public function collectClass(string $className): void