改名
This commit is contained in:
@@ -5,6 +5,7 @@ namespace Annotation\Model;
|
|||||||
|
|
||||||
|
|
||||||
use Attribute;
|
use Attribute;
|
||||||
|
use Database\ActiveRecord;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -311,40 +311,14 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
*/
|
*/
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
$attributes = Snowflake::app()->getAttributes();
|
|
||||||
$callback = $attributes->getByClass(static::class);
|
|
||||||
|
|
||||||
$data = $this->_attributes;
|
$data = $this->_attributes;
|
||||||
foreach ($callback as $key => $item) {
|
foreach ($this->getAnnotation() as $key => $item) {
|
||||||
$data = $this->resolveAttributes($item, $data);
|
$data[$key] = call_user_func($item, $data[$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_merge($data, $this->runRelate());
|
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
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ declare(strict_types=1);
|
|||||||
namespace Database\Base;
|
namespace Database\Base;
|
||||||
|
|
||||||
|
|
||||||
|
use Annotation\Model\Get;
|
||||||
use HttpServer\Http\Context;
|
use HttpServer\Http\Context;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Snowflake\Abstracts\Component;
|
use Snowflake\Abstracts\Component;
|
||||||
@@ -53,6 +54,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
|
|||||||
/** @var null|string */
|
/** @var null|string */
|
||||||
protected ?string $primary = NULL;
|
protected ?string $primary = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
private array $_annotations = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
@@ -73,8 +77,34 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
|
|||||||
} else {
|
} else {
|
||||||
$this->_relation = Context::getContext(Relation::class);
|
$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 string $column
|
||||||
* @param int $value
|
* @param int $value
|
||||||
@@ -616,7 +646,6 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
|
|||||||
/**
|
/**
|
||||||
* @param $attributes
|
* @param $attributes
|
||||||
* @param $changeAttributes
|
* @param $changeAttributes
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function afterSave($attributes, $changeAttributes): void
|
public function afterSave($attributes, $changeAttributes): void
|
||||||
@@ -666,32 +695,52 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function __get($name): mixed
|
public function __get($name): mixed
|
||||||
{
|
{
|
||||||
$attributes = Snowflake::app()->getAttributes();
|
$value = $this->_attributes[$name] ?? null;
|
||||||
$callback = $attributes->getByClass(static::class, $name);
|
if ($this->hasAnnotation($name)) {
|
||||||
var_dump($callback);
|
return call_user_func($this->_annotations[$name], $value);
|
||||||
|
|
||||||
$method = 'get' . ucfirst($name);
|
|
||||||
if (method_exists($this, $method . 'Attribute')) {
|
|
||||||
return $this->{$method . 'Attribute'}($this->_attributes[$name] ?? null);
|
|
||||||
}
|
}
|
||||||
|
if (array_key_exists($name, $this->_attributes)) {
|
||||||
if (isset($this->_attributes[$name]) || array_key_exists($name, $this->_attributes)) {
|
return static::getColumns()->_decode($name, $value);
|
||||||
return static::getColumns()->_decode($name, $this->_attributes[$name]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->_relate[$name])) {
|
if (isset($this->_relate[$name])) {
|
||||||
$gets = $this->{$this->_relate[$name]}();
|
$gets = $this->{$this->_relate[$name]}();
|
||||||
} else if (method_exists($this, $method)) {
|
|
||||||
$gets = $this->$method();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($gets)) {
|
if (isset($gets)) {
|
||||||
return $this->resolveClass($gets);
|
return $this->resolveClass($gets);
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent::__get($name);
|
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
|
* @param $name
|
||||||
* @return bool
|
* @return bool
|
||||||
|
|||||||
@@ -97,6 +97,7 @@ class Server extends Application
|
|||||||
* @param array $configs
|
* @param array $configs
|
||||||
* @return Packet|Websocket|Receive|Http|null
|
* @return Packet|Websocket|Receive|Http|null
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function initCore(array $configs): Packet|Websocket|Receive|Http|null
|
public function initCore(array $configs): Packet|Websocket|Receive|Http|null
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user