This commit is contained in:
2023-04-17 00:30:28 +08:00
parent a475e3a396
commit 6d47eb377d
3 changed files with 24 additions and 25 deletions
-6
View File
@@ -5,17 +5,11 @@ defined('APP_PATH') or define('APP_PATH', realpath(__DIR__ . '/../../'));
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Config;
use Kiri\Annotation\Annotation;
use Kiri\Annotation\Route\Route;
use Kiri\Main;
use Kiri\Core\ArrayAccess;
use Kiri\Di\TargetManager;
use Kiri\Error\StdoutLoggerInterface;
use Kiri\Events\EventDispatch;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Message\Handler\Router;
use Psr\Log\LoggerInterface;
use Swoole\Process;
if (!function_exists('make')) {
+9 -5
View File
@@ -13,6 +13,7 @@ namespace Kiri\Abstracts;
use Exception;
use Kiri;
use Kiri\Di\LocalService;
use ReflectionException;
use Kiri\Error\{StdoutLogger, StdoutLoggerInterface};
use Kiri\Exception\{InitException};
use Psr\Container\ContainerInterface;
@@ -40,7 +41,7 @@ abstract class BaseMain extends Component
*
* @throws
*/
public function __construct(public ContainerInterface $container, public EventProvider $provider)
public function __construct()
{
$config = sweep(APP_PATH . '/config');
$this->mapping($config['mapping'] ?? [], $config['components']);
@@ -182,26 +183,27 @@ abstract class BaseMain extends Component
*/
private function addEvent($key, $value): void
{
$provider = Kiri::getDi()->get(EventProvider::class);
if ($value instanceof \Closure || is_object($value)) {
$this->provider->on($key, $value, 0);
$provider->on($key, $value, 0);
return;
}
if (!is_array($value)) {
return;
}
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
$this->provider->on($key, $value, 0);
$provider->on($key, $value, 0);
return;
} else if (is_string($value[0])) {
$value[0] = Kiri::createObject($value[0]);
$this->provider->on($key, $value, 0);
$provider->on($key, $value, 0);
return;
}
foreach ($value as $item) {
if (!is_callable($item, true)) {
throw new InitException("Class does not hav callback.");
}
$this->provider->on($key, $item, 0);
$provider->on($key, $item, 0);
}
}
@@ -242,6 +244,7 @@ abstract class BaseMain extends Component
/**
* @param $id
* @param $definition
* @throws ReflectionException
*/
public function set($id, $definition): void
{
@@ -252,6 +255,7 @@ abstract class BaseMain extends Component
/**
* @param $id
* @return bool
* @throws ReflectionException
*/
public function has($id): bool
{
+15 -14
View File
@@ -48,12 +48,11 @@ class Main extends BaseMain
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function init(): void
{
$error = $this->container->get(ErrorHandler::class);
$error = Kiri::getDi()->get(ErrorHandler::class);
$error->registerShutdownHandler(Config::get('error.shutdown', []));
$error->registerExceptionHandler(Config::get('error.exception', []));
$error->registerErrorHandler(Config::get('error.error', []));
@@ -72,7 +71,7 @@ class Main extends BaseMain
}
$class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this->container->get(LocalService::class));
$class->onImport(Kiri::getDi()->get(LocalService::class));
}
return $this;
}
@@ -81,8 +80,7 @@ class Main extends BaseMain
/**
* @param Kernel $kernel
* @return $this
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function commands(Kernel $kernel): static
{
@@ -96,13 +94,13 @@ class Main extends BaseMain
/**
* @param string $command
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function command(string $command): void
{
$console = $this->container->get(ConsoleApplication::class);
$console->add($this->container->get($command));
$container = Kiri::getDi();
$console = $container->get(ConsoleApplication::class);
$console->add($container->get($command));
}
@@ -116,11 +114,13 @@ class Main extends BaseMain
*/
public function execute(array $argv): void
{
$container = Kiri::getDi();
[$input, $output] = $this->argument($argv);
$console = $this->container->get(ConsoleApplication::class);
$console = $container->get(ConsoleApplication::class);
$command = $console->find($input->getFirstArgument());
$scanner = $this->container->get(Scanner::class);
$scanner = $container->get(Scanner::class);
$scanner->read(APP_PATH . 'app/');
fire(new OnBeforeCommandExecute());
@@ -137,11 +137,12 @@ class Main extends BaseMain
*/
private function argument($argv): array
{
$container = Kiri::getDi();
$input = new ArgvInput($argv);
$this->container->bind(ArgvInput::class, $input);
$container->bind(ArgvInput::class, $input);
$output = new ConsoleOutput();
$this->container->bind(OutputInterface::class, $output);
$container->bind(OutputInterface::class, $output);
return [$input, $output];
}