diff --git a/Annotation/Model/Get.php b/Annotation/Model/Get.php index 50155263..91a67a19 100644 --- a/Annotation/Model/Get.php +++ b/Annotation/Model/Get.php @@ -5,6 +5,7 @@ namespace Annotation\Model; use Attribute; +use Database\ActiveRecord; /** diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index c56afd82..7df4f62b 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -311,40 +311,14 @@ class ActiveRecord extends BaseActiveRecord */ public function toArray(): array { - $attributes = Snowflake::app()->getAttributes(); - $callback = $attributes->getByClass(static::class); - $data = $this->_attributes; - foreach ($callback as $key => $item) { - $data = $this->resolveAttributes($item, $data); + foreach ($this->getAnnotation() as $key => $item) { + $data[$key] = call_user_func($item, $data[$key]); } - return array_merge($data, $this->runRelate()); } - /** - * @param $item - * @param $data - * @return array - */ - private function resolveAttributes($item, $data): array - { - if (!isset($item['attributes'])) { - return $data; - } - foreach ($item['attributes'] as $attribute) { - if (!($attribute instanceof Get)) { - continue; - } - $name = $attribute->name; - $result = call_user_func($item['handler'], $data[$name]); - $data[$name] = $result; - } - return $data; - } - - /** * @return array * @throws Exception diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index d642826c..df45ec90 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Database\Base; +use Annotation\Model\Get; use HttpServer\Http\Context; use ReflectionException; use Snowflake\Abstracts\Component; @@ -53,6 +54,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess /** @var null|string */ protected ?string $primary = NULL; + + private array $_annotations = []; + /** * @var bool */ @@ -73,8 +77,34 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess } else { $this->_relation = Context::getContext(Relation::class); } + $this->parseAnnotation(); } + + /** + * @throws ComponentException + */ + private function parseAnnotation(): void + { + $attributes = Snowflake::app()->getAttributes(); + $annotations = $attributes->getByClass(static::class); + + $response = []; + foreach ($annotations as $annotation) { + if (!isset($annotation['attributes'])) { + continue; + } + foreach ($annotation['attributes'] as $attribute) { + if (!($attribute instanceof Get)) { + continue; + } + $response[$attribute->name] = $attribute['handler']; + } + } + $this->_annotations = $response; + } + + /** * @param string $column * @param int $value @@ -616,7 +646,6 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess /** * @param $attributes * @param $changeAttributes - * @return mixed * @throws Exception */ public function afterSave($attributes, $changeAttributes): void @@ -666,32 +695,52 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess */ public function __get($name): mixed { - $attributes = Snowflake::app()->getAttributes(); - $callback = $attributes->getByClass(static::class, $name); - var_dump($callback); - - $method = 'get' . ucfirst($name); - if (method_exists($this, $method . 'Attribute')) { - return $this->{$method . 'Attribute'}($this->_attributes[$name] ?? null); + $value = $this->_attributes[$name] ?? null; + if ($this->hasAnnotation($name)) { + return call_user_func($this->_annotations[$name], $value); } - - if (isset($this->_attributes[$name]) || array_key_exists($name, $this->_attributes)) { - return static::getColumns()->_decode($name, $this->_attributes[$name]); + if (array_key_exists($name, $this->_attributes)) { + return static::getColumns()->_decode($name, $value); } - if (isset($this->_relate[$name])) { $gets = $this->{$this->_relate[$name]}(); - } else if (method_exists($this, $method)) { - $gets = $this->$method(); } - if (isset($gets)) { return $this->resolveClass($gets); } - return parent::__get($name); } + + /** + * @return array + */ + protected function getAnnotation(): array + { + return $this->_annotations; + } + + + /** + * @param $name + * @return bool + */ + protected function hasAnnotation($name): bool + { + return isset($this->_annotations[$name]); + } + + + /** + * @param $item + * @param $data + * @return array + */ + protected function resolveAttributes($item, $data): array + { + return call_user_func($item, $data); + } + /** * @param $name * @return bool diff --git a/HttpServer/Server.php b/HttpServer/Server.php index 43153b19..37efdb41 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -97,6 +97,7 @@ class Server extends Application * @param array $configs * @return Packet|Websocket|Receive|Http|null * @throws ConfigException + * @throws Exception */ public function initCore(array $configs): Packet|Websocket|Receive|Http|null {