From b0287e52b35274f582737ccc45d3f9525b9c4956 Mon Sep 17 00:00:00 2001 From: Json <501807312@qq.com> Date: Thu, 27 Oct 2022 14:58:32 +0800 Subject: [PATCH] 1 --- src/Core/Container.php | 15 +++++++-------- tests/test.php | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Core/Container.php b/src/Core/Container.php index 5bfe4c7..02253ee 100644 --- a/src/Core/Container.php +++ b/src/Core/Container.php @@ -20,7 +20,7 @@ class Container * 绑定到容器 * @param mixed $abstract * @param null $concrete - * @return $this + * @return null */ public function bind($abstract, $concrete = null) { @@ -38,21 +38,20 @@ public function bind($abstract, $concrete = null) $this->bindings[$abstract] = $concrete; } - - return $this; } /** * 取出容器中的数据 * @param mixed $abstract * @param array $params - * @return mixed|object|$abstract + * @return mixed */ public function make($abstract, $params = []) { -// if (!isset($this->bindings[$abstract])) { -// throw new \Exception('没有在容器中找到'); -// } + // 容器中没有就绑定 + if (!isset($this->bindings[$abstract])) { + $this->bind($abstract); + } $concrete = $this->bindings[$abstract]; if ($concrete instanceof \Closure) { @@ -70,7 +69,7 @@ public function make($abstract, $params = []) /** * 获取实例 - * @return $this + * @return static */ public static function getInstance() { diff --git a/tests/test.php b/tests/test.php index 8825699..7eaa47c 100644 --- a/tests/test.php +++ b/tests/test.php @@ -65,7 +65,7 @@ //Container::getInstance()->bind('test', 111); //Container::getInstance()->bind('test', [1,2,3,4]); //Container::getInstance()->bind('test', function(){ -// return 'sb'; +// return 'test'; //}); //Container::getInstance()->bind([ // 'test' => [1,2,3,4], @@ -73,7 +73,7 @@ //]); //Container::getInstance()->bind(Faker::class); //dump(Container::getInstance()->bindings); -//dump(Container::getInstance()->make(Faker::class)); + //Container::getInstance()->bind('cache', function($name, $name2){return 'cache '.$name.' '. $name2;});