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