Compare commits

...

10 Commits

Author SHA1 Message Date
as2252258 45319c3733 Revert "改名"
This reverts commit fdf58326
2022-01-04 17:27:37 +08:00
as2252258 06c9459f14 Revert "改名"
This reverts commit fdf58326
2022-01-04 16:25:03 +08:00
as2252258 5fd2abcb69 Revert "改名"
This reverts commit fdf58326
2022-01-04 16:22:37 +08:00
as2252258 58a3d91df3 Merge remote-tracking branch 'origin/master' 2022-01-04 16:05:08 +08:00
as2252258 c61842402a Revert "改名"
This reverts commit fdf58326
2022-01-04 16:04:22 +08:00
as2252258 623bae9e97 1 2022-01-04 00:07:26 +08:00
as2252258 12610c218c 1 2022-01-04 00:07:14 +08:00
as2252258 c3ca24884e Revert "改名"
This reverts commit fdf58326
2021-12-31 16:47:17 +08:00
as2252258 02c14874a2 Revert "改名"
This reverts commit fdf58326
2021-12-31 16:46:13 +08:00
as2252258 6eff48e22f Revert "改名"
This reverts commit fdf58326
2021-12-30 18:10:51 +08:00
16 changed files with 396 additions and 229 deletions
-1
View File
@@ -935,7 +935,6 @@ if (!function_exists('di')) {
/**
* @param string $className
* @return mixed
* @throws ReflectionException
*/
function di(string $className): mixed
{
+4 -5
View File
@@ -223,22 +223,21 @@ abstract class BaseApplication extends Component
*/
private function addEvent($key, $value): void
{
$eventProvider = di(EventProvider::class);
if ($value instanceof \Closure || is_object($value)) {
$eventProvider->on($key, $value, 0);
$this->eventProvider->on($key, $value, 0);
return;
}
if (is_array($value)) {
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
$eventProvider->on($key, $value, 0);
$this->eventProvider->on($key, $value, 0);
return;
}
if (is_string($value[0])) {
$value[0] = Kiri::createObject($value[0]);
$eventProvider->on($key, $value, 0);
$this->eventProvider->on($key, $value, 0);
return;
}
@@ -247,7 +246,7 @@ abstract class BaseApplication extends Component
if (!is_callable($item, true)) {
throw new InitException("Class does not hav callback.");
}
$eventProvider->on($key, $item, 0);
$this->eventProvider->on($key, $item, 0);
}
}
+25 -18
View File
@@ -15,7 +15,10 @@ use JetBrains\PhpStorm\Pure;
use Kiri\Di\Container;
use Kiri\Events\EventProvider;
use Kiri\Kiri;
use Note\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* Class Component
@@ -26,6 +29,8 @@ use Psr\Container\ContainerInterface;
class Component implements Configure
{
/**
* BaseAbstract constructor.
*
@@ -40,6 +45,26 @@ class Component implements Configure
}
/**
* @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);
}
/**
* @throws Exception
*/
@@ -48,24 +73,6 @@ class Component implements Configure
}
/**
* @return EventProvider
* @throws \ReflectionException
*/
public function getEventProvider(): EventProvider
{
return Kiri::getDi()->get(EventProvider::class);
}
/**
* @return Container
*/
#[Pure] public function getContainer(): ContainerInterface
{
return Kiri::getDi();
}
/**
* @return string
+174 -175
View File
@@ -3,11 +3,10 @@
namespace Kiri\Abstracts;
use DirectoryIterator;
use Kiri\Kiri;
use Note\Inject;
use Exception;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Psr\Log\LoggerInterface;
use ReflectionException;
use Server\Events\OnWorkerStop;
@@ -19,212 +18,212 @@ 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';
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 = [];
private array $_loggers = [];
const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG];
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']);
}
/**
* @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 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 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 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 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 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 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 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 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)) {
return;
}
/**
* @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);
$_string = '[' . now() . '] production.' . $level . ': ' . $this->_string($message, $context);
file_put_contents('php://output', $_string);
file_put_contents('php://output', $_string);
$this->_loggers[] = $_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/');
/**
* @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);
}
}
file_put_contents($filename, $loggers);
}
}
/**
* @return void
* @throws Exception
*/
public function flush()
{
$this->removeFile(storage());
}
/**
* @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 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;
}
/**
* @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;
}
}
+20
View File
@@ -19,6 +19,7 @@ use Kiri\Kiri;
use Kiri\Pool\Redis as PoolRedis;
use Note\Inject;
use Server\Events\OnWorkerExit;
use Swoole\Timer;
/**
* Class Redis
@@ -80,6 +81,25 @@ class Redis extends Component
}
/**
* @param $key
* @param int $timeout
* @return bool
*/
public function waite($key, int $timeout = 5): bool
{
$time = time();
while (!$this->setNx($key, 1)) {
if (time()- $time >= $timeout) {
return FALSE;
}
usleep(1000);
}
$this->expire($key, $timeout);
return TRUE;
}
/**
* @param $key
* @param int $timeout
+2 -4
View File
@@ -27,7 +27,7 @@ class DateFormat
if ($time === null) {
$time = time();
} else if (is_numeric($time)) {
$length = strlen(floatval($time));
$length = strlen((string)$time);
if ($length != 10 && $length != 13) {
return false;
}
@@ -86,9 +86,7 @@ class DateFormat
return false;
}
$time = date('t', $time);
return $time;
return date('t', $time);
}
/**
+1 -3
View File
@@ -21,6 +21,7 @@ class Dtl extends Component
/**
* Dtl constructor.
* @param $params
* @throws Exception
*/
public function __construct($params)
{
@@ -35,9 +36,6 @@ class Dtl extends Component
*/
public function toArray(): array
{
if (!is_array($this->params)) {
return ArrayAccess::toArray($this->params);
}
return $this->params;
}
+7 -4
View File
@@ -3,6 +3,7 @@
namespace Kiri\Core;
use JetBrains\PhpStorm\Pure;
use ReturnTypeWillChange;
class HashMap implements \ArrayAccess
{
@@ -62,7 +63,7 @@ class HashMap implements \ArrayAccess
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset): bool
public function offsetExists(mixed $offset): bool
{
return isset($this->lists[$offset]);
}
@@ -72,7 +73,7 @@ class HashMap implements \ArrayAccess
* @param mixed $offset
* @return mixed
*/
#[Pure] public function offsetGet($offset): mixed
#[Pure] public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}
@@ -82,7 +83,8 @@ class HashMap implements \ArrayAccess
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
#[ReturnTypeWillChange]
public function offsetSet(mixed $offset, mixed $value)
{
$this->put($offset, $value);
}
@@ -91,7 +93,8 @@ class HashMap implements \ArrayAccess
/**
* @param mixed $offset
*/
public function offsetUnset($offset)
#[ReturnTypeWillChange]
public function offsetUnset(mixed $offset)
{
unset($this->lists[$offset]);
}
+1
View File
@@ -59,6 +59,7 @@ class Help
/**
* @param $xml
* @return mixed
* @throws Exception
*/
public static function toArray($xml): mixed
{
+5 -6
View File
@@ -43,7 +43,7 @@ class Json
* @param bool $asArray
* @return mixed
*/
public static function decode($data, $asArray = true): mixed
public static function decode($data, bool $asArray = true): mixed
{
if (is_array($data) || is_numeric($data)) {
return $data;
@@ -55,14 +55,13 @@ class Json
/**
* @param $code
* @param string $message
* @param array $data
* @param string|array $message
* @param array|int $data
* @param int $count
* @param array $exPageInfo
* @return mixed
* @throws
* @return string|bool
*/
public static function to($code, $message = '', $data = [], $count = 0, $exPageInfo = []): mixed
public static function to($code, string|array $message = '', array|int $data = [], int $count = 0, array $exPageInfo = []): string|bool
{
$params['code'] = $code;
if (!is_string($message)) {
+2 -10
View File
@@ -27,7 +27,7 @@ use Psr\Container\ContainerInterface;
* Class Container
* @package Kiri\Di
*/
class Container extends Component implements ContainerInterface
class Container implements ContainerInterface
{
/**
@@ -65,7 +65,7 @@ class Container extends Component implements ContainerInterface
/**
* @param string $id
* @return mixed
* @throws ReflectionException
* @throws
*/
public function get(string $id): mixed
{
@@ -108,7 +108,6 @@ class Container extends Component implements ContainerInterface
/**
* @param $class
* @return bool
* @throws ReflectionException
*/
public function isInterface($class): bool
{
@@ -212,7 +211,6 @@ class Container extends Component implements ContainerInterface
* @param $className
* @param $method
* @return array
* @throws ReflectionException
*/
public function getMethodAttribute($className, $method = null): array
{
@@ -228,7 +226,6 @@ class Container extends Component implements ContainerInterface
* @param string $class
* @param string|null $property
* @return ReflectionProperty|ReflectionProperty[]|null
* @throws ReflectionException
*/
public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array
{
@@ -339,9 +336,6 @@ class Container extends Component implements ContainerInterface
if (!isset($this->_parameters[$class])) {
$this->_parameters[$class] = [];
}
if (!isset($this->_parameters[$class][$method])) {
$this->_parameters[$class][$method] = [];
}
return $this->_parameters[$class][$method] = $parameters;
}
@@ -360,7 +354,6 @@ class Container extends Component implements ContainerInterface
/**
* @param ReflectionMethod|ReflectionFunction $reflectionMethod
* @return array
* @throws ReflectionException
*/
private function resolveMethodParameters(ReflectionMethod|ReflectionFunction $reflectionMethod): array
{
@@ -394,7 +387,6 @@ class Container extends Component implements ContainerInterface
/**
* @param $class
* @return ReflectionClass|null
* @throws ReflectionException
*/
public function getReflect($class): ?ReflectionClass
{
-1
View File
@@ -13,7 +13,6 @@ use Note\Inject;
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Core\Json;
use Kiri\Events\EventProvider;
use Kiri\Kiri;
use Psr\Log\LoggerInterface;
use Throwable;
-2
View File
@@ -119,10 +119,8 @@ class Gii
*/
private function makeByDatabases($make, InputInterface $input): array
{
$redis = Kiri::getDi()->get(Redis::class);
if ($input->hasOption('name')) {
$this->tableName = $input->getOption('name');
$redis->del('column:' . $this->tableName);
}
return match ($make) {
'controller' => $this->getTable(1, 0),
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace Kiri\Websocket;
class FdCollector
{
public function set($fd)
{
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace Kiri\Websocket;
class Sender
{
public function push($fd, $data)
{
}
public function close($fd)
{
}
}
+120
View File
@@ -0,0 +1,120 @@
<?php
namespace Kiri\Websocket;
use Http\Handler\DataGrip;
use Http\Handler\Router;
use Kiri\Abstracts\Component;
use Kiri\Server\Contract\OnOpenInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Server\Contract\OnCloseInterface;
use Server\Contract\OnHandshakeInterface;
use Server\Contract\OnMessageInterface;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\CloseFrame;
use Swoole\WebSocket\Frame;
/**
*
*/
class Server extends Component implements OnHandshakeInterface, OnMessageInterface, OnCloseInterface
{
public Router $router;
public string $serverName = 'ws';
public mixed $callback = null;
public mixed $server;
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function init()
{
$this->router = $this->container->get(DataGrip::class)->get($this->serverName);
$handler = $this->router->find('/', 'GET');
if (is_int($handler) || is_null($handler)) {
return;
}
$this->callback = $handler->callback[0];
}
/**
* @param \Swoole\Server $server
* @param int $fd
*/
public function onClose(\Swoole\Server $server, int $fd): void
{
if ($this->callback instanceof OnCloseInterface) {
$this->callback->onClose($server, $fd);
}
}
/**
* @param Request $request
* @param Response $response
*/
public function onHandshake(Request $request, Response $response): void
{
try {
if (!$this->callback instanceof OnHandshakeInterface) {
throw new \Exception('Page not found.');
}
$this->callback->onHandshake($request, $response);
$this->afterHandshake($request);
if ($this->server instanceof \Swoole\Coroutine\Http\Server) {
while (true) {
$data = $response->recv();
if ($data === '' || $data === false || $data instanceof CloseFrame) {
$this->onClose($this->server, $response->fd);
break;
}
$this->onMessage($this->server, $data);
}
}
} catch (\Throwable $throwable) {
$response->status(500, $throwable->getMessage());
$response->end();
}
}
/**
* @param $request
*/
public function afterHandshake($request)
{
if (!($this->callback instanceof OnOpenInterface)) {
return;
}
$this->callback->onOpen($this->server, $request);
}
/**
* @param \Swoole\Server $server
* @param Frame $frame
*/
public function onMessage(\Swoole\Server $server, Frame $frame): void
{
if ($this->callback instanceof OnMessageInterface) {
$this->callback->onMessage($server, $frame);
}
}
}