This commit is contained in:
2023-07-31 23:09:00 +08:00
parent c062a8f6a3
commit 0fbb08ed58
7 changed files with 94 additions and 422 deletions
+29 -12
View File
@@ -17,6 +17,7 @@ use Kiri\Di\LocalService;
use Kiri\Config\ConfigProvider;
use Kiri\Error\StdoutLogger;
use Kiri\Exception\{InitException};
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Kiri\Events\EventProvider;
@@ -34,8 +35,24 @@ abstract class BaseApplication extends Component
*/
public string $storage = APP_PATH . 'storage';
/**
* @var LocalService|mixed
*/
public LocalService $localService;
/**
* @var ContainerInterface
*/
public ContainerInterface $container;
/**
* @var EventProvider
*/
public EventProvider $provider;
/**
* Init constructor.
*
@@ -44,10 +61,12 @@ abstract class BaseApplication extends Component
*/
public function __construct()
{
$this->localService = make(LocalService::class);
$this->container = Kiri::getContainer();
$this->localService = $this->container->get(LocalService::class);
/** @var ConfigProvider $config */
$config = make(ConfigProvider::class);
$this->provider = $this->container->get(EventProvider::class);
$config = $this->container->get(ConfigProvider::class);
$this->mapping($config);
$this->parseStorage($config);
@@ -62,14 +81,13 @@ abstract class BaseApplication extends Component
*/
public function mapping(ConfigProvider $config): void
{
$di = Kiri::getDi();
$di->set(LoggerInterface::class, StdoutLogger::class);
$this->container->set(LoggerInterface::class, StdoutLogger::class);
foreach ($config->get('mapping', []) as $interface => $class) {
$di->set($interface, $class);
$this->container->set($interface, $class);
}
foreach ($config->get('components', []) as $id => $component) {
$this->set($id, $component);
$this->container->set($id, $component);
}
}
@@ -121,27 +139,26 @@ abstract class BaseApplication extends Component
*/
private function addEvent($key, $value): void
{
$provider = Kiri::getDi()->get(EventProvider::class);
if ($value instanceof \Closure || is_object($value)) {
$provider->on($key, $value, 0);
$this->provider->on($key, $value, 0);
return;
}
if (!is_array($value)) {
return;
}
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
$provider->on($key, $value, 0);
$this->provider->on($key, $value, 0);
return;
} else if (is_string($value[0])) {
$value[0] = Kiri::createObject($value[0]);
$provider->on($key, $value, 0);
$this->provider->on($key, $value, 0);
return;
}
foreach ($value as $item) {
if (!is_callable($item, true)) {
throw new InitException("Class does not hav callback.");
}
$provider->on($key, $item, 0);
$this->provider->on($key, $item, 0);
}
}
+2 -2
View File
@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Kiri\Abstracts;
use Kiri\Di\Inject\Container;
use Kiri;
use Psr\Container\ContainerInterface;
/**
@@ -21,7 +21,7 @@ abstract class Providers extends Component implements Provider
*/
public function getContainer(): ContainerInterface
{
return \Kiri::getDi();
return Kiri::getDi();
}
}