This commit is contained in:
2021-03-29 16:17:22 +08:00
parent 8bc0971f25
commit ce0ba186a7
5 changed files with 88 additions and 11 deletions
-1
View File
@@ -42,7 +42,6 @@ use Snowflake\Snowflake;
$handler[1] = new ReflectionProperty($handler[0], $handler[1]); $handler[1] = new ReflectionProperty($handler[0], $handler[1]);
} }
var_dump(get_class($handler[0]) . '::' . $handler[1]);
/** @var ReflectionProperty $handler [1] */ /** @var ReflectionProperty $handler [1] */
if ($handler[1]->isPrivate() || $handler[1]->isProtected()) { if ($handler[1]->isPrivate() || $handler[1]->isProtected()) {
$method = 'set' . ucfirst($handler[1]->getName()); $method = 'set' . ucfirst($handler[1]->getName());
+5 -2
View File
@@ -10,9 +10,11 @@ declare(strict_types=1);
namespace Database\Base; namespace Database\Base;
use Annotation\Aspect;
use Annotation\Event; use Annotation\Event;
use Annotation\Inject; use Annotation\Inject;
use ArrayAccess; use ArrayAccess;
use Database\InjectProperty;
use Database\SqlBuilder; use Database\SqlBuilder;
use Database\Traits\HasBase; use Database\Traits\HasBase;
use HttpServer\Http\Context; use HttpServer\Http\Context;
@@ -136,6 +138,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
/** /**
* @throws Exception * @throws Exception
*/ */
#[Aspect(InjectProperty::class)]
public function init() public function init()
{ {
if (!Context::hasContext(Relation::class)) { if (!Context::hasContext(Relation::class)) {
@@ -144,14 +147,13 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
} else { } else {
$this->_relation = Context::getContext(Relation::class); $this->_relation = Context::getContext(Relation::class);
} }
$this->createAnnotation();
} }
/** /**
* @throws Exception * @throws Exception
*/ */
private function createAnnotation() public function createAnnotation()
{ {
$annotation = Snowflake::app()->getAttributes(); $annotation = Snowflake::app()->getAttributes();
$methods = $annotation->getMethods(get_called_class()); $methods = $annotation->getMethods(get_called_class());
@@ -160,6 +162,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
$attribute->execute([$this, $method]); $attribute->execute([$this, $method]);
} }
} }
$annotation->injectProperty($this);
} }
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace Database;
use JetBrains\PhpStorm\Pure;
use Snowflake\IAspect;
/**
* Class InjectProperty
* @package Database
*/
class InjectProperty implements IAspect
{
private string $className = '';
private string $methodName = '';
/**
* InjectProperty constructor.
* @param array $handler
*/
public function __construct(public array $handler)
{
}
/**
* @return mixed
*/
public function invoke(): mixed
{
$data = call_user_func($this->handler, func_get_args());
$this->handler[0]->createAnnotation();
echo 'inject property.' . PHP_EOL;
return $data;
}
}
+31
View File
@@ -13,6 +13,7 @@ use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use ReflectionException; use ReflectionException;
use Snowflake\Aop;
use Snowflake\Error\Logger; use Snowflake\Error\Logger;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
@@ -145,6 +146,36 @@ class BaseObject implements Configure
} }
/**
* @param string $name
* @param array $arguments
* @return mixed
* @throws Exception
*/
public function __call(string $name, array $arguments): mixed
{
if (!Snowflake::app()->has('aop')) {
return call_user_func([$this, $name], $arguments);
}
return \aop([$this, $name], $arguments);
}
/**
* @param string $name
* @param array $arguments
* @return mixed
* @throws Exception
*/
public static function __callStatic(string $name, array $arguments): mixed
{
if (!Snowflake::app()->has('aop')) {
return call_user_func([get_called_class(), $name], $arguments);
}
return \aop([get_called_class(), $name], $arguments);
}
/** /**
* @param mixed $message * @param mixed $message
* @param string $method * @param string $method
+7 -8
View File
@@ -4,6 +4,7 @@
namespace Snowflake\Error; namespace Snowflake\Error;
use JetBrains\PhpStorm\Pure;
use Snowflake\IAspect; use Snowflake\IAspect;
@@ -22,26 +23,24 @@ class LoggerAspect implements IAspect
* LoggerAspect constructor. * LoggerAspect constructor.
* @param array $handler * @param array $handler
*/ */
public function __construct(public array $handler) #[Pure] public function __construct(public array $handler)
{ {
$this->className = get_class($this->handler[0]); $this->className = get_class($this->handler[0]);
$this->methodName = $this->handler[1]; $this->methodName = $this->handler[1];
} }
/** /**
* @return mixed|void * @return mixed
*/ */
public function invoke() public function invoke(): mixed
{ {
$startTime = microtime(true); $startTime = microtime(true);
$data = call_user_func($this->handler, func_get_args()); $data = call_user_func($this->handler, func_get_args());
$this->print_runtime($startTime); $this->print_runtime($startTime);
if ($data === null) {
return;
}
return $data; return $data;
} }