e
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
abstract class AbstractServer extends Component
|
||||
{
|
||||
|
||||
|
||||
public string $name = 'http';
|
||||
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
abstract class AbstractServer extends Component
|
||||
{
|
||||
|
||||
|
||||
public string $name = 'http';
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,451 +1,451 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/10/7 0007
|
||||
* Time: 2:13
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Database\Connection;
|
||||
use Exception;
|
||||
use Http\Handler\Router;
|
||||
use Kafka\KafkaProvider;
|
||||
use Kiri\{Async, Kiri};
|
||||
use Kiri\Annotation\Annotation as SAnnotation;
|
||||
use Kiri\Cache\Redis;
|
||||
use Kiri\Di\LocalService;
|
||||
use Kiri\Error\{ErrorHandler, Logger};
|
||||
use Kiri\Exception\{InitException, NotFindClassException};
|
||||
use ReflectionException;
|
||||
use Server\{Contract\OnTaskInterface, Server, ServerManager, Tasker\AsyncTaskExecute};
|
||||
use Swoole\Table;
|
||||
|
||||
/**
|
||||
* Class BaseApplication
|
||||
* @package Kiri\Kiri\Base
|
||||
*/
|
||||
abstract class BaseApplication extends Component
|
||||
{
|
||||
|
||||
use TraitApplication;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $storage = APP_PATH . 'storage';
|
||||
|
||||
public string $envPath = APP_PATH . '.env';
|
||||
|
||||
/**
|
||||
* Init constructor.
|
||||
*
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
Kiri::init($this);
|
||||
|
||||
$config = sweep(APP_PATH . '/config');
|
||||
|
||||
$this->moreComponents();
|
||||
$this->parseInt($config);
|
||||
$this->parseEvents($config);
|
||||
$this->initErrorHandler();
|
||||
$this->enableEnvConfig();
|
||||
$this->mapping($config['mapping'] ?? []);
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $mapping
|
||||
*/
|
||||
public function mapping(array $mapping)
|
||||
{
|
||||
$di = Kiri::getDi();
|
||||
foreach ($mapping as $interface => $class) {
|
||||
$di->mapping($interface, $class);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
if ($this->has($name)) {
|
||||
return $this->get($name);
|
||||
}
|
||||
return parent::__get($name); // TODO: Change the autogenerated stub
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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)) {
|
||||
$value = Kiri::createObject($value);
|
||||
}
|
||||
$this->addEvent($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param OnTaskInterface $execute
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function task(OnTaskInterface $execute): void
|
||||
{
|
||||
di(AsyncTaskExecute::class)->execute($execute);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @throws InitException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addEvent($key, $value): void
|
||||
{
|
||||
if ($value instanceof \Closure || is_object($value)) {
|
||||
$this->eventProvider->on($key, $value, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (is_array($value)) {
|
||||
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
|
||||
$this->eventProvider->on($key, $value, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_string($value[0])) {
|
||||
$value[0] = Kiri::createObject($value[0]);
|
||||
$this->eventProvider->on($key, $value, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
foreach ($value as $item) {
|
||||
if (!is_callable($item, true)) {
|
||||
throw new InitException("Class does not hav callback.");
|
||||
}
|
||||
$this->eventProvider->on($key, $item, 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function get($name): mixed
|
||||
{
|
||||
return di(LocalService::class)->get($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLocalIps(): mixed
|
||||
{
|
||||
return swoole_get_local_ip();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFirstLocal(): mixed
|
||||
{
|
||||
return current($this->getLocalIps());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Logger
|
||||
* @throws
|
||||
*/
|
||||
public function getLogger(): Logger
|
||||
{
|
||||
return $this->get('logger');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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
|
||||
* @throws
|
||||
*/
|
||||
public function getServer(): Server
|
||||
{
|
||||
return $this->get('server');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Async
|
||||
* @throws
|
||||
*/
|
||||
public function getAsync(): Async
|
||||
{
|
||||
return $this->get('async');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $array
|
||||
*/
|
||||
private function setComponents($array): void
|
||||
{
|
||||
di(LocalService::class)->setComponents($array);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $definition
|
||||
*/
|
||||
public function set($id, $definition): void
|
||||
{
|
||||
di(LocalService::class)->set($id, $definition);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return bool
|
||||
*/
|
||||
public function has($id): bool
|
||||
{
|
||||
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],
|
||||
'jwt' => ['class' => Jwt::class],
|
||||
'async' => ['class' => Async::class],
|
||||
'kafka-container' => ['class' => KafkaProvider::class],
|
||||
]);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/10/7 0007
|
||||
* Time: 2:13
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Database\Connection;
|
||||
use Exception;
|
||||
use Http\Handler\Router;
|
||||
use Kafka\KafkaProvider;
|
||||
use Kiri\{Async, Kiri};
|
||||
use Kiri\Annotation\Annotation as SAnnotation;
|
||||
use Kiri\Cache\Redis;
|
||||
use Kiri\Di\LocalService;
|
||||
use Kiri\Error\{ErrorHandler, Logger};
|
||||
use Kiri\Exception\{InitException, NotFindClassException};
|
||||
use ReflectionException;
|
||||
use Server\{Contract\OnTaskInterface, Server, ServerManager, Tasker\AsyncTaskExecute};
|
||||
use Swoole\Table;
|
||||
|
||||
/**
|
||||
* Class BaseApplication
|
||||
* @package Kiri\Kiri\Base
|
||||
*/
|
||||
abstract class BaseApplication extends Component
|
||||
{
|
||||
|
||||
use TraitApplication;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $storage = APP_PATH . 'storage';
|
||||
|
||||
public string $envPath = APP_PATH . '.env';
|
||||
|
||||
/**
|
||||
* Init constructor.
|
||||
*
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
Kiri::init($this);
|
||||
|
||||
$config = sweep(APP_PATH . '/config');
|
||||
|
||||
$this->moreComponents();
|
||||
$this->parseInt($config);
|
||||
$this->parseEvents($config);
|
||||
$this->initErrorHandler();
|
||||
$this->enableEnvConfig();
|
||||
$this->mapping($config['mapping'] ?? []);
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $mapping
|
||||
*/
|
||||
public function mapping(array $mapping)
|
||||
{
|
||||
$di = Kiri::getDi();
|
||||
foreach ($mapping as $interface => $class) {
|
||||
$di->mapping($interface, $class);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
if ($this->has($name)) {
|
||||
return $this->get($name);
|
||||
}
|
||||
return parent::__get($name); // TODO: Change the autogenerated stub
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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)) {
|
||||
$value = Kiri::createObject($value);
|
||||
}
|
||||
$this->addEvent($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param OnTaskInterface $execute
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function task(OnTaskInterface $execute): void
|
||||
{
|
||||
di(AsyncTaskExecute::class)->execute($execute);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @throws InitException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addEvent($key, $value): void
|
||||
{
|
||||
if ($value instanceof \Closure || is_object($value)) {
|
||||
$this->eventProvider->on($key, $value, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (is_array($value)) {
|
||||
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
|
||||
$this->eventProvider->on($key, $value, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_string($value[0])) {
|
||||
$value[0] = Kiri::createObject($value[0]);
|
||||
$this->eventProvider->on($key, $value, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
foreach ($value as $item) {
|
||||
if (!is_callable($item, true)) {
|
||||
throw new InitException("Class does not hav callback.");
|
||||
}
|
||||
$this->eventProvider->on($key, $item, 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function get($name): mixed
|
||||
{
|
||||
return di(LocalService::class)->get($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLocalIps(): mixed
|
||||
{
|
||||
return swoole_get_local_ip();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFirstLocal(): mixed
|
||||
{
|
||||
return current($this->getLocalIps());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Logger
|
||||
* @throws
|
||||
*/
|
||||
public function getLogger(): Logger
|
||||
{
|
||||
return $this->get('logger');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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
|
||||
* @throws
|
||||
*/
|
||||
public function getServer(): Server
|
||||
{
|
||||
return $this->get('server');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Async
|
||||
* @throws
|
||||
*/
|
||||
public function getAsync(): Async
|
||||
{
|
||||
return $this->get('async');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $array
|
||||
*/
|
||||
private function setComponents($array): void
|
||||
{
|
||||
di(LocalService::class)->setComponents($array);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $definition
|
||||
*/
|
||||
public function set($id, $definition): void
|
||||
{
|
||||
di(LocalService::class)->set($id, $definition);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return bool
|
||||
*/
|
||||
public function has($id): bool
|
||||
{
|
||||
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],
|
||||
'jwt' => ['class' => Jwt::class],
|
||||
'async' => ['class' => Async::class],
|
||||
'kafka-container' => ['class' => KafkaProvider::class],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
|
||||
use Swoole\Coroutine;
|
||||
|
||||
abstract class BaseContext
|
||||
{
|
||||
protected static array $pool = [];
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
|
||||
use Swoole\Coroutine;
|
||||
|
||||
abstract class BaseContext
|
||||
{
|
||||
protected static array $pool = [];
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Core\Json;
|
||||
|
||||
/**
|
||||
* Class BaseGoto
|
||||
* @package Kiri\Abstracts
|
||||
*/
|
||||
class BaseGoto extends Component
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param int $statusCode
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function end(string $message, int $statusCode = 200): mixed
|
||||
{
|
||||
throw new Exception(Json::to(12350, $message), $statusCode);
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Core\Json;
|
||||
|
||||
/**
|
||||
* Class BaseGoto
|
||||
* @package Kiri\Abstracts
|
||||
*/
|
||||
class BaseGoto extends Component
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param int $statusCode
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function end(string $message, int $statusCode = 200): mixed
|
||||
{
|
||||
throw new Exception(Json::to(12350, $message), $statusCode);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
abstract class Command extends Component
|
||||
{
|
||||
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
abstract class Command extends Component
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
+260
-260
@@ -1,260 +1,260 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/30 0030
|
||||
* Time: 14:28
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Di\Container;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
/**
|
||||
* Class Component
|
||||
* @package Kiri\Kiri\Base
|
||||
* @property ContainerInterface|Container $container
|
||||
* @property EventProvider $eventProvider
|
||||
* @property EventDispatch $eventDispatch
|
||||
*/
|
||||
class Component implements Configure
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* BaseAbstract constructor.
|
||||
*
|
||||
* @param array $config
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
if (!empty($config) && is_array($config)) {
|
||||
Kiri::configure($this, $config);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Container|ContainerInterface
|
||||
*/
|
||||
#[Pure] public function getContainer(): ContainerInterface|Container
|
||||
{
|
||||
return Kiri::getDi();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return EventProvider
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function getEventProvider(): EventProvider
|
||||
{
|
||||
return $this->getContainer()->get(EventProvider::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return EventDispatch
|
||||
*/
|
||||
protected function getEventDispatch(): EventDispatch
|
||||
{
|
||||
return Kiri::getDi()->get(EventDispatch::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public static function className(): string
|
||||
{
|
||||
return static::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$method = 'set' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->{$method}($value);
|
||||
} else {
|
||||
throw new Exception('The set name ' . $name . ' not find in class ' . static::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
$method = 'get' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
return $this->$method();
|
||||
} else {
|
||||
throw new Exception('The get name ' . $name . ' not find in class ' . 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));
|
||||
} else {
|
||||
if (!is_string($message)) {
|
||||
$message = json_encode($message, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
$this->error($message);
|
||||
}
|
||||
Kiri::app()->getLogger()->fail($message, $model);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Logger
|
||||
* @throws Exception
|
||||
*/
|
||||
private function logger(): Logger
|
||||
{
|
||||
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;
|
||||
} else {
|
||||
if (is_null($method)) {
|
||||
$method = [];
|
||||
}
|
||||
$context = $method;
|
||||
}
|
||||
if (!empty($method)) $context['method'] = $method;
|
||||
if (!empty($file)) $context['file'] = $file;
|
||||
|
||||
$this->logger()->error($message, $context);
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/30 0030
|
||||
* Time: 14:28
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Di\Container;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
/**
|
||||
* Class Component
|
||||
* @package Kiri\Kiri\Base
|
||||
* @property ContainerInterface|Container $container
|
||||
* @property EventProvider $eventProvider
|
||||
* @property EventDispatch $eventDispatch
|
||||
*/
|
||||
class Component implements Configure
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* BaseAbstract constructor.
|
||||
*
|
||||
* @param array $config
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
if (!empty($config) && is_array($config)) {
|
||||
Kiri::configure($this, $config);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Container|ContainerInterface
|
||||
*/
|
||||
#[Pure] public function getContainer(): ContainerInterface|Container
|
||||
{
|
||||
return Kiri::getDi();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return EventProvider
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function getEventProvider(): EventProvider
|
||||
{
|
||||
return $this->getContainer()->get(EventProvider::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return EventDispatch
|
||||
*/
|
||||
protected function getEventDispatch(): EventDispatch
|
||||
{
|
||||
return Kiri::getDi()->get(EventDispatch::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public static function className(): string
|
||||
{
|
||||
return static::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$method = 'set' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->{$method}($value);
|
||||
} else {
|
||||
throw new Exception('The set name ' . $name . ' not find in class ' . static::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
$method = 'get' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
return $this->$method();
|
||||
} else {
|
||||
throw new Exception('The get name ' . $name . ' not find in class ' . 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));
|
||||
} else {
|
||||
if (!is_string($message)) {
|
||||
$message = json_encode($message, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
$this->error($message);
|
||||
}
|
||||
Kiri::app()->getLogger()->fail($message, $model);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Logger
|
||||
* @throws Exception
|
||||
*/
|
||||
private function logger(): Logger
|
||||
{
|
||||
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;
|
||||
} else {
|
||||
if (is_null($method)) {
|
||||
$method = [];
|
||||
}
|
||||
$context = $method;
|
||||
}
|
||||
if (!empty($method)) $context['method'] = $method;
|
||||
if (!empty($file)) $context['file'] = $file;
|
||||
|
||||
$this->logger()->error($message, $context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+128
-128
@@ -1,128 +1,128 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/5/24 0024
|
||||
* Time: 11:50
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
use Kiri\Exception\ConfigException;
|
||||
|
||||
|
||||
/**
|
||||
* Class Config
|
||||
* @package Kiri\Kiri\Base
|
||||
*/
|
||||
class Config extends Component
|
||||
{
|
||||
|
||||
const ERROR_MESSAGE = 'The not find %s in app configs.';
|
||||
|
||||
protected static mixed $data = [];
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getData(): mixed
|
||||
{
|
||||
return static::$data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
public static function setData($key, $value): mixed
|
||||
{
|
||||
return static::$data[$key] = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $configs
|
||||
*/
|
||||
public static function sets(array $configs)
|
||||
{
|
||||
if (empty($configs)) {
|
||||
return;
|
||||
}
|
||||
static::$data = $configs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param bool $try
|
||||
* @param mixed|null $default
|
||||
* @return mixed
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public static function get($key, mixed $default = null, bool $try = FALSE): mixed
|
||||
{
|
||||
$instance = static::$data;
|
||||
if (!str_contains($key, '.')) {
|
||||
return $instance[$key] ?? $default;
|
||||
}
|
||||
foreach (explode('.', $key) as $value) {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($instance[$value])) {
|
||||
if ($try) {
|
||||
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
if (!is_array($instance[$value])) {
|
||||
return $instance[$value];
|
||||
}
|
||||
$instance = $instance[$value];
|
||||
}
|
||||
return empty($instance) ? $default : $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
public static function set($key, $value): mixed
|
||||
{
|
||||
$explode = explode('.', $key);
|
||||
$parent = &static::$data;
|
||||
foreach ($explode as $item) {
|
||||
if (!isset($parent[$item])) {
|
||||
$parent[$item] = [];
|
||||
}
|
||||
$parent = &$parent[$item];
|
||||
}
|
||||
$parent = $value;
|
||||
|
||||
unset($parent);
|
||||
|
||||
return static::$data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param bool $must_not_null
|
||||
* @return bool
|
||||
*/
|
||||
public static function has($key, bool $must_not_null = false): bool
|
||||
{
|
||||
if (!isset(static::$data[$key])) {
|
||||
return false;
|
||||
}
|
||||
$config = static::$data[$key];
|
||||
if ($must_not_null === false) {
|
||||
return true;
|
||||
}
|
||||
return !empty($config);
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/5/24 0024
|
||||
* Time: 11:50
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
use Kiri\Exception\ConfigException;
|
||||
|
||||
|
||||
/**
|
||||
* Class Config
|
||||
* @package Kiri\Kiri\Base
|
||||
*/
|
||||
class Config extends Component
|
||||
{
|
||||
|
||||
const ERROR_MESSAGE = 'The not find %s in app configs.';
|
||||
|
||||
protected static mixed $data = [];
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getData(): mixed
|
||||
{
|
||||
return static::$data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
public static function setData($key, $value): mixed
|
||||
{
|
||||
return static::$data[$key] = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $configs
|
||||
*/
|
||||
public static function sets(array $configs)
|
||||
{
|
||||
if (empty($configs)) {
|
||||
return;
|
||||
}
|
||||
static::$data = $configs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param bool $try
|
||||
* @param mixed|null $default
|
||||
* @return mixed
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public static function get($key, mixed $default = null, bool $try = FALSE): mixed
|
||||
{
|
||||
$instance = static::$data;
|
||||
if (!str_contains($key, '.')) {
|
||||
return $instance[$key] ?? $default;
|
||||
}
|
||||
foreach (explode('.', $key) as $value) {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($instance[$value])) {
|
||||
if ($try) {
|
||||
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
if (!is_array($instance[$value])) {
|
||||
return $instance[$value];
|
||||
}
|
||||
$instance = $instance[$value];
|
||||
}
|
||||
return empty($instance) ? $default : $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
public static function set($key, $value): mixed
|
||||
{
|
||||
$explode = explode('.', $key);
|
||||
$parent = &static::$data;
|
||||
foreach ($explode as $item) {
|
||||
if (!isset($parent[$item])) {
|
||||
$parent[$item] = [];
|
||||
}
|
||||
$parent = &$parent[$item];
|
||||
}
|
||||
$parent = $value;
|
||||
|
||||
unset($parent);
|
||||
|
||||
return static::$data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param bool $must_not_null
|
||||
* @return bool
|
||||
*/
|
||||
public static function has($key, bool $must_not_null = false): bool
|
||||
{
|
||||
if (!isset(static::$data[$key])) {
|
||||
return false;
|
||||
}
|
||||
$config = static::$data[$key];
|
||||
if ($must_not_null === false) {
|
||||
return true;
|
||||
}
|
||||
return !empty($config);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/30 0030
|
||||
* Time: 14:11
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
/**
|
||||
* Interface Configure
|
||||
* @package Kiri\Kiri\Base
|
||||
*/
|
||||
interface Configure
|
||||
{
|
||||
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/3/30 0030
|
||||
* Time: 14:11
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
/**
|
||||
* Interface Configure
|
||||
* @package Kiri\Kiri\Base
|
||||
*/
|
||||
interface Configure
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Kiri\Core\Dtl;
|
||||
|
||||
|
||||
/**
|
||||
* Interface IListener
|
||||
* @package Kiri\Abstracts
|
||||
*/
|
||||
interface IListener
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Dtl $dtl
|
||||
* @return mixed
|
||||
*/
|
||||
public function execute(Dtl $dtl): mixed;
|
||||
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Kiri\Core\Dtl;
|
||||
|
||||
|
||||
/**
|
||||
* Interface IListener
|
||||
* @package Kiri\Abstracts
|
||||
*/
|
||||
interface IListener
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Dtl $dtl
|
||||
* @return mixed
|
||||
*/
|
||||
public function execute(Dtl $dtl): mixed;
|
||||
|
||||
|
||||
}
|
||||
|
||||
+111
-111
@@ -1,111 +1,111 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
class Input
|
||||
{
|
||||
|
||||
private array $_argv = [];
|
||||
|
||||
|
||||
private string $_command = '';
|
||||
|
||||
|
||||
/**
|
||||
* Input constructor.
|
||||
* @param $argv
|
||||
* @throws
|
||||
*/
|
||||
public function __construct($argv)
|
||||
{
|
||||
$this->_argv = $this->resolve($argv);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCommandName(): string
|
||||
{
|
||||
return $this->_command;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null): mixed
|
||||
{
|
||||
return $this->_argv[$key] ?? $default;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($key): bool
|
||||
{
|
||||
return isset($this->_argv[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return $this
|
||||
*/
|
||||
public function set($key, $value): static
|
||||
{
|
||||
$this->_argv[$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
*/
|
||||
public function toJson(): bool|string
|
||||
{
|
||||
return json_encode($this->_argv, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $parameters
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function resolve($parameters): array
|
||||
{
|
||||
$arrays = [];
|
||||
$parameters = array_slice($parameters, 1);
|
||||
if (empty($parameters)) {
|
||||
return $arrays;
|
||||
}
|
||||
$this->_command = array_shift($parameters);
|
||||
foreach ($parameters as $parameter) {
|
||||
$explode = explode('=', $parameter);
|
||||
if (count($explode) < 2) {
|
||||
continue;
|
||||
}
|
||||
$arrays[array_shift($explode)] = current($explode);
|
||||
}
|
||||
return $arrays;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCommand(): string
|
||||
{
|
||||
return $this->_command;
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
class Input
|
||||
{
|
||||
|
||||
private array $_argv = [];
|
||||
|
||||
|
||||
private string $_command = '';
|
||||
|
||||
|
||||
/**
|
||||
* Input constructor.
|
||||
* @param $argv
|
||||
* @throws
|
||||
*/
|
||||
public function __construct($argv)
|
||||
{
|
||||
$this->_argv = $this->resolve($argv);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCommandName(): string
|
||||
{
|
||||
return $this->_command;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param null $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null): mixed
|
||||
{
|
||||
return $this->_argv[$key] ?? $default;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($key): bool
|
||||
{
|
||||
return isset($this->_argv[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return $this
|
||||
*/
|
||||
public function set($key, $value): static
|
||||
{
|
||||
$this->_argv[$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
*/
|
||||
public function toJson(): bool|string
|
||||
{
|
||||
return json_encode($this->_argv, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $parameters
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function resolve($parameters): array
|
||||
{
|
||||
$arrays = [];
|
||||
$parameters = array_slice($parameters, 1);
|
||||
if (empty($parameters)) {
|
||||
return $arrays;
|
||||
}
|
||||
$this->_command = array_shift($parameters);
|
||||
foreach ($parameters as $parameter) {
|
||||
$explode = explode('=', $parameter);
|
||||
if (count($explode) < 2) {
|
||||
continue;
|
||||
}
|
||||
$arrays[array_shift($explode)] = current($explode);
|
||||
}
|
||||
return $arrays;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCommand(): string
|
||||
{
|
||||
return $this->_command;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
interface Kernel
|
||||
{
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getCommands(): array;
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
interface Kernel
|
||||
{
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getCommands(): array;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class Listener
|
||||
* @package Kiri\Abstracts
|
||||
* 监听的名称
|
||||
*/
|
||||
abstract class Listener extends Component implements IListener
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class Listener
|
||||
* @package Kiri\Abstracts
|
||||
* 监听的名称
|
||||
*/
|
||||
abstract class Listener extends Component implements IListener
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
+229
-229
@@ -1,229 +1,229 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
use DirectoryIterator;
|
||||
use Exception;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ReflectionException;
|
||||
use Server\Events\OnWorkerStop;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Logger implements LoggerInterface
|
||||
{
|
||||
|
||||
const EMERGENCY = 'emergency';
|
||||
const ALERT = 'alert';
|
||||
const CRITICAL = 'critical';
|
||||
const ERROR = 'error';
|
||||
const WARNING = 'warning';
|
||||
const NOTICE = 'notice';
|
||||
const INFO = 'info';
|
||||
const DEBUG = 'debug';
|
||||
|
||||
|
||||
private array $_loggers = [];
|
||||
|
||||
|
||||
const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG];
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
Kiri::getDi()->get(EventProvider::class)->on(OnWorkerStop::class, [$this, 'onAfterRequest']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*
|
||||
* 紧急情况
|
||||
*/
|
||||
public function emergency($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement emergency() method.
|
||||
$this->log(Logger::EMERGENCY, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*
|
||||
* 应该警惕的
|
||||
*/
|
||||
public function alert($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement alert() method.
|
||||
$this->log(Logger::ALERT, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*
|
||||
* 关键性的日志
|
||||
*/
|
||||
public function critical($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement critical() method.
|
||||
$this->log(Logger::CRITICAL, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function error($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement error() method.
|
||||
$this->log(Logger::ERROR, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function warning($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement warning() method.
|
||||
$this->log(Logger::WARNING, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function notice($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement notice() method.
|
||||
$this->log(Logger::NOTICE, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function info($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement info() method.
|
||||
$this->log(Logger::INFO, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function debug($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement debug() method.
|
||||
$this->log(Logger::DEBUG, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
// TODO: Implement log() method.
|
||||
$levels = Config::get('log.level', Logger::LOGGER_LEVELS);
|
||||
if (!in_array($level, $levels) || str_contains($message, 'Event::rshutdown')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$_string = '[' . now() . '] production.' . $level . ': ' . $this->_string($message, $context);
|
||||
|
||||
file_put_contents('php://output', $_string);
|
||||
|
||||
$this->_loggers[] = $_string;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param OnWorkerStop $param
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onAfterRequest(OnWorkerStop $param)
|
||||
{
|
||||
$loggers = implode(PHP_EOL, $this->_loggers);
|
||||
$this->_loggers = [];
|
||||
if (!empty($loggers)) {
|
||||
$filename = storage('log-' . date('Y-m-d') . '.log', 'log/');
|
||||
|
||||
file_put_contents($filename, $loggers);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function flush()
|
||||
{
|
||||
$this->removeFile(storage());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $dirname
|
||||
* @return void
|
||||
*/
|
||||
private function removeFile(string $dirname)
|
||||
{
|
||||
$paths = new DirectoryIterator($dirname);
|
||||
/** @var DirectoryIterator $path */
|
||||
foreach ($paths as $path) {
|
||||
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
|
||||
continue;
|
||||
}
|
||||
if ($path->isDir()) {
|
||||
$directory = rtrim($path->getRealPath(), '/');
|
||||
$this->removeFile($directory);
|
||||
}
|
||||
@unlink($path->getRealPath());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $context
|
||||
* @return string
|
||||
*/
|
||||
private function _string($message, $context): string
|
||||
{
|
||||
if (!empty($context)) {
|
||||
return $message . ' ' . PHP_EOL . print_r($context, TRUE) . PHP_EOL;
|
||||
}
|
||||
return $message . PHP_EOL;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
use DirectoryIterator;
|
||||
use Exception;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ReflectionException;
|
||||
use Server\Events\OnWorkerStop;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Logger implements LoggerInterface
|
||||
{
|
||||
|
||||
const EMERGENCY = 'emergency';
|
||||
const ALERT = 'alert';
|
||||
const CRITICAL = 'critical';
|
||||
const ERROR = 'error';
|
||||
const WARNING = 'warning';
|
||||
const NOTICE = 'notice';
|
||||
const INFO = 'info';
|
||||
const DEBUG = 'debug';
|
||||
|
||||
|
||||
private array $_loggers = [];
|
||||
|
||||
|
||||
const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG];
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
Kiri::getDi()->get(EventProvider::class)->on(OnWorkerStop::class, [$this, 'onAfterRequest']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*
|
||||
* 紧急情况
|
||||
*/
|
||||
public function emergency($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement emergency() method.
|
||||
$this->log(Logger::EMERGENCY, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*
|
||||
* 应该警惕的
|
||||
*/
|
||||
public function alert($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement alert() method.
|
||||
$this->log(Logger::ALERT, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*
|
||||
* 关键性的日志
|
||||
*/
|
||||
public function critical($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement critical() method.
|
||||
$this->log(Logger::CRITICAL, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function error($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement error() method.
|
||||
$this->log(Logger::ERROR, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function warning($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement warning() method.
|
||||
$this->log(Logger::WARNING, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function notice($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement notice() method.
|
||||
$this->log(Logger::NOTICE, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function info($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement info() method.
|
||||
$this->log(Logger::INFO, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function debug($message, array $context = [])
|
||||
{
|
||||
// TODO: Implement debug() method.
|
||||
$this->log(Logger::DEBUG, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
// TODO: Implement log() method.
|
||||
$levels = Config::get('log.level', Logger::LOGGER_LEVELS);
|
||||
if (!in_array($level, $levels) || str_contains($message, 'Event::rshutdown')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$_string = '[' . now() . '] production.' . $level . ': ' . $this->_string($message, $context);
|
||||
|
||||
file_put_contents('php://output', $_string);
|
||||
|
||||
$this->_loggers[] = $_string;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param OnWorkerStop $param
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onAfterRequest(OnWorkerStop $param)
|
||||
{
|
||||
$loggers = implode(PHP_EOL, $this->_loggers);
|
||||
$this->_loggers = [];
|
||||
if (!empty($loggers)) {
|
||||
$filename = storage('log-' . date('Y-m-d') . '.log', 'log/');
|
||||
|
||||
file_put_contents($filename, $loggers);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function flush()
|
||||
{
|
||||
$this->removeFile(storage());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $dirname
|
||||
* @return void
|
||||
*/
|
||||
private function removeFile(string $dirname)
|
||||
{
|
||||
$paths = new DirectoryIterator($dirname);
|
||||
/** @var DirectoryIterator $path */
|
||||
foreach ($paths as $path) {
|
||||
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
|
||||
continue;
|
||||
}
|
||||
if ($path->isDir()) {
|
||||
$directory = rtrim($path->getRealPath(), '/');
|
||||
$this->removeFile($directory);
|
||||
}
|
||||
@unlink($path->getRealPath());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $context
|
||||
* @return string
|
||||
*/
|
||||
private function _string($message, $context): string
|
||||
{
|
||||
if (!empty($context)) {
|
||||
return $message . ' ' . PHP_EOL . print_r($context, TRUE) . PHP_EOL;
|
||||
}
|
||||
return $message . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Kiri\Application;
|
||||
|
||||
interface Provider
|
||||
{
|
||||
|
||||
public function onImport(Application $application);
|
||||
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Kiri\Application;
|
||||
|
||||
interface Provider
|
||||
{
|
||||
|
||||
public function onImport(Application $application);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
/**
|
||||
* Class Providers
|
||||
* @package Kiri\Abstracts
|
||||
*/
|
||||
abstract class Providers extends Component implements Provider
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
/**
|
||||
* Class Providers
|
||||
* @package Kiri\Abstracts
|
||||
*/
|
||||
abstract class Providers extends Component implements Provider
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Kiri\Annotation\Annotation as SAnnotation;
|
||||
use Database\Connection;
|
||||
use Database\DatabasesProviders;
|
||||
use Http\Handler\Router;
|
||||
use Server\Server;
|
||||
use Kiri\Async;
|
||||
use Kiri\Error\Logger;
|
||||
use Kiri\Jwt\JWTAuth;
|
||||
|
||||
/**
|
||||
* Trait TraitApplication
|
||||
* @package Kiri\Abstracts
|
||||
* @property Router $router
|
||||
* @property Server $server
|
||||
* @property DatabasesProviders $db
|
||||
* @property Async $async
|
||||
* @property Logger $logger
|
||||
* @property JWTAuth $jwt
|
||||
* @property SAnnotation $annotation
|
||||
* @property BaseGoto $goto
|
||||
* @property Connection $databases
|
||||
*/
|
||||
trait TraitApplication
|
||||
{
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
|
||||
use Kiri\Annotation\Annotation as SAnnotation;
|
||||
use Database\Connection;
|
||||
use Database\DatabasesProviders;
|
||||
use Http\Handler\Router;
|
||||
use Server\Server;
|
||||
use Kiri\Async;
|
||||
use Kiri\Error\Logger;
|
||||
use Kiri\Jwt\JWTAuth;
|
||||
|
||||
/**
|
||||
* Trait TraitApplication
|
||||
* @package Kiri\Abstracts
|
||||
* @property Router $router
|
||||
* @property Server $server
|
||||
* @property DatabasesProviders $db
|
||||
* @property Async $async
|
||||
* @property Logger $logger
|
||||
* @property JWTAuth $jwt
|
||||
* @property SAnnotation $annotation
|
||||
* @property BaseGoto $goto
|
||||
* @property Connection $databases
|
||||
*/
|
||||
trait TraitApplication
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user