modify plugin name

This commit is contained in:
2022-02-23 16:32:08 +08:00
parent fa66eef192
commit 99c824c467
37 changed files with 317 additions and 937 deletions
+23 -126
View File
@@ -14,15 +14,17 @@ use Database\Connection;
use Exception;
use Kafka\KafkaProvider;
use Kiri;
use Kiri\Annotation\Annotation as SAnnotation;
use Kiri\Cache\Redis;
use Kiri\Di\LocalService;
use Kiri\Error\{ErrorHandler, Logger};
use Kiri\Error\{ErrorHandler};
use Kiri\Error\StdoutLogger;
use Kiri\Error\StdoutLoggerInterface;
use Kiri\Exception\{InitException, NotFindClassException};
use Kiri\Message\Handler\Router;
use Kiri\Server\{Server, ServerManager};
use Kiri\Task\AsyncTaskExecute;
use Kiri\Task\OnTaskInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Kiri\Server\{Server};
use Psr\Log\LoggerInterface;
use ReflectionException;
use Swoole\Table;
@@ -53,6 +55,8 @@ abstract class BaseApplication extends Component
{
Kiri::init($this);
$this->mapping($config['mapping'] ?? []);
$config = sweep(APP_PATH . '/config');
$this->moreComponents();
@@ -60,7 +64,6 @@ abstract class BaseApplication extends Component
$this->parseEvents($config);
$this->initErrorHandler();
$this->enableEnvConfig();
$this->mapping($config['mapping'] ?? []);
parent::__construct();
}
@@ -72,6 +75,8 @@ abstract class BaseApplication extends Component
public function mapping(array $mapping)
{
$di = Kiri::getDi();
$di->mapping(StdoutLoggerInterface::class, StdoutLogger::class);
$di->mapping(LoggerInterface::class, Logger::class);
foreach ($mapping as $interface => $class) {
$di->mapping($interface, $class);
}
@@ -109,10 +114,9 @@ abstract class BaseApplication extends Component
// Read file into an array of lines with auto-detected line endings
// $autodetect = ini_get('auto_detect_line_endings');
// ini_set('auto_detect_line_endings', '1');
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// ini_set('auto_detect_line_endings', $autodetect);
// ini_set('auto_detect_line_endings', $autodetect);
return $lines;
return file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
/**
@@ -199,39 +203,34 @@ abstract class BaseApplication extends Component
}
/**
* @param OnTaskInterface $execute
* @throws ReflectionException|Exception
*/
public function task(OnTaskInterface $execute): void
{
di(AsyncTaskExecute::class)->execute($execute);
}
/**
* @param $key
* @param $value
* @return void
* @throws InitException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
private function addEvent($key, $value): void
{
$provider = $this->getEventProvider();
if ($value instanceof \Closure || is_object($value)) {
$this->getEventProvider()->on($key, $value, 0);
$provider->on($key, $value, 0);
return;
}
if (is_array($value)) {
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
$this->getEventProvider()->on($key, $value, 0);
$provider->on($key, $value, 0);
return;
}
if (is_string($value[0])) {
$value[0] = Kiri::createObject($value[0]);
$this->getEventProvider()->on($key, $value, 0);
$provider->on($key, $value, 0);
return;
}
@@ -240,7 +239,7 @@ abstract class BaseApplication extends Component
if (!is_callable($item, true)) {
throw new InitException("Class does not hav callback.");
}
$this->getEventProvider()->on($key, $item, 0);
$provider->on($key, $item, 0);
}
}
@@ -263,7 +262,8 @@ abstract class BaseApplication extends Component
*/
public function initErrorHandler()
{
$this->get('error')->register();
$error = $this->container->get(ErrorHandler::class);
$error->register();
}
@@ -305,65 +305,6 @@ abstract class BaseApplication extends Component
}
/**
* @return \Redis|Redis
* @throws
*/
public function getRedis(): Redis|\Redis
{
return Kiri::getDi()->get(Redis::class);
}
/**
* @param $ip
* @return bool
*/
public function isLocal($ip): bool
{
return $this->getFirstLocal() == $ip;
}
/**
* @return ErrorHandler
* @throws
*/
public function getError(): ErrorHandler
{
return $this->get('error');
}
/**
* @param $name
* @return Table
* @throws
*/
public function getTable($name): Table
{
return $this->get($name);
}
/**
* @return Config
* @throws
*/
public function getConfig(): Config
{
return $this->get('config');
}
/**
* @return Router
* @throws
*/
public function getRouter(): Router
{
return Kiri::getDi()->get(Router::class);
}
/**
* @return Server
@@ -375,34 +316,6 @@ abstract class BaseApplication extends Component
}
/**
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
* @throws
*/
public function getSwoole(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
{
return di(ServerManager::class)->getServer();
}
/**
* @return SAnnotation
* @throws
*/
public function getAnnotation(): SAnnotation
{
return $this->get('Annotation');
}
/**
* @param $array
*/
private function setComponents($array): void
{
di(LocalService::class)->setComponents($array);
}
/**
* @param $id
@@ -422,20 +335,4 @@ abstract class BaseApplication extends Component
{
return di(LocalService::class)->has($id);
}
/**
* @throws Exception
*/
protected function moreComponents(): void
{
$this->setComponents([
'error' => ['class' => ErrorHandler::class],
'config' => ['class' => Config::class],
'logger' => ['class' => Logger::class],
'Annotation' => ['class' => SAnnotation::class],
'databases' => ['class' => Connection::class],
'kafka-container' => ['class' => KafkaProvider::class],
]);
}
}
+48 -136
View File
@@ -12,10 +12,11 @@ namespace Kiri\Abstracts;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri;
use Kiri\Di\Container;
use Kiri\Error\StdoutLogger;
use Kiri\Events\EventDispatch;
use Kiri\Events\EventProvider;
use Kiri;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -23,11 +24,17 @@ use Psr\Container\NotFoundExceptionInterface;
/**
* Class Component
* @package Kiri\Base
* @property EventDispatch $eventDispatch
* @property EventProvider $eventProvider
* @property Container $container
*/
class Component implements Configure
{
protected ?StdoutLogger $logger = null;
/**
* BaseAbstract constructor.
*
@@ -36,11 +43,30 @@ class Component implements Configure
*/
public function __construct(array $config = [])
{
if (is_null($this->logger)) {
$this->logger = Kiri::getDi()->get(StdoutLogger::class);
}
if (!empty($config) && is_array($config)) {
Kiri::configure($this, $config);
}
}
/**
* @throws Exception
*/
public function init()
{
}
/**
* @return string
*/
#[Pure] public static function className(): string
{
return static::class;
}
/**
* @return Container|ContainerInterface
@@ -72,156 +98,42 @@ class Component implements Configure
return $this->getContainer()->get(EventDispatch::class);
}
/**
* @param string $name
* @return mixed
* @throws Exception
*/
public function init()
public function __get(string $name)
{
}
/**
* @return string
*/
#[Pure] public static function className(): string
{
return static::class;
}
/**
* @param $message
* @param string $model
* @return bool
* @throws Exception
*/
public function addError($message, string $model = 'app'): bool
{
if ($message instanceof \Throwable) {
$this->error($message = jTraceEx($message));
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->{$method}();
} else if (method_exists($this, $name)) {
return $this->{$name};
} else {
if (!is_string($message)) {
$message = json_encode($message, JSON_UNESCAPED_UNICODE);
}
$this->error($message);
throw new Exception('Unable getting property ' . get_called_class() . '::' . $name);
}
Kiri::app()->getLogger()->fail($message, $model);
return FALSE;
}
/**
* @return Logger
* @param string $name
* @param $value
* @return void
* @throws Exception
*/
protected function logger(): Logger
public function __set(string $name, $value): void
{
return Kiri::getDi()->get(Logger::class);
}
/**
* @param mixed $message
* @param string $method
* @param string $file
* @throws Exception
*/
public function debug(mixed $message, string $method = '', string $file = '')
{
if (!is_string($message)) {
$message = print_r($message, true);
}
$context = [];
if (!empty($method)) $context['method'] = $method;
if (!empty($file)) $context['file'] = $file;
$this->logger()->debug($message, $context);
}
/**
* @param mixed $message
* @param string $method
* @param string $file
* @throws Exception
*/
public function info(mixed $message, string $method = '', string $file = '')
{
if (!is_string($message)) {
$message = print_r($message, true);
}
$context = [];
if (!empty($method)) $context['method'] = $method;
if (!empty($file)) $context['file'] = $file;
$this->logger()->info($message, $context);
}
/**
* @param mixed $message
* @param string $method
* @param string $file
* @throws Exception
*/
public function success(mixed $message, string $method = '', string $file = '')
{
if (!is_string($message)) {
$message = print_r($message, true);
}
$context = [];
if (!empty($method)) $context['method'] = $method;
if (!empty($file)) $context['file'] = $file;
$this->logger()->notice($message, $context);
}
/**
* @param mixed $message
* @param string $method
* @param string $file
* @throws Exception
*/
public function warning(mixed $message, string $method = '', string $file = '')
{
if (!is_string($message)) {
$message = print_r($message, true);
}
$context = [];
if (!empty($method)) $context['method'] = $method;
if (!empty($file)) $context['file'] = $file;
$this->logger()->critical($message, $context);
}
/**
* @param mixed $message
* @param null $method
* @param null $file
* @throws Exception
*/
public function error(mixed $message, $method = null, $file = null)
{
if ($message instanceof \Throwable) {
$message = $message->getMessage() . " on line " . $message->getLine() . " at file " . $message->getFile();
}
$context = [];
if (is_string($method)) {
$message = (empty($method) ? '' : $method . ': ') . $message;
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
$this->{$method}($value);
} else if (method_exists($this, $name)) {
$this->{$name} = $value;
} else {
if (is_null($method)) {
$method = [];
}
$context = $method;
throw new Exception('Unable setting property ' . get_called_class() . '::' . $name);
}
if (!empty($method)) $context['method'] = $method;
if (!empty($file)) $context['file'] = $file;
$this->logger()->error($message, $context);
}
}
-1
View File
@@ -36,7 +36,6 @@ class Logger implements LoggerInterface
/**
* @return void
* @throws ReflectionException
*/
public function init()
{
@@ -9,7 +9,6 @@ use Database\Connection;
use Database\DatabasesProviders;
use Kiri\Message\Handler\Router;
use Kiri\Server\Server;
use Kiri\Error\Logger;
use Kiri\Jwt\JWTAuth;
/**
@@ -18,7 +17,6 @@ use Kiri\Jwt\JWTAuth;
* @property Router $router
* @property Server $server
* @property DatabasesProviders $db
* @property Logger $logger
* @property JWTAuth $jwt
* @property SAnnotation $annotation
* @property Connection $databases