From b25987c865f5f222c77a50a42c94ece88b3eb9e2 Mon Sep 17 00:00:00 2001 From: whwyy Date: Thu, 30 Nov 2023 17:46:50 +0800 Subject: [PATCH] eee --- Kiri.php | 2 +- kiri-engine/Abstracts/BaseApplication.php | 2 +- kiri-engine/Abstracts/LocalService.php | 65 +++++++++++++++++++ .../Abstracts/LocalServiceInterface.php | 8 +++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 kiri-engine/Abstracts/LocalService.php create mode 100644 kiri-engine/Abstracts/LocalServiceInterface.php diff --git a/Kiri.php b/Kiri.php index 216df6db..3c13f3d9 100644 --- a/Kiri.php +++ b/Kiri.php @@ -71,7 +71,7 @@ class Kiri /** * @return \Kiri\Pool\Pool - * @throws ReflectionException + * @throws ReflectionException|Exception */ public static function getPool(): \Kiri\Pool\Pool { diff --git a/kiri-engine/Abstracts/BaseApplication.php b/kiri-engine/Abstracts/BaseApplication.php index c16f275e..fc8fbca8 100644 --- a/kiri-engine/Abstracts/BaseApplication.php +++ b/kiri-engine/Abstracts/BaseApplication.php @@ -28,7 +28,7 @@ use Kiri\Error\StdoutLogger; * @package Kiri\Base * @property DatabasesProviders $connections */ -abstract class BaseApplication extends Component +abstract class BaseApplication extends LocalService { diff --git a/kiri-engine/Abstracts/LocalService.php b/kiri-engine/Abstracts/LocalService.php new file mode 100644 index 00000000..75bf2f4e --- /dev/null +++ b/kiri-engine/Abstracts/LocalService.php @@ -0,0 +1,65 @@ +_definition[$name]) || isset($this->_components[$name]); + } + + + /** + * @param string $name + * @return mixed + * @throws Exception + */ + public function get(string $name): mixed + { + if (isset($this->_components[$name])) return $this->_components[$name]; + if (!isset($this->_definition[$name])) { + throw new Exception('Undefined component ' . $name); + } + $definition = $this->_definition[$name]; + if (!($definition instanceof \Closure)) { + $this->_components[$name] = \Kiri::createObject($definition); + } else { + $this->_components[$name] = call_user_func($definition); + } + return $this->_components[$name]; + } + + + /** + * @param string $name + * @param array $value + * @return void + */ + public function set(string $name, array $value): void + { + $this->_definition[$name] = $value; + unset($this->_components[$name]); + } + +} \ No newline at end of file diff --git a/kiri-engine/Abstracts/LocalServiceInterface.php b/kiri-engine/Abstracts/LocalServiceInterface.php new file mode 100644 index 00000000..42cef730 --- /dev/null +++ b/kiri-engine/Abstracts/LocalServiceInterface.php @@ -0,0 +1,8 @@ +