This commit is contained in:
2021-07-21 13:49:55 +08:00
parent 088745109f
commit d7302ceba5
7 changed files with 309 additions and 244 deletions
+2 -1
View File
@@ -8,6 +8,7 @@ namespace PHPSTORM_META {
override(Container::get(0), map('@')); override(Container::get(0), map('@'));
// override(\Hyperf\Utils\Context::get(0), map('@')); // override(\Hyperf\Utils\Context::get(0), map('@'));
// override(\make(0), map('@')); // override(\make(0), map('@'));
// override(\di(0), map('@')); override(\di(0), map('@'));
override(\duplicate(0), map('@'));
} }
+22 -7
View File
@@ -7,6 +7,7 @@ namespace Annotation;
use Exception; use Exception;
use ReflectionException; use ReflectionException;
use ReflectionProperty; use ReflectionProperty;
use Snowflake\Core\Str;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -37,16 +38,12 @@ use Snowflake\Snowflake;
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
$injectValue = $this->parseInjectValue(); $injectValue = $this->parseInjectValue();
if (!($method instanceof ReflectionProperty)) { if (!($method = $this->getProperty($class, $method))) {
$method = Snowflake::getDi()->getClassProperty($class, $method); return false;
if (!$method) {
return false;
}
} }
/** @var ReflectionProperty $class */ /** @var ReflectionProperty $class */
if ($method->isPrivate() || $method->isProtected()) { if ($method->isPrivate() || $method->isProtected()) {
$method = 'set' . ucfirst($class->getName()); $method = 'set' . ucfirst(Str::convertUnderline($method->getName()));
if (!method_exists($class, $method)) { if (!method_exists($class, $method)) {
return false; return false;
} }
@@ -58,6 +55,24 @@ use Snowflake\Snowflake;
} }
/**
* @param $class
* @param $method
* @return ReflectionProperty|bool
*/
private function getProperty($class, $method): ReflectionProperty|bool
{
if ($method instanceof ReflectionProperty) {
return $method;
}
$method = Snowflake::getDi()->getClassProperty($class, $method);
if (!$method) {
return false;
}
return $method;
}
/** /**
* @return mixed * @return mixed
* @throws Exception * @throws Exception
+3 -3
View File
@@ -47,10 +47,10 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
* *
* @param $query * @param $query
* @param array $array * @param array $array
* @param null|string|ActiveRecord $model * @param string|ActiveRecord|null $model
* @throws Exception * @throws Exception
*/ */
public function __construct($query, array $array = [], $model = null) public function __construct($query, array $array = [], string|ActiveRecord $model = null)
{ {
$this->_item = $array; $this->_item = $array;
$this->query = $query; $this->query = $query;
@@ -111,7 +111,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
public function getModel(): ActiveRecord public function getModel(): ActiveRecord
{ {
if (!is_object($this->model)) { if (!is_object($this->model)) {
$this->model = $this->model::populate([]); $this->model = duplicate($this->model);
$this->model->setIsCreate(false); $this->model->setIsCreate(false);
} }
return $this->model; return $this->model;
+10 -8
View File
@@ -24,7 +24,6 @@ use Database\Relation;
use Database\SqlBuilder; use Database\SqlBuilder;
use Database\Traits\HasBase; use Database\Traits\HasBase;
use Exception; use Exception;
use HttpServer\Http\Context;
use ReflectionException; use ReflectionException;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
@@ -141,13 +140,21 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
} }
/**
* @param Relation $relation
*/
public function setRelation(Relation $relation)
{
$this->_relation = $relation;
}
/** /**
* @throws Exception * @throws Exception
*/ */
public function init() public function init()
{ {
$this->container = Snowflake::app(); $this->container = Snowflake::app();
$an = Snowflake::app()->getAnnotation(); $an = Snowflake::app()->getAnnotation();
$an->injectProperty($this); $an->injectProperty($this);
} }
@@ -1010,12 +1017,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
*/ */
public static function populate(array $data): static public static function populate(array $data): static
{ {
// $class = Snowflake::app()->getChannel(); $model = duplicate(static::class);
// /** @var static $model */
// $model = $class->pop(static::class, function () {
// return new static();
// });
$model = new static();
$model->_attributes = $data; $model->_attributes = $data;
$model->_oldAttributes = $data; $model->_oldAttributes = $data;
$model->setIsCreate(false); $model->setIsCreate(false);
+1 -1
View File
@@ -41,7 +41,7 @@ class CollectionIterator extends \ArrayIterator
* @param int $flags * @param int $flags
* @throws Exception * @throws Exception
*/ */
public function __construct($model, $query, $array = array(), $flags = 0) public function __construct($model, $query, array $array = [], int $flags = 0)
{ {
$this->model = $model; $this->model = $model;
$this->query = $query; $this->query = $query;
+235 -223
View File
@@ -14,252 +14,264 @@ use Exception;
class Str class Str
{ {
const STRING = 'abcdefghijklmnopqrstuvwxyz'; const STRING = 'abcdefghijklmnopqrstuvwxyz';
const NUMBER = '01234567890'; const NUMBER = '01234567890';
/** /**
* @param int $length * @param int $length
* *
* @return string * @return string
* 获取随机字符串 * 获取随机字符串
*/ */
public static function rand(int $length = 20): string public static function rand(int $length = 20): string
{ {
$string = ''; $string = '';
if ($length < 1) $length = 20; if ($length < 1) $length = 20;
$default = self::STRING . strtoupper(self::STRING) . self::NUMBER; $default = self::STRING . strtoupper(self::STRING) . self::NUMBER;
$default = str_split($default); $default = str_split($default);
$string .= str_repeat($default[array_rand($default)], $length); $string .= str_repeat($default[array_rand($default)], $length);
return (string)$string; return (string)$string;
} }
/** /**
* @param int $length * @param int $length
* *
* @return int|string 获取随机数字 * @return int|string 获取随机数字
* 获取随机数字 * 获取随机数字
*/ */
public static function random(int $length = 20): int|string public static function random(int $length = 20): int|string
{ {
$number = ''; $number = '';
$default = str_split(self::NUMBER); $default = str_split(self::NUMBER);
if ($length < 1) $length = 1; if ($length < 1) $length = 1;
$number .= str_repeat($default[array_rand($default)], $length); $number .= str_repeat($default[array_rand($default)], $length);
return $number; return $number;
} }
/** /**
* @param $string * @param $string
* @param $sullen * @param $sullen
* @param bool $strip_tags * @param bool $strip_tags
* @param string $append * @param string $append
* *
* @return string * @return string
*/ */
public static function cut_str_utf8($string, $sullen, bool $strip_tags = true, string $append = '...'): string public static function cut_str_utf8($string, $sullen, bool $strip_tags = true, string $append = '...'): string
{ {
if ($strip_tags) { if ($strip_tags) {
$string = strip_tags($string); $string = strip_tags($string);
}//去掉签标 }//去掉签标
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($pa, $string, $t_string); preg_match_all($pa, $string, $t_string);
$str = ""; $str = "";
for ($i = 0; $i < count($t_string[0]); $i++) { for ($i = 0; $i < count($t_string[0]); $i++) {
$str .= $t_string[0][$i]; $str .= $t_string[0][$i];
//转为gbk,一个汉字长度为2 //转为gbk,一个汉字长度为2
if (strlen(@iconv('utf-8', 'gbk', $str)) >= $sullen) { if (strlen(@iconv('utf-8', 'gbk', $str)) >= $sullen) {
if ($i != count($t_string[0]) - 1) $str .= $append; if ($i != count($t_string[0]) - 1) $str .= $append;
break; break;
} }
} }
return $str; return $str;
} }
/** /**
* @param $data * @param $data
* *
* @param null $callback * @param null $callback
* @return bool * @return bool
* 判断是否为json字符串 * 判断是否为json字符串
*/ */
public static function isJson($data, $callback = null): bool public static function isJson($data, $callback = null): bool
{ {
$json = !is_null(json_decode($data)) && !is_numeric($data); $json = !is_null(json_decode($data)) && !is_numeric($data);
if ($json && is_callable($callback, true)) { if ($json && is_callable($callback, true)) {
return call_user_func($callback, $data); return call_user_func($callback, $data);
} }
return $json; return $json;
} }
/** /**
* @param $data * @param $data
* *
* @param null $callBack * @param null $callBack
* @return bool * @return bool
* 判断是否序列化字符串 * 判断是否序列化字符串
*/ */
public static function isSerialize($data, $callBack = null): bool public static function isSerialize($data, $callBack = null): bool
{ {
$false = !empty($data) && swoole_unserialize($data) !== false; $false = !empty($data) && swoole_unserialize($data) !== false;
if ($false && is_callable($callBack, true)) { if ($false && is_callable($callBack, true)) {
return call_user_func($callBack, $data); return call_user_func($callBack, $data);
} }
return $false; return $false;
} }
/** /**
* @param $string * @param $string
* @param int $length * @param int $length
* *
* @param string $append * @param string $append
* @return string * @return string
*/ */
public static function cut($string, int $length = 20, string $append = '...'): string public static function cut($string, int $length = 20, string $append = '...'): string
{ {
if (empty($string)) { if (empty($string)) {
return ''; return '';
} }
if ($length < 1) { if ($length < 1) {
$length = 1; $length = 1;
} }
$array = str_split($string); $array = str_split($string);
if (count($array) <= $length) { if (count($array) <= $length) {
return implode('', $array); return implode('', $array);
} }
$string = implode('', array_slice($array, 0, $length)); $string = implode('', array_slice($array, 0, $length));
if (!empty($append)) { if (!empty($append)) {
$string .= $append; $string .= $append;
} }
return $string; return $string;
} }
/** /**
* @param $str * @param $str
* @param int $number * @param int $number
* @param string $key * @param string $key
* *
* @return string * @return string
*/ */
public static function encrypt($str, int $number = 10, string $key = 'xshucai.com'): string public static function encrypt($str, int $number = 10, string $key = 'xshucai.com'): string
{ {
$res = []; $res = [];
$add = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $add = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$len = strlen($key) < 0 ? 1 : (strlen($key) + 5 > strlen($add) ? strlen($add) - 5 : strlen($key)); $len = strlen($key) < 0 ? 1 : (strlen($key) + 5 > strlen($add) ? strlen($add) - 5 : strlen($key));
if ($number < 1) $number = 10; if ($number < 1) $number = 10;
$array = str_split($str); $array = str_split($str);
asort($array); asort($array);
$str = implode('', $array); $str = implode('', $array);
for ($i = 0; $i < $number; $i++) { for ($i = 0; $i < $number; $i++) {
$_tmp = md5($key) . md5($str) . mb_substr($add, $len, $len + 5, 'utf-8'); $_tmp = md5($key) . md5($str) . mb_substr($add, $len, $len + 5, 'utf-8');
$res[] = md5($_tmp); $res[] = md5($_tmp);
} }
sort($res, SORT_STRING); sort($res, SORT_STRING);
return hash('sha384', implode('', $res)); return hash('sha384', implode('', $res));
} }
/** /**
* @param $file * @param $file
* @param $type * @param $type
* @return string * @return string
*/ */
public static function filename($file, $type): string public static function filename($file, $type): string
{ {
switch ($type) { switch ($type) {
case 'image/png': case 'image/png':
return md5_file($file) . '.png'; return md5_file($file) . '.png';
case 'image/jpeg': case 'image/jpeg':
case 'image/jpg': case 'image/jpg':
return md5_file($file) . '.jpg'; return md5_file($file) . '.jpg';
case 'image/gif': case 'image/gif':
return md5_file($file) . '.gif'; return md5_file($file) . '.gif';
break; break;
} }
return md5_file($file); return md5_file($file);
} }
/** /**
* @param $endTime * @param $endTime
* @param int|null $startTime * @param int|null $startTime
* @return array * @return array
* 剩余天,带分秒 * 剩余天,带分秒
*/ */
public static function timeout($endTime, int $startTime = null): array public static function timeout($endTime, int $startTime = null): array
{ {
$endTime = $endTime - (!empty($startTime) ? $startTime : time()); $endTime = $endTime - (!empty($startTime) ? $startTime : time());
$day = intval($endTime / (3600 * 24)); $day = intval($endTime / (3600 * 24));
$hours = intval(($endTime - ($day * (3600 * 24))) / 3600); $hours = intval(($endTime - ($day * (3600 * 24))) / 3600);
$minute = intval(($endTime - ($day * (3600 * 24) + $hours * 3600)) / 60); $minute = intval(($endTime - ($day * (3600 * 24) + $hours * 3600)) / 60);
$scrod = intval(($endTime - ($day * (3600 * 24) + $hours * 3600 + $minute * 60))); $scrod = intval(($endTime - ($day * (3600 * 24) + $hours * 3600 + $minute * 60)));
return [$day, $hours, $minute, $scrod]; return [$day, $hours, $minute, $scrod];
} }
/** /**
* @return false|int * @return false|int
*/ */
public static function get_sy_time(): bool|int public static function get_sy_time(): bool|int
{ {
$time = strtotime('+1days', strtotime(date('Y-m-d'))); $time = strtotime('+1days', strtotime(date('Y-m-d')));
return $time - time(); return $time - time();
} }
/** /**
* @param string $string * @param string $string
* @return string * @return string
*/ */
public static function encode(string $string): string public static function encode(string $string): string
{ {
return addslashes($string); return addslashes($string);
} }
/** /**
* @param string $string * @param string $string
* @return string|string[]|null * @return string|string[]|null
* 清除标点符号 * 清除标点符号
*/ */
public static function clear(string $string): array|string|null public static function clear(string $string): array|string|null
{ {
$char = '。、!?:;﹑•"…‘’“”〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼()'; $char = '。、!?:;﹑•"…‘’“”〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼()';
return preg_replace(array("/[[:punct:]]/i", '/[' . $char . ']/u', '/[ ]{2,}/'), '', $string); return preg_replace(array("/[[:punct:]]/i", '/[' . $char . ']/u', '/[ ]{2,}/'), '', $string);
} }
/** /**
* @param int $user * @param int $user
* @param array $param * @param array $param
* @param null $requestTime * @param null $requestTime
* *
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public static function token(int $user, array $param = [], $requestTime = NULL): string public static function token(int $user, array $param = [], $requestTime = NULL): string
{ {
$str = ''; $str = '';
if (!$requestTime) { if (!$requestTime) {
$requestTime = microtime(true); $requestTime = microtime(true);
} }
$_user = str_split(md5($user . md5((string)$user))); $_user = str_split(md5($user . md5((string)$user)));
ksort($_user); ksort($_user);
foreach ($_user as $key => $val) { foreach ($_user as $key => $val) {
$str .= md5(sha1($key . $val . 'www.xshucai.com')); $str .= md5(sha1($key . $val . 'www.xshucai.com'));
} }
if (is_array($param)) { if (is_array($param)) {
foreach ($param as $key => $val) { foreach ($param as $key => $val) {
$str .= md5($str . sha1($key . md5($val))); $str .= md5($str . sha1($key . md5($val)));
} }
} }
$str .= sha1(base64_encode((string)$requestTime)); $str .= sha1(base64_encode((string)$requestTime));
$md5 = md5($str . $user); $md5 = md5($str . $user);
return preg_replace('/(\w{10})(\w{3})(\w{4})(\w{9})(\w{6})/', '$1-$2-$3-$4-$5', $md5); return preg_replace('/(\w{10})(\w{3})(\w{4})(\w{9})(\w{6})/', '$1-$2-$3-$4-$5', $md5);
} }
/**
* @param string $str
* @param bool $unfairest
* @return string
*/
public static function convertUnderline(string $str, bool $unfairest = true): string
{
$str = ucwords(str_replace('_', ' ', $str));
$str = str_replace(' ', '', lcfirst($str));
return $unfairest ? ucfirst($str) : $str;
}
} }
+36 -1
View File
@@ -21,6 +21,7 @@ use Snowflake\Core\Json;
use Snowflake\Error\Logger; use Snowflake\Error\Logger;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\WebSocket\Server; use Swoole\WebSocket\Server;
@@ -681,13 +682,47 @@ if (!function_exists('env')) {
} }
if (!function_exists('di')) {
/**
* @param string $className
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
*/
function di(string $className): mixed
{
return Snowflake::getDi()->get($className);
}
}
if (!function_exists('duplicate')) {
/**
* @param string $className
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
*/
function duplicate(string $className): mixed
{
$class = di($className);
return clone $class;
}
}
if (!function_exists('sweep')) { if (!function_exists('sweep')) {
/** /**
* @param string $configPath * @param string $configPath
* @return array|false|string|null * @return array|false|string|null
*/ */
function sweep($configPath = APP_PATH . 'config'): bool|array|string|null function sweep(string $configPath = APP_PATH . 'config'): bool|array|string|null
{ {
$array = []; $array = [];
foreach (glob($configPath . '/*') as $config) { foreach (glob($configPath . '/*') as $config) {