From 05cd3dde9eadac1372a8cd181b370b7d7a89362a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Sun, 25 Apr 2021 16:16:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Database/Base/BaseActiveRecord.php | 45 ++++++++++++++++++++++-------- System/Di/Service.php | 16 +++++++---- System/Snowflake.php | 11 ++++++++ 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index 562047ab..d7988d9c 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -824,19 +824,44 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public function __get($name): mixed { + if (isService($name)) { + return Snowflake::getApp($name); + } $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(); - if (!empty($method = $loader->getGetMethodName(static::class, $name))) { - return $this->{$method}(...[$value]); + $method = $loader->getGetMethodName(static::class, $name); + if (empty($method)) { + return $this->_decode($name, $value); } - if (array_key_exists($name, $this->_attributes)) { - return static::getColumns()->_decode($name, $value); + $_value = $this->{$method}(...[$value]); + if ($_value instanceof HasBase) { + return $_value->get(); } - if (Snowflake::app()->has($name)) { - return Snowflake::app()->get($name); - } - return parent::__get($name); + return $_value; + } + + + /** + * @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 { - $create = microtime(true); /** @var static $model */ $model = Snowflake::createObject(static::class); $model->_attributes = $data; $model->_oldAttributes = $data; $model->setIsCreate(false); - - echo 'end create -> ' . microtime(true) . '::' . $create; - echo PHP_EOL; return $model; } diff --git a/System/Di/Service.php b/System/Di/Service.php index 2f2fe416..eebc0f48 100644 --- a/System/Di/Service.php +++ b/System/Di/Service.php @@ -10,11 +10,10 @@ declare(strict_types=1); namespace Snowflake\Di; -use ReflectionException; -use Snowflake\Exception\ComponentException; -use Snowflake\Abstracts\Component; use Exception; -use Snowflake\Exception\NotFindClassException; +use JetBrains\PhpStorm\Pure; +use Snowflake\Abstracts\Component; +use Snowflake\Exception\ComponentException; use Snowflake\Snowflake; /** @@ -30,6 +29,9 @@ class Service extends Component private array $_definition = []; + private array $_ids = []; + + protected array $_alias = []; /** @@ -49,6 +51,8 @@ class Service extends Component } throw new ComponentException("Unknown component ID: $id"); } + + $this->_ids[] = $id; if (isset($this->_definition[$id])) { $config = $this->_definition[$id]; if (is_object($config)) { @@ -102,9 +106,9 @@ class Service extends Component * @param $id * @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); } /** diff --git a/System/Snowflake.php b/System/Snowflake.php index bc957eaa..3088272d 100644 --- a/System/Snowflake.php +++ b/System/Snowflake.php @@ -72,6 +72,17 @@ class Snowflake 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 */