modify
This commit is contained in:
+139
-71
@@ -16,92 +16,160 @@ class Annotation extends Component
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
private Loader $_loader;
|
private Loader $_loader;
|
||||||
|
|
||||||
|
|
||||||
public function init(): void
|
private array $_model_sets = [];
|
||||||
{
|
private array $_model_gets = [];
|
||||||
$this->_loader = new Loader();
|
private array $_model_relate = [];
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Loader
|
* @param string $class
|
||||||
*/
|
* @param string $setName
|
||||||
public function getLoader(): Loader
|
* @param string $method
|
||||||
{
|
*/
|
||||||
return $this->_loader;
|
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 Loader $loader
|
* @param string $class
|
||||||
* @return Loader
|
* @param string $setName
|
||||||
*/
|
* @return mixed|null
|
||||||
public function setLoader(Loader $loader): Loader
|
*/
|
||||||
{
|
public function getGetMethodName(string $class, string $setName): ?string
|
||||||
return $this->_loader = $loader;
|
{
|
||||||
}
|
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 $className
|
/**
|
||||||
* @param string $method
|
* @param string $class
|
||||||
* @return array 根据类名获取注解
|
* @param string $setName
|
||||||
* 根据类名获取注解
|
* @return mixed|null
|
||||||
*/
|
*/
|
||||||
public function getMethods(string $className, string $method = ''): mixed
|
public function getSetMethodName(string $class, string $setName): ?string
|
||||||
{
|
{
|
||||||
return $this->_loader->getMethod($className, $method);
|
if (isset($this->_model_sets[$class . '::' . $setName])) {
|
||||||
}
|
return $this->_model_relate[$class . '::' . $setName];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
public function init(): void
|
||||||
* @param object $class
|
{
|
||||||
*/
|
$this->_loader = new Loader();
|
||||||
public function injectProperty(object $class)
|
}
|
||||||
{
|
|
||||||
$this->_loader->injectProperty(get_class($class), $class);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $path
|
* @return Loader
|
||||||
* @param string $namespace
|
*/
|
||||||
* @param string $alias
|
public function getLoader(): Loader
|
||||||
* @return void
|
{
|
||||||
* @throws Exception
|
return $this->_loader;
|
||||||
*/
|
}
|
||||||
public function read(string $path, string $namespace, string $alias = 'root'): void
|
|
||||||
{
|
|
||||||
|
|
||||||
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $dir
|
* @param Loader $loader
|
||||||
* @param string|array $outPath
|
* @return Loader
|
||||||
* @throws Exception
|
*/
|
||||||
*/
|
public function setLoader(Loader $loader): Loader
|
||||||
public function runtime(string $dir, string|array $outPath = '')
|
{
|
||||||
{
|
return $this->_loader = $loader;
|
||||||
if (empty($outPath)) {
|
}
|
||||||
$outPath = [];
|
|
||||||
} else if (is_string($outPath)) {
|
|
||||||
$outPath = [$outPath];
|
|
||||||
}
|
|
||||||
$this->_loader->loadByDirectory($dir, $outPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $filename
|
* @param string $className
|
||||||
* @return mixed
|
* @param string $method
|
||||||
*/
|
* @return array 根据类名获取注解
|
||||||
public function getFilename(string $filename): mixed
|
* 根据类名获取注解
|
||||||
{
|
*/
|
||||||
return $this->_loader->getClassByFilepath($filename);
|
public function getMethods(string $className, string $method = ''): mixed
|
||||||
}
|
{
|
||||||
|
return $this->_loader->getMethod($className, $method);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param object $class
|
||||||
|
*/
|
||||||
|
public function injectProperty(object $class)
|
||||||
|
{
|
||||||
|
$this->_loader->injectProperty(get_class($class), $class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param string $namespace
|
||||||
|
* @param string $alias
|
||||||
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function read(string $path, string $namespace, string $alias = 'root'): void
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $dir
|
||||||
|
* @param string|array $outPath
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function runtime(string $dir, string|array $outPath = '')
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-2
@@ -4,6 +4,9 @@
|
|||||||
namespace Annotation;
|
namespace Annotation;
|
||||||
|
|
||||||
|
|
||||||
|
use Annotation\Model\Get;
|
||||||
|
use Annotation\Model\Relation;
|
||||||
|
use Annotation\Model\Set;
|
||||||
use Attribute;
|
use Attribute;
|
||||||
use DirectoryIterator;
|
use DirectoryIterator;
|
||||||
use Exception;
|
use Exception;
|
||||||
@@ -394,19 +397,31 @@ class Loader extends BaseObject
|
|||||||
if (empty($classes)) {
|
if (empty($classes)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
$annotation = Snowflake::app()->getAnnotation();
|
||||||
foreach ($classes as $className) {
|
foreach ($classes as $className) {
|
||||||
$annotations = $this->_classes[$className] ?? null;
|
$annotations = $this->_classes[$className] ?? null;
|
||||||
if ($annotations === null) {
|
if ($annotations === null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$class = Snowflake::createObject($annotations['handler']);
|
$class = Snowflake::createObject($annotations['handler']);
|
||||||
foreach ($annotations['target'] ?? [] as $value) {
|
foreach ($annotations['target'] ?? [] as $value) {
|
||||||
$value->execute([$class]);
|
$value->execute([$class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$isGet = $annotations instanceof Get;
|
||||||
|
$isSet = $annotations instanceof Set;
|
||||||
|
$isRelate = $annotations instanceof Relation;
|
||||||
foreach ($annotations['methods'] as $name => $attribute) {
|
foreach ($annotations['methods'] as $name => $attribute) {
|
||||||
foreach ($attribute as $value) {
|
foreach ($attribute as $value) {
|
||||||
$value->execute([$class, $name]);
|
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()
|
public function init()
|
||||||
{
|
{
|
||||||
|
$this->event = Snowflake::app()->getEvent();
|
||||||
if (!Context::hasContext(Relation::class)) {
|
if (!Context::hasContext(Relation::class)) {
|
||||||
$relation = Snowflake::createObject(Relation::class);
|
$relation = Snowflake::createObject(Relation::class);
|
||||||
$this->_relation = Context::setContext(Relation::class, $relation);
|
$this->_relation = Context::setContext(Relation::class, $relation);
|
||||||
@@ -149,14 +150,14 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function createAnnotation()
|
public function createAnnotation()
|
||||||
{
|
{
|
||||||
$annotation = Snowflake::getAnnotation();
|
// $annotation = Snowflake::getAnnotation();
|
||||||
$annotation->injectProperty($this);
|
// $annotation->injectProperty($this);
|
||||||
$methods = $annotation->getMethods(get_called_class());
|
// $methods = $annotation->getMethods(get_called_class());
|
||||||
foreach ($methods as $method => $attributes) {
|
// foreach ($methods as $method => $attributes) {
|
||||||
foreach ($attributes as $attribute) {
|
// foreach ($attributes as $attribute) {
|
||||||
$attribute->execute([$this, $method]);
|
// $attribute->execute([$this, $method]);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -833,11 +834,11 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
parent::__set($name, $value);
|
parent::__set($name, $value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ($this->hasAnnotation($name, 'set')) {
|
$method = annotation()->getSetMethodName(get_called_class(), $name);
|
||||||
$this->runAnnotation($name, $value, self::ANNOTATION_SET);
|
if (!empty($method)) {
|
||||||
} else {
|
$value = $this->{$method}($value);
|
||||||
$this->_attributes[$name] = $value;
|
|
||||||
}
|
}
|
||||||
|
$this->_attributes[$name] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -849,15 +850,14 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
|||||||
public function __get($name): mixed
|
public function __get($name): mixed
|
||||||
{
|
{
|
||||||
$value = $this->_attributes[$name] ?? null;
|
$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)) {
|
if (array_key_exists($name, $this->_attributes)) {
|
||||||
return static::getColumns()->_decode($name, $value);
|
return static::getColumns()->_decode($name, $value);
|
||||||
}
|
}
|
||||||
if (in_array($name, $this->_with)) {
|
|
||||||
return $this->with($name);
|
|
||||||
}
|
|
||||||
if (Snowflake::app()->has($name)) {
|
if (Snowflake::app()->has($name)) {
|
||||||
return Snowflake::app()->get($name);
|
return Snowflake::app()->get($name);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user