This commit is contained in:
2021-04-25 17:01:57 +08:00
parent 4b0e457318
commit c17647859a
5 changed files with 204 additions and 163 deletions
+167 -149
View File
@@ -16,185 +16,203 @@ class Annotation extends Component
{ {
private Loader $_loader; private Loader $_loader;
private array $_model_sets = []; private array $_model_sets = [];
private array $_model_gets = []; private array $_model_gets = [];
private array $_model_relate = []; private array $_model_relate = [];
/** /**
* @param string $class * @param string $class
* @param string $setName * @param string $setName
* @param string $method * @param string $method
*/ */
public function addSets(string $class, string $setName, string $method) public function addSets(string $class, string $setName, string $method)
{ {
$this->_model_sets[$class][$setName] = $method; $this->_model_sets[$class][$setName] = $method;
} }
/** /**
* @param string $class * @param string $class
* @param string $setName * @param string $setName
* @param string $method * @param string $method
*/ */
public function addGets(string $class, string $setName, string $method) public function addGets(string $class, string $setName, string $method)
{ {
$this->_model_gets[$class][$setName] = $method; $this->_model_gets[$class][$setName] = $method;
} }
/** /**
* @param string $class * @param string $class
* @param string $setName * @param string $setName
* @param string $method * @param string $method
*/ */
public function addRelate(string $class, string $setName, string $method) public function addRelate(string $class, string $setName, string $method)
{ {
$this->_model_relate[$class][$setName] = $method; $this->_model_relate[$class][$setName] = $method;
} }
/** /**
* @param string $class * @param $class
* @param string $setName * @return array
* @return mixed|null */
*/ public function getGets($class): array
public function getGetMethodName(string $class, string $setName): ?string {
{ return $this->_model_gets[$class] ?? [];
$gets = $this->_model_gets[$class] ?? $this->_model_relate[$class] ?? null; }
if ($gets == null) {
return null; /**
} * @param $class
return $gets[$setName] ?? null; * @return array
} */
public function getSets($class): array
{
return $this->_model_gets[$class] ?? [];
}
/** /**
* @param string $class * @param string $class
* @return array * @param string $setName
*/ * @return mixed|null
public function getModelMethods(string $class): array */
{ public function getGetMethodName(string $class, string $setName): ?string
return $this->_model_gets[$class] ?? []; {
} $gets = $this->_model_gets[$class] ?? $this->_model_relate[$class] ?? null;
if ($gets == null) {
return null;
}
return $gets[$setName] ?? null;
}
/** /**
* @param string $class * @param string $class
* @return array * @return array
*/ */
public function getRelateMethods(string $class): array public function getModelMethods(string $class): array
{ {
return $this->_model_relate[$class] ?? []; return $this->_model_gets[$class] ?? [];
} }
/**
/** * @param string $class
* @param string $class * @return array
* @param string $setName */
* @return mixed|null public function getRelateMethods(string $class): array
*/ {
public function getSetMethodName(string $class, string $setName): ?string return $this->_model_relate[$class] ?? [];
{ }
if (!isset($this->_model_sets[$class])) {
return null;
}
$lists = $this->_model_sets[$class];
if (isset($lists[$setName])) {
return $lists[$setName];
}
return null;
}
public function init(): void /**
{ * @param string $class
$this->_loader = new Loader(); * @param string $setName
} * @return mixed|null
*/
public function getSetMethodName(string $class, string $setName): ?string
{
if (!isset($this->_model_sets[$class])) {
return null;
}
$lists = $this->_model_sets[$class];
if (isset($lists[$setName])) {
return $lists[$setName];
}
return null;
}
/** public function init(): void
* @return Loader {
*/ $this->_loader = new Loader();
public function getLoader(): Loader }
{
return $this->_loader;
}
/** /**
* @param Loader $loader * @return Loader
* @return Loader */
*/ public function getLoader(): Loader
public function setLoader(Loader $loader): Loader {
{ return $this->_loader;
return $this->_loader = $loader; }
}
/** /**
* @param string $className * @param Loader $loader
* @param string $method * @return Loader
* @return array 根据类名获取注解 */
* 根据类名获取注解 public function setLoader(Loader $loader): Loader
*/ {
public function getMethods(string $className, string $method = ''): mixed return $this->_loader = $loader;
{ }
return $this->_loader->getMethod($className, $method);
}
/** /**
* @param object $class * @param string $className
*/ * @param string $method
public function injectProperty(object $class) * @return array 根据类名获取注解
{ * 根据类名获取注解
$this->_loader->injectProperty($class::class, $class); */
} public function getMethods(string $className, string $method = ''): mixed
{
return $this->_loader->getMethod($className, $method);
}
/** /**
* @param string $path * @param object $class
* @param string $namespace */
* @param string $alias public function injectProperty(object $class)
* @return void {
* @throws Exception $this->_loader->injectProperty($class::class, $class);
*/ }
public function read(string $path, string $namespace, string $alias = 'root'): void
{
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
}
/** /**
* @param string $dir * @param string $path
* @param string|array $outPath * @param string $namespace
* @throws Exception * @param string $alias
*/ * @return void
public function runtime(string $dir, string|array $outPath = '') * @throws Exception
{ */
if (empty($outPath)) { public function read(string $path, string $namespace, string $alias = 'root'): void
$outPath = []; {
} else if (is_string($outPath)) {
$outPath = [$outPath]; $this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
} }
$this->_loader->loadByDirectory($dir, $outPath);
}
/** /**
* @param string $filename * @param string $dir
* @return mixed * @param string|array $outPath
*/ * @throws Exception
public function getFilename(string $filename): mixed */
{ public function runtime(string $dir, string|array $outPath = '')
return $this->_loader->getClassByFilepath($filename); {
} if (empty($outPath)) {
$outPath = [];
} else if (is_string($outPath)) {
$outPath = [$outPath];
}
$this->_loader->loadByDirectory($dir, $outPath);
}
/**
* @param string $filename
* @return mixed
*/
public function getFilename(string $filename): mixed
{
return $this->_loader->getClassByFilepath($filename);
}
} }
+1 -1
View File
@@ -307,7 +307,7 @@ class ActiveRecord extends BaseActiveRecord
{ {
$data = $this->_attributes; $data = $this->_attributes;
$lists = Snowflake::getAnnotation()->getModelMethods(static::class); $lists = $this->getAnnotation(self::ANNOTATION_GET);
foreach ($lists as $key => $item) { foreach ($lists as $key => $item) {
$data[$key] = $this->{$item}($data[$key] ?? null); $data[$key] = $this->{$item}($data[$key] ?? null);
} }
+3 -4
View File
@@ -115,11 +115,10 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
*/ */
public function getModel(): ActiveRecord public function getModel(): ActiveRecord
{ {
$model = $this->model;; if (!is_object($this->model)) {
if (is_object($model)) { $this->model = $this->model::populate([]);
return $model;
} }
return $model::populate([]); return $this->model;
} }
+26 -4
View File
@@ -139,6 +139,11 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
} else { } else {
$this->_relation = Context::getContext(Relation::class); $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);
} }
@@ -809,7 +814,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
parent::__set($name, $value); parent::__set($name, $value);
return; return;
} }
$method = annotation()->getSetMethodName(static::class, $name); $method = $this->_get_annotation($name, self::ANNOTATION_SET);
if (!empty($method)) { if (!empty($method)) {
$value = $this->{$method}($value); $value = $this->{$method}($value);
} }
@@ -817,6 +822,22 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
} }
/**
* @param $name
* @param string $method
* @return string|null
*/
protected function _get_annotation(string $name = null, string $method = self::ANNOTATION_GET): ?string
{
$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;
}
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
@@ -840,9 +861,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
private function _gets(string $name, mixed $value): mixed private function _gets(string $name, mixed $value): mixed
{ {
$loader = Snowflake::app()->getAnnotation(); if (!empty($method = $this->_get_annotation($name))) {
$method = $loader->getGetMethodName(static::class, $name); return $this->{$method}(...[$value]);
if (empty($method)) { }
if (empty($method = $this->_get_annotation())) {
return $this->_decode($name, $value); return $this->_decode($name, $value);
} }
$_value = $this->{$method}(...[$value]); $_value = $this->{$method}(...[$value]);
+7 -5
View File
@@ -8,10 +8,6 @@ namespace Database\Base;
use Database\ActiveQuery; use Database\ActiveQuery;
use Database\ActiveRecord; use Database\ActiveRecord;
use Exception; use Exception;
use ReflectionException;
use Snowflake\Channel;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/** /**
@@ -28,6 +24,9 @@ class CollectionIterator extends \ArrayIterator
private ActiveQuery $query; private ActiveQuery $query;
private ?ActiveRecord $_clone = null;
public function clean() public function clean()
{ {
unset($this->query); unset($this->query);
@@ -57,7 +56,10 @@ class CollectionIterator extends \ArrayIterator
*/ */
protected function newModel($current): ActiveRecord protected function newModel($current): ActiveRecord
{ {
return $this->model::populate($current); $model = clone $this->model;
$model->setAttributes($current);
$model->refresh();
return $model;
} }