This commit is contained in:
2020-09-02 11:38:47 +08:00
parent 3e46f5790f
commit 8397c2ac8a
46 changed files with 1021 additions and 824 deletions
+1 -1
View File
@@ -307,7 +307,6 @@ abstract class BaseApplication extends Service
}
/**
* @return Router
* @throws ComponentException
@@ -348,6 +347,7 @@ abstract class BaseApplication extends Service
'router' => ['class' => Router::class],
'redis' => ['class' => Redis::class],
'jwt' => ['class' => Jwt::class],
'server' => ['class' => Server::class],
]);
}
}
+1 -6
View File
@@ -134,7 +134,6 @@ class BaseObject implements Configure
}
echo "\033[35m[DEBUG][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
echo PHP_EOL;
Logger::trance($message, 'debug');
}
@@ -151,7 +150,6 @@ class BaseObject implements Configure
}
echo "\033[34m[INFO][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
echo PHP_EOL;
Logger::trance($message, 'info');
}
/**
@@ -167,7 +165,6 @@ class BaseObject implements Configure
}
echo "\033[36m[SUCCESS][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
echo PHP_EOL;
Logger::trance($message, 'success');
}
@@ -184,7 +181,6 @@ class BaseObject implements Configure
}
echo "\033[33m[SUCCESS][" . date('Y-m-d H:i:s') . ']: ' . $message . "\033[0m";
echo PHP_EOL;
Logger::trance($message, 'warning');
}
@@ -194,7 +190,7 @@ class BaseObject implements Configure
* @param string|null $file
* @throws Exception
*/
public function error($message, string $method = null, string $file = null)
public function error($message, $method = null, $file = null)
{
if (!empty($file)) {
echo "\033[41;37m[ERROR][" . date('Y-m-d H:i:s') . ']: ' . $file . "\033[0m";
@@ -205,7 +201,6 @@ class BaseObject implements Configure
}
echo "\033[41;37m[ERROR][" . date('Y-m-d H:i:s') . ']: ' . (empty($method) ? '' : $method . ': ') . $message . "\033[0m";
echo PHP_EOL;
Logger::error($message, 'error');
}
}
+5 -9
View File
@@ -34,16 +34,12 @@ class Application extends BaseApplication
*/
public function init()
{
/** @var Server $https */
$https = $this->make(Server::class, Server::class);
$https->initCore(Config::get('servers', true));
$process = Snowflake::get()->processes;
if (Config::has('servers', true)) {
/** @var Server $https */
$https = $this->make(Server::class, Server::class);
$servers = $https->initCore(Config::get('servers'));
$process->push($servers);
}
if (Config::has('processes', true)) {
$process->push(Config::get('processes'));
}
$process->initCore();
}
+3 -3
View File
@@ -101,7 +101,7 @@ class ErrorHandler extends Component implements ErrorInterface
$data = JSON::to(500, 'Error : ' . $error[1], $path);
Logger::error($data, 'error');
Snowflake::get()->getLogger()->error($data, 'error');
$event = Snowflake::get()->event;
$event->trigger(Event::RELEASE_ALL);
@@ -123,7 +123,7 @@ class ErrorHandler extends Component implements ErrorInterface
$data = JSON::to($code, $this->category . ': ' . $message, $path);
Logger::trance($data, $this->category);
Snowflake::get()->getLogger()->trance($data, $this->category);
return response()->send($data);
}
@@ -154,6 +154,6 @@ class ErrorHandler extends Component implements ErrorInterface
*/
public function writer($message, $category = 'app')
{
Logger::debug($message, $category);
Snowflake::get()->getLogger()->debug($message, $category);
}
}
+40 -33
View File
@@ -9,6 +9,7 @@
namespace Snowflake\Error;
use Exception;
use Snowflake\Abstracts\Component;
use Snowflake\Core\JSON;
use Snowflake\Snowflake;
use Swoole\Process;
@@ -17,21 +18,23 @@ use Swoole\Process;
* Class Logger
* @package Snowflake\Snowflake\Error
*/
class Logger
class Logger extends Component
{
private static $logs = [];
private $logs = [];
public static $worker_id;
public $worker_id;
/**
* @param $message
* @param string $category
* @param null $_
* @throws Exception
*/
public static function debug($message, $category = 'app')
public function debug($message, $category = 'app', $_ = null)
{
static::writer($message, $category);
parent::debug($message);
$this->writer($message, $category);
}
@@ -40,30 +43,34 @@ class Logger
* @param string $category
* @throws Exception
*/
public static function trance($message, $category = 'app')
public function trance($message, $category = 'app')
{
static::writer($message, $category);
$this->writer($message, $category);
}
/**
* @param $message
* @param string $category
* @param null $_
* @throws Exception
*/
public static function error($message, $category = 'app')
public function error($message, $category = 'error', $_ = null)
{
static::writer($message, $category);
parent::error($message);
$this->writer($message, $category);
}
/**
* @param $message
* @param string $category
* @param null $_
* @throws Exception
*/
public static function success($message, $category = 'app')
public function success($message, $category = 'app', $_ = null)
{
static::writer($message, $category);
parent::success($message);
$this->writer($message, $category);
}
/**
@@ -72,23 +79,23 @@ class Logger
* @return string
* @throws Exception
*/
private static function writer($message, $category = 'app')
private function writer($message, $category = 'app')
{
if ($message instanceof \Throwable) {
$message = $message->getMessage();
} else {
if (is_array($message) || is_object($message)) {
$message = static::arrayFormat($message);
$message = $this->arrayFormat($message);
}
}
if (is_array($message)) {
$message = static::arrayFormat($message);
$message = $this->arrayFormat($message);
}
if (!empty($message)) {
if (!is_array(static::$logs)) {
static::$logs = [];
if (!is_array($this->$logs)) {
$this->$logs = [];
}
static::$logs[] = [$category, $message];
$this->$logs[] = [$category, $message];
}
return $message;
}
@@ -99,7 +106,7 @@ class Logger
* @param $category
* @throws Exception
*/
public static function print_r($message, $category = '')
public function print_r($message, $category = '')
{
/** @var Process $logger */
$logger = Snowflake::get()->logger;
@@ -111,10 +118,10 @@ class Logger
* @param string $application
* @return mixed
*/
public static function getLastError($application = 'app')
public function getLastError($application = 'app')
{
$_tmp = [];
foreach (static::$logs as $key => $val) {
foreach ($this->logs as $key => $val) {
if ($val[0] != $application) {
continue;
}
@@ -131,7 +138,7 @@ class Logger
* @param string $category
* @throws Exception
*/
public static function write(string $messages, $category = 'app')
public function write(string $messages, $category = 'app')
{
if (empty($messages)) {
return;
@@ -146,7 +153,7 @@ class Logger
* @param $logFile
* @return false|string
*/
private static function getSource($logFile)
private function getSource($logFile)
{
if (!file_exists($logFile)) {
shell_exec('echo 3 > /proc/sys/vm/drop_caches');
@@ -162,37 +169,37 @@ class Logger
* @throws Exception
* 写入日志
*/
public static function insert()
public function insert()
{
if (empty(static::$logs)) {
if (empty($this->logs)) {
return;
}
foreach (static::$logs as $log) {
foreach ($this->logs as $log) {
[$category, $message] = $log;
static::write($message, $category);
$this->write($message, $category);
}
static::$logs = [];
$this->logs = [];
}
/**
* @return array
*/
public static function clear()
public function clear()
{
return static::$logs = [];
return $this->logs = [];
}
/**
* @param $data
* @return string
*/
private static function arrayFormat($data)
private function arrayFormat($data)
{
if (is_string($data)) {
return $data;
}
if ($data instanceof Exception) {
$data = static::getException($data);
$data = $this->getException($data);
} else if (is_object($data)) {
$data = get_object_vars($data);
}
@@ -200,7 +207,7 @@ class Logger
$_tmp = [];
foreach ($data as $key => $val) {
if (is_array($val)) {
$_tmp[] = static::arrayFormat($val);
$_tmp[] = $this->arrayFormat($val);
} else {
$_tmp[] = (is_string($key) ? $key . ' : ' : '') . $val;
}
@@ -212,7 +219,7 @@ class Logger
* @param Exception $exception
* @return array
*/
private static function getException($exception)
private function getException($exception)
{
$_tmp = [$exception->getMessage()];
$_tmp[] = $exception->getFile() . ' on line ' . $exception->getLine();
+3 -81
View File
@@ -5,7 +5,6 @@ namespace Snowflake;
use Exception;
use HttpServer\ServerManager;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use Snowflake\Abstracts\Component;
@@ -27,75 +26,11 @@ class Processes extends Component
*/
public function initCore()
{
$application = Snowflake::get();
$server = $application->set(Pool::class, new Pool($this->size(), SWOOLE_IPC_UNIXSOCK));
$server->on('workerStart', function (Pool $pool, int $workerId) use ($application) {
ServerManager::create($pool, $this->processes[$workerId], $workerId);
});
$server->on('workerStop', function (Pool $pool, int $workerId) {
$event = Snowflake::get()->event;
if ($event->exists(Event::PROCESS_WORKER_STOP)) {
$event->trigger(Event::PROCESS_WORKER_STOP);
}
$manager = Snowflake::get()->servers;
$email = Config::get('admin.email');
if (!empty($email)) {
$nickname = Config::get('admin.name', false, '亲爱的开发者');
$this->system_mail($email, $nickname);
}
});
$server->on('message', function (Pool $pool, $message) {
file_put_contents(storage('a.log'), $message);
});
return $server;
}
$serverConfig = Config::get('servers', true);
/**
* @param $email
* @param $nickname
*/
protected function system_mail($email, $nickname)
{
try {
$mail = new PHPMailer(true);
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp1.example.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('system@example.com', '系统管理员');
$mail->addAddress($email, $nickname); // Add a recipient
// Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
/**
* @return int
*/
protected function size()
{
return count($this->processes);
return $manager->initCore($serverConfig);
}
@@ -107,17 +42,4 @@ class Processes extends Component
$server = $this->initCore();
$server->start();
}
/**
* @param array $servers
* @return $this
*/
public function push(array $servers)
{
foreach ($servers as $server) {
$this->processes[] = $server;
}
return $this;
}
}