2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/10/7 0007
|
|
|
|
|
* Time: 2:13
|
|
|
|
|
*/
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2021-08-11 01:04:57 +08:00
|
|
|
namespace Kiri\Abstracts;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
2021-04-27 16:50:55 +08:00
|
|
|
use Annotation\Annotation as SAnnotation;
|
2021-08-09 00:15:03 +08:00
|
|
|
use Database\Connection;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Exception;
|
2021-08-17 16:43:50 +08:00
|
|
|
use Http\Http\HttpHeaders;
|
|
|
|
|
use Http\Http\HttpParams;
|
|
|
|
|
use Http\Http\Request;
|
|
|
|
|
use Http\Http\Response;
|
|
|
|
|
use Http\Route\Router;
|
|
|
|
|
use Http\Server;
|
|
|
|
|
use Http\Shutdown;
|
2021-02-22 10:57:12 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2021-08-04 14:38:20 +08:00
|
|
|
use Kafka\KafkaProvider;
|
2021-08-11 01:04:57 +08:00
|
|
|
use Kiri\Aop;
|
|
|
|
|
use Kiri\Async;
|
|
|
|
|
use Kiri\Cache\Redis;
|
|
|
|
|
use Kiri\Di\LocalService;
|
|
|
|
|
use Kiri\Error\ErrorHandler;
|
|
|
|
|
use Kiri\Error\Logger;
|
2021-08-13 14:58:58 +08:00
|
|
|
use Kiri\Events\EventProvider;
|
2021-08-11 01:04:57 +08:00
|
|
|
use Kiri\Exception\InitException;
|
|
|
|
|
use Kiri\Exception\NotFindClassException;
|
|
|
|
|
use Kiri\Jwt\Jwt;
|
|
|
|
|
use Kiri\Kiri;
|
2021-08-13 14:37:46 +08:00
|
|
|
use ReflectionException;
|
|
|
|
|
use Server\ServerManager;
|
|
|
|
|
use Server\SInterface\TaskExecute;
|
2021-02-22 10:57:12 +08:00
|
|
|
use Swoole\Table;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class BaseApplication
|
2021-08-11 01:04:57 +08:00
|
|
|
* @package Kiri\Kiri\Base
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2021-08-04 01:07:45 +08:00
|
|
|
abstract class BaseApplication extends Component
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
|
2021-08-02 16:38:50 +08:00
|
|
|
use TraitApplication;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public string $storage = APP_PATH . 'storage';
|
|
|
|
|
|
|
|
|
|
public string $envPath = APP_PATH . '.env';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Init constructor.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
2021-08-11 01:04:57 +08:00
|
|
|
Kiri::init($this);
|
2021-08-02 16:38:50 +08:00
|
|
|
|
|
|
|
|
$config = sweep(APP_PATH . '/config');
|
|
|
|
|
|
|
|
|
|
$this->moreComponents();
|
|
|
|
|
$this->parseInt($config);
|
|
|
|
|
$this->parseEvents($config);
|
|
|
|
|
$this->initErrorHandler();
|
|
|
|
|
$this->enableEnvConfig();
|
2021-08-13 17:07:39 +08:00
|
|
|
$this->mapping($config['mapping'] ?? []);
|
2021-08-02 16:38:50 +08:00
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-13 17:06:52 +08:00
|
|
|
/**
|
|
|
|
|
* @param array $mapping
|
|
|
|
|
*/
|
|
|
|
|
public function mapping(array $mapping)
|
|
|
|
|
{
|
|
|
|
|
$di = Kiri::getDi();
|
|
|
|
|
foreach ($mapping as $interface => $class) {
|
|
|
|
|
$di->mapping($interface, $class);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-02 16:38:50 +08:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function enableEnvConfig(): array
|
|
|
|
|
{
|
|
|
|
|
if (!file_exists($this->envPath)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$lines = $this->readLinesFromFile($this->envPath);
|
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
|
if (!$this->isComment($line) && $this->looksLikeSetter($line)) {
|
|
|
|
|
[$key, $value] = explode('=', $line);
|
|
|
|
|
putenv(trim($key) . '=' . trim($value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read lines from the file, auto detecting line endings.
|
|
|
|
|
*
|
|
|
|
|
* @param string $filePath
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function readLinesFromFile(string $filePath): array
|
|
|
|
|
{
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
return $lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the line in the file is a comment, e.g. begins with a #.
|
|
|
|
|
*
|
|
|
|
|
* @param string $line
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function isComment(string $line): bool
|
|
|
|
|
{
|
|
|
|
|
$line = ltrim($line);
|
|
|
|
|
|
|
|
|
|
return isset($line[0]) && $line[0] === '#';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the given line looks like it's setting a variable.
|
|
|
|
|
*
|
|
|
|
|
* @param string $line
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
#[Pure] protected function looksLikeSetter(string $line): bool
|
|
|
|
|
{
|
|
|
|
|
return str_contains($line, '=');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $config
|
|
|
|
|
*
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function parseInt($config)
|
|
|
|
|
{
|
|
|
|
|
Config::sets($config);
|
|
|
|
|
if ($storage = Config::get('storage', 'storage')) {
|
|
|
|
|
if (!str_contains($storage, APP_PATH)) {
|
|
|
|
|
$storage = APP_PATH . $storage . '/';
|
|
|
|
|
}
|
|
|
|
|
if (!is_dir($storage)) {
|
|
|
|
|
mkdir($storage);
|
|
|
|
|
}
|
|
|
|
|
if (!is_dir($storage) || !is_writeable($storage)) {
|
|
|
|
|
throw new InitException("Directory {$storage} does not have write permission");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-05 15:15:42 +08:00
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return mixed
|
2021-08-12 17:27:19 +08:00
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
|
|
|
|
* @throws Exception
|
2021-08-05 15:15:42 +08:00
|
|
|
*/
|
2021-08-04 01:18:30 +08:00
|
|
|
public function __get($name): mixed
|
2021-08-05 15:15:42 +08:00
|
|
|
{
|
|
|
|
|
if ($this->has($name)) {
|
|
|
|
|
return $this->get($name);
|
|
|
|
|
}
|
|
|
|
|
return parent::__get($name); // TODO: Change the autogenerated stub
|
|
|
|
|
}
|
2021-08-04 01:18:30 +08:00
|
|
|
|
|
|
|
|
|
2021-08-05 15:15:42 +08:00
|
|
|
/**
|
2021-08-02 16:38:50 +08:00
|
|
|
* @param $config
|
|
|
|
|
*
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function parseEvents($config)
|
|
|
|
|
{
|
|
|
|
|
if (!isset($config['events']) || !is_array($config['events'])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
foreach ($config['events'] as $key => $value) {
|
|
|
|
|
if (is_string($value)) {
|
2021-08-11 01:04:57 +08:00
|
|
|
$value = Kiri::createObject($value);
|
2021-08-02 16:38:50 +08:00
|
|
|
}
|
|
|
|
|
$this->addEvent($key, $value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-13 14:37:46 +08:00
|
|
|
/**
|
|
|
|
|
* @param TaskExecute $execute
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function task(TaskExecute $execute): void
|
|
|
|
|
{
|
|
|
|
|
ServerManager::getContext()->task($execute);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-02 16:38:50 +08:00
|
|
|
/**
|
|
|
|
|
* @param $key
|
|
|
|
|
* @param $value
|
|
|
|
|
* @throws InitException
|
|
|
|
|
* @throws NotFindClassException
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function addEvent($key, $value): void
|
|
|
|
|
{
|
2021-08-13 14:58:58 +08:00
|
|
|
$eventProvider = di(EventProvider::class);
|
|
|
|
|
if ($value instanceof \Closure || is_object($value)) {
|
2021-08-13 17:06:52 +08:00
|
|
|
$eventProvider->on($key, $value, 0);
|
2021-08-02 16:38:50 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (is_array($value)) {
|
|
|
|
|
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
|
2021-08-13 17:06:52 +08:00
|
|
|
$eventProvider->on($key, $value, 0);
|
2021-08-02 16:38:50 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_string($value[0])) {
|
2021-08-11 01:04:57 +08:00
|
|
|
$value[0] = Kiri::createObject($value[0]);
|
2021-08-13 17:06:52 +08:00
|
|
|
$eventProvider->on($key, $value, 0);
|
2021-08-02 16:38:50 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($value as $item) {
|
|
|
|
|
if (!is_callable($item, true)) {
|
|
|
|
|
throw new InitException("Class does not hav callback.");
|
|
|
|
|
}
|
2021-08-13 17:06:52 +08:00
|
|
|
$eventProvider->on($key, $item, 0);
|
2021-08-02 16:38:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function clone($name): mixed
|
|
|
|
|
{
|
|
|
|
|
return clone $this->get($name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function initErrorHandler()
|
|
|
|
|
{
|
|
|
|
|
$this->get('error')->register();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-04 01:07:45 +08:00
|
|
|
|
2021-08-05 15:15:42 +08:00
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return mixed
|
2021-08-12 17:27:19 +08:00
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
2021-08-05 15:15:42 +08:00
|
|
|
*/
|
2021-08-04 01:07:45 +08:00
|
|
|
public function get($name): mixed
|
2021-08-05 15:15:42 +08:00
|
|
|
{
|
2021-08-09 00:15:03 +08:00
|
|
|
return di(LocalService::class)->get($name);
|
2021-08-05 15:15:42 +08:00
|
|
|
}
|
2021-08-04 01:07:45 +08:00
|
|
|
|
|
|
|
|
|
2021-08-02 16:38:50 +08:00
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getLocalIps(): mixed
|
|
|
|
|
{
|
|
|
|
|
return swoole_get_local_ip();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getFirstLocal(): mixed
|
|
|
|
|
{
|
|
|
|
|
return current($this->getLocalIps());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Logger
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getLogger(): Logger
|
|
|
|
|
{
|
|
|
|
|
return $this->get('logger');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return \Redis|Redis
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getRedis(): Redis|\Redis
|
|
|
|
|
{
|
|
|
|
|
return $this->get('redis');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $ip
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isLocal($ip): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->getFirstLocal() == $ip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return ErrorHandler
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getError(): ErrorHandler
|
|
|
|
|
{
|
|
|
|
|
return $this->get('error');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return Table
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getTable($name): Table
|
|
|
|
|
{
|
|
|
|
|
return $this->get($name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Config
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getConfig(): Config
|
|
|
|
|
{
|
|
|
|
|
return $this->get('config');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Router
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getRouter(): Router
|
|
|
|
|
{
|
|
|
|
|
return $this->get('router');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Jwt
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getJwt(): Jwt
|
|
|
|
|
{
|
|
|
|
|
return $this->get('jwt');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Server
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getServer(): Server
|
|
|
|
|
{
|
|
|
|
|
return $this->get('server');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
|
|
|
|
|
*/
|
|
|
|
|
public function getSwoole(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
|
|
|
|
|
{
|
|
|
|
|
return ServerManager::getContext()->getServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return SAnnotation
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getAnnotation(): SAnnotation
|
|
|
|
|
{
|
|
|
|
|
return $this->get('annotation');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Async
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getAsync(): Async
|
|
|
|
|
{
|
|
|
|
|
return $this->get('async');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return \Rpc\Producer
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-08-09 00:15:03 +08:00
|
|
|
public function getRpc(): Producer
|
2021-08-02 16:38:50 +08:00
|
|
|
{
|
|
|
|
|
return $this->get('rpc');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-05 15:15:42 +08:00
|
|
|
/**
|
|
|
|
|
* @param $array
|
2021-08-12 17:27:19 +08:00
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
2021-08-05 15:15:42 +08:00
|
|
|
*/
|
2021-08-04 01:07:45 +08:00
|
|
|
private function setComponents($array): void
|
2021-08-05 15:15:42 +08:00
|
|
|
{
|
2021-08-09 00:15:03 +08:00
|
|
|
di(LocalService::class)->setComponents($array);
|
2021-08-05 15:15:42 +08:00
|
|
|
}
|
2021-08-04 01:07:45 +08:00
|
|
|
|
|
|
|
|
|
2021-08-05 15:15:42 +08:00
|
|
|
/**
|
|
|
|
|
* @param $id
|
|
|
|
|
* @param $definition
|
2021-08-12 17:27:19 +08:00
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
2021-08-05 15:15:42 +08:00
|
|
|
*/
|
2021-08-04 01:09:14 +08:00
|
|
|
public function set($id, $definition): void
|
2021-08-05 15:15:42 +08:00
|
|
|
{
|
2021-08-09 00:15:03 +08:00
|
|
|
di(LocalService::class)->set($id, $definition);
|
2021-08-05 15:15:42 +08:00
|
|
|
}
|
2021-08-04 01:09:14 +08:00
|
|
|
|
|
|
|
|
|
2021-08-05 15:15:42 +08:00
|
|
|
/**
|
|
|
|
|
* @param $id
|
|
|
|
|
* @param $definition
|
2021-08-12 17:27:19 +08:00
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
2021-08-05 15:15:42 +08:00
|
|
|
*/
|
2021-08-04 01:09:58 +08:00
|
|
|
public function has($id): bool
|
2021-08-05 15:15:42 +08:00
|
|
|
{
|
2021-08-09 00:15:03 +08:00
|
|
|
return di(LocalService::class)->has($id);
|
2021-08-05 15:15:42 +08:00
|
|
|
}
|
2021-08-04 01:09:58 +08:00
|
|
|
|
2021-08-04 01:07:45 +08:00
|
|
|
|
2021-08-02 16:38:50 +08:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function moreComponents(): void
|
|
|
|
|
{
|
|
|
|
|
$this->setComponents([
|
2021-08-13 14:37:46 +08:00
|
|
|
'error' => ['class' => ErrorHandler::class],
|
|
|
|
|
'config' => ['class' => Config::class],
|
|
|
|
|
'logger' => ['class' => Logger::class],
|
|
|
|
|
'annotation' => ['class' => SAnnotation::class],
|
|
|
|
|
'router' => ['class' => Router::class],
|
|
|
|
|
'redis' => ['class' => Redis::class],
|
|
|
|
|
'databases' => ['class' => Connection::class],
|
|
|
|
|
'aop' => ['class' => Aop::class],
|
|
|
|
|
'input' => ['class' => HttpParams::class],
|
|
|
|
|
'header' => ['class' => HttpHeaders::class],
|
|
|
|
|
'jwt' => ['class' => Jwt::class],
|
|
|
|
|
'async' => ['class' => Async::class],
|
|
|
|
|
'kafka-container' => ['class' => KafkaProvider::class],
|
|
|
|
|
'response' => ['class' => Response::class],
|
|
|
|
|
'request' => ['class' => Request::class],
|
|
|
|
|
'shutdown' => ['class' => Shutdown::class],
|
2021-08-02 16:38:50 +08:00
|
|
|
]);
|
|
|
|
|
}
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|