From fb01ab3aff55fcf04c9fcbcadc0be7d534d46ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 27 Apr 2021 15:02:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Annotation/Annotation.php | 31 +++++----- Database/ActiveRecord.php | 6 +- Database/Base/BaseActiveRecord.php | 97 ++++++++++++------------------ 3 files changed, 54 insertions(+), 80 deletions(-) diff --git a/Annotation/Annotation.php b/Annotation/Annotation.php index b1aca7d0..dd08265c 100644 --- a/Annotation/Annotation.php +++ b/Annotation/Annotation.php @@ -77,36 +77,33 @@ class Annotation extends Component /** * @param string $class - * @param string $setName - * @return mixed|null + * @param string|null $setName + * @return array|string|null */ - public function getGetMethodName(string $class, string $setName): ?string + public function getGetMethodName(string $class, string $setName = null): array|null|string { - $gets = $this->_model_gets[$class] ?? $this->_model_relate[$class] ?? null; + $gets = $this->_model_gets[$class] ?? null; if ($gets == null) { return null; } + if (empty($setName)) return $gets; return $gets[$setName] ?? null; } /** * @param string $class - * @return array + * @param string|null $method + * @return array|string|null */ - public function getModelMethods(string $class): array + public function getRelateMethods(string $class, string $method = null): array|null|string { - return $this->_model_gets[$class] ?? []; - } - - - /** - * @param string $class - * @return array - */ - public function getRelateMethods(string $class): array - { - return $this->_model_relate[$class] ?? []; + $gets = $this->_model_relate[$class] ?? null; + if ($gets == null) { + return null; + } + if (empty($method)) return $gets; + return $gets[$method] ?? null; } diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index 5581c92d..4539dc02 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -307,14 +307,14 @@ class ActiveRecord extends BaseActiveRecord { $data = $this->_attributes; - $lists = $this->getAnnotation(self::ANNOTATION_GET); + $lists = $this->getAnnotation(self::GET); foreach ($lists as $key => $item) { $data[$key] = $this->{$item}($data[$key] ?? null); } $data = array_merge($data, $this->runRelate()); - $class = Snowflake::app()->getChannel(); - $class->push($this, static::class); +// $class = Snowflake::app()->getChannel(); +// $class->push($this, static::class); return $data; } diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index 8052a138..1bc854f6 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -52,8 +52,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess const BEFORE_SAVE = 'before::save'; - const ANNOTATION_GET = 'get'; - const ANNOTATION_SET = 'set'; + const GET = 'get'; + const SET = 'set'; + const RELATE = 'RELATE'; /** @var array */ protected array $_attributes = []; @@ -139,11 +140,6 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess } else { $this->_relation = Context::getContext(Relation::class); } - $annotation = Snowflake::app()->getAnnotation(); - $this->_annotations[self::ANNOTATION_GET] = $annotation->getGets(static::class); - $this->_annotations[self::ANNOTATION_SET] = $annotation->getSets(static::class); - $this->_relate = $annotation->getRelateMethods(static::class); - } @@ -154,10 +150,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public function addGets($name, $method): static { - if (!isset($this->_annotations[self::ANNOTATION_GET])) { - $this->_annotations[self::ANNOTATION_GET] = []; + if (!isset($this->_annotations[self::GET])) { + $this->_annotations[self::GET] = []; } - $this->_annotations[self::ANNOTATION_GET][$name] = [$this, $method]; + $this->_annotations[self::GET][$name] = [$this, $method]; return $this; } @@ -169,10 +165,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public function addSets($name, $method): static { - if (!isset($this->_annotations[self::ANNOTATION_SET])) { - $this->_annotations[self::ANNOTATION_SET] = []; + if (!isset($this->_annotations[self::SET])) { + $this->_annotations[self::SET] = []; } - $this->_annotations[self::ANNOTATION_SET][$name] = [$this, $method]; + $this->_annotations[self::SET][$name] = [$this, $method]; return $this; } @@ -611,7 +607,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess * @param string $type * @return mixed */ - protected function runAnnotation(string $name, mixed $value, string $type = self::ANNOTATION_GET): mixed + protected function runAnnotation(string $name, mixed $value, string $type = self::GET): mixed { return call_user_func($this->_annotations[$type][$name], $value); } @@ -713,15 +709,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess /** * @param $name - * @return mixed + * @return array|string|null * @throws Exception */ - public function getRelate($name): mixed + public function getRelate($name): null|array|string { - if (empty($this->_relate[$name])) { - $this->_relate = Snowflake::getAnnotation()->getRelateMethods(static::class); - } - return $this->_relate[$name] ?? null; + return Snowflake::getAnnotation()->getRelateMethods(static::class, $name); } @@ -814,7 +807,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess parent::__set($name, $value); return; } - $method = $this->_get_annotation($name, self::ANNOTATION_SET); + $method = $this->_get_annotation($name, self::SET); if (!empty($method)) { $value = $this->{$method}($value); } @@ -828,21 +821,16 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess * @return string|null * @throws Exception */ - protected function _get_annotation(string $name = null, string $method = self::ANNOTATION_GET): ?string + protected function _get_annotation(string $name = null, string $method = self::GET): ?string { $annotation = Snowflake::app()->getAnnotation(); - if (!isset($this->_annotations[self::ANNOTATION_GET])) { - $this->_annotations[self::ANNOTATION_GET] = $annotation->getGets(static::class); + if ($method == static::SET) { + return $annotation->getSetMethodName(static::class, $name); } - if (!isset($this->_annotations[self::ANNOTATION_SET])) { - $this->_annotations[self::ANNOTATION_SET] = $annotation->getSets(static::class); + if ($method == static::GET) { + return $annotation->getGetMethodName(static::class, $name); } - $matches = match ($method) { - self::ANNOTATION_GET => $this->_annotations[self::ANNOTATION_GET], - self::ANNOTATION_SET => $this->_annotations[self::ANNOTATION_SET], - default => $this->_relate - }; - return $matches[$name] ?? null; + return $annotation->getRelateMethods(static::class, $name); } @@ -853,33 +841,21 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public function __get($name): mixed { - if (isService($name)) { - return Snowflake::getApp($name); - } $value = $this->_attributes[$name] ?? null; - return $this->_gets($name, $value); - } - - - /** - * @param string $name - * @param mixed $value - * @return mixed - * @throws Exception - */ - private function _gets(string $name, mixed $value): mixed - { - if (!empty($method = $this->_get_annotation($name))) { + $method = $this->_get_annotation($name, static::GET); + if (!empty($method)) { return $this->{$method}(...[$value]); } - if (empty($method = $this->_get_annotation())) { + + $relation = $this->_get_annotation($name, static::RELATE); + if (empty($relation)) { return $this->_decode($name, $value); } - $_value = $this->{$method}(...[$value]); - if ($_value instanceof HasBase) { - return $_value->get(); + + if (($value = $this->{$relation}(...[$value])) instanceof HasBase) { + return $value->get(); } - return $_value; + return $value; } @@ -913,7 +889,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess * @param string $type * @return array */ - protected function getAnnotation($type = self::ANNOTATION_GET): array + protected function getAnnotation($type = self::GET): array { return $this->_annotations[$type] ?? []; } @@ -924,7 +900,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess * @param string $type * @return bool */ - protected function hasAnnotation($name, $type = self::ANNOTATION_GET): bool + protected function hasAnnotation($name, $type = self::GET): bool { if (!isset($this->_annotations[$type])) { return false; @@ -1060,11 +1036,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public static function populate(array $data): static { - $class = Snowflake::app()->getChannel(); - /** @var static $model */ - $model = $class->pop(static::class, function () { - return new static(); - }); +// $class = Snowflake::app()->getChannel(); +// /** @var static $model */ +// $model = $class->pop(static::class, function () { +// return new static(); +// }); + $model = new static(); $model->_attributes = $data; $model->_oldAttributes = $data; $model->setIsCreate(false);