From 320a01d82240816e63f8bfe939a032202cb220d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 4 Nov 2021 17:34:20 +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 --- kiri-engine/Di/LocalService.php | 112 ++++++++++++++++---------------- 1 file changed, 57 insertions(+), 55 deletions(-) diff --git a/kiri-engine/Di/LocalService.php b/kiri-engine/Di/LocalService.php index 76fe99cd..55c2f1a6 100644 --- a/kiri-engine/Di/LocalService.php +++ b/kiri-engine/Di/LocalService.php @@ -13,74 +13,76 @@ class LocalService extends Component { - private array $_components = []; + private array $_components = []; - private array $_definition = []; + private array $_definition = []; - /** - * @param $name - * @param $define - */ - public function set($name, $define) - { - unset($this->_components[$name]); + /** + * @param $name + * @param $define + */ + public function set($name, $define) + { + unset($this->_components[$name]); - $this->_definition[$name] = $define; - $this->_components[$name] = $define; - } + $this->_definition[$name] = $define; + if (is_object($define) || $define instanceof \Closure) { + $this->_components[$name] = $define; + } + } - /** - * @throws \Exception - */ - public function get(string $name, $throwException = true) - { - if (isset($this->_components[$name])) { - return $this->_components[$name]; - } - if (isset($this->_definition[$name])) { - $definition = $this->_definition[$name]; - if (is_object($definition) && !$definition instanceof \Closure) { - return $this->_components[$name] = $definition; - } - return $this->_components[$name] = Kiri::createObject($definition); - } else if ($throwException) { - throw new \Exception("Unknown component ID: $name"); - } - return null; - } + /** + * @throws \Exception + */ + public function get(string $name, $throwException = true) + { + if (isset($this->_components[$name])) { + return $this->_components[$name]; + } + if (isset($this->_definition[$name])) { + $definition = $this->_definition[$name]; + if (is_object($definition) && !$definition instanceof \Closure) { + return $this->_components[$name] = $definition; + } + return $this->_components[$name] = Kiri::createObject($definition); + } else if ($throwException) { + throw new \Exception("Unknown component ID: $name"); + } + return null; + } - /** - * @param array $components - */ - public function setComponents(array $components) - { - foreach ($components as $name => $component) { - $this->set($name, $component); - } - } + /** + * @param array $components + */ + public function setComponents(array $components) + { + foreach ($components as $name => $component) { + $this->set($name, $component); + } + } - /** - * @param $id - * @return bool - */ - public function has($id): bool - { - return isset($this->_components[$id]) || isset($this->_definition[$id]); - } + /** + * @param $id + * @return bool + */ + public function has($id): bool + { + return isset($this->_components[$id]) || isset($this->_definition[$id]); + } - /** - * @param $id - */ - public function remove($id): void - { - unset($this->_components[$id], $this->_definition[$id]); - } + /** + * @param $id + */ + public function remove($id): void + { + unset($this->_components[$id], $this->_definition[$id]); + } }