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();
}
}
+14 -5
View File
@@ -18,6 +18,7 @@ use Kiri\Di\Scanner;
use Kiri\Error\ErrorHandler;
use Kiri\Events\{OnAfterCommandExecute, OnBeforeCommandExecute};
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Symfony\Component\Console\{Application as ConsoleApplication,
@@ -44,16 +45,24 @@ class Application extends BaseApplication
public string $state = '';
/**
* @param ErrorHandler $errorHandler
*/
public function __construct(public ErrorHandler $errorHandler)
{
parent::__construct();
}
/**
* @return void
* @throws ReflectionException
*/
public function init(): void
{
$error = Kiri::getDi()->get(ErrorHandler::class);
$error->registerShutdownHandler(\config('error.shutdown', []));
$error->registerExceptionHandler(\config('error.exception', []));
$error->registerErrorHandler(\config('error.error', []));
$this->errorHandler->registerShutdownHandler(\config('error.shutdown', []));
$this->errorHandler->registerExceptionHandler(\config('error.exception', []));
$this->errorHandler->registerErrorHandler(\config('error.error', []));
$this->id = \config('id', uniqid('id.'));
}
@@ -69,7 +78,7 @@ class Application extends BaseApplication
}
$class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport(Kiri::getDi()->get(LocalService::class));
$class->onImport($this->localService);
}
return $this;
}
+22 -13
View File
@@ -13,12 +13,12 @@ use Closure;
use Exception;
use Kiri;
use Kiri\Abstracts\Component;
use Kiri\Core\Json;
use Kiri\Events\EventDispatch;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Kiri\Di\Inject\Container;
use Kiri\Abstracts\Logger;
use Kiri\Events\OnSystemError;
/**
* Class ErrorHandler
@@ -35,17 +35,26 @@ class ErrorHandler extends Component implements ErrorInterface
public string $category = 'app';
/**
* @param ContainerInterface $container
*/
public function __construct(public ContainerInterface $container)
{
parent::__construct();
}
/**
* @param array|Closure|null $callback
* @return void
* @throws ReflectionException
* @throws
*/
public function registerExceptionHandler(null|array|Closure $callback): void
{
if (empty($callback)) {
$callback = [$this, 'exceptionHandler'];
} else if (is_array($callback) && is_string($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
$callback[0] = $this->container->get($callback[0]);
}
set_exception_handler($callback);
}
@@ -54,14 +63,14 @@ class ErrorHandler extends Component implements ErrorInterface
/**
* @param array|Closure|null $callback
* @return void
* @throws ReflectionException
* @throws
*/
public function registerErrorHandler(null|array|Closure $callback): void
{
if (empty($callback)) {
$callback = [$this, 'errorHandler'];
} else if (is_array($callback) && is_string($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
$callback[0] = $this->container->get($callback[0]);
}
set_error_handler($callback);
}
@@ -70,14 +79,14 @@ class ErrorHandler extends Component implements ErrorInterface
/**
* @param array|Closure|null $callback
* @return void
* @throws ReflectionException
* @throws
*/
public function registerShutdownHandler(null|array|Closure $callback): void
{
if (empty($callback)) {
$callback = [$this, 'shutdown'];
} else if (is_array($callback) && is_string($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
$callback[0] = $this->container->get($callback[0]);
}
register_shutdown_function($callback);
}
@@ -99,7 +108,7 @@ class ErrorHandler extends Component implements ErrorInterface
error("\033[31m" . $lastError['message'] . "\033[0m" . $lastError['file'] . " at line " . $lastError['line'] . PHP_EOL);
event(new Kiri\Events\OnSystemError());
event(new OnSystemError());
}
@@ -114,9 +123,9 @@ class ErrorHandler extends Component implements ErrorInterface
{
$this->category = 'exception';
Kiri::getLogger()->error($exception, []);
Logger::_error(jTraceEx($exception), []);
event(new Kiri\Events\OnSystemError());
event(new OnSystemError());
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
}
@@ -134,7 +143,7 @@ class ErrorHandler extends Component implements ErrorInterface
error("\033[31m" . $error[1] . "\033[0m" . $error[2] . " at line " . $error[3] . PHP_EOL);
event(new Kiri\Events\OnSystemError());
event(new OnSystemError());
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
}
+4 -3
View File
@@ -21,14 +21,15 @@ class StdoutLogger extends Logger
* @param string $model
* @return bool
*/
public function addError($message, string $model = 'app'): bool
public function failure($message, string $model = 'app'): bool
{
if ($message instanceof \Exception) {
$this->errors[$model] = $message->getMessage();
} else {
$this->errors[$model] = $message;
}
return false;
}
$this->error($model, [$message]);
return false;
}
+1 -1
View File
@@ -167,7 +167,7 @@ SCRIPT;
try {
$response = $client->{$name}(...$arguments);
} catch (\Throwable $throwable) {
$response = addError($throwable, 'redis');
$response = trigger_print_error($throwable, 'redis');
} finally {
$this->pool()->push($this->host, $client);
}