modify
This commit is contained in:
@@ -19,6 +19,74 @@ class Annotation extends Component
|
||||
private Loader $_loader;
|
||||
|
||||
|
||||
private array $_model_sets = [];
|
||||
private array $_model_gets = [];
|
||||
private array $_model_relate = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $setName
|
||||
* @param string $method
|
||||
*/
|
||||
public function addSets(string $class, string $setName, string $method)
|
||||
{
|
||||
$this->_model_sets[$class . '::' . $setName] = $method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $setName
|
||||
* @param string $method
|
||||
*/
|
||||
public function addGets(string $class, string $setName, string $method)
|
||||
{
|
||||
$this->_model_gets[$class . '::' . $setName] = $method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $setName
|
||||
* @param string $method
|
||||
*/
|
||||
public function addRelate(string $class, string $setName, string $method)
|
||||
{
|
||||
$this->_model_relate[$class . '::' . $setName] = $method;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $setName
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getGetMethodName(string $class, string $setName): ?string
|
||||
{
|
||||
if (isset($this->_model_relate[$class . '::' . $setName])) {
|
||||
return $this->_model_relate[$class . '::' . $setName];
|
||||
}
|
||||
if (isset($this->_model_gets[$class . '::' . $setName])) {
|
||||
return $this->_model_gets[$class . '::' . $setName];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $setName
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getSetMethodName(string $class, string $setName): ?string
|
||||
{
|
||||
if (isset($this->_model_sets[$class . '::' . $setName])) {
|
||||
return $this->_model_relate[$class . '::' . $setName];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function init(): void
|
||||
{
|
||||
$this->_loader = new Loader();
|
||||
|
||||
+16
-1
@@ -4,6 +4,9 @@
|
||||
namespace Annotation;
|
||||
|
||||
|
||||
use Annotation\Model\Get;
|
||||
use Annotation\Model\Relation;
|
||||
use Annotation\Model\Set;
|
||||
use Attribute;
|
||||
use DirectoryIterator;
|
||||
use Exception;
|
||||
@@ -394,22 +397,34 @@ class Loader extends BaseObject
|
||||
if (empty($classes)) {
|
||||
return;
|
||||
}
|
||||
$annotation = Snowflake::app()->getAnnotation();
|
||||
foreach ($classes as $className) {
|
||||
$annotations = $this->_classes[$className] ?? null;
|
||||
if ($annotations === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$class = Snowflake::createObject($annotations['handler']);
|
||||
foreach ($annotations['target'] ?? [] as $value) {
|
||||
$value->execute([$class]);
|
||||
}
|
||||
|
||||
$isGet = $annotations instanceof Get;
|
||||
$isSet = $annotations instanceof Set;
|
||||
$isRelate = $annotations instanceof Relation;
|
||||
foreach ($annotations['methods'] as $name => $attribute) {
|
||||
foreach ($attribute as $value) {
|
||||
if ($isGet) {
|
||||
$annotation->addGets($annotations['handler'], $value->name, $name);
|
||||
} else if ($isSet) {
|
||||
$annotation->addSets($annotations['handler'], $value->name, $name);
|
||||
} else if ($isRelate) {
|
||||
$annotation->addRelate($annotations['handler'], $value->name, $name);
|
||||
} else {
|
||||
$value->execute([$class, $name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -134,6 +134,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->event = Snowflake::app()->getEvent();
|
||||
if (!Context::hasContext(Relation::class)) {
|
||||
$relation = Snowflake::createObject(Relation::class);
|
||||
$this->_relation = Context::setContext(Relation::class, $relation);
|
||||
@@ -149,14 +150,14 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
*/
|
||||
public function createAnnotation()
|
||||
{
|
||||
$annotation = Snowflake::getAnnotation();
|
||||
$annotation->injectProperty($this);
|
||||
$methods = $annotation->getMethods(get_called_class());
|
||||
foreach ($methods as $method => $attributes) {
|
||||
foreach ($attributes as $attribute) {
|
||||
$attribute->execute([$this, $method]);
|
||||
}
|
||||
}
|
||||
// $annotation = Snowflake::getAnnotation();
|
||||
// $annotation->injectProperty($this);
|
||||
// $methods = $annotation->getMethods(get_called_class());
|
||||
// foreach ($methods as $method => $attributes) {
|
||||
// foreach ($attributes as $attribute) {
|
||||
// $attribute->execute([$this, $method]);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@@ -833,11 +834,11 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
parent::__set($name, $value);
|
||||
return;
|
||||
}
|
||||
if ($this->hasAnnotation($name, 'set')) {
|
||||
$this->runAnnotation($name, $value, self::ANNOTATION_SET);
|
||||
} else {
|
||||
$this->_attributes[$name] = $value;
|
||||
$method = annotation()->getSetMethodName(get_called_class(), $name);
|
||||
if (!empty($method)) {
|
||||
$value = $this->{$method}($value);
|
||||
}
|
||||
$this->_attributes[$name] = $value;
|
||||
}
|
||||
|
||||
|
||||
@@ -849,15 +850,14 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
public function __get($name): mixed
|
||||
{
|
||||
$value = $this->_attributes[$name] ?? null;
|
||||
if ($this->hasAnnotation($name)) {
|
||||
return $this->runAnnotation($name, $value);
|
||||
|
||||
$loader = Snowflake::app()->getAnnotation();
|
||||
if (!empty($method = $loader->getGetMethodName(get_called_class(), $name))) {
|
||||
return $this->{$method}();
|
||||
}
|
||||
if (array_key_exists($name, $this->_attributes)) {
|
||||
return static::getColumns()->_decode($name, $value);
|
||||
}
|
||||
if (in_array($name, $this->_with)) {
|
||||
return $this->with($name);
|
||||
}
|
||||
if (Snowflake::app()->has($name)) {
|
||||
return Snowflake::app()->get($name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user