2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-09-04 01:05:33 +08:00
|
|
|
namespace HttpServer\Abstracts;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
2021-03-02 18:33:12 +08:00
|
|
|
use Database\Connection;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Exception;
|
2020-09-04 01:05:33 +08:00
|
|
|
use Snowflake\Abstracts\Config;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Snowflake\Event;
|
2020-09-02 19:09:32 +08:00
|
|
|
use Snowflake\Exception\ConfigException;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
|
2021-02-20 15:23:43 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Callback
|
|
|
|
|
* @package HttpServer\Abstracts
|
|
|
|
|
*/
|
2021-02-20 17:33:28 +08:00
|
|
|
abstract class Callback extends HttpService
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2021-06-25 11:20:38 +08:00
|
|
|
const EVENT_ERROR = 'WORKER:ERROR';
|
|
|
|
|
const EVENT_STOP = 'WORKER:STOP';
|
|
|
|
|
const EVENT_EXIT = 'WORKER:EXIT';
|
2021-04-04 03:01:12 +08:00
|
|
|
|
|
|
|
|
|
2021-06-25 11:20:38 +08:00
|
|
|
private array $_MESSAGE = [
|
|
|
|
|
self::EVENT_ERROR => 'The server error. at No.',
|
|
|
|
|
self::EVENT_STOP => 'The server stop. at No.',
|
|
|
|
|
self::EVENT_EXIT => 'The server exit. at No.',
|
|
|
|
|
];
|
2021-04-04 03:01:12 +08:00
|
|
|
|
2021-06-25 11:20:38 +08:00
|
|
|
/**
|
|
|
|
|
* @param $messageContent
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function system_mail($messageContent)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$email = Config::get('email');
|
|
|
|
|
if (empty($email) || !$email['enable']) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$transport = (new \Swift_SmtpTransport($email['host'], $email['465']))
|
|
|
|
|
->setUsername($email['username'])
|
|
|
|
|
->setPassword($email['password']);
|
|
|
|
|
$mailer = new \Swift_Mailer($transport);
|
|
|
|
|
|
|
|
|
|
// Create a message
|
|
|
|
|
$message = (new \Swift_Message('Wonderful Subject'))
|
|
|
|
|
->setFrom([$email['send']['address'] => $email['send']['nickname']])
|
|
|
|
|
->setBody('Here is the message itself');
|
|
|
|
|
|
|
|
|
|
foreach ($email['receive'] as $item) {
|
|
|
|
|
$message->setTo([$item['address'], $item['address'] => $item['nickname']]);
|
|
|
|
|
}
|
|
|
|
|
$mailer->send($messageContent);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
$this->addError($e, 'email');
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-04 03:01:12 +08:00
|
|
|
|
|
|
|
|
|
2021-06-25 11:20:38 +08:00
|
|
|
/**
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function clearMysqlClient()
|
|
|
|
|
{
|
|
|
|
|
$databases = Config::get('databases', []);
|
|
|
|
|
if (empty($databases)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$application = Snowflake::app();
|
|
|
|
|
foreach ($databases as $name => $database) {
|
|
|
|
|
/** @var Connection $connection */
|
|
|
|
|
$connection = $application->get('databases.' . $name, false);
|
|
|
|
|
if (empty($connection)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$connection->disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-04 03:01:12 +08:00
|
|
|
|
|
|
|
|
|
2021-04-07 14:15:50 +08:00
|
|
|
/**
|
2021-06-25 11:20:38 +08:00
|
|
|
* @param array $clientInfo
|
|
|
|
|
* @param string $event
|
|
|
|
|
* @return string
|
2021-04-07 14:15:50 +08:00
|
|
|
*/
|
2021-06-25 11:20:38 +08:00
|
|
|
protected function getName(array $clientInfo, string $event): string
|
2021-04-07 14:15:50 +08:00
|
|
|
{
|
2021-06-25 11:20:38 +08:00
|
|
|
return 'listen ' . $clientInfo['server_port'] . ' ' . Event::SERVER_CONNECT;
|
2021-04-07 14:15:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-06-25 11:20:38 +08:00
|
|
|
|
2021-05-06 14:58:42 +08:00
|
|
|
/**
|
2021-06-25 11:20:38 +08:00
|
|
|
* @throws ConfigException
|
2021-05-06 14:58:42 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-06-25 11:20:38 +08:00
|
|
|
protected function clearRedisClient()
|
|
|
|
|
{
|
|
|
|
|
$redis = Snowflake::app()->getRedis();
|
|
|
|
|
$redis->destroy();
|
|
|
|
|
}
|
2021-03-02 18:33:12 +08:00
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|