This commit is contained in:
2021-04-25 11:52:31 +08:00
parent be8faa5cb0
commit 7cf69eff7e
3 changed files with 982 additions and 961 deletions
+1 -1
View File
@@ -119,7 +119,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
if (is_object($model)) { if (is_object($model)) {
return $model; return $model;
} }
return new $model(); return $model::populate([]);
} }
+1 -19
View File
@@ -27,7 +27,6 @@ use HttpServer\Http\Context;
use ReflectionException; use ReflectionException;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\TraitApplication; use Snowflake\Abstracts\TraitApplication;
use Snowflake\Channel;
use Snowflake\Event as SEvent; use Snowflake\Event as SEvent;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -140,25 +139,9 @@ 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
*/
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]);
// }
// }
}
/** /**
* @param $name * @param $name
@@ -679,7 +662,6 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
public function setRelate($name, $value) public function setRelate($name, $value)
{ {
var_dump($name, $value);
$this->_relate[$name] = $value; $this->_relate[$name] = $value;
} }
@@ -1024,7 +1006,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
public static function populate(array $data): static public static function populate(array $data): static
{ {
$model = new static(); $model = Snowflake::createObject(static::class);
$model->_attributes = $data; $model->_attributes = $data;
$model->setIsCreate(false); $model->setIsCreate(false);
$model->refresh(); $model->refresh();
+66 -27
View File
@@ -10,13 +10,12 @@ declare(strict_types=1);
namespace Snowflake\Di; namespace Snowflake\Di;
use Annotation\Inject; use Annotation\Inject;
use Annotation\Target; use Exception;
use Database\Connection;
use HttpServer\Http\HttpHeaders;
use ReflectionClass; use ReflectionClass;
use Snowflake\Abstracts\BaseObject;
use ReflectionException; use ReflectionException;
use Snowflake\Exception\ComponentException; use ReflectionMethod;
use ReflectionProperty;
use Snowflake\Abstracts\BaseObject;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -48,6 +47,10 @@ class Container extends BaseObject
*/ */
private array $_reflection = []; private array $_reflection = [];
private array $_property = [];
/** /**
* @var array * @var array
* *
@@ -63,6 +66,7 @@ class Container extends BaseObject
* @return mixed * @return mixed
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception
*/ */
public function get($class, $constrict = [], $config = []): mixed public function get($class, $constrict = [], $config = []): mixed
{ {
@@ -94,8 +98,9 @@ class Container extends BaseObject
* @return mixed * @return mixed
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception
*/ */
private function resolveDefinition($definition, $class, $config, $constrict) private function resolveDefinition($definition, $class, $config, $constrict): mixed
{ {
if (!isset($definition['class'])) { if (!isset($definition['class'])) {
throw new NotFindClassException($class); throw new NotFindClassException($class);
@@ -121,9 +126,7 @@ class Container extends BaseObject
* @param $config * @param $config
* *
* @return object * @return object
* @throws NotFindClassException * @throws Exception
* @throws ReflectionException
* @throws ComponentException
*/ */
private function resolve($class, $constrict, $config): object private function resolve($class, $constrict, $config): object
{ {
@@ -158,34 +161,45 @@ class Container extends BaseObject
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @param $object * @param $object
* @return mixed * @return mixed
* @throws NotFindClassException * @throws Exception
* @throws ReflectionException
* @throws ComponentException
*/ */
private function propertyInject(ReflectionClass $reflect, $object): mixed private function propertyInject(ReflectionClass $reflect, $object): mixed
{ {
$inject = $reflect->getProperties( if (!isset($this->_property[$reflect->getName()])) {
\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED | return $object;
\ReflectionProperty::IS_PUBLIC
);
foreach ($inject as $reflectionProperty) {
$attributes = $reflectionProperty->getAttributes(Inject::class);
foreach ($attributes as $attribute) {
/** @var Inject $class */
$class = $attribute->newInstance();
$class->execute([$object, $reflectionProperty->getName()]);
} }
foreach ($this->_property[$reflect->getName()] as $property => $inject) {
/** @var Inject $inject */
$inject->execute([$object, $property]);
} }
return $object; return $object;
} }
/**
* @param string $class
* @param string|null $property
* @return ReflectionProperty|array|null
*/
public function getClassProperty(string $class, string $property = null): ReflectionProperty|null|array
{
if (!isset($this->_property[$class])) {
return null;
}
$properties = $this->_property[$class];
if (!empty($property)) {
return $properties[$property] ?? null;
}
return $properties;
}
/** /**
* @param $object * @param $object
* @param $config * @param $config
* @return mixed * @return mixed
*/ */
private function onAfterInit($object, $config) private function onAfterInit($object, $config): mixed
{ {
Snowflake::configure($object, $config); Snowflake::configure($object, $config);
if (method_exists($object, 'afterInit')) { if (method_exists($object, 'afterInit')) {
@@ -198,8 +212,7 @@ class Container extends BaseObject
* @param $class * @param $class
* @param array $constrict * @param array $constrict
* @return array|null * @return array|null
* @throws NotFindClassException * @throws ReflectionException|NotFindClassException
* @throws ReflectionException
*/ */
private function resolveDependencies($class, $constrict = []): ?array private function resolveDependencies($class, $constrict = []): ?array
{ {
@@ -218,17 +231,43 @@ class Container extends BaseObject
if (!is_null($construct = $reflection->getConstructor())) { if (!is_null($construct = $reflection->getConstructor())) {
$constrict = $this->resolveMethodParam($construct); $constrict = $this->resolveMethodParam($construct);
} }
$this->scanProperty($reflection);
return [$reflection, $constrict]; return [$reflection, $constrict];
} }
/** /**
* @param \ReflectionMethod|null $method * @param ReflectionClass $reflectionClass
* @return $this
*/
private function scanProperty(ReflectionClass $reflectionClass): static
{
$lists = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED
);
$className = $reflectionClass->getName();
foreach ($lists as $list) {
$targets = $list->getAttributes(Inject::class);
if (count($targets)) {
continue;
}
$this->_property[$className][$list->getName()] = $targets[0]->newInstance();
}
return $this;
}
/**
* @param ReflectionMethod|null $method
* @return array * @return array
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function resolveMethodParam(?\ReflectionMethod $method): array private function resolveMethodParam(?ReflectionMethod $method): array
{ {
$array = []; $array = [];
foreach ($method->getParameters() as $key => $parameter) { foreach ($method->getParameters() as $key => $parameter) {