Compare commits
189 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c095b69f67 | |||
| 4c8c5ac239 | |||
| cfc847133e | |||
| 30b430f1cf | |||
| 516c7612a5 | |||
| 3bb3d5a1d7 | |||
| fb48a0b508 | |||
| a3964faa72 | |||
| 0fea7ac770 | |||
| 7eccee5d16 | |||
| 565943b76f | |||
| adb95af124 | |||
| b554b53cfd | |||
| 0e0caaf8a9 | |||
| c37c11a768 | |||
| 51a2f448fd | |||
| eb6aaf3509 | |||
| 12766a3b74 | |||
| ec00c2d104 | |||
| 40a2331626 | |||
| d528e4d603 | |||
| 39865db17a | |||
| 04434f72d4 | |||
| f95f576a52 | |||
| 7fde689244 | |||
| b083984efd | |||
| f83538b687 | |||
| d0c579455d | |||
| 5aae1f0ba4 | |||
| ca874beaaf | |||
| 15e8d7a5a7 | |||
| 2959115a23 | |||
| b661581baf | |||
| ab8a7d7fff | |||
| 31a5053bed | |||
| be9c469a0f | |||
| 3c3ba68aba | |||
| 4d2b462676 | |||
| c075662c59 | |||
| 35ce80c19b | |||
| 3345ff697b | |||
| 2ea7289c43 | |||
| 3e53f8d3c2 | |||
| af3b482c1f | |||
| cf8b25d7db | |||
| 0ba9fc0ce7 | |||
| a03c28d7e5 | |||
| ab32584cf2 | |||
| 423d406f3e | |||
| c545686b8f | |||
| 7246d88e44 | |||
| 1aef68902b | |||
| 41ee154c90 | |||
| 37b745b659 | |||
| 88cdf3a547 | |||
| 1a012150ce | |||
| 74bcdf257e | |||
| d3fd4385e6 | |||
| e94b3ca1d9 | |||
| 2ca845a65c | |||
| ee23b78872 | |||
| bccf0e2a1e | |||
| d98392b6af | |||
| c17e829f75 | |||
| 216bc1d894 | |||
| 77b457b620 | |||
| 6cdf8052e6 | |||
| 9ea0b9d34b | |||
| f91194b5c8 | |||
| a671719896 | |||
| 6612040b3a | |||
| 4c2068482a | |||
| d762d392e8 | |||
| 3399c31e70 | |||
| 5e8676b844 | |||
| 9b9a6233dc | |||
| a233ff72ab | |||
| 8999b7ad3f | |||
| 36db8aa4a8 | |||
| d47e1309a9 | |||
| a77d138898 | |||
| 0466268ac8 | |||
| 2c37c5c494 | |||
| 42db5c83dc | |||
| c439e9949c | |||
| d0f3469997 | |||
| 1fad90e1e7 | |||
| 968be07fbb | |||
| f0fe2c1c11 | |||
| b66b2867ab | |||
| 956a68bbff | |||
| bcf865ff34 | |||
| de475736ba | |||
| 7a39375657 | |||
| 8e700e4086 | |||
| 19bd6882d1 | |||
| 40f315f4d9 | |||
| 632d96a197 | |||
| 2e32dea501 | |||
| 4df34aac0b | |||
| 3dff8272ea | |||
| 5c3a197b52 | |||
| 8ebfb295ab | |||
| 49892feff4 | |||
| 7fcf535617 | |||
| ccc9f9bbf1 | |||
| e2e43959a8 | |||
| 382a6ff1b5 | |||
| 9feebc2b73 | |||
| 3f9a7b98e8 | |||
| 1f9116c9c0 | |||
| c0f9506dc6 | |||
| 65ff89e3ed | |||
| 058beb2c26 | |||
| 2036d4ebd9 | |||
| 1f7403b8c0 | |||
| d7b84a804d | |||
| 4096050303 | |||
| b23cf6187c | |||
| 6f0e423773 | |||
| f8a28967d5 | |||
| a3f02701ca | |||
| 2e6ca9b4fa | |||
| c3d8a92b33 | |||
| ba9086b56b | |||
| d34eb2bf53 | |||
| c4c6d9a58e | |||
| ee255c02f9 | |||
| e4c65484fe | |||
| a70e080153 | |||
| b5bd2d12f8 | |||
| c4cf051139 | |||
| 2f17c40444 | |||
| 84d120de92 | |||
| 89e7f2d7bc | |||
| 0db42d3a38 | |||
| aa72ac2eb9 | |||
| fbe83aff27 | |||
| 7e7652795d | |||
| 15da06a63d | |||
| db65cb46c2 | |||
| f3f29c1342 | |||
| 0436076614 | |||
| 53cbc714ed | |||
| 5e9df58276 | |||
| b76c262397 | |||
| e31a66686f | |||
| a8244921ae | |||
| 164ce98990 | |||
| 7e25c361bf | |||
| 557d0fd583 | |||
| 9b7e6e1396 | |||
| 56df262e56 | |||
| 6d49e73ec2 | |||
| f7fb56fc06 | |||
| add59693c1 | |||
| 84071fecde | |||
| 4e5787b20d | |||
| 52823100af | |||
| 2b9d09f835 | |||
| e576af2071 | |||
| 07c2b3f6cf | |||
| aed5e9469d | |||
| 9da5d5a332 | |||
| 1c48f27e4a | |||
| 760192d63a | |||
| c786999fe1 | |||
| cd133a8ec7 | |||
| d40d3cc282 | |||
| 33508652ad | |||
| 8286e27407 | |||
| 188f53d3b0 | |||
| d353e019df | |||
| e0b3954909 | |||
| b6ac2c75ee | |||
| 558008ad08 | |||
| 10805c1022 | |||
| 3d9266dc16 | |||
| b80b2c18db | |||
| a7630df003 | |||
| 2cea45051b | |||
| da703055b3 | |||
| 78bc474f2b | |||
| 1622986699 | |||
| 0935f54f06 | |||
| 894feb3506 | |||
| 04ef4473cc | |||
| ed4051171c | |||
| 6fb5bb1d41 |
@@ -7,10 +7,6 @@ namespace Annotation\Route;
|
||||
use Annotation\Attribute;
|
||||
use Exception;
|
||||
use HttpServer\Route\Router;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
@@ -30,17 +26,14 @@ use Snowflake\Snowflake;
|
||||
* @param string|null $uri
|
||||
* @param string $version
|
||||
*/
|
||||
public function __construct(
|
||||
public string $event,
|
||||
public ?string $uri = null,
|
||||
public string $version = 'v.1.0'
|
||||
)
|
||||
public function __construct(public string $event, public ?string $uri = null, public string $version = 'v.1.0')
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $handler
|
||||
* @param mixed $class
|
||||
* @param mixed|null $method
|
||||
* @return Router
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
@@ -132,7 +132,7 @@ class ActiveQuery extends Component implements ISqlBuilder
|
||||
*/
|
||||
public function execute($sql, $params = []): Command
|
||||
{
|
||||
return $this->modelClass::getDb()->createCommand($sql, $params);
|
||||
return $this->modelClass::getDb()->createCommand($sql, $this->modelClass::getDbName(), $params);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ use Database\Traits\HasBase;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Channel;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
@@ -195,7 +196,7 @@ class ActiveRecord extends BaseActiveRecord
|
||||
if (is_bool($create)) {
|
||||
return false;
|
||||
}
|
||||
return static::getDb()->createCommand($create[0], $create[1])->exec();
|
||||
return static::getDb()->createCommand($create[0], static::getDbName(), $create[1])->exec();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -26,8 +26,10 @@ use Exception;
|
||||
use HttpServer\Http\Context;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Abstracts\TraitApplication;
|
||||
use Snowflake\Event as SEvent;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use validator\Validator;
|
||||
@@ -433,7 +435,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
|
||||
// $trance = $dbConnection->beginTransaction();
|
||||
try {
|
||||
if (!($lastId = (int)$dbConnection->createCommand($sql, $param)->save(true, $this))) {
|
||||
if (!($lastId = (int)$dbConnection->createCommand($sql, static::getDbName(), $param)->save(true, $this))) {
|
||||
throw new Exception('保存失败.');
|
||||
}
|
||||
// $trance->commit();
|
||||
@@ -499,7 +501,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
}
|
||||
|
||||
// $trance = $command->beginTransaction();
|
||||
if (!$command->createCommand(...$generate)->save(false, $this)) {
|
||||
if (!$command->createCommand($generate[0], static::getDbName(), $generate[1])->save(false, $this)) {
|
||||
// $trance->rollback();
|
||||
return false;
|
||||
}
|
||||
@@ -733,16 +735,16 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
}
|
||||
|
||||
if (str_starts_with($table, $tablePrefix)) {
|
||||
return $table;
|
||||
return '`' . static::getDbName() . '`.' . $table;
|
||||
} else if (!str_starts_with($table, '{{%')) {
|
||||
return $table;
|
||||
return '`' . static::getDbName() . '`.' . $table;
|
||||
}
|
||||
|
||||
$table = trim($table, '{{%}}');
|
||||
if ($tablePrefix) {
|
||||
$table = $tablePrefix . $table;
|
||||
}
|
||||
return $table;
|
||||
return '`' . static::getDbName() . '`.' . $table;
|
||||
}
|
||||
|
||||
|
||||
@@ -769,6 +771,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private static string $ab_name = '';
|
||||
|
||||
|
||||
/**
|
||||
* @return Connection
|
||||
* @throws Exception
|
||||
@@ -1032,9 +1038,22 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
*/
|
||||
public static function setDatabaseConnect($dbName): Connection
|
||||
{
|
||||
static::$ab_name = $dbName;
|
||||
|
||||
return Snowflake::app()->db->get($dbName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public static function getDbName(): string
|
||||
{
|
||||
return Config::get('databases.connections.' . static::$ab_name . '.database');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Columns
|
||||
* @throws Exception
|
||||
@@ -1053,11 +1072,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
*/
|
||||
public static function populate(array $data): static
|
||||
{
|
||||
$class = Snowflake::app()->getChannel();
|
||||
/** @var static $model */
|
||||
$model = $class->pop(static::class, function () {
|
||||
return new static();
|
||||
});
|
||||
// $class = Snowflake::app()->getChannel();
|
||||
// /** @var static $model */
|
||||
// $model = $class->pop(static::class, function () {
|
||||
// return new static();
|
||||
// });
|
||||
$model = new static();
|
||||
$model->_attributes = $data;
|
||||
$model->_oldAttributes = $data;
|
||||
$model->setIsCreate(false);
|
||||
|
||||
+12
-8
@@ -14,7 +14,7 @@ use Exception;
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Swoole\Coroutine;
|
||||
use Snowflake\Core\Json;
|
||||
|
||||
/**
|
||||
* Class Command
|
||||
@@ -56,11 +56,11 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @param bool $isInsert
|
||||
* @param bool $hasAutoIncrement
|
||||
* @param mixed $hasAutoIncrement
|
||||
* @return int|bool|array|string|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function save($isInsert = TRUE, $hasAutoIncrement = null): int|bool|array|string|null
|
||||
public function save(bool $isInsert = TRUE, mixed $hasAutoIncrement = null): int|bool|array|string|null
|
||||
{
|
||||
return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement);
|
||||
}
|
||||
@@ -114,18 +114,22 @@ class Command extends Component
|
||||
/**
|
||||
* @param $type
|
||||
* @param null $isInsert
|
||||
* @param bool $hasAutoIncrement
|
||||
* @param bool|null $hasAutoIncrement
|
||||
* @return int|bool|array|string|null
|
||||
* @throws Exception
|
||||
*/
|
||||
private function execute($type, $isInsert = null, $hasAutoIncrement = null): int|bool|array|string|null
|
||||
private function execute($type, $isInsert = null, mixed $hasAutoIncrement = null): int|bool|array|string|null
|
||||
{
|
||||
try {
|
||||
$time = microtime(true);
|
||||
if ($type === static::EXECUTE) {
|
||||
$result = $this->insert_or_change($isInsert, $hasAutoIncrement);
|
||||
} else {
|
||||
$result = $this->search($type);
|
||||
}
|
||||
if (microtime(true) - $time >= 0.02) {
|
||||
$this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time));
|
||||
}
|
||||
if ($this->prepare) {
|
||||
$this->prepare->closeCursor();
|
||||
}
|
||||
@@ -206,7 +210,7 @@ class Command extends Component
|
||||
* @param $prepare
|
||||
* @return string
|
||||
*/
|
||||
private function errorMessage($prepare)
|
||||
private function errorMessage($prepare): string
|
||||
{
|
||||
return $this->sql . ':' . ($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
|
||||
}
|
||||
@@ -235,7 +239,7 @@ class Command extends Component
|
||||
* @return bool|int
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function checkResponse($prepare, $connect)
|
||||
private function checkResponse($prepare, $connect): bool|int
|
||||
{
|
||||
$result = $prepare->execute($this->params);
|
||||
if ($result === false) {
|
||||
@@ -278,7 +282,7 @@ class Command extends Component
|
||||
* @return int|bool|array|string|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exec($scope = null, $insert = false): int|bool|array|string|null
|
||||
public function exec($scope = null, bool $insert = false): int|bool|array|string|null
|
||||
{
|
||||
return $this->execute(static::EXECUTE, $insert, $scope);
|
||||
}
|
||||
|
||||
+21
-8
@@ -17,6 +17,7 @@ use JetBrains\PhpStorm\Pure;
|
||||
use PDO;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
@@ -37,9 +38,12 @@ class Connection extends Component
|
||||
public string $charset = 'utf-8';
|
||||
public string $tablePrefix = '';
|
||||
|
||||
public string $database = '';
|
||||
|
||||
public int $timeout = 1900;
|
||||
|
||||
public int $maxNumber = 200;
|
||||
public int $maxNumber = 30;
|
||||
public int $minNumber = 10;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
@@ -103,9 +107,11 @@ class Connection extends Component
|
||||
public function fill()
|
||||
{
|
||||
$connections = $this->connections();
|
||||
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
|
||||
if (!empty($this->slaveConfig)) {
|
||||
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
|
||||
$pool = Config::get('databases.pool.max', 10);
|
||||
|
||||
$connections->initConnections('Mysql:' . $this->cds, true, $pool);
|
||||
if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) {
|
||||
$connections->initConnections('Mysql:' . $this->slaveConfig['cds'], false, $pool);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,7 +178,10 @@ class Connection extends Component
|
||||
public function masterInstance(): PDO
|
||||
{
|
||||
return $this->connections()->get([
|
||||
'cds' => $this->cds, 'username' => $this->username, 'password' => $this->password
|
||||
'cds' => $this->cds,
|
||||
'username' => $this->username,
|
||||
'password' => $this->password,
|
||||
'database' => $this->database
|
||||
], true);
|
||||
}
|
||||
|
||||
@@ -185,6 +194,9 @@ class Connection extends Component
|
||||
if (empty($this->slaveConfig) || Db::transactionsActive()) {
|
||||
return $this->masterInstance();
|
||||
}
|
||||
if ($this->slaveConfig['cds'] == $this->cds) {
|
||||
return $this->masterInstance();
|
||||
}
|
||||
return $this->connections()->get($this->slaveConfig, false);
|
||||
}
|
||||
|
||||
@@ -252,12 +264,13 @@ class Connection extends Component
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sql
|
||||
* @param null $sql
|
||||
* @param string $dbname
|
||||
* @param array $attributes
|
||||
* @return Command
|
||||
* @throws
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createCommand($sql = null, $attributes = []): Command
|
||||
public function createCommand($sql = null, string $dbname = '', array $attributes = []): Command
|
||||
{
|
||||
$command = new Command(['db' => $this, 'sql' => $sql]);
|
||||
return $command->bindValues($attributes);
|
||||
|
||||
@@ -51,6 +51,8 @@ class DatabasesProviders extends Providers
|
||||
return $application->get('databases.' . $name);
|
||||
}
|
||||
$config = $this->getConfig($name);
|
||||
|
||||
$max = Config::get('databases.pool.max', 30);
|
||||
return $application->set('databases.' . $name, [
|
||||
'class' => Connection::class,
|
||||
'id' => $config['id'],
|
||||
@@ -58,7 +60,8 @@ class DatabasesProviders extends Providers
|
||||
'username' => $config['username'],
|
||||
'password' => $config['password'],
|
||||
'tablePrefix' => $config['tablePrefix'],
|
||||
'maxNumber' => $config['maxNumber'],
|
||||
'maxNumber' => $max,
|
||||
'database' => $config['database'],
|
||||
'charset' => $config['charset'] ?? 'utf8mb4',
|
||||
'slaveConfig' => $config['slaveConfig']
|
||||
]);
|
||||
@@ -71,10 +74,13 @@ class DatabasesProviders extends Providers
|
||||
*/
|
||||
public function createPool()
|
||||
{
|
||||
$databases = Config::get('databases', []);
|
||||
$databases = Config::get('databases.connections', []);
|
||||
if (empty($databases)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$max = Config::get('databases.pool', ['max' => 10, 'min' => 10]);
|
||||
|
||||
$application = Snowflake::app();
|
||||
foreach ($databases as $name => $database) {
|
||||
/** @var Connection $connection */
|
||||
@@ -85,7 +91,9 @@ class DatabasesProviders extends Providers
|
||||
'username' => $database['username'],
|
||||
'password' => $database['password'],
|
||||
'tablePrefix' => $database['tablePrefix'],
|
||||
'maxNumber' => $database['maxNumber'],
|
||||
'database' => $database['database'],
|
||||
'maxNumber' => $max['max'],
|
||||
'minNumber' => $max['min'],
|
||||
'charset' => $database['charset'] ?? 'utf8mb4',
|
||||
'slaveConfig' => $database['slaveConfig']
|
||||
]);
|
||||
@@ -101,7 +109,7 @@ class DatabasesProviders extends Providers
|
||||
*/
|
||||
public function getConfig($name): mixed
|
||||
{
|
||||
return Config::get('databases.' . $name,null, true);
|
||||
return Config::get('databases.connections.' . $name, null, true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+29
-9
@@ -11,7 +11,6 @@ namespace Database;
|
||||
|
||||
use Database\Traits\QueryTrait;
|
||||
use Exception;
|
||||
use HttpServer\Http\Context;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
@@ -93,6 +92,27 @@ class Db implements ISqlBuilder
|
||||
return 'ANY_VALUE(' . $column . ') as ' . $alias;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @return string
|
||||
*/
|
||||
public static function increment(string $column): string
|
||||
{
|
||||
return '+ ' . $column;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @return string
|
||||
*/
|
||||
public static function decrement(string $column): string
|
||||
{
|
||||
return '- ' . $column;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Connection|null $db
|
||||
* @return mixed
|
||||
@@ -167,19 +187,19 @@ class Db implements ISqlBuilder
|
||||
*/
|
||||
public static function findAllBySql(string $sql, array $attributes = [], Connection $db = NULL): int|bool|array|string|null
|
||||
{
|
||||
return $db->createCommand($sql, $attributes)->all();
|
||||
return $db->createCommand($sql, $db?->database, $attributes)->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $attributes
|
||||
* @param Connection|NULL $db
|
||||
* @return array|mixed
|
||||
* @return string|array|bool|int|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL): mixed
|
||||
public static function findBySql(string $sql, array $attributes = [], Connection $db = NULL): string|array|bool|int|null
|
||||
{
|
||||
return $db->createCommand($sql, $attributes)->one();
|
||||
return $db->createCommand($sql, $db?->database, $attributes)->one();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,7 +261,7 @@ class Db implements ISqlBuilder
|
||||
if (empty($db)) {
|
||||
$db = Snowflake::app()->get('db');
|
||||
}
|
||||
return $db->createCommand('DROP TABLE ' . $table)->delete();
|
||||
return $db->createCommand('DROP TABLE `' . $db->database . '`.' . $table)->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -257,7 +277,7 @@ class Db implements ISqlBuilder
|
||||
$db = Snowflake::app()->get('db');
|
||||
}
|
||||
|
||||
return $db->createCommand('TRUNCATE ' . $table)->exec();
|
||||
return $db->createCommand('TRUNCATE `' . $db->database . '`.' . $table)->exec();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -278,7 +298,7 @@ class Db implements ISqlBuilder
|
||||
return null;
|
||||
}
|
||||
|
||||
return $db->createCommand('SHOW CREATE TABLE ' . $table)->one();
|
||||
return $db->createCommand('SHOW CREATE TABLE `' . $db->database . '`.' . $table)->one();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -297,7 +317,7 @@ class Db implements ISqlBuilder
|
||||
return null;
|
||||
}
|
||||
|
||||
return $db->createCommand('SHOW FULL FIELDS FROM ' . $table)->all();
|
||||
return $db->createCommand('SHOW FULL FIELDS FROM `' . $db->database . '`.' . $table)->all();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,8 +7,6 @@ namespace Database;
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Event;
|
||||
use Swoole\Coroutine;
|
||||
|
||||
/**
|
||||
* Class Pagination
|
||||
@@ -190,7 +188,7 @@ class Pagination extends Component
|
||||
*/
|
||||
private function get(): Collection|array
|
||||
{
|
||||
if ($this->_length + $this->_limit > $this->_max) {
|
||||
if ($this->_max > 0 && $this->_length + $this->_limit > $this->_max) {
|
||||
$this->_limit = $this->_length + $this->_limit - $this->_max;
|
||||
}
|
||||
$data = $this->activeQuery->limit($this->_offset, $this->_limit)->get();
|
||||
|
||||
+46
-24
@@ -52,19 +52,8 @@ class SqlBuilder extends Component
|
||||
public function update(array $attributes): bool|array
|
||||
{
|
||||
[$string, $array] = $this->builderParams($attributes);
|
||||
if (empty($string) || empty($array)) {
|
||||
return $this->addError('None data update.');
|
||||
}
|
||||
|
||||
$condition = $this->conditionToString();
|
||||
if (!empty($condition)) {
|
||||
$condition = ' WHERE ' . $condition;
|
||||
}
|
||||
|
||||
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
|
||||
$update .= $this->builderLimit($this->query);
|
||||
|
||||
return [$update, $array];
|
||||
return $this->__updateBuilder($string, $array);
|
||||
}
|
||||
|
||||
|
||||
@@ -74,14 +63,24 @@ class SqlBuilder extends Component
|
||||
* @return bool|array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function mathematics(array $attributes, $opera = '+'): bool|array
|
||||
public function mathematics(array $attributes, string $opera = '+'): bool|array
|
||||
{
|
||||
$string = $array = [];
|
||||
|
||||
$string = [];
|
||||
foreach ($attributes as $attribute => $value) {
|
||||
$string[] = $attribute . '=' . $attribute . $opera . $value;
|
||||
}
|
||||
return $this->__updateBuilder($string, []);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $string
|
||||
* @param array $params
|
||||
* @return array|bool
|
||||
* @throws Exception
|
||||
*/
|
||||
private function __updateBuilder(array $string, array $params): array|bool
|
||||
{
|
||||
if (empty($string)) {
|
||||
return $this->addError('None data update.');
|
||||
}
|
||||
@@ -92,9 +91,9 @@ class SqlBuilder extends Component
|
||||
}
|
||||
|
||||
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
|
||||
$update .= $this->builderLimit($this->query);
|
||||
$update .= $this->builderLimit($this->query, false);
|
||||
|
||||
return [$update, []];
|
||||
return [$update, $params];
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +103,7 @@ class SqlBuilder extends Component
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function insert(array $attributes, $isBatch = false): array
|
||||
public function insert(array $attributes, bool $isBatch = false): array
|
||||
{
|
||||
$update = sprintf('INSERT INTO %s', $this->tableName());
|
||||
if ($isBatch === false) {
|
||||
@@ -156,16 +155,39 @@ class SqlBuilder extends Component
|
||||
* @return array[]
|
||||
* a=:b,
|
||||
*/
|
||||
private function builderParams(array $attributes, bool $isInsert = false, $params = [], $order = 0): array
|
||||
#[Pure] private function builderParams(array $attributes, bool $isInsert = false, array $params = [], int $order = 0): array
|
||||
{
|
||||
$keys = [];
|
||||
foreach ($attributes as $key => $value) {
|
||||
if ($isInsert === true) {
|
||||
$keys[] = ':' . $key . $order;
|
||||
} else {
|
||||
$keys[] = $key . '=:' . $key . $order;
|
||||
}
|
||||
$params[$key . $order] = $value;
|
||||
} else {
|
||||
[$keys, $params] = $this->resolveParams($key, $value, $order, $params, $keys);
|
||||
}
|
||||
}
|
||||
return [$keys, $params];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param int $order
|
||||
* @param array $params
|
||||
* @param array $keys
|
||||
* @return array
|
||||
*/
|
||||
private function resolveParams(string $key, mixed $value, int $order, array $params, array $keys): array
|
||||
{
|
||||
if (
|
||||
str_starts_with($value, '+ ') ||
|
||||
str_starts_with($value, '- ')
|
||||
) {
|
||||
$keys[] = $key . '=' . $key . ' ' . $value;
|
||||
} else {
|
||||
$params[$key . $order] = $value;
|
||||
$keys[] = $key . '=:' . $key . $order;
|
||||
}
|
||||
return [$keys, $params];
|
||||
}
|
||||
@@ -226,7 +248,7 @@ class SqlBuilder extends Component
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function _prefix($hasOrder = false): string
|
||||
private function _prefix(bool $hasOrder = false): string
|
||||
{
|
||||
$select = '';
|
||||
if (!empty($this->query->from)) {
|
||||
@@ -286,7 +308,7 @@ class SqlBuilder extends Component
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get($isCount = false): string
|
||||
public function get(bool $isCount = false): string
|
||||
{
|
||||
if ($isCount === false) {
|
||||
return $this->all();
|
||||
|
||||
@@ -104,14 +104,15 @@ trait Builder
|
||||
|
||||
/**
|
||||
* @param ActiveQuery|Query $query
|
||||
* @param bool $hasLimit
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] private function builderLimit(ActiveQuery|Query $query): string
|
||||
#[Pure] private function builderLimit(ActiveQuery|Query $query, bool $hasLimit = true): string
|
||||
{
|
||||
if (!is_numeric($query->limit) || $query->limit < 1) {
|
||||
return "";
|
||||
}
|
||||
if ($query->offset !== null) {
|
||||
if ($query->offset !== null && $hasLimit) {
|
||||
return ' LIMIT ' . $query->offset . ',' . $query->limit;
|
||||
}
|
||||
return ' LIMIT ' . $query->limit;
|
||||
|
||||
@@ -16,7 +16,6 @@ use Database\ActiveRecord;
|
||||
use Database\Condition\MathematicsCondition;
|
||||
use Database\Query;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
+3
-3
@@ -12,9 +12,6 @@ namespace Gii;
|
||||
use Database\Connection;
|
||||
use Database\Db;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Abstracts\Input;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
@@ -42,6 +39,9 @@ class Gii
|
||||
public string $controllerNamespace = 'App\\Http\\Controllers\\';
|
||||
|
||||
|
||||
public static array $createSqls = [];
|
||||
|
||||
|
||||
public array $keyword = [
|
||||
'ADD', 'ALL', 'ALTER', 'AND', 'AS', 'ASC', 'ASENSITIVE', 'BEFORE', 'BETWEEN', 'BIGINT', 'BINARY', 'BLOB', 'BOTH', 'BY', 'CALL', 'CASCADE', 'CASE', 'CHANGE', 'CHAR', 'CHARACTER', 'CHECK', 'COLLATE', 'COLUMN', 'CONDITION', 'CONNECTION', 'CONSTRAINT', 'CONTINUE', 'CONVERT', 'CREATE', 'CROSS', 'CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'CURRENT_USER', 'CURSOR', 'DATABASE', 'DATABASES', 'DAY_HOUR', 'DAY_MICROSECOND', 'DAY_MINUTE', 'DAY_SECOND', 'DEC', 'DECIMAL', 'DECLARE', 'DEFAULT', 'DELAYED', 'DELETE', 'DESC', 'DESCRIBE', 'DETERMINISTIC', 'DISTINCT', 'DISTINCTROW', 'DIV', 'DOUBLE', 'DROP', 'DUAL', 'EACH', 'ELSE', 'ELSEIF', 'ENCLOSED', 'ESCAPED', 'EXISTS', 'EXIT', 'EXPLAIN', 'FALSE', 'FETCH', 'FLOAT', 'FLOAT4', 'FLOAT8', 'FOR', 'FORCE', 'FOREIGN', 'FROM', 'FULLTEXT', 'GOTO', 'GRANT', 'GROUP', 'HAVING', 'HIGH_PRIORITY', 'HOUR_MICROSECOND', 'HOUR_MINUTE', 'HOUR_SECOND', 'IF', 'IGNORE', 'IN', 'INDEX', 'INFILE', 'INNER', 'INOUT', 'INSENSITIVE', 'INSERT', 'INT', 'INT1', 'INT2', 'INT3', 'INT4', 'INT8', 'INTEGER', 'INTERVAL', 'INTO', 'IS', 'ITERATE', 'JOIN', 'KEY', 'KEYS', 'KILL', 'LABEL', 'LEADING', 'LEAVE', 'LEFT', 'LIKE', 'LIMIT', 'LINEAR', 'LINES', 'LOAD', 'LOCALTIME', 'LOCALTIMESTAMP', 'LOCK', 'LONG', 'LONGBLOB', 'LONGTEXT', 'LOOP', 'LOW_PRIORITY', 'MATCH', 'MEDIUMBLOB', 'MEDIUMINT', 'MEDIUMTEXT', 'MIDDLEINT', 'MINUTE_MICROSECOND', 'MINUTE_SECOND', 'MOD', 'MODIFIES', 'NATURAL', 'NOT', 'NO_WRITE_TO_BINLOG', 'NULL', 'NUMERIC', 'ON', 'OPTIMIZE', 'OPTION', 'OPTIONALLY', 'OR', 'ORDER', 'OUT', 'OUTER', 'OUTFILE', 'PRECISION', 'PRIMARY', 'PROCEDURE', 'PURGE', 'RAID0', 'RANGE', 'READ', 'READS', 'REAL', 'REFERENCES', 'REGEXP', 'RELEASE', 'RENAME', 'REPEAT', 'REPLACE', 'REQUIRE', 'RESTRICT', 'RETURN', 'REVOKE', 'RIGHT', 'RLIKE', 'SCHEMA', 'SCHEMAS', 'SECOND_MICROSECOND', 'SELECT', 'SENSITIVE', 'SEPARATOR', 'SET', 'SHOW', 'SMALLINT', 'SPATIAL', 'SPECIFIC', 'SQL', 'SQLEXCEPTION', 'SQLSTATE', 'SQLWARNING', 'SQL_BIG_RESULT', 'SQL_CALC_FOUND_ROWS', 'SQL_SMALL_RESULT', 'SSL', 'STARTING', 'STRAIGHT_JOIN', 'TABLE', 'TERMINATED', 'THEN', 'TINYBLOB', 'TINYINT', 'TINYTEXT', 'TO', 'TRAILING', 'TRIGGER', 'TRUE', 'UNDO', 'UNION', 'UNIQUE', 'UNLOCK', 'UNSIGNED', 'UPDATE', 'USAGE', 'USE', 'USING', 'UTC_DATE', 'UTC_TIME', 'UTC_TIMESTAMP', 'VALUES', 'VARBINARY', 'VARCHAR', 'VARCHARACTER', 'VARYING', 'WHEN', 'WHERE', 'WHILE', 'WITH', 'WRITE', 'X509', 'XOR', 'YEAR_MONTH', 'ZEROFILL'
|
||||
];
|
||||
|
||||
+20
-12
@@ -22,12 +22,12 @@ class GiiModel extends GiiBase
|
||||
public ?array $fields;
|
||||
|
||||
/**
|
||||
* ModelFile constructor.
|
||||
* @param $classFileName
|
||||
* @param $tableName
|
||||
* @param $visible
|
||||
* @param $res
|
||||
* @param $fields
|
||||
* GiiModel constructor.
|
||||
* @param string $classFileName
|
||||
* @param string $tableName
|
||||
* @param array $visible
|
||||
* @param array $res
|
||||
* @param array $fields
|
||||
*/
|
||||
public function __construct(string $classFileName, string $tableName, array $visible, array $res, array $fields)
|
||||
{
|
||||
@@ -308,16 +308,20 @@ use Database\ActiveRecord;
|
||||
if (empty($data)) return '';
|
||||
$string = [];
|
||||
foreach ($data as $key => $_val) {
|
||||
if (is_string($key) && str_contains($key, ',')) {
|
||||
$key = '[' . $key . ']';
|
||||
if (in_array($_val[0][1], $this->type['float'])) {
|
||||
$e_x = explode(',', $key);
|
||||
$key = '\'round\' => ' . $e_x[1] . ', \'maxLength\' => ' . ((int)$e_x[0] + 1);
|
||||
} else if (is_string($key) && str_contains($key, ',')) {
|
||||
$key = '\'between\' => [' . $key . ']';
|
||||
} else {
|
||||
$key = '\'maxLength\' => ' . $key;
|
||||
}
|
||||
if (count($_val) == 1) {
|
||||
[$typeRule, $type, $rule, $field] = current($_val);
|
||||
$_tmp = '
|
||||
[\'' . $field . '\', \'' . ($type == 'enum' ? 'enum' : 'maxLength') . '\' => ' . $key . ']';
|
||||
[\'' . $_val[0][3] . '\', ' . ($_val[0][1] == 'enum' ? '\'enum\' => ' . $key : $key) . ']';
|
||||
} else {
|
||||
$_tmp = '
|
||||
[[\'' . implode('\', \'', array_column($_val, 3)) . '\'], \'maxLength\' => ' . $key . ']';
|
||||
[[\'' . implode('\', \'', array_column($_val, 3)) . '\'], ' . $key . ']';
|
||||
}
|
||||
$string[] = $_tmp;
|
||||
}
|
||||
@@ -412,6 +416,10 @@ use Database\ActiveRecord;
|
||||
*/
|
||||
private function setCreateSql($table): string
|
||||
{
|
||||
if (isset(Gii::$createSqls[$table])) {
|
||||
return Gii::$createSqls[$table];
|
||||
}
|
||||
|
||||
$text = Db::showCreateSql($table, $this->db)['Create Table'] ?? '';
|
||||
|
||||
$_tmp = [];
|
||||
@@ -419,7 +427,7 @@ use Database\ActiveRecord;
|
||||
$_tmp[] = '// ' . $val;
|
||||
}
|
||||
|
||||
return implode(PHP_EOL, $_tmp);
|
||||
return Gii::$createSqls[$table] = implode(PHP_EOL, $_tmp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -6,20 +6,10 @@ namespace HttpServer\Abstracts;
|
||||
|
||||
use Database\Connection;
|
||||
use Exception;
|
||||
use HttpServer\Http\Request;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\SMTP;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Error\LoggerProcess;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Process;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
/**
|
||||
@@ -30,7 +20,6 @@ abstract class Callback extends HttpService
|
||||
{
|
||||
|
||||
|
||||
|
||||
const EVENT_ERROR = 'WORKER:ERROR';
|
||||
const EVENT_STOP = 'WORKER:STOP';
|
||||
const EVENT_EXIT = 'WORKER:EXIT';
|
||||
@@ -42,43 +31,6 @@ abstract class Callback extends HttpService
|
||||
self::EVENT_EXIT => 'The server exit. at No.',
|
||||
];
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return PHPMailer
|
||||
* @throws \PHPMailer\PHPMailer\Exception
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function createEmail(): PHPMailer
|
||||
{
|
||||
$mail = new PHPMailer(true);
|
||||
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
|
||||
$mail->isSMTP(); // Send using SMTP
|
||||
$mail->Host = Config::get('email.host'); // Set the SMTP server to send through
|
||||
$mail->SMTPAuth = true; // Enable SMTP authentication
|
||||
$mail->Debugoutput = false; // Enable SMTP authentication
|
||||
$mail->CharSet = "UTF8"; // Enable SMTP authentication
|
||||
$mail->Username = Config::get('email.username'); // SMTP username
|
||||
$mail->Password = Config::get('email.password'); // SMTP password
|
||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
|
||||
$mail->Port = Config::get('email.port'); // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
|
||||
$mail->setFrom(Config::get('email.send.address'), Config::get('email.send.nickname'));
|
||||
return $mail;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $fd
|
||||
* @param $data
|
||||
* @param $reID
|
||||
* @return Request
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _request($fd, $data, $reID): Request
|
||||
{
|
||||
return Request::createListenRequest($fd, $data, $reID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $messageContent
|
||||
* @throws Exception
|
||||
@@ -132,6 +84,17 @@ abstract class Callback extends HttpService
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $clientInfo
|
||||
* @param string $event
|
||||
* @return string
|
||||
*/
|
||||
protected function getName(array $clientInfo, string $event): string
|
||||
{
|
||||
return 'listen ' . $clientInfo['server_port'] . ' ' . Event::SERVER_CONNECT;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
|
||||
@@ -22,7 +22,7 @@ abstract class HttpService extends Component
|
||||
* @param string $category
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function write($message, $category = 'app')
|
||||
protected function write($message, string $category = 'app')
|
||||
{
|
||||
$logger = Snowflake::app()->getLogger();
|
||||
$logger->write($message, $category);
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace HttpServer\Events;
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
@@ -28,24 +27,11 @@ class OnClose extends Callback
|
||||
{
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
$clientInfo = $server->getClientInfo($fd);
|
||||
if (!Event::exists(($name = $this->getName($clientInfo)))) {
|
||||
return;
|
||||
}
|
||||
Event::trigger($name, [$server, $fd]);
|
||||
|
||||
Event::trigger(Event::SERVER_CLIENT_CLOSE, [$server, $fd]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'throwable');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $server_port
|
||||
* @return string
|
||||
*/
|
||||
private function getName($server_port): string
|
||||
{
|
||||
return 'listen ' . $server_port['server_port'] . ' ' . Event::SERVER_CLIENT_CLOSE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,25 +27,17 @@ class OnConnect extends Callback
|
||||
{
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
if (($clientInfo = $server->getClientInfo($fd, $reactorId)) === false) {
|
||||
return;
|
||||
}
|
||||
fire($this->getName($clientInfo), [$server, $fd, $reactorId]);
|
||||
if (isset($clientInfo['websocket_status'])) {
|
||||
return;
|
||||
}
|
||||
fire($this->getName($clientInfo, Event::SERVER_CONNECT), [$server, $fd, $reactorId]);
|
||||
} catch (\Throwable $throwable) {
|
||||
$this->addError($throwable, 'connect');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $clientInfo
|
||||
* @return string
|
||||
*/
|
||||
private function getName(array $clientInfo): string
|
||||
{
|
||||
return 'listen ' . $clientInfo['server_port'] . ' ' . Event::SERVER_CONNECT;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ use HttpServer\Http\HttpHeaders;
|
||||
use HttpServer\Http\HttpParams;
|
||||
use HttpServer\Http\Request;
|
||||
use HttpServer\Http\Response;
|
||||
use HttpServer\Route\Router;
|
||||
use ReflectionException;
|
||||
use Snowflake\Core\ArrayAccess;
|
||||
use Snowflake\Event;
|
||||
@@ -32,9 +33,10 @@ class OnHandshake extends Callback
|
||||
/**
|
||||
* @param $request
|
||||
* @param $response
|
||||
* @return Router
|
||||
* @throws Exception
|
||||
*/
|
||||
private function resolveParse($request, $response)
|
||||
private function _protocol($request, $response): Router
|
||||
{
|
||||
/** @var Server $server */
|
||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||
@@ -55,36 +57,23 @@ class OnHandshake extends Callback
|
||||
foreach ($headers as $key => $val) {
|
||||
$response->header($key, $val);
|
||||
}
|
||||
return Snowflake::app()->getRouter();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param SResponse $response
|
||||
* @param int $code
|
||||
* @return false
|
||||
* @return void
|
||||
*/
|
||||
private function disconnect(SResponse $response, $code = 500): bool
|
||||
private function disconnect(SResponse $response, int $code = 500): void
|
||||
{
|
||||
$server = Snowflake::getWebSocket();
|
||||
if (!$server->exist($response->fd)) {
|
||||
return false;
|
||||
if (!$server->isEstablished($response->fd)) {
|
||||
return;
|
||||
}
|
||||
$response->status($code);
|
||||
$response->end();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $response
|
||||
* @param int $code
|
||||
* @return false
|
||||
*/
|
||||
private function connect($response, $code = 101): bool
|
||||
{
|
||||
$response->status($code);
|
||||
$response->end();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -99,7 +88,15 @@ class OnHandshake extends Callback
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
$this->execute($request, $response);
|
||||
$router = $this->_protocol($request, $response);
|
||||
|
||||
[$sRequest, $sResponse] = $this->sRequest($request, $response);
|
||||
|
||||
if (($node = $router->find_path($sRequest)) !== null) {
|
||||
$node->dispatch($sRequest, $sResponse);
|
||||
} else {
|
||||
$this->disconnect($response, 404);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'throwable');
|
||||
$response->status(500);
|
||||
@@ -108,33 +105,13 @@ class OnHandshake extends Callback
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param SRequest $request
|
||||
* @param SResponse $response
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
private function execute(SRequest $request, SResponse $response): mixed
|
||||
{
|
||||
$this->resolveParse($request, $response);
|
||||
|
||||
$router = Snowflake::app()->getRouter();
|
||||
|
||||
[$sRequest, $sResponse] = $this->sRequest($request, $response);
|
||||
|
||||
if (($node = $router->find_path($sRequest)) !== null) {
|
||||
return $node->dispatch($sRequest, $sResponse);
|
||||
}
|
||||
return $this->disconnect($response, 404);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @param SResponse $response
|
||||
* @return array
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function sRequest($request, SResponse $response): array
|
||||
{
|
||||
|
||||
@@ -18,7 +18,7 @@ class OnManagerStop extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $server
|
||||
* @param Server $server
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server)
|
||||
|
||||
@@ -4,22 +4,8 @@ declare(strict_types=1);
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Annotation\Route\Socket;
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use HttpServer\Http\Context;
|
||||
use HttpServer\Http\HttpHeaders;
|
||||
use HttpServer\Http\HttpParams;
|
||||
use HttpServer\Http\Request;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use Swoole\WebSocket\Server;
|
||||
|
||||
@@ -42,7 +28,10 @@ class OnMessage extends Callback
|
||||
if ($frame->opcode === 0x08) {
|
||||
return;
|
||||
}
|
||||
Event::trigger($this->getName($server, $frame), [$frame, $server]);
|
||||
|
||||
$clientInfo = $this->getName($server->getClientInfo($frame->fd), Event::SERVER_MESSAGE);
|
||||
|
||||
Event::trigger($clientInfo, [$frame, $server]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'websocket');
|
||||
if (!swoole()->isEstablished($frame->fd)) {
|
||||
@@ -52,16 +41,4 @@ class OnMessage extends Callback
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $clientInfo
|
||||
* @return string
|
||||
*/
|
||||
private function getName(Server $server, Frame $frame): string
|
||||
{
|
||||
$clientInfo = $server->getClientInfo($frame->fd);
|
||||
|
||||
return 'listen ' . $clientInfo['server_port'] . ' ' . Event::SERVER_MESSAGE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
@@ -18,45 +17,30 @@ use Swoole\Server;
|
||||
class OnPacket extends Callback
|
||||
{
|
||||
|
||||
|
||||
public int $port = 0;
|
||||
|
||||
|
||||
public string $host = '';
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param $data
|
||||
* @param $clientInfo
|
||||
* @param string $data
|
||||
* @param array $client
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, string $data, array $clientInfo): mixed
|
||||
public function onHandler(Server $server, string $data, array $client): mixed
|
||||
{
|
||||
[$host, $port] = [$clientInfo['address'], $clientInfo['port']];
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
$request = $this->_request($clientInfo, $server, $data);
|
||||
$client['server_port'] = $client['port'];
|
||||
$name = $this->getName($client, Event::SERVER_RECEIVE);
|
||||
|
||||
$router = Snowflake::app()->getRouter();
|
||||
if (($node = $router->find_path($request)) === null) {
|
||||
return $server->sendto($host, $port, Json::encode(['state' => 404]));
|
||||
}
|
||||
|
||||
$dispatch = $node->dispatch();
|
||||
if (!is_string($dispatch)) $dispatch = Json::encode($dispatch);
|
||||
if (empty($dispatch)) {
|
||||
$dispatch = Json::encode(['state' => 0, 'message' => 'ok']);
|
||||
}
|
||||
return $server->sendto($host, $port, $dispatch);
|
||||
$result = Event::trigger($name, [$server, $data, $client]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'packet');
|
||||
|
||||
$response = Json::encode(['state' => 500, 'message' => $exception->getMessage()]);
|
||||
|
||||
return $server->sendto($host, $port, $response);
|
||||
$result = logger()->exception($exception);
|
||||
} finally {
|
||||
if (is_array($result) || is_object($result)) {
|
||||
$result = Json::encode($result);
|
||||
}
|
||||
$sendData = [$client['address'], $client['port'], $result];
|
||||
return $server->sendto(...$sendData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,9 @@ namespace HttpServer\Events;
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use HttpServer\Http\Request;
|
||||
use HttpServer\Route\Router;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
@@ -22,21 +19,6 @@ use Swoole\Server;
|
||||
class OnReceive extends Callback
|
||||
{
|
||||
|
||||
public int $port = 0;
|
||||
|
||||
|
||||
public string $host = '';
|
||||
|
||||
|
||||
private Router $router;
|
||||
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->router = Snowflake::app()->getRouter();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
@@ -50,26 +32,21 @@ class OnReceive extends Callback
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
$request = $this->_request($fd, $data, $reID);
|
||||
if (($node = $this->router->find_path($request)) === null) {
|
||||
return $server->send($fd, Json::encode(['state' => 404]));
|
||||
$client = $server->getClientInfo($fd, $reID);
|
||||
$name = $this->getName($client, Event::SERVER_RECEIVE);
|
||||
|
||||
if (Config::get('rpc.port', 0) == $client['server_port']) {
|
||||
$result = router()->find_path(Request::rpcRequest($fd, $data, $reID))?->dispatch();
|
||||
} else {
|
||||
$result = Event::trigger($name, [$server, $data, $client]);
|
||||
}
|
||||
$dispatch = $node->dispatch();
|
||||
if (!is_string($dispatch)) $dispatch = Json::encode($dispatch);
|
||||
if (empty($dispatch)) {
|
||||
$dispatch = Json::encode(['state' => 0, 'message' => 'ok']);
|
||||
if (is_array($result) || is_object($result)) {
|
||||
$result = Json::encode($result);
|
||||
}
|
||||
if ($server->exist($fd)) {
|
||||
return $server->send($fd, $dispatch);
|
||||
}
|
||||
return $dispatch;
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'receive');
|
||||
$error = ['state' => 500, 'message' => $exception->getMessage()];
|
||||
if ($server->exist($fd)) {
|
||||
return $server->send($fd, Json::encode($error));
|
||||
}
|
||||
return Json::encode($error);
|
||||
$result = logger()->exception($exception);
|
||||
} finally {
|
||||
return $server->send($fd, $result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ use Snowflake\Snowflake;
|
||||
use Swoole\Error;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\Http\Status;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class OnRequest
|
||||
@@ -59,7 +61,7 @@ class OnRequest extends Callback
|
||||
return $response->close(404);
|
||||
}
|
||||
return $this->router->dispatch();
|
||||
} catch (ExitException | Error | \Throwable $exception) {
|
||||
} catch (ExitException | Error | Throwable $exception) {
|
||||
$this->addError($exception, 'throwable');
|
||||
return $this->sendErrorMessage($request, $response, $exception);
|
||||
}
|
||||
@@ -81,11 +83,11 @@ class OnRequest extends Callback
|
||||
/**
|
||||
* @param $sRequest
|
||||
* @param $sResponse
|
||||
* @param $exception
|
||||
* @param Throwable $exception
|
||||
* @return bool|string
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function sendErrorMessage($sRequest, $sResponse, $exception): bool|string
|
||||
protected function sendErrorMessage($sRequest, $sResponse, Throwable $exception): bool|string
|
||||
{
|
||||
$this->addError($exception, 'throwable');
|
||||
if ($sResponse instanceof Response) {
|
||||
@@ -95,6 +97,7 @@ class OnRequest extends Callback
|
||||
$headers = $sRequest->headers->get('access-control-request-headers');
|
||||
$methods = $sRequest->headers->get('access-control-request-method');
|
||||
|
||||
/** @var HResponse $sResponse */
|
||||
$sResponse->addHeader('Access-Control-Allow-Origin', '*');
|
||||
$sResponse->addHeader('Access-Control-Allow-Headers', $headers);
|
||||
$sResponse->addHeader('Access-Control-Request-Method', $methods);
|
||||
|
||||
@@ -102,7 +102,7 @@ class OnTask extends Callback
|
||||
private function runTaskHandler($data): ?array
|
||||
{
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_CLEAN));
|
||||
|
||||
$serialize = $this->before($data);
|
||||
if ($serialize === null) {
|
||||
|
||||
@@ -6,9 +6,7 @@ namespace HttpServer\Events;
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Snowflake\Error\Logger;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,7 +23,7 @@ use HttpServer\IInterface\IFormatter;
|
||||
class HtmlFormatter extends HttpService implements IFormatter
|
||||
{
|
||||
|
||||
public $data;
|
||||
public mixed $data;
|
||||
|
||||
/** @var Response */
|
||||
public Response $status;
|
||||
|
||||
+16
-31
@@ -415,10 +415,10 @@ class Request extends HttpService
|
||||
{
|
||||
$mainstay = sprintf("%.6f", microtime(true)); // 带毫秒的时间戳
|
||||
|
||||
$timestamp = floor($mainstay); // 时间戳
|
||||
$timestamp = floatval($mainstay); // 时间戳
|
||||
$milliseconds = round(($mainstay - $timestamp) * 1000); // 毫秒
|
||||
|
||||
$datetime = date("Y-m-d H:i:s", $timestamp) . '.' . $milliseconds;
|
||||
$datetime = date("Y-m-d H:i:s", (int)$timestamp) . '.' . $milliseconds;
|
||||
|
||||
$tmp = [
|
||||
'[Debug ' . $datetime . '] ',
|
||||
@@ -427,7 +427,6 @@ class Request extends HttpService
|
||||
'`' . $this->headers->getHeader('user-agent') . '`',
|
||||
$this->getRuntime()
|
||||
];
|
||||
|
||||
return implode(' ', $tmp);
|
||||
}
|
||||
|
||||
@@ -466,7 +465,7 @@ class Request extends HttpService
|
||||
$sRequest->params->setPosts($request->post ?? []);
|
||||
}
|
||||
|
||||
$sRequest->headers = new HttpHeaders(ArrayAccess::merge($request->server, $request->header));
|
||||
$sRequest->headers = new HttpHeaders(array_merge($request->server, $request->header));
|
||||
$sRequest->uri = $sRequest->headers->get('request_uri');
|
||||
|
||||
$sRequest->parseUri();
|
||||
@@ -476,11 +475,11 @@ class Request extends HttpService
|
||||
|
||||
/**
|
||||
* @param $frame
|
||||
* @param $route
|
||||
* @param string $route
|
||||
* @param string $event
|
||||
* @return Request
|
||||
*/
|
||||
public static function socketQuery($frame, $event = Socket::MESSAGE, $route = 'event'): Request
|
||||
public static function socketQuery($frame, string $event = Socket::MESSAGE, string $route = 'event'): Request
|
||||
{
|
||||
$sRequest = new Request();
|
||||
$sRequest->fd = $frame->fd;
|
||||
@@ -500,10 +499,11 @@ class Request extends HttpService
|
||||
* @param $fd
|
||||
* @param $data
|
||||
* @param int $reID
|
||||
* @return Request
|
||||
* @return mixed|null
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function createListenRequest($fd, $data, $reID = 0): Request
|
||||
public static function rpcRequest($fd, $data, int $reID = 0): Request|null
|
||||
{
|
||||
$sRequest = new Request();
|
||||
|
||||
@@ -514,28 +514,11 @@ class Request extends HttpService
|
||||
$sRequest->params = new HttpParams($data, [], []);
|
||||
|
||||
$port = $sRequest->clientInfo['server_port'];
|
||||
|
||||
$sRequest->headers->setRequestUri('add-port-listen/port_' . $port);
|
||||
$sRequest->headers->setRequestMethod(self::HTTP_LISTEN);
|
||||
|
||||
$sRequest->checkIsRpcClient()->parseUri();
|
||||
|
||||
return Context::setContext('request', $sRequest);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function checkIsRpcClient(): static
|
||||
{
|
||||
$port = $this->clientInfo['server_port'];
|
||||
if (($rpc = Config::get('rpc.port', 0)) !== $port) {
|
||||
return $this;
|
||||
return null;
|
||||
}
|
||||
|
||||
[$cmd, $repeat, $body] = explode("\n", $this->params->getBody());
|
||||
[$cmd, $repeat, $body] = explode("\n", $sRequest->params->getBody());
|
||||
if (is_null($body) || is_null($cmd) || !empty($repeat)) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
@@ -543,10 +526,12 @@ class Request extends HttpService
|
||||
if (is_string($body) && is_null($data = Json::decode($body))) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
$this->headers->setRequestUri('rpc/p' . $rpc . '/' . ltrim($cmd, '/'));
|
||||
$this->headers->setRequestMethod(Request::HTTP_CMD);
|
||||
|
||||
return $this;
|
||||
$sRequest->params->setPosts($data);
|
||||
$sRequest->headers->setRequestUri('rpc/p' . $rpc . '/' . ltrim($cmd, '/'));
|
||||
$sRequest->headers->setRequestMethod(Request::HTTP_CMD);
|
||||
|
||||
return Context::setContext('request', $sRequest);
|
||||
}
|
||||
|
||||
|
||||
@@ -556,7 +541,7 @@ class Request extends HttpService
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function getClientInfo($fd, $re = 0): mixed
|
||||
private static function getClientInfo($fd, int $re = 0): mixed
|
||||
{
|
||||
$server = Snowflake::app()->getSwoole();
|
||||
if (!is_array($fd)) {
|
||||
|
||||
@@ -20,6 +20,7 @@ use Snowflake\Core\Json;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Http\Response as SResponse;
|
||||
use Swoole\Http2\Response as S2Response;
|
||||
use Swoole\WebSocket\Server;
|
||||
|
||||
/**
|
||||
* Class Response
|
||||
@@ -149,12 +150,12 @@ class Response extends HttpService
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $context
|
||||
* @param mixed $context
|
||||
* @param int $statusCode
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function send($context = '', $statusCode = 200): mixed
|
||||
public function send(mixed $context = '', int $statusCode = 200): mixed
|
||||
{
|
||||
$sendData = $this->parseData($context);
|
||||
|
||||
@@ -191,19 +192,18 @@ class Response extends HttpService
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
* @return string
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function printResult($result): string
|
||||
private function printResult($result): void
|
||||
{
|
||||
$result = Help::toString($result);
|
||||
$string = PHP_EOL . 'Command Result: ' . PHP_EOL . PHP_EOL;
|
||||
|
||||
fire('CONSOLE_END');
|
||||
if (str_contains((string)$result, 'Event::rshutdown(): Event::wait()')) {
|
||||
return (string)$result;
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($result)) {
|
||||
$string .= 'success!' . PHP_EOL . PHP_EOL;
|
||||
} else {
|
||||
@@ -211,8 +211,6 @@ class Response extends HttpService
|
||||
}
|
||||
$string .= 'Command End!' . PHP_EOL . PHP_EOL;
|
||||
print_r($string);
|
||||
|
||||
return (string)$result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -223,7 +221,8 @@ class Response extends HttpService
|
||||
*/
|
||||
private function sendData($response, $sendData, $status): void
|
||||
{
|
||||
if (!Snowflake::getWebSocket()->exist($response->fd)) {
|
||||
$server = Snowflake::app()->getSwoole();
|
||||
if (!$server->exist($response->fd)) {
|
||||
return;
|
||||
}
|
||||
if (is_array($sendData)) {
|
||||
@@ -248,7 +247,7 @@ class Response extends HttpService
|
||||
return $response;
|
||||
}
|
||||
foreach ($this->headers as $key => $header) {
|
||||
$response->header($key, $header, true);
|
||||
$response->header($key, $header);
|
||||
}
|
||||
$this->headers = [];
|
||||
return $response;
|
||||
@@ -295,7 +294,7 @@ class Response extends HttpService
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function close($statusCode = 200, $message = ''): mixed
|
||||
public function close(int $statusCode = 200, string $message = ''): mixed
|
||||
{
|
||||
return $this->send($message, $statusCode);
|
||||
}
|
||||
@@ -307,7 +306,7 @@ class Response extends HttpService
|
||||
* @param string $message
|
||||
* @return mixed
|
||||
*/
|
||||
public function closeClient($clientId, $statusCode = 200, $message = ''): mixed
|
||||
public function closeClient($clientId,int $statusCode = 200,string $message = ''): mixed
|
||||
{
|
||||
$socket = Snowflake::getWebSocket();
|
||||
if (!$socket->exist($clientId)) {
|
||||
@@ -324,7 +323,7 @@ class Response extends HttpService
|
||||
* @param int $sleep
|
||||
* @return string
|
||||
*/
|
||||
public function sendFile(string $path, $offset = 0, $limit = 1024000, $sleep = 0): string
|
||||
public function sendFile(string $path, int $offset = 0, int $limit = 1024000, int $sleep = 0): string
|
||||
{
|
||||
$open = fopen($path, 'r');
|
||||
|
||||
|
||||
@@ -16,12 +16,10 @@ use HttpServer\Abstracts\HttpService;
|
||||
use HttpServer\Controller;
|
||||
use HttpServer\Http\Context;
|
||||
use HttpServer\Http\Request;
|
||||
use HttpServer\HttpFilter;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Snowflake;
|
||||
use Throwable;
|
||||
use function Input;
|
||||
|
||||
/**
|
||||
* Class Node
|
||||
@@ -449,15 +447,11 @@ class Node extends HttpService
|
||||
if (empty($this->childes)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($this->childes[$search])) {
|
||||
return $this->childes[$search];
|
||||
}
|
||||
|
||||
$_searchMatch = '/<(\w+)?:(.+)?>/';
|
||||
foreach ($this->childes as $key => $val) {
|
||||
if (preg_match($_searchMatch, (string)$key, $match)) {
|
||||
Input()->addGetParam($match[1] ?? '--', $search);
|
||||
if ($search == $key) {
|
||||
return $this->childes[$key];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,10 @@ use Closure;
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\HttpService;
|
||||
use HttpServer\Controller;
|
||||
use HttpServer\Exception\RequestException;
|
||||
use HttpServer\Http\Context;
|
||||
use HttpServer\Http\Request;
|
||||
use HttpServer\Http\Response;
|
||||
use HttpServer\IInterface\Middleware;
|
||||
use HttpServer\IInterface\RouterInterface;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
@@ -34,7 +37,7 @@ class Router extends HttpService implements RouterInterface
|
||||
const NOT_FOUND = 'Page not found or method not allowed.';
|
||||
|
||||
/** @var string[] */
|
||||
public array $methods = ['get', 'post', 'options', 'put', 'delete', 'receive'];
|
||||
public array $methods = ['get', 'post', 'options', 'put', 'delete', 'receive', 'head'];
|
||||
|
||||
|
||||
public ?Closure $middleware = null;
|
||||
@@ -261,7 +264,7 @@ class Router extends HttpService implements RouterInterface
|
||||
public function any($route, $handler): Any
|
||||
{
|
||||
$nodes = [];
|
||||
foreach (['get', 'post', 'options', 'put', 'delete'] as $method) {
|
||||
foreach (['get', 'post', 'options', 'put', 'delete', 'head'] as $method) {
|
||||
$nodes[] = $this->addRoute($route, $handler, $method);
|
||||
}
|
||||
return new Any($nodes);
|
||||
@@ -278,6 +281,19 @@ class Router extends HttpService implements RouterInterface
|
||||
return $this->addRoute($route, $handler, 'delete');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $route
|
||||
* @param $handler
|
||||
* @return \HttpServer\Route\Node|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function head($route, $handler): ?Node
|
||||
{
|
||||
return $this->addRoute($route, $handler, 'head');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $route
|
||||
* @param $handler
|
||||
@@ -490,7 +506,7 @@ class Router extends HttpService implements RouterInterface
|
||||
{
|
||||
$node = $this->find_path(\request());
|
||||
if (!($node instanceof Node)) {
|
||||
return send(self::NOT_FOUND);
|
||||
throw new RequestException(self::NOT_FOUND, 404);
|
||||
}
|
||||
send(($response = $node->dispatch()), 200);
|
||||
if (!$node->hasAfter()) {
|
||||
|
||||
@@ -304,9 +304,13 @@ class Server extends HttpService
|
||||
return;
|
||||
}
|
||||
foreach ($config['events'] as $name => $_event) {
|
||||
if ($name === Event::SERVER_CLIENT_CLOSE) {
|
||||
Event::on($name, $_event);
|
||||
}else{
|
||||
Event::on('listen ' . $config['port'] . ' ' . $name, $_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $config
|
||||
|
||||
+49
-17
@@ -6,7 +6,6 @@ namespace HttpServer;
|
||||
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
|
||||
|
||||
/**
|
||||
@@ -51,7 +50,7 @@ class Shutdown extends Component
|
||||
|
||||
$master_pid = Server()->setting['pid_file'] ?? PID_PATH;
|
||||
if (file_exists($master_pid)) {
|
||||
$this->close($master_pid);
|
||||
$this->close(file_get_contents($master_pid));
|
||||
}
|
||||
$this->closeOther();
|
||||
}
|
||||
@@ -109,33 +108,66 @@ class Shutdown extends Component
|
||||
*/
|
||||
public function directoryCheck(string $path): bool
|
||||
{
|
||||
$dir = new \DirectoryIterator($path);
|
||||
if ($dir->getSize() < 1) {
|
||||
return true;
|
||||
}
|
||||
foreach ($dir as $value) {
|
||||
if ($value->isDot()) continue;
|
||||
$values = $this->getProcessPidS($path);
|
||||
if (empty($values)) return false;
|
||||
|
||||
if (!$value->valid()) continue;
|
||||
|
||||
$this->close($value->getRealPath());
|
||||
$diff = array_diff($values, $this->getPidS());
|
||||
foreach ($diff as $value) {
|
||||
$this->pidIsExists($value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @param $path
|
||||
* @return array|bool
|
||||
*/
|
||||
public function close(string $value)
|
||||
private function getProcessPidS($path): bool|array
|
||||
{
|
||||
$content = file_get_contents($value);
|
||||
$values = [];
|
||||
$dir = new \DirectoryIterator($path);
|
||||
if ($dir->getSize() < 1) {
|
||||
return $values;
|
||||
}
|
||||
foreach ($dir as $value) {
|
||||
if ($value->isDot()) continue;
|
||||
|
||||
while ($this->pidIsExists($content)) {
|
||||
exec('kill -15 ' . $content);
|
||||
sleep(1);
|
||||
if (!$value->valid()) continue;
|
||||
|
||||
$_value = file_get_contents($value->getRealPath());
|
||||
if (empty($_value)) {
|
||||
continue;
|
||||
}
|
||||
$values[] = intval($_value);
|
||||
|
||||
@unlink($value->getRealPath());
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getPidS(): array
|
||||
{
|
||||
exec('ps -eo pid', $output);
|
||||
return array_filter($output, function ($value) {
|
||||
return intval($value);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function close(mixed $value)
|
||||
{
|
||||
while ($this->pidIsExists($value)) {
|
||||
exec('kill -15 ' . $value);
|
||||
usleep(100);
|
||||
}
|
||||
clearstatcache($value);
|
||||
if (file_exists($value)) {
|
||||
@unlink($value);
|
||||
|
||||
@@ -38,6 +38,7 @@ use Snowflake\Event;
|
||||
use Snowflake\Exception\InitException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Jwt\Jwt;
|
||||
use Snowflake\Pool\ClientsPool;
|
||||
use Snowflake\Pool\Connection;
|
||||
use Snowflake\Pool\Pool as SPool;
|
||||
use Snowflake\Pool\Redis as SRedis;
|
||||
@@ -434,6 +435,16 @@ abstract class BaseApplication extends Service
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return ClientsPool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getClientsPool(): ClientsPool
|
||||
{
|
||||
return $this->get('clientsPool');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
@@ -444,6 +455,7 @@ abstract class BaseApplication extends Service
|
||||
'connections' => ['class' => Connection::class],
|
||||
'redis_connections' => ['class' => SRedis::class],
|
||||
'pool' => ['class' => SPool::class],
|
||||
'clientsPool' => ['class' => ClientsPool::class],
|
||||
'config' => ['class' => Config::class],
|
||||
'logger' => ['class' => Logger::class],
|
||||
'annotation' => ['class' => SAnnotation::class],
|
||||
|
||||
@@ -11,7 +11,6 @@ namespace Snowflake\Abstracts;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
|
||||
@@ -104,7 +103,7 @@ class BaseObject implements Configure
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addError($message, $model = 'app'): bool
|
||||
public function addError($message, string $model = 'app'): bool
|
||||
{
|
||||
if ($message instanceof \Throwable) {
|
||||
$Throwable = $message;
|
||||
@@ -218,10 +217,10 @@ class BaseObject implements Configure
|
||||
}
|
||||
$content = (empty($method) ? '' : $method . ': ') . $message;
|
||||
|
||||
$message = "\033[41;37m" . PHP_EOL . "[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $content . PHP_EOL . "\033[0m";
|
||||
$message = "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $content . "\033[0m";
|
||||
|
||||
if (!empty($file)) {
|
||||
$message .= "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $file . "\033[0m";
|
||||
$message .= PHP_EOL . "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $file . "\033[0m";
|
||||
}
|
||||
$socket->output($message . PHP_EOL);
|
||||
}
|
||||
|
||||
@@ -57,12 +57,8 @@ class Config extends Component
|
||||
if (empty($configs)) {
|
||||
return;
|
||||
}
|
||||
if (!empty($config->data)) {
|
||||
$config->data = ArrayAccess::merge($config->data, $configs);
|
||||
} else {
|
||||
$config->data = $configs;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
|
||||
+48
-57
@@ -73,17 +73,15 @@ abstract class Pool extends Component
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function Heartbeat_detection()
|
||||
public function Heartbeat_detection($ticker, string $name)
|
||||
{
|
||||
if (env('state') == 'exit') {
|
||||
Timer::clear($this->creates);
|
||||
$this->creates = -1;
|
||||
} else if ($this->lastTime != 0) {
|
||||
[$firstClear, $lastClear] = $this->getClearTime();
|
||||
if ($this->lastTime + $firstClear < time()) {
|
||||
$this->flush(0);
|
||||
} else if ($this->lastTime + $lastClear < time()) {
|
||||
$this->flush(2);
|
||||
} else {
|
||||
$min = Config::get('databases.pool.min', 1);
|
||||
if (($length = $this->getChannel($name)->length()) > $min) {
|
||||
$this->flush($min);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -121,12 +119,12 @@ abstract class Pool extends Component
|
||||
|
||||
|
||||
/**
|
||||
* @param $channel
|
||||
* @param Channel $channel
|
||||
* @param $name
|
||||
* @param $retain_number
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function pop($channel, $name, $retain_number): void
|
||||
protected function pop(Channel $channel, $name, $retain_number): void
|
||||
{
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return;
|
||||
@@ -147,7 +145,7 @@ abstract class Pool extends Component
|
||||
* @param false $isMaster
|
||||
* @param int $max
|
||||
*/
|
||||
public function initConnections($driver, $name, $isMaster = false, $max = 60)
|
||||
public function initConnections($driver, $name, bool $isMaster = false, int $max = 60)
|
||||
{
|
||||
$name = $this->name($driver, $name, $isMaster);
|
||||
if (isset(static::$_items[$name]) && static::$_items[$name] instanceof Channel) {
|
||||
@@ -156,8 +154,28 @@ abstract class Pool extends Component
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return;
|
||||
}
|
||||
static::$_items[$name] = new Channel((int)$max);
|
||||
$this->max = (int)$max;
|
||||
if ($this->creates === -1) {
|
||||
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection'], $name);
|
||||
}
|
||||
static::$_items[$name] = new Channel($max);
|
||||
$this->max = $max;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return Channel
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function getChannel($name): Channel
|
||||
{
|
||||
if (!isset(static::$_items[$name])) {
|
||||
static::$_items[$name] = new Channel(Config::get('databases.pool.max', 10));
|
||||
if ($this->creates === -1) {
|
||||
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection'], $name);
|
||||
}
|
||||
}
|
||||
return static::$_items[$name];
|
||||
}
|
||||
|
||||
|
||||
@@ -172,36 +190,23 @@ abstract class Pool extends Component
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return $this->createClient($name, $callback);
|
||||
}
|
||||
$channel = static::$_items[$name] ?? new Channel($this->max);
|
||||
if (!((static::$_items[$name] ?? null) instanceof Channel)) {
|
||||
static::$_items[$name] = $channel;
|
||||
}
|
||||
if ($channel->isEmpty()) {
|
||||
$this->createByCallback($channel, $name, $callback);
|
||||
}
|
||||
|
||||
$channel = $this->getChannel($name);
|
||||
if (!$channel->isEmpty()) {
|
||||
$connection = $channel->pop();
|
||||
if (!$this->checkCanUse($name, $connection)) {
|
||||
return $this->createClient($name, $callback);
|
||||
} else {
|
||||
if ($this->checkCanUse($name, $connection)) {
|
||||
return $connection;
|
||||
}
|
||||
}
|
||||
return $this->createClient($name, $callback);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $channel
|
||||
* @param $name
|
||||
* @param mixed $callback
|
||||
* @param string $name
|
||||
* @param mixed $config
|
||||
* @return mixed
|
||||
*/
|
||||
private function createByCallback(Channel $channel, $name, mixed $callback): void
|
||||
{
|
||||
if ($this->creates === -1 && !is_callable($callback)) {
|
||||
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
|
||||
}
|
||||
$channel->push($this->createClient($name, $callback));
|
||||
}
|
||||
|
||||
|
||||
abstract public function createClient(string $name, mixed $config): mixed;
|
||||
|
||||
|
||||
@@ -211,7 +216,7 @@ abstract class Pool extends Component
|
||||
* @param false $isMaster
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public function name($driver, $cds, $isMaster = false): string
|
||||
#[Pure] public function name($driver, $cds, bool $isMaster = false): string
|
||||
{
|
||||
if ($isMaster === true) {
|
||||
return $cds . '_master';
|
||||
@@ -223,7 +228,7 @@ abstract class Pool extends Component
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param $client
|
||||
* @param mixed $client
|
||||
* @return bool
|
||||
* 检查连接可靠性
|
||||
*/
|
||||
@@ -235,7 +240,7 @@ abstract class Pool extends Component
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param $isMaster
|
||||
* @param bool $isMaster
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
@@ -249,19 +254,6 @@ abstract class Pool extends Component
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function canCreate(string $name): bool
|
||||
{
|
||||
if (!isset(static::$hasCreate[$name])) {
|
||||
static::$hasCreate[$name] = 0;
|
||||
}
|
||||
return static::$hasCreate[$name] < $this->max;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasItem(string $name): bool
|
||||
{
|
||||
if (isset(static::$_items[$name])) {
|
||||
@@ -272,7 +264,7 @@ abstract class Pool extends Component
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function size(string $name): mixed
|
||||
@@ -288,18 +280,16 @@ abstract class Pool extends Component
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $client
|
||||
* @param string $name
|
||||
* @param mixed $client
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function push(string $name, mixed $client)
|
||||
{
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return;
|
||||
}
|
||||
$channel = static::$_items[$name] ?? new Channel($this->max);
|
||||
if (!isset(static::$_items[$name])) {
|
||||
static::$_items[$name] = $channel;
|
||||
}
|
||||
$channel = $this->getChannel($name);
|
||||
if (!$channel->isFull()) {
|
||||
$channel->push($client);
|
||||
}
|
||||
@@ -321,6 +311,7 @@ abstract class Pool extends Component
|
||||
if ($this->creates > -1) {
|
||||
Timer::clear($this->creates);
|
||||
}
|
||||
$channel->close();
|
||||
static::$_items[$name] = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ use Snowflake\Channel;
|
||||
use Snowflake\Error\Logger;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Jwt\Jwt;
|
||||
use Snowflake\Pool\ClientsPool;
|
||||
use Snowflake\Pool\Connection;
|
||||
use Snowflake\Pool\Pool as SPool;
|
||||
use Rpc\Producer as RPCProducer;
|
||||
@@ -54,6 +55,7 @@ use Rpc\Producer as RPCProducer;
|
||||
* @property Channel $channel
|
||||
* @property Shutdown $shutdown
|
||||
* @property Emit $emit
|
||||
* @property ClientsPool $clientsPool
|
||||
*/
|
||||
trait TraitApplication
|
||||
{
|
||||
|
||||
@@ -35,7 +35,6 @@ use function Co\run;
|
||||
* @package Snowflake
|
||||
*
|
||||
* @property-read Config $config
|
||||
* @property-read WchatProviders $wchat
|
||||
*/
|
||||
class Application extends BaseApplication
|
||||
{
|
||||
@@ -62,6 +61,9 @@ class Application extends BaseApplication
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Closure|array $closure
|
||||
* @return $this
|
||||
|
||||
+17
-16
@@ -12,6 +12,7 @@ namespace Snowflake\Cache;
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Snowflake;
|
||||
@@ -20,6 +21,7 @@ use Snowflake\Snowflake;
|
||||
* Class Redis
|
||||
* @package Snowflake\Snowflake\Cache
|
||||
* @see \Redis
|
||||
*
|
||||
*/
|
||||
class Redis extends Component
|
||||
{
|
||||
@@ -51,11 +53,10 @@ class Redis extends Component
|
||||
$connections = Snowflake::app()->getRedisFromPool();
|
||||
|
||||
$config = $this->get_config();
|
||||
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
|
||||
|
||||
$length = env('REDIS.POOL_LENGTH', 100);
|
||||
$length = (int)env('REDIS.POOL_LENGTH', 100);
|
||||
|
||||
$connections->initConnections('redis', 'redis:' . $name, true, $length);
|
||||
$connections->initConnections('Redis:' . $config['host'], true, $length);
|
||||
}
|
||||
|
||||
|
||||
@@ -67,11 +68,17 @@ class Redis extends Component
|
||||
*/
|
||||
public function __call($name, $arguments): mixed
|
||||
{
|
||||
$time = microtime(true);
|
||||
if (method_exists($this, $name)) {
|
||||
$data = $this->{$name}(...$arguments);
|
||||
} else {
|
||||
$data = $this->proxy()->{$name}(...$arguments);
|
||||
}
|
||||
|
||||
if (microtime(true) - $time >= 0.02) {
|
||||
$this->warning('Redis:' . Json::encode([$name, $arguments]) . (microtime(true) - $time));
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
@@ -82,7 +89,7 @@ class Redis extends Component
|
||||
* @return bool|int
|
||||
* @throws Exception
|
||||
*/
|
||||
public function lock($key, $timeout = 5): bool|int
|
||||
public function lock($key, int $timeout = 5): bool|int
|
||||
{
|
||||
$script = <<<SCRIPT
|
||||
local _nx = redis.call('setnx',KEYS[1], ARGV[1])
|
||||
@@ -92,7 +99,7 @@ if (_nx ~= 0) then
|
||||
end
|
||||
return 0
|
||||
SCRIPT;
|
||||
return $this->proxy()->eval($script, [$key, $timeout], 1);
|
||||
return $this->proxy()->eval($script, ['{lock}:' . $key, $timeout], 1);
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +111,7 @@ SCRIPT;
|
||||
public function unlock($key): int
|
||||
{
|
||||
$redis = $this->proxy();
|
||||
return $redis->del($key);
|
||||
return $redis->del('{lock}:' . $key);
|
||||
}
|
||||
|
||||
|
||||
@@ -137,7 +144,9 @@ SCRIPT;
|
||||
{
|
||||
$connections = Snowflake::app()->getRedisFromPool();
|
||||
|
||||
$client = $connections->get($this->get_config(), true);
|
||||
$config = $this->get_config();
|
||||
|
||||
$client = $connections->get($config, true);
|
||||
if (!($client instanceof \Redis)) {
|
||||
throw new Exception('Redis connections more.');
|
||||
}
|
||||
@@ -150,15 +159,7 @@ SCRIPT;
|
||||
*/
|
||||
public function get_config(): array
|
||||
{
|
||||
return Config::get('cache.redis', false, [
|
||||
'host' => '127.0.0.1',
|
||||
'port' => '6379',
|
||||
'prefix' => Config::get('id'),
|
||||
'auth' => '',
|
||||
'databases' => '0',
|
||||
'read_timeout' => -1,
|
||||
'timeout' => -1,
|
||||
]);
|
||||
return Config::get('cache.redis', null, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+33
-4
@@ -21,6 +21,36 @@ class Channel extends Component
|
||||
private static array $_channels = [];
|
||||
|
||||
|
||||
private static array $_waitRecover = [];
|
||||
|
||||
|
||||
public function init()
|
||||
{
|
||||
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'recover']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 回收对象
|
||||
*/
|
||||
public function recover()
|
||||
{
|
||||
foreach (Channel::$_waitRecover as $key => $value) {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
$channel = $this->channelInit($key);
|
||||
if ($channel->count() >= 100) {
|
||||
continue;
|
||||
}
|
||||
foreach ($value as $item) {
|
||||
$channel->enqueue($item);
|
||||
}
|
||||
}
|
||||
Channel::$_waitRecover = [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param string $name
|
||||
@@ -28,11 +58,10 @@ class Channel extends Component
|
||||
*/
|
||||
public function push(mixed $value, string $name = ''): void
|
||||
{
|
||||
$channel = $this->channelInit($name);
|
||||
if ($channel->count() >= 100) {
|
||||
return;
|
||||
if (!isset(Channel::$_waitRecover[$name])) {
|
||||
Channel::$_waitRecover[$name] = [];
|
||||
}
|
||||
$channel->enqueue($value);
|
||||
Channel::$_waitRecover[$name][] = $value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -184,6 +184,19 @@ abstract class Crontab extends BaseObject
|
||||
abstract public function isStop(): bool;
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
if (!isset($this->params[$name])) {
|
||||
return null;
|
||||
}
|
||||
return $this->params[$name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
@@ -16,7 +16,7 @@ use Snowflake\Snowflake;
|
||||
class Producer extends Component
|
||||
{
|
||||
|
||||
const CRONTAB_KEY = '_application:system:crontab';
|
||||
const CRONTAB_KEY = '_application:{crontab}:system:crontab';
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -118,11 +118,16 @@ class Zookeeper extends Process
|
||||
*/
|
||||
private function loadCarobTask(Redis|\Redis $redis): array
|
||||
{
|
||||
$range = $redis->zRangeByScore(Producer::CRONTAB_KEY, '0', (string)time());
|
||||
$script = <<<SCRIPT
|
||||
local _two = redis.call('zRangeByScore', KEYS[1], '0', ARGV[1])
|
||||
|
||||
$redis->zRem(Producer::CRONTAB_KEY, ...$range);
|
||||
if (table.getn(_two) > 0) then
|
||||
redis.call('ZREM', KEYS[1], unpack(_two))
|
||||
end
|
||||
|
||||
return $range;
|
||||
return _two
|
||||
SCRIPT;
|
||||
return $redis->eval($script, [Producer::CRONTAB_KEY, (string)time()], 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+30
-10
@@ -5,11 +5,12 @@ namespace Snowflake\Jwt;
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Http\HttpHeaders;
|
||||
use Snowflake\Cache\Redis;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Cache\Redis;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Core\Str;
|
||||
use Snowflake\Exception\AuthException;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
@@ -96,7 +97,7 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $timeout
|
||||
* @param int $timeout
|
||||
*/
|
||||
public function setTimeout(int $timeout)
|
||||
{
|
||||
@@ -104,7 +105,7 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $timeout
|
||||
* @param string $timeout
|
||||
*/
|
||||
public function setKey(string $timeout)
|
||||
{
|
||||
@@ -127,7 +128,7 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function create(int $unionId, $headers = []): array
|
||||
public function create(int $unionId, array $headers = []): array
|
||||
{
|
||||
$this->user = $unionId;
|
||||
$this->config['time'] = time();
|
||||
@@ -181,7 +182,7 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
private function assembly(array $param, $update = FALSE): array
|
||||
private function assembly(array $param, bool $update = FALSE): array
|
||||
{
|
||||
if (isset($param['sign'])) {
|
||||
unset($param['sign']);
|
||||
@@ -204,7 +205,7 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function refresh($headers = []): array
|
||||
public function refresh(array $headers = []): array
|
||||
{
|
||||
$this->data = $headers;
|
||||
if (!openssl_public_decrypt(base64_decode($headers['refresh']), $data, $this->public)) {
|
||||
@@ -222,8 +223,27 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
|
||||
return $this->create($this->user, $headers);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $param
|
||||
* @param array $headers
|
||||
* @return mixed
|
||||
* @throws AuthException
|
||||
*/
|
||||
public function getTokenUser(array $headers = []): int
|
||||
{
|
||||
$this->data = $headers;
|
||||
if (!openssl_public_decrypt(base64_decode($headers['refresh']), $data, $this->public)) {
|
||||
throw new AuthException('信息解码失败.');
|
||||
}
|
||||
|
||||
$data = Json::decode($data, true);
|
||||
|
||||
return (int)$data['user'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $param
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -292,7 +312,7 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function token(int $user, $param = [], $requestTime = NULL): string
|
||||
private function token(int $user, array $param = [], $requestTime = NULL): string
|
||||
{
|
||||
$str = '';
|
||||
|
||||
@@ -349,7 +369,7 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY=
|
||||
* @param array $data
|
||||
* @param int $user
|
||||
* @return bool
|
||||
* @throws AuthException
|
||||
* @throws AuthException|Exception
|
||||
*/
|
||||
public function check(array $data, int $user): bool
|
||||
{
|
||||
|
||||
@@ -0,0 +1,332 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Pool;
|
||||
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Coroutine\Channel;
|
||||
use Swoole\Timer;
|
||||
|
||||
|
||||
/**
|
||||
* Class ClientsPool
|
||||
* @package Snowflake\Pool
|
||||
*/
|
||||
class ClientsPool extends Component
|
||||
{
|
||||
|
||||
/** @var Channel[] */
|
||||
private static array $_connections = [];
|
||||
|
||||
public int $max = 60;
|
||||
|
||||
public int $creates = -1;
|
||||
|
||||
|
||||
private array $_times = [];
|
||||
|
||||
protected static array $hasCreate = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function increment(string $name)
|
||||
{
|
||||
if (!isset(static::$hasCreate[$name])) {
|
||||
static::$hasCreate[$name] = 0;
|
||||
}
|
||||
static::$hasCreate[$name] += 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function decrement(string $name)
|
||||
{
|
||||
if (!isset(static::$hasCreate[$name])) {
|
||||
return;
|
||||
}
|
||||
if (static::$hasCreate[$name] <= 0) {
|
||||
return;
|
||||
}
|
||||
static::$hasCreate[$name] -= 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function getClearTime(): array
|
||||
{
|
||||
$firstClear = Config::get('pool.clear.start', 600);
|
||||
$lastClear = Config::get('pool.clear.end', 300);
|
||||
return [$firstClear, $lastClear];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function Heartbeat_detection($ticker)
|
||||
{
|
||||
if (env('state') == 'exit') {
|
||||
Timer::clear($this->creates);
|
||||
foreach (static::$_connections as $channel) {
|
||||
$this->flush($channel, 0);
|
||||
$channel->close();
|
||||
}
|
||||
static::$_connections = [];
|
||||
$this->creates = -1;
|
||||
} else {
|
||||
$this->heartbeat_flush();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function heartbeat_flush()
|
||||
{
|
||||
$num = [];
|
||||
$total = 0;
|
||||
$min = Config::get('databases.pool.min', 1);
|
||||
foreach (static::$_connections as $key => $channel) {
|
||||
if (!isset($num[$key])) {
|
||||
$num[$key] = 0;
|
||||
}
|
||||
if (time() - ($this->_times[$key] ?? time()) > 120) {
|
||||
$this->flush($channel, 0);
|
||||
} else if ($channel->length() > $min) {
|
||||
$this->flush($channel, $min);
|
||||
}
|
||||
$num[$key] += ($length = $channel->length());
|
||||
if (str_starts_with($key, 'Mysql') && (Snowflake::isWorker() || Snowflake::isTask()) && $length > 0) {
|
||||
$this->debug('Worker #' . env('worker') . ' use client -> ' . $key . ':' . $length);
|
||||
}
|
||||
$total += $length;
|
||||
}
|
||||
write(var_export($num, true), 'connections');
|
||||
if ($total < 1) {
|
||||
Timer::clear($this->creates);
|
||||
if (Snowflake::isWorker() || Snowflake::isTask()) {
|
||||
$this->debug('Worker #' . env('worker') . ' clear time tick.');
|
||||
}
|
||||
$this->creates = -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $channel
|
||||
* @param $retain_number
|
||||
* @throws Exception
|
||||
*/
|
||||
public function flush($channel, $retain_number)
|
||||
{
|
||||
$this->pop($channel, $retain_number);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Channel $channel
|
||||
* @param $retain_number
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function pop(Channel $channel, $retain_number): void
|
||||
{
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return;
|
||||
}
|
||||
while ($channel->length() > $retain_number) {
|
||||
$connection = $channel->pop();
|
||||
if ($connection) {
|
||||
unset($connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param false $isMaster
|
||||
* @param int $max
|
||||
*/
|
||||
public function initConnections($name, bool $isMaster = false, int $max = 60)
|
||||
{
|
||||
$name = $this->name($name, $isMaster);
|
||||
if (isset(static::$_connections[$name]) && static::$_connections[$name] instanceof Channel) {
|
||||
return;
|
||||
}
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return;
|
||||
}
|
||||
if ($this->creates === -1) {
|
||||
$this->creates = Timer::tick(60000, [$this, 'Heartbeat_detection']);
|
||||
}
|
||||
static::$_connections[$name] = new Channel($max);
|
||||
$this->max = $max;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return Channel
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getChannel($name): Channel
|
||||
{
|
||||
if (!isset(static::$_connections[$name])) {
|
||||
static::$_connections[$name] = new Channel(Config::get('databases.pool.max', 10));
|
||||
}
|
||||
if (static::$_connections[$name]->errCode == SWOOLE_CHANNEL_CLOSED){
|
||||
throw new Exception('Channel is Close.');
|
||||
}
|
||||
if ($this->creates === -1) {
|
||||
$this->creates = Timer::tick(60000, [$this, 'Heartbeat_detection']);
|
||||
}
|
||||
return static::$_connections[$name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getFromChannel($name): mixed
|
||||
{
|
||||
$this->_times[$name] = time();
|
||||
$channel = $this->getChannel($name);
|
||||
if (!$channel->isEmpty()) {
|
||||
$connection = $channel->pop();
|
||||
if ($this->checkCanUse($name, $connection)) {
|
||||
return $connection;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $cds
|
||||
* @param false $isMaster
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] public function name($cds, bool $isMaster = false): string
|
||||
{
|
||||
if ($isMaster === true) {
|
||||
return $cds . '_master';
|
||||
} else {
|
||||
return $cds . '_slave';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $client
|
||||
* @return bool
|
||||
* 检查连接可靠性
|
||||
*/
|
||||
public function checkCanUse(string $name, mixed $client): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(mixed $config, bool $isMaster): mixed
|
||||
{
|
||||
throw new Exception('Undefined system processing function.');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasItem(string $name): bool
|
||||
{
|
||||
if (isset(static::$_connections[$name])) {
|
||||
return !static::$_connections[$name]->isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function size(string $name): mixed
|
||||
{
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return 0;
|
||||
}
|
||||
if (!isset(static::$_connections[$name])) {
|
||||
return 0;
|
||||
}
|
||||
return static::$_connections[$name]->length();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $client
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function push(string $name, mixed $client)
|
||||
{
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return;
|
||||
}
|
||||
$channel = $this->getChannel($name);
|
||||
if (!$channel->isFull()) {
|
||||
$channel->push($client);
|
||||
}
|
||||
unset($client);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function clean(string $name)
|
||||
{
|
||||
if (Coroutine::getCid() === -1 || !isset(static::$_connections[$name])) {
|
||||
return;
|
||||
}
|
||||
$channel = static::$_connections[$name];
|
||||
$this->pop($channel, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Channel[]
|
||||
*/
|
||||
protected function getChannels(): array
|
||||
{
|
||||
return static::$_connections;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+62
-49
@@ -6,7 +6,8 @@ namespace Snowflake\Pool;
|
||||
use Exception;
|
||||
use HttpServer\Http\Context;
|
||||
use PDO;
|
||||
use Snowflake\Abstracts\Pool;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Error;
|
||||
use Swoole\Runtime;
|
||||
@@ -16,51 +17,36 @@ use Throwable;
|
||||
* Class Connection
|
||||
* @package Snowflake\Pool
|
||||
*/
|
||||
class Connection extends Pool
|
||||
class Connection extends Component
|
||||
{
|
||||
|
||||
private ?ClientsPool $clientsPool = null;
|
||||
|
||||
public int $timeout = 1900;
|
||||
|
||||
/**
|
||||
* @param $timeout
|
||||
*/
|
||||
public function setTimeout($timeout)
|
||||
{
|
||||
$this->timeout = $timeout;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setLength($value)
|
||||
{
|
||||
$this->max = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $cds
|
||||
* @return bool
|
||||
*
|
||||
* db is in transaction
|
||||
* @throws Exception
|
||||
*/
|
||||
public function inTransaction($cds): bool
|
||||
{
|
||||
return Context::getContext('begin_' . $this->name('mysql', $cds, true)) == 0;
|
||||
return Context::getContext('begin_' . $this->getPool()->name('Mysql:' . $cds, true)) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @throws Exception
|
||||
*/
|
||||
public function beginTransaction($coroutineName)
|
||||
{
|
||||
$coroutineName = $this->name('mysql', $coroutineName, true);
|
||||
$coroutineName = $this->getPool()->name('Mysql:' . $coroutineName, true);
|
||||
if (!Context::hasContext('begin_' . $coroutineName)) {
|
||||
Context::setContext('begin_' . $coroutineName, 0);
|
||||
}
|
||||
Context::increment('begin_' . $coroutineName);
|
||||
if (!Context::getContext('begin_' . $coroutineName) !== 0) {
|
||||
if (Context::getContext('begin_' . $coroutineName) != 0) {
|
||||
return;
|
||||
}
|
||||
$connection = Context::getContext($coroutineName);
|
||||
@@ -71,10 +57,11 @@ class Connection extends Pool
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @throws Exception
|
||||
*/
|
||||
public function commit($coroutineName)
|
||||
{
|
||||
$coroutineName = $this->name('mysql', $coroutineName, true);
|
||||
$coroutineName = $this->getPool()->name('Mysql:' . $coroutineName, true);
|
||||
if (!Context::hasContext('begin_' . $coroutineName)) {
|
||||
return;
|
||||
}
|
||||
@@ -92,22 +79,13 @@ class Connection extends Pool
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param false $isMaster
|
||||
* @return array
|
||||
*/
|
||||
private function getIndex($name, $isMaster = false): array
|
||||
{
|
||||
return [Coroutine::getCid(), $this->name('mysql', $name, $isMaster)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $coroutineName
|
||||
* @throws Exception
|
||||
*/
|
||||
public function rollback($coroutineName)
|
||||
{
|
||||
$coroutineName = $this->name('mysql', $coroutineName, true);
|
||||
$coroutineName = $this->getPool()->name('Mysql:' . $coroutineName, true);
|
||||
if (!Context::hasContext('begin_' . $coroutineName)) {
|
||||
return;
|
||||
}
|
||||
@@ -129,13 +107,21 @@ class Connection extends Pool
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(mixed $config, $isMaster = false): mixed
|
||||
public function get(mixed $config, bool $isMaster = false): mixed
|
||||
{
|
||||
$coroutineName = $this->name('mysql', $config['cds'], $isMaster);
|
||||
$coroutineName = $this->getPool()->name('Mysql:' . $config['cds'], $isMaster);
|
||||
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
|
||||
return $pdo;
|
||||
}
|
||||
$connections = $this->getFromChannel($coroutineName, $config);
|
||||
if (Coroutine::getCid() === -1) {
|
||||
$connections = $this->createClient($coroutineName, $config);
|
||||
} else {
|
||||
/** @var PDO $connections */
|
||||
$connections = $this->getPool()->getFromChannel($coroutineName);
|
||||
if (empty($connections)) {
|
||||
$connections = $this->createClient($coroutineName, $config);
|
||||
}
|
||||
}
|
||||
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
|
||||
$number > 0 && $connections->beginTransaction();
|
||||
}
|
||||
@@ -143,6 +129,18 @@ class Connection extends Pool
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $isMaster
|
||||
* @param $max
|
||||
* @throws Exception
|
||||
*/
|
||||
public function initConnections($name, $isMaster, $max)
|
||||
{
|
||||
$this->getPool()->initConnections($name, $isMaster, $max);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $config
|
||||
@@ -154,10 +152,11 @@ class Connection extends Pool
|
||||
if (Coroutine::getCid() === -1) {
|
||||
Runtime::enableCoroutine(false);
|
||||
}
|
||||
$link = new PDO($config['cds'], $config['username'], $config['password'], [
|
||||
$cds = 'mysql:dbname=' . $config['database'] . ';host=' . $config['cds'];
|
||||
$link = new PDO($cds, $config['username'], $config['password'], [
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
PDO::ATTR_CASE => PDO::CASE_NATURAL,
|
||||
PDO::ATTR_TIMEOUT => $this->timeout,
|
||||
PDO::ATTR_TIMEOUT => 60,
|
||||
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
|
||||
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4')
|
||||
]);
|
||||
@@ -175,7 +174,7 @@ class Connection extends Pool
|
||||
*/
|
||||
public function release($coroutineName, $isMaster)
|
||||
{
|
||||
$coroutineName = $this->name('mysql', $coroutineName, $isMaster);
|
||||
$coroutineName = $this->getPool()->name('Mysql:' . $coroutineName, $isMaster);
|
||||
|
||||
/** @var PDO $client */
|
||||
if (!($client = Context::getContext($coroutineName)) instanceof PDO) {
|
||||
@@ -184,8 +183,8 @@ class Connection extends Pool
|
||||
if ($client->inTransaction()) {
|
||||
$client->commit();
|
||||
}
|
||||
$this->push($coroutineName, $client);
|
||||
$this->lastTime = time();
|
||||
$this->getPool()->push($coroutineName, $client);
|
||||
Context::remove($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
@@ -203,9 +202,9 @@ class Connection extends Pool
|
||||
* batch release
|
||||
* @throws Exception
|
||||
*/
|
||||
public function connection_clear()
|
||||
public function connection_clear($name, $isMaster)
|
||||
{
|
||||
$this->flush(0);
|
||||
$this->getPool()->clean($this->getPool()->name($name, $isMaster));
|
||||
}
|
||||
|
||||
|
||||
@@ -227,7 +226,7 @@ class Connection extends Pool
|
||||
$result = $this->addError($exception, 'mysql');
|
||||
} finally {
|
||||
if (!$result) {
|
||||
$this->decrement($name);
|
||||
$this->getPool()->decrement($name);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
@@ -239,10 +238,24 @@ class Connection extends Pool
|
||||
* @param bool $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function disconnect($coroutineName, $isMaster = false)
|
||||
public function disconnect($coroutineName, bool $isMaster = false)
|
||||
{
|
||||
$coroutineName = $this->name($coroutineName, $isMaster);
|
||||
$this->clean($coroutineName);
|
||||
Context::remove($coroutineName);
|
||||
$coroutineName = $this->getPool()->name('Mysql:' . $coroutineName, $isMaster);
|
||||
$this->getPool()->clean($coroutineName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return ClientsPool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPool(): ClientsPool
|
||||
{
|
||||
if (!$this->clientsPool) {
|
||||
$this->clientsPool = Snowflake::app()->getClientsPool();
|
||||
}
|
||||
return $this->clientsPool;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+44
-60
@@ -8,8 +8,10 @@ namespace Snowflake\Pool;
|
||||
use Exception;
|
||||
use HttpServer\Http\Context;
|
||||
use Redis as SRedis;
|
||||
use Snowflake\Abstracts\Pool;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\RedisConnectException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Runtime;
|
||||
|
||||
@@ -17,32 +19,10 @@ use Swoole\Runtime;
|
||||
* Class RedisClient
|
||||
* @package Snowflake\Snowflake\Pool
|
||||
*/
|
||||
class Redis extends Pool
|
||||
class Redis extends Component
|
||||
{
|
||||
|
||||
|
||||
public int $_create = 0;
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setLength($value)
|
||||
{
|
||||
$this->max = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function canCreate(string $name): bool
|
||||
{
|
||||
if (!isset(static::$hasCreate[$name])) {
|
||||
static::$hasCreate[$name] = 0;
|
||||
}
|
||||
return static::$hasCreate[$name] >= $this->max;
|
||||
}
|
||||
private ?ClientsPool $clientsPool = null;
|
||||
|
||||
|
||||
/**
|
||||
@@ -51,15 +31,21 @@ class Redis extends Pool
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(mixed $config, $isMaster = false): mixed
|
||||
public function get(mixed $config, bool $isMaster = false): mixed
|
||||
{
|
||||
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
|
||||
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
|
||||
if (!Context::hasContext($coroutineName)) {
|
||||
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
|
||||
}
|
||||
$coroutineName = $this->getPool()->name('Redis:' . $config['host'], $isMaster);
|
||||
if (Context::hasContext($coroutineName)) {
|
||||
return Context::getContext($coroutineName);
|
||||
}
|
||||
if (Coroutine::getCid() === -1) {
|
||||
return Context::setContext($coroutineName, $this->createClient($coroutineName, $config));
|
||||
}
|
||||
$clients = $this->getPool()->getFromChannel($coroutineName);
|
||||
if (empty($clients)) {
|
||||
return Context::setContext($coroutineName, $this->createClient($coroutineName, $config));
|
||||
}
|
||||
return Context::setContext($coroutineName, $clients);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -88,7 +74,7 @@ class Redis extends Pool
|
||||
$redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']);
|
||||
$redis->setOption(SRedis::OPT_PREFIX, $config['prefix']);
|
||||
|
||||
$this->increment($name);
|
||||
$this->getPool()->increment($name);
|
||||
|
||||
return $redis;
|
||||
}
|
||||
@@ -97,18 +83,18 @@ class Redis extends Pool
|
||||
/**
|
||||
* @param array $config
|
||||
* @param bool $isMaster
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function release(array $config, $isMaster = false)
|
||||
public function release(array $config, bool $isMaster = false)
|
||||
{
|
||||
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
|
||||
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
|
||||
$coroutineName = $this->getPool()->name('Redis:' . $config['host'], $isMaster);
|
||||
if (!Context::hasContext($coroutineName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->push($coroutineName, Context::getContext($coroutineName));
|
||||
$this->getPool()->push($coroutineName, Context::getContext($coroutineName));
|
||||
Context::remove($coroutineName);
|
||||
$this->lastTime = time();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,42 +102,40 @@ class Redis extends Pool
|
||||
* @param bool $isMaster
|
||||
* @throws Exception
|
||||
*/
|
||||
public function destroy(array $config, $isMaster = false)
|
||||
public function destroy(array $config, bool $isMaster = false)
|
||||
{
|
||||
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
|
||||
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
|
||||
$coroutineName = $this->getPool()->name('Redis:' . $config['host'], $isMaster);
|
||||
if (Context::hasContext($coroutineName)) {
|
||||
$this->decrement($coroutineName);
|
||||
$this->getPool()->decrement($coroutineName);
|
||||
}
|
||||
$this->getPool()->clean($coroutineName);
|
||||
Context::remove($coroutineName);
|
||||
$this->flush(0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $client
|
||||
* @return bool
|
||||
* @return ClientsPool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function checkCanUse(string $name, mixed $client): bool
|
||||
public function getPool(): ClientsPool
|
||||
{
|
||||
try {
|
||||
if (!($client instanceof SRedis)) {
|
||||
$result = false;
|
||||
} else {
|
||||
$result = true;
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'redis');
|
||||
$result = false;
|
||||
} finally {
|
||||
if (!$result) {
|
||||
$this->decrement($name);
|
||||
}
|
||||
return $result;
|
||||
if (!$this->clientsPool) {
|
||||
$this->clientsPool = Snowflake::app()->getClientsPool();
|
||||
}
|
||||
return $this->clientsPool;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $isMaster
|
||||
* @param $max
|
||||
* @throws Exception
|
||||
*/
|
||||
public function initConnections($name, $isMaster, $max)
|
||||
{
|
||||
$this->getPool()->initConnections($name, $isMaster, $max);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ abstract class Process extends \Swoole\Process implements SProcess
|
||||
public function __construct($application, $name, $enable_coroutine = true)
|
||||
{
|
||||
parent::__construct([$this, '_load'], false, 1, $enable_coroutine);
|
||||
Snowflake::setProcessId($this->pid);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,11 +36,13 @@ abstract class Process extends \Swoole\Process implements SProcess
|
||||
*/
|
||||
public function _load(Process $process)
|
||||
{
|
||||
Snowflake::setProcessId($this->pid);
|
||||
|
||||
putenv('environmental=' . Snowflake::PROCESS);
|
||||
|
||||
fire(Event::SERVER_WORKER_START);
|
||||
if (Snowflake::getPlatform()->isLinux()) {
|
||||
swoole_set_process_name($this->getProcessName());
|
||||
name($this->pid, $this->getProcessName());
|
||||
}
|
||||
if (method_exists($this, 'before')) {
|
||||
$this->before($process);
|
||||
|
||||
@@ -46,7 +46,7 @@ abstract class BaseValidator
|
||||
* BaseValidator constructor.
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct($config = [])
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->regConfig($config);
|
||||
}
|
||||
|
||||
@@ -37,14 +37,11 @@ class LengthValidator extends BaseValidator
|
||||
if (is_null($value)) {
|
||||
return $this->addError('The param :attribute is null');
|
||||
}
|
||||
switch (strtolower($this->method)) {
|
||||
case self::MAX_LENGTH:
|
||||
return $this->maxLength($value);
|
||||
case self::MIN_LENGTH:
|
||||
return $this->minLength($value);
|
||||
default:
|
||||
return $this->defaultLength($value);
|
||||
}
|
||||
return match (strtolower($this->method)) {
|
||||
self::MAX_LENGTH => $this->maxLength($value),
|
||||
self::MIN_LENGTH => $this->minLength($value),
|
||||
default => $this->defaultLength($value),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace validator;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class RoundValidator
|
||||
* @package validator
|
||||
*/
|
||||
class RoundValidator extends BaseValidator
|
||||
{
|
||||
|
||||
|
||||
public ?int $value = null;
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function trigger(): bool
|
||||
{
|
||||
$value = $this->model->getAttribute($this->field);
|
||||
if ($value == null || round($value, $this->value) != $value) {
|
||||
return $this->addError('The param :attribute length error');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -102,6 +102,9 @@ class Validator extends BaseValidator
|
||||
'class' => 'validator\LengthValidator',
|
||||
'method' => 'default',
|
||||
],
|
||||
'round' => [
|
||||
'class' => 'validator\RoundValidator',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
+34
-2
@@ -17,6 +17,7 @@ use JetBrains\PhpStorm\Pure;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Application;
|
||||
use Snowflake\Core\ArrayAccess;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Error\Logger;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
@@ -375,6 +376,35 @@ if (!function_exists('logger')) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('trim_blank')) {
|
||||
|
||||
|
||||
/**
|
||||
* 空白字符替换
|
||||
* @param string $content 内容
|
||||
* @param int $len 截取长度
|
||||
* @param string $encode 编码
|
||||
* @param bool $htmlTags
|
||||
* @return array|string|null
|
||||
*/
|
||||
function trim_blank(string $content, $len = 0, $encode = 'utf-8', $htmlTags = true): array|string|null
|
||||
{
|
||||
$str = trim($content);
|
||||
if ($htmlTags) {
|
||||
$str = strip_tags($str);
|
||||
}
|
||||
$str = preg_replace('/[\n|\r|\t]+/', '', $str);
|
||||
$str = preg_replace("/(\s|\ \;| |\xc2\xa0)/", '', $str);
|
||||
if ($len > 0) {
|
||||
return mb_substr($str, 0, $len, $encode);
|
||||
} else {
|
||||
return $str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('get_file_extension')) {
|
||||
|
||||
function get_file_extension($filename)
|
||||
@@ -615,12 +645,14 @@ if (!function_exists('send')) {
|
||||
|
||||
/**
|
||||
* @param $context
|
||||
* @param $statusCode
|
||||
* @param int $statusCode
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
function send($context, $statusCode = 404): mixed
|
||||
function send($context, int $statusCode = 404): mixed
|
||||
{
|
||||
if (is_array($context)) $context = Json::encode($context);
|
||||
|
||||
return \response()->send($context, $statusCode);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user