This commit is contained in:
2021-04-25 16:16:13 +08:00
parent d331ce4349
commit 05cd3dde9e
3 changed files with 54 additions and 18 deletions
+33 -12
View File
@@ -824,19 +824,44 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
public function __get($name): mixed public function __get($name): mixed
{ {
if (isService($name)) {
return Snowflake::getApp($name);
}
$value = $this->_attributes[$name] ?? null; $value = $this->_attributes[$name] ?? null;
return $this->_gets($name, $value);
}
/**
* @param string $name
* @param mixed $value
* @return mixed
* @throws Exception
*/
private function _gets(string $name, mixed $value): mixed
{
$loader = Snowflake::app()->getAnnotation(); $loader = Snowflake::app()->getAnnotation();
if (!empty($method = $loader->getGetMethodName(static::class, $name))) { $method = $loader->getGetMethodName(static::class, $name);
return $this->{$method}(...[$value]); if (empty($method)) {
return $this->_decode($name, $value);
} }
if (array_key_exists($name, $this->_attributes)) { $_value = $this->{$method}(...[$value]);
return static::getColumns()->_decode($name, $value); if ($_value instanceof HasBase) {
return $_value->get();
} }
if (Snowflake::app()->has($name)) { return $_value;
return Snowflake::app()->get($name); }
}
return parent::__get($name);
/**
* @param $name
* @param $value
* @return mixed
* @throws Exception
*/
private function _decode($name, $value): mixed
{
return static::getColumns()->_decode($name, $value);
} }
@@ -1005,15 +1030,11 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
public static function populate(array $data): static public static function populate(array $data): static
{ {
$create = microtime(true);
/** @var static $model */ /** @var static $model */
$model = Snowflake::createObject(static::class); $model = Snowflake::createObject(static::class);
$model->_attributes = $data; $model->_attributes = $data;
$model->_oldAttributes = $data; $model->_oldAttributes = $data;
$model->setIsCreate(false); $model->setIsCreate(false);
echo 'end create -> ' . microtime(true) . '::' . $create;
echo PHP_EOL;
return $model; return $model;
} }
+10 -6
View File
@@ -10,11 +10,10 @@ declare(strict_types=1);
namespace Snowflake\Di; namespace Snowflake\Di;
use ReflectionException;
use Snowflake\Exception\ComponentException;
use Snowflake\Abstracts\Component;
use Exception; use Exception;
use Snowflake\Exception\NotFindClassException; use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Component;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -30,6 +29,9 @@ class Service extends Component
private array $_definition = []; private array $_definition = [];
private array $_ids = [];
protected array $_alias = []; protected array $_alias = [];
/** /**
@@ -49,6 +51,8 @@ class Service extends Component
} }
throw new ComponentException("Unknown component ID: $id"); throw new ComponentException("Unknown component ID: $id");
} }
$this->_ids[] = $id;
if (isset($this->_definition[$id])) { if (isset($this->_definition[$id])) {
$config = $this->_definition[$id]; $config = $this->_definition[$id];
if (is_object($config)) { if (is_object($config)) {
@@ -102,9 +106,9 @@ class Service extends Component
* @param $id * @param $id
* @return bool * @return bool
*/ */
public function has($id): bool #[Pure] public function has($id): bool
{ {
return isset($this->_definition[$id]) || isset($this->_components[$id]) || isset($this->_alias[$id]); return in_array($id, $this->_ids);
} }
/** /**
+11
View File
@@ -72,6 +72,17 @@ class Snowflake
return static::app()->set($alias, $array); return static::app()->set($alias, $array);
} }
/**
* @param string $name
* @return mixed
* @throws Exception
*/
public static function getApp(string $name): mixed
{
return static::app()->get($name);
}
/** /**
* @return Application|null * @return Application|null
*/ */