This commit is contained in:
2023-11-30 17:02:20 +08:00
parent c01514e516
commit f552dc994c
2 changed files with 3 additions and 94 deletions
+3 -2
View File
@@ -127,11 +127,12 @@ class Container implements ContainerInterface
/**
* @param string $interface
* @param object $object
* @return void
* @return object
*/
public function bind(string $interface, object $object): void
public function bind(string $interface, object $object): object
{
$this->_singletons[$interface] = $object;
return $object;
}
-92
View File
@@ -1,92 +0,0 @@
<?php
declare(strict_types=1);
namespace Kiri\Di;
use Closure;
use Exception;
use Kiri;
/**
* 服务定位器
*/
class LocalService
{
private array $_components = [];
private array $_definition = [];
/**
* @param $name
* @param $define
* @throws Exception
*/
public function set($name, $define): void
{
unset($this->_components[$name]);
$this->_definition[$name] = $define;
if (is_object($define) || $define instanceof Closure) {
$this->_components[$name] = $this->get($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;
}
/**
* @param array $components
* @throws Exception
*/
public function setComponents(array $components): void
{
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
*/
public function remove($id): void
{
unset($this->_components[$id], $this->_definition[$id]);
}
}