改名
This commit is contained in:
+185
-172
@@ -15,6 +15,7 @@ use JetBrains\PhpStorm\Pure;
|
|||||||
use Snowflake\Error\Logger;
|
use Snowflake\Error\Logger;
|
||||||
use Snowflake\Exception\ComponentException;
|
use Snowflake\Exception\ComponentException;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
use Swoole\Coroutine;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BaseObject
|
* Class BaseObject
|
||||||
@@ -26,195 +27,207 @@ use Snowflake\Snowflake;
|
|||||||
class BaseObject implements Configure
|
class BaseObject implements Configure
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BaseAbstract constructor.
|
* BaseAbstract constructor.
|
||||||
*
|
*
|
||||||
* @param array $config
|
* @param array $config
|
||||||
*/
|
*/
|
||||||
public function __construct($config = [])
|
public function __construct($config = [])
|
||||||
{
|
{
|
||||||
if (!empty($config) && is_array($config)) {
|
if (!empty($config) && is_array($config)) {
|
||||||
Snowflake::configure($this, $config);
|
Snowflake::configure($this, $config);
|
||||||
}
|
}
|
||||||
$this->init();
|
$this->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function init()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
#[Pure] public static function className(): string
|
|
||||||
{
|
|
||||||
return get_called_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 {
|
|
||||||
$this->error('set ' . $name . ' not exists ' . get_called_class());
|
|
||||||
throw new Exception('The set name ' . $name . ' not find in class ' . get_class($this));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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 ' . get_class($this));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
public function init()
|
||||||
* @param $message
|
{
|
||||||
* @param string $model
|
|
||||||
* @return bool
|
}
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function addError($message, $model = 'app'): bool
|
|
||||||
{
|
|
||||||
if ($message instanceof \Throwable) {
|
|
||||||
$format = 'Error: ' . $message->getMessage() . PHP_EOL;
|
|
||||||
$format .= 'File: ' . $message->getFile() . PHP_EOL;
|
|
||||||
$format .= 'Line: ' . $message->getLine();
|
|
||||||
$this->error($format);
|
|
||||||
} else {
|
|
||||||
if (!is_string($message)) {
|
|
||||||
$message = json_encode($message, JSON_UNESCAPED_UNICODE);
|
|
||||||
}
|
|
||||||
$this->error($message);
|
|
||||||
}
|
|
||||||
$logger = Snowflake::app()->getLogger();
|
|
||||||
$logger->error($message, $model);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $message
|
* @param array|callable $callback
|
||||||
* @param string $method
|
* @param object $scope
|
||||||
* @param string $file
|
*/
|
||||||
* @throws ComponentException
|
public function async_create(array|callable $callback, object $scope)
|
||||||
*/
|
{
|
||||||
public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
Coroutine::create($callback, $scope);
|
||||||
{
|
}
|
||||||
if (!is_string($message)) {
|
|
||||||
$message = print_r($message, true);
|
|
||||||
}
|
|
||||||
$message = "\033[35m[DEBUG][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
|
||||||
$message .= PHP_EOL;
|
|
||||||
|
|
||||||
$socket = Snowflake::app()->getLogger();
|
|
||||||
$socket->output($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $message
|
* @return string
|
||||||
* @param string $method
|
*/
|
||||||
* @param string $file
|
#[Pure] public static function className(): string
|
||||||
* @throws ComponentException
|
{
|
||||||
*/
|
return get_called_class();
|
||||||
public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
}
|
||||||
{
|
|
||||||
if (!is_string($message)) {
|
|
||||||
$message = print_r($message, true);
|
|
||||||
}
|
|
||||||
$message = "\033[34m[INFO][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
|
||||||
$message .= PHP_EOL;
|
|
||||||
|
|
||||||
$socket = Snowflake::app()->getLogger();
|
/**
|
||||||
$socket->output($message);
|
* @param $name
|
||||||
}
|
* @param $value
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function __set($name, $value)
|
||||||
|
{
|
||||||
|
$method = 'set' . ucfirst($name);
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
$this->{$method}($value);
|
||||||
|
} else {
|
||||||
|
$this->error('set ' . $name . ' not exists ' . get_called_class());
|
||||||
|
throw new Exception('The set name ' . $name . ' not find in class ' . get_class($this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $message
|
* @param $name
|
||||||
* @param string $method
|
*
|
||||||
* @param string $file
|
* @return mixed
|
||||||
* @throws ComponentException
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
public function __get($name): mixed
|
||||||
{
|
{
|
||||||
if (!is_string($message)) {
|
$method = 'get' . ucfirst($name);
|
||||||
$message = print_r($message, true);
|
if (method_exists($this, $method)) {
|
||||||
}
|
return $this->$method();
|
||||||
|
} else {
|
||||||
$message = "\033[36m[SUCCESS][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
throw new Exception('The get name ' . $name . ' not find in class ' . get_class($this));
|
||||||
$message .= PHP_EOL;
|
}
|
||||||
|
}
|
||||||
$socket = Snowflake::app()->getLogger();
|
|
||||||
$socket->output($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $message
|
* @param $message
|
||||||
* @param string $method
|
* @param string $model
|
||||||
* @param string $file
|
* @return bool
|
||||||
* @throws ComponentException
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
public function addError($message, $model = 'app'): bool
|
||||||
{
|
{
|
||||||
if (!is_string($message)) {
|
if ($message instanceof \Throwable) {
|
||||||
$message = print_r($message, true);
|
$format = 'Error: ' . $message->getMessage() . PHP_EOL;
|
||||||
}
|
$format .= 'File: ' . $message->getFile() . PHP_EOL;
|
||||||
|
$format .= 'Line: ' . $message->getLine();
|
||||||
$message = "\033[33m[WARNING][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
$this->error($format);
|
||||||
$message .= PHP_EOL;
|
} else {
|
||||||
|
if (!is_string($message)) {
|
||||||
|
$message = json_encode($message, JSON_UNESCAPED_UNICODE);
|
||||||
|
}
|
||||||
|
$this->error($message);
|
||||||
|
}
|
||||||
|
$logger = Snowflake::app()->getLogger();
|
||||||
|
$logger->error($message, $model);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$socket = Snowflake::app()->getLogger();
|
/**
|
||||||
$socket->output($message);
|
* @param mixed $message
|
||||||
}
|
* @param string $method
|
||||||
|
* @param string $file
|
||||||
|
* @throws ComponentException
|
||||||
|
*/
|
||||||
|
public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
||||||
|
{
|
||||||
|
if (!is_string($message)) {
|
||||||
|
$message = print_r($message, true);
|
||||||
|
}
|
||||||
|
$message = "\033[35m[DEBUG][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
||||||
|
$message .= PHP_EOL;
|
||||||
|
|
||||||
|
$socket = Snowflake::app()->getLogger();
|
||||||
|
$socket->output($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $message
|
* @param mixed $message
|
||||||
* @param null $method
|
* @param string $method
|
||||||
* @param null $file
|
* @param string $file
|
||||||
* @throws ComponentException
|
* @throws ComponentException
|
||||||
*/
|
*/
|
||||||
public function error(mixed $message, $method = null, $file = null)
|
public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
||||||
{
|
{
|
||||||
if (!empty($file)) {
|
if (!is_string($message)) {
|
||||||
echo "\033[41;37m[ERROR][" . date('Y-m-d H:i:s') . ']: ' . $file . "\033[0m";
|
$message = print_r($message, true);
|
||||||
echo PHP_EOL;
|
}
|
||||||
}
|
$message = "\033[34m[INFO][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
||||||
if (!is_string($message)) {
|
$message .= PHP_EOL;
|
||||||
$message = print_r($message, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
$content = (empty($method) ? '' : $method . ': ') . $message;
|
$socket = Snowflake::app()->getLogger();
|
||||||
|
$socket->output($message);
|
||||||
|
}
|
||||||
|
|
||||||
$length = strlen('[ERROR][2021-02-20 08:32:02]:');
|
/**
|
||||||
|
* @param mixed $message
|
||||||
|
* @param string $method
|
||||||
|
* @param string $file
|
||||||
|
* @throws ComponentException
|
||||||
|
*/
|
||||||
|
public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
||||||
|
{
|
||||||
|
if (!is_string($message)) {
|
||||||
|
$message = print_r($message, true);
|
||||||
|
}
|
||||||
|
|
||||||
$message = "\033[41;37m" . PHP_EOL . "[ERROR][" . date('Y-m-d H:i:s') . ']: ' . PHP_EOL . "\033[0m";
|
$message = "\033[36m[SUCCESS][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
||||||
$message .= "\033[41;37m" . str_pad($content, $length, ' ', STR_PAD_LEFT) . "\033[0m";
|
$message .= PHP_EOL;
|
||||||
$message .= PHP_EOL;
|
|
||||||
|
|
||||||
$socket = Snowflake::app()->getLogger();
|
$socket = Snowflake::app()->getLogger();
|
||||||
$socket->output($message);
|
$socket->output($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $message
|
||||||
|
* @param string $method
|
||||||
|
* @param string $file
|
||||||
|
* @throws ComponentException
|
||||||
|
*/
|
||||||
|
public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__)
|
||||||
|
{
|
||||||
|
if (!is_string($message)) {
|
||||||
|
$message = print_r($message, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$message = "\033[33m[WARNING][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
|
||||||
|
$message .= PHP_EOL;
|
||||||
|
|
||||||
|
|
||||||
|
$socket = Snowflake::app()->getLogger();
|
||||||
|
$socket->output($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $message
|
||||||
|
* @param null $method
|
||||||
|
* @param null $file
|
||||||
|
* @throws ComponentException
|
||||||
|
*/
|
||||||
|
public function error(mixed $message, $method = null, $file = null)
|
||||||
|
{
|
||||||
|
if (!empty($file)) {
|
||||||
|
echo "\033[41;37m[ERROR][" . date('Y-m-d H:i:s') . ']: ' . $file . "\033[0m";
|
||||||
|
echo PHP_EOL;
|
||||||
|
}
|
||||||
|
if (!is_string($message)) {
|
||||||
|
$message = print_r($message, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = (empty($method) ? '' : $method . ': ') . $message;
|
||||||
|
|
||||||
|
$length = strlen('[ERROR][2021-02-20 08:32:02]:');
|
||||||
|
|
||||||
|
$message = "\033[41;37m" . PHP_EOL . "[ERROR][" . date('Y-m-d H:i:s') . ']: ' . PHP_EOL . "\033[0m";
|
||||||
|
$message .= "\033[41;37m" . str_pad($content, $length, ' ', STR_PAD_LEFT) . "\033[0m";
|
||||||
|
$message .= PHP_EOL;
|
||||||
|
|
||||||
|
$socket = Snowflake::app()->getLogger();
|
||||||
|
$socket->output($message);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-12
@@ -10,6 +10,7 @@ use Snowflake\Event;
|
|||||||
use Snowflake\Exception\ComponentException;
|
use Snowflake\Exception\ComponentException;
|
||||||
use Snowflake\Exception\NotFindClassException;
|
use Snowflake\Exception\NotFindClassException;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
use Swoole\Coroutine;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ObjectPool
|
* Class ObjectPool
|
||||||
@@ -81,21 +82,23 @@ class ObjectPool extends \Snowflake\Abstracts\Pool
|
|||||||
*/
|
*/
|
||||||
public function destruct()
|
public function destruct()
|
||||||
{
|
{
|
||||||
if (empty($this->_waitRecover)) {
|
$this->async_create(function ($scope) {
|
||||||
return;
|
if (empty($scope->_waitRecover)) {
|
||||||
}
|
return;
|
||||||
foreach ($this->_waitRecover as $name => $value) {
|
|
||||||
if (empty($value)) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
foreach ($value as $object) {
|
foreach ($scope->_waitRecover as $name => $value) {
|
||||||
if (method_exists($object, 'clean')) {
|
if (empty($value)) {
|
||||||
$object->clean();
|
continue;
|
||||||
}
|
}
|
||||||
$this->push($name, $object);
|
foreach ($value as $object) {
|
||||||
|
if (method_exists($object, 'clean')) {
|
||||||
|
$object->clean();
|
||||||
|
}
|
||||||
|
$scope->push($name, $object);
|
||||||
|
}
|
||||||
|
unset($scope->_waitRecover[$name]);
|
||||||
}
|
}
|
||||||
}
|
}, $this);
|
||||||
$this->_waitRecover = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user