This commit is contained in:
2021-11-27 17:43:29 +08:00
parent 3ead688d5f
commit f26539c41f
4 changed files with 42 additions and 31 deletions
+39 -16
View File
@@ -21,6 +21,7 @@ use ReflectionException;
use ReflectionFunction; use ReflectionFunction;
use ReflectionMethod; use ReflectionMethod;
use ReflectionProperty; use ReflectionProperty;
use Psr\Container\ContainerInterface;
/** /**
* Class Container * Class Container
@@ -61,26 +62,39 @@ class Container extends BaseObject implements ContainerInterface
]; ];
/** /**
* @param $class * @param string $id
* @param array $constrict * @return mixed
* @param array $config * @throws ReflectionException
* */
* @return mixed public function get(string $id): mixed
* @throws
*/
public function get($class, array $constrict = [], array $config = []): mixed
{ {
if ($this->isInterface($class)) { return $this->make($id, [], []);
$class = $this->_interfaces[$class];
}
if (!isset($this->_singletons[$class])) {
$this->_singletons[$class] = $this->resolve($class, $constrict, $config);
}
return $this->_singletons[$class];
} }
/**
* @param $class
* @param array $constrict
* @param array $config
* @return mixed
* @throws ReflectionException
* @throws Exception
*/
public function make($class, array $constrict = [], array $config = []): mixed
{
if ($this->isInterface($class)) {
$class = $this->_interfaces[$class];
}
if (!isset($this->_singletons[$class])) {
$this->_singletons[$class] = $this->resolve($class, $constrict, $config);
}
return $this->_singletons[$class];
}
/** /**
* @param string $interface * @param string $interface
* @param string $class * @param string $class
@@ -435,4 +449,13 @@ class Container extends BaseObject implements ContainerInterface
} }
return $old; return $old;
} }
/**
* @param string $id
* @return bool
*/
public function has(string $id): bool
{
return isset($this->_singletons[$id]) || isset($this->_interfaces[$id]);
}
} }
-12
View File
@@ -1,12 +0,0 @@
<?php
namespace Kiri\Di;
/**
* @mixin Container
*/
interface ContainerInterface
{
}
+2 -2
View File
@@ -72,9 +72,9 @@ class HotReload extends Command
$this->dirs = Config::get('inotify', [APP_PATH . 'app']); $this->dirs = Config::get('inotify', [APP_PATH . 'app']);
swoole_async_set(['enable_coroutine' => false]); swoole_async_set(['enable_coroutine' => false]);
if (!extension_loaded('inotify')) { if (!extension_loaded('inotify')) {
$this->driver = Kiri::getDi()->get(Scaner::class, [$this->dirs, $this]); $this->driver = Kiri::getDi()->make(Scaner::class, [$this->dirs, $this]);
} else { } else {
$this->driver = Kiri::getDi()->get(Inotify::class, [$this->dirs, $this]); $this->driver = Kiri::getDi()->make(Inotify::class, [$this->dirs, $this]);
} }
if (Kiri::getPlatform()->isLinux()) { if (Kiri::getPlatform()->isLinux()) {
swoole_set_process_name('[' . Config::get('id', 'sw service.') . '].sw:wather'); swoole_set_process_name('[' . Config::get('id', 'sw service.') . '].sw:wather');
+1 -1
View File
@@ -13,7 +13,7 @@ use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Core\Json; use Kiri\Core\Json;
use Kiri\Di\Container; use Kiri\Di\Container;
use Kiri\Di\ContainerInterface; use Psr\Container\ContainerInterface;
use ReflectionException; use ReflectionException;
use Server\ServerManager; use Server\ServerManager;
use Swoole\Coroutine; use Swoole\Coroutine;