modify
This commit is contained in:
@@ -154,8 +154,9 @@ abstract class BaseApplication extends Service
|
||||
*/
|
||||
public function parseInt($config)
|
||||
{
|
||||
$_config = Snowflake::app()->getConfig();
|
||||
foreach ($config as $key => $value) {
|
||||
Config::set($key, $value);
|
||||
$_config->setData($key, $value);
|
||||
}
|
||||
if ($storage = Config::get('storage', 'storage')) {
|
||||
if (!str_contains($storage, APP_PATH)) {
|
||||
|
||||
@@ -23,119 +23,6 @@ use Snowflake\Snowflake;
|
||||
class Component extends BaseObject
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $_events = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $name [事件名称]
|
||||
* @param $callback [回调函数]
|
||||
* @param array $param [函数参数]
|
||||
*
|
||||
* {
|
||||
* 事件名, 回调, 参数
|
||||
* }
|
||||
*/
|
||||
public function on($name, $callback, $param = [])
|
||||
{
|
||||
if (isset($this->_events[$name])) {
|
||||
array_push($this->_events[$name], [$callback, $param]);
|
||||
} else {
|
||||
$this->_events[$name][] = [$callback, $param];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $callback
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function hasEvent($name, $callback = null): bool
|
||||
{
|
||||
if (!isset($this->_events[$name])) {
|
||||
return false;
|
||||
}
|
||||
if (!is_array($this->_events[$name])) {
|
||||
return false;
|
||||
}
|
||||
foreach ($this->_events[$name] as $event) {
|
||||
[$_callback, $param] = $event;
|
||||
if ($_callback === $callback) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $event
|
||||
* @param array $params
|
||||
* @param bool $isRemove
|
||||
* @throws Exception
|
||||
*/
|
||||
public function trigger($name, $event = null, $params = [], $isRemove = false)
|
||||
{
|
||||
$aEvents = Snowflake::app()->getEvent();
|
||||
if (isset($this->_events[$name])) {
|
||||
$events = $this->_events[$name];
|
||||
foreach ($events as $key => $_event) {
|
||||
if (!empty($event)) {
|
||||
$_event = $event;
|
||||
}
|
||||
call_user_func($_event, ...$params);
|
||||
if ($isRemove) {
|
||||
unset($this->_events[$name][$key]);
|
||||
$aEvents->of($name, $_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
$aEvents->trigger($name, $event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $handler
|
||||
* @return void
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function off($name, $handler = NULL): void
|
||||
{
|
||||
$aEvents = Snowflake::app()->getEvent();
|
||||
if (!isset($this->_events[$name])) {
|
||||
$aEvents->of($name, $handler);
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($handler)) {
|
||||
unset($this->_events[$name]);
|
||||
|
||||
$aEvents->of($name, $handler);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->_events[$name] as $key => $val) {
|
||||
if ($val[0] != $handler) {
|
||||
continue;
|
||||
}
|
||||
unset($this->_events[$name][$key]);
|
||||
|
||||
break;
|
||||
}
|
||||
$aEvents->of($name, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function offAll()
|
||||
{
|
||||
$this->_events = [];
|
||||
$aEvents = Snowflake::app()->getEvent();
|
||||
$aEvents->clean();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
|
||||
@@ -9,16 +9,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Snowflake\Cache;
|
||||
|
||||
use Annotation\Aspect;
|
||||
use Database\InjectProperty;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
|
||||
+66
-59
@@ -18,72 +18,79 @@ class Channel extends Component
|
||||
{
|
||||
|
||||
|
||||
private array $_channels = [];
|
||||
private array $_channels = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function push(mixed $value, string $name = ''): void
|
||||
{
|
||||
$channel = $this->channelInit($name);
|
||||
$channel->enqueue($value);
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function push(mixed $value, string $name = ''): void
|
||||
{
|
||||
unset($value);
|
||||
return;
|
||||
$channel = $this->channelInit($name);
|
||||
$channel->enqueue($value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool|SplQueue
|
||||
*/
|
||||
private function channelInit(string $name = ''): bool|SplQueue
|
||||
{
|
||||
if (!isset($this->_channels[$name]) || !($this->_channels[$name] instanceof SplQueue)) {
|
||||
$this->_channels[$name] = new SplQueue();
|
||||
}
|
||||
return $this->_channels[$name];
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool|SplQueue
|
||||
*/
|
||||
private function channelInit(string $name = ''): bool|SplQueue
|
||||
{
|
||||
if (!isset($this->_channels[$name]) || !($this->_channels[$name] instanceof SplQueue)) {
|
||||
$this->_channels[$name] = new SplQueue();
|
||||
}
|
||||
return $this->_channels[$name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* 清空缓存
|
||||
*/
|
||||
public function cleanAll()
|
||||
{
|
||||
/** @var SplQueue $channel */
|
||||
foreach ($this->_channels as $channel) {
|
||||
while ($channel->count() > 0) {
|
||||
$channel->dequeue();
|
||||
}
|
||||
}
|
||||
$this->_channels = [];
|
||||
}
|
||||
/**
|
||||
*
|
||||
* 清空缓存
|
||||
*/
|
||||
public function cleanAll()
|
||||
{
|
||||
/** @var SplQueue $channel */
|
||||
foreach ($this->_channels as $channel) {
|
||||
if (!($channel instanceof SplQueue)) {
|
||||
continue;
|
||||
}
|
||||
while ($channel->count() > 0) {
|
||||
$channel->dequeue();
|
||||
}
|
||||
}
|
||||
$this->_channels = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $timeout
|
||||
* @param Closure $closure
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function pop(string $name, Closure $closure, int|float $timeout = null): mixed
|
||||
{
|
||||
if (($channel = $this->channelInit($name)) == false) {
|
||||
return $this->addError('Channel is full.');
|
||||
}
|
||||
if (!$channel->isEmpty()) {
|
||||
return $channel->pop();
|
||||
}
|
||||
if ($timeout !== null) {
|
||||
$data = $channel->dequeue();
|
||||
}
|
||||
if (empty($data)) {
|
||||
$data = call_user_func($closure);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* @param $timeout
|
||||
* @param Closure $closure
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function pop(string $name, Closure $closure, int|float $timeout = null): mixed
|
||||
{
|
||||
return call_user_func($closure);
|
||||
|
||||
if (($channel = $this->channelInit($name)) == false) {
|
||||
return $this->addError('Channel is full.');
|
||||
}
|
||||
if (!$channel->isEmpty()) {
|
||||
return $channel->shift();
|
||||
}
|
||||
if ($timeout !== null) {
|
||||
$data = $channel->dequeue();
|
||||
}
|
||||
if (empty($data)) {
|
||||
$data = call_user_func($closure);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ class Logger extends Component
|
||||
|
||||
private array $logs = [];
|
||||
|
||||
public int $worker_id;
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
|
||||
@@ -25,8 +25,6 @@ class LoggerAspect implements IAspect
|
||||
*/
|
||||
#[Pure] public function __construct(public array $handler)
|
||||
{
|
||||
$this->className = get_class($this->handler[0]);
|
||||
$this->methodName = $this->handler[1];
|
||||
}
|
||||
|
||||
|
||||
@@ -47,8 +45,11 @@ class LoggerAspect implements IAspect
|
||||
|
||||
private function print_runtime($startTime)
|
||||
{
|
||||
$className = get_class($this->handler[0]);
|
||||
$methodName = $this->handler[1];
|
||||
|
||||
$runTime = round(microtime(true) - $startTime, 6);
|
||||
echo sprintf('run %s::%s use time %6f', $this->className, $this->methodName, $runTime);
|
||||
echo sprintf('run %s::%s use time %6f', $className, $methodName, $runTime);
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,19 +3,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace Snowflake\Jwt;
|
||||
|
||||
use Annotation\Aspect;
|
||||
use Database\InjectProperty;
|
||||
use Exception;
|
||||
use HttpServer\Http\HttpHeaders;
|
||||
use ReflectionException;
|
||||
use Snowflake\Cache\Redis;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Core\Str;
|
||||
use Snowflake\Exception\AuthException;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
|
||||
|
||||
@@ -23,10 +23,6 @@ class Connection extends Pool
|
||||
|
||||
public int $timeout = 1900;
|
||||
|
||||
/** @var PDO[] */
|
||||
protected array $connections = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $timeout
|
||||
*/
|
||||
|
||||
@@ -20,7 +20,7 @@ use Swoole\Coroutine\System;
|
||||
abstract class Process extends \Swoole\Process implements SProcess
|
||||
{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Process constructor.
|
||||
* @param $application
|
||||
|
||||
Reference in New Issue
Block a user