258 lines
7.1 KiB
PHP
258 lines
7.1 KiB
PHP
|
<?php
|
|||
|
|
|||
|
use libs\core\Config;
|
|||
|
use libs\core\Container;
|
|||
|
use libs\db\Db;
|
|||
|
use Workerman\Worker;
|
|||
|
|
|||
|
define('BASE_PATH', dirname(__DIR__));
|
|||
|
|
|||
|
/**
|
|||
|
* * envs环境参数获取
|
|||
|
* * @param $key
|
|||
|
* * @param null $default
|
|||
|
* * @return mixed|null
|
|||
|
* */
|
|||
|
function envs($key, $default = null)
|
|||
|
{
|
|||
|
|
|||
|
static $env_config = [];
|
|||
|
$env = getenv('APP_ENV');
|
|||
|
$file_path = "";
|
|||
|
|
|||
|
if ($env === 'DEV') {
|
|||
|
$file_path = config_path() . '/.env.development.php';
|
|||
|
} else if ($env) {
|
|||
|
$file_path = config_path() . "/.env.$env.php";
|
|||
|
} else {
|
|||
|
$file_path = config_path() . "/.env.php";
|
|||
|
}
|
|||
|
if (!file_exists($file_path)) {
|
|||
|
print_r("不存在env文件:$file_path");
|
|||
|
exit;
|
|||
|
}
|
|||
|
$env_config = include $file_path;
|
|||
|
return $env_config[$key] ?? $default;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* if the param $path equal false,will return this program current execute directory
|
|||
|
* @param string|false $path
|
|||
|
* @return string
|
|||
|
*/
|
|||
|
function base_path($path = ''): string
|
|||
|
{
|
|||
|
if (false === $path) {
|
|||
|
return run_path();
|
|||
|
}
|
|||
|
return path_combine(BASE_PATH, $path);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Config path
|
|||
|
* @param string $path
|
|||
|
* @return string
|
|||
|
*/
|
|||
|
function config_path(string $path = ''): string
|
|||
|
{
|
|||
|
return path_combine(BASE_PATH . DIRECTORY_SEPARATOR . 'config', $path);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* app path
|
|||
|
* @param string $path
|
|||
|
* @return string
|
|||
|
*/
|
|||
|
function app_path(string $path = ''): string
|
|||
|
{
|
|||
|
return path_combine(BASE_PATH . DIRECTORY_SEPARATOR . 'app', $path);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Get config
|
|||
|
* @param string|null $key
|
|||
|
* @param $default
|
|||
|
* @return array|mixed|null
|
|||
|
*/
|
|||
|
function config(string $key = null, $default = null)
|
|||
|
{
|
|||
|
return Config::get($key, $default);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Generate paths based on given information
|
|||
|
* @param string $front
|
|||
|
* @param string $back
|
|||
|
* @return string
|
|||
|
*/
|
|||
|
function path_combine(string $front, string $back): string
|
|||
|
{
|
|||
|
return $front . ($back ? (DIRECTORY_SEPARATOR . ltrim($back, DIRECTORY_SEPARATOR)) : $back);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @param $action
|
|||
|
* @param int $errcode
|
|||
|
* @param string $errmsg
|
|||
|
* @throws Exception
|
|||
|
*/
|
|||
|
function error($action, $errcode = 4001, $errmsg = '')
|
|||
|
{
|
|||
|
throw new \Exception( $errmsg , $errcode);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 查看可选筛选字段是否满足
|
|||
|
* @param array $rule 验证规则,['tray', 'string', '托盘号'] 位置1:验证的字段,位置2:验证字段类型,位置3:提示信息
|
|||
|
* @param array $param 原数据
|
|||
|
* @return array
|
|||
|
*/
|
|||
|
function check_valid($action, array $rule, array $param)
|
|||
|
{
|
|||
|
if (!is_array($rule)) {
|
|||
|
error($action, 4001, '数据验证失败');
|
|||
|
}
|
|||
|
|
|||
|
$not_param = []; // 定义没有传入的值
|
|||
|
|
|||
|
foreach ($rule as $item) {
|
|||
|
// 检测数据格式
|
|||
|
if (isset($item[1]) && !empty($item[1])) {
|
|||
|
//判断是否有传入该字段
|
|||
|
if (!isset($param[$item[0]])) {
|
|||
|
array_push($not_param, $item[0]);
|
|||
|
continue;
|
|||
|
}
|
|||
|
// 判断是否有提示参数传入
|
|||
|
$tip = '';
|
|||
|
if (isset($item[2]) && !empty($item[2])) {
|
|||
|
$tip = $item[2];
|
|||
|
}
|
|||
|
switch ($item[1]) {
|
|||
|
case 'float': //如果要判断数据是否为float
|
|||
|
if ((string)floatval($param[$item[0]]) <> $param[$item[0]]) {
|
|||
|
error($action, 4001, "[{$item[0]}]{$tip}参数类型不符合,系统需求为浮点型[float]类型");
|
|||
|
}
|
|||
|
break;
|
|||
|
case 'string': //如果要判断数据是否为字符串
|
|||
|
if (empty($param[$item[0]])) {
|
|||
|
error($action, 4001, "[{$item[0]}]{$tip}参数不为空");
|
|||
|
}
|
|||
|
if (!is_string($param[$item[0]]) || strlen($param[$item[0]]) == 0) {
|
|||
|
error($action, 4001, "[{$item[0]}]{$tip}参数类型不符合,系统需求为字符串[string]类型");
|
|||
|
}
|
|||
|
break;
|
|||
|
case 'array': //如果要判断数据是否为数组
|
|||
|
if (!is_array($param[$item[0]])) {
|
|||
|
error($action, 4001, "[{$item[0]}]{$tip}参数类型不符合,系统需求为数组[array]类型");
|
|||
|
}
|
|||
|
if (count($param[$item[0]]) == 0) {
|
|||
|
error($action, 4001, "[{$item[0]}]{$tip}参数不为空");
|
|||
|
}
|
|||
|
break;
|
|||
|
case 'int': //如果要判断数据是否为整数
|
|||
|
if (!is_int($param[$item[0]])) {
|
|||
|
error($action, 4001, "[{$item[0]}]{$tip}参数类型不符合,系统需求为整数[int]类型");
|
|||
|
}
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
if (isset($param[$item[0]]) && !empty($param[$item[0]])) {
|
|||
|
if (!is_array($param[$item[0]])) { //如果是数组,跳过trim
|
|||
|
$param[$item[0]] = trim($param[$item[0]]);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//判断是否有未传入字段
|
|||
|
if (!empty($not_param)) {
|
|||
|
error($action, 4001, '请上传:' . implode(',', $not_param) . '参数');
|
|||
|
}
|
|||
|
|
|||
|
unset($rule);
|
|||
|
|
|||
|
return $param;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Start worker
|
|||
|
* @param $process_name
|
|||
|
* @param $config
|
|||
|
* @return void
|
|||
|
*/
|
|||
|
function worker_start($process_name, $config)
|
|||
|
{
|
|||
|
$worker = new Worker($config['listen'] ?? null, $config['context'] ?? []);
|
|||
|
$property_map = [
|
|||
|
'count',
|
|||
|
'user',
|
|||
|
'group',
|
|||
|
'reloadable',
|
|||
|
'reusePort',
|
|||
|
'transport',
|
|||
|
'protocol',
|
|||
|
];
|
|||
|
$worker->name = $process_name;
|
|||
|
foreach ($property_map as $property) {
|
|||
|
if (isset($config[$property])) {
|
|||
|
$worker->$property = $config[$property];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
$worker->onWorkerStart = function ($worker) use ($config) {
|
|||
|
// 加载数据库实例
|
|||
|
Db::instance();
|
|||
|
|
|||
|
if (isset($config['handler'])) {
|
|||
|
if (!\class_exists($config['handler'])) {
|
|||
|
echo "process error: class {$config['handler']} not exists\r\n";
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
$instance = Container::make($config['handler'], $config['constructor'] ?? []);
|
|||
|
\worker_bind($worker, $instance);
|
|||
|
}
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Bind worker
|
|||
|
* @param $worker
|
|||
|
* @param $class
|
|||
|
*/
|
|||
|
function worker_bind($worker, $class)
|
|||
|
{
|
|||
|
$callback_map = [
|
|||
|
'onConnect',
|
|||
|
'onMessage',
|
|||
|
'onClose',
|
|||
|
'onError',
|
|||
|
'onBufferFull',
|
|||
|
'onBufferDrain',
|
|||
|
'onWorkerStop',
|
|||
|
'onWebSocketConnect'
|
|||
|
];
|
|||
|
foreach ($callback_map as $name) {
|
|||
|
if (\method_exists($class, $name)) {
|
|||
|
$worker->$name = [$class, $name];
|
|||
|
}
|
|||
|
}
|
|||
|
if (\method_exists($class, 'onWorkerStart')) {
|
|||
|
\call_user_func([$class, 'onWorkerStart'], $worker);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
function record_sql_log($msg,$sql){
|
|||
|
$time = date('Y-m-d H:i:s',time());
|
|||
|
$dir = '/home/mes/log/workerman/' . 'sql-' . date('Y') . '-' . date('m') . '-' . date('d') . '.log';
|
|||
|
$dir_name = dirname($dir);
|
|||
|
if(!file_exists($dir_name)){
|
|||
|
$res = mkdir(iconv("UTF-8", "GBK", $dir_name),0777,true);
|
|||
|
}
|
|||
|
$fp = fopen($dir,"a");
|
|||
|
$data = "[{$time}] sql.DEBUG:{$sql} {messge:{$msg}}";
|
|||
|
//fwrite($fp,var_export($data,true)."\r\n");//写入文件
|
|||
|
fwrite($fp,$data."\r\n");//写入文件
|
|||
|
fclose($fp);
|
|||
|
}
|