first commit
This commit is contained in:
144
Xboard/app/Traits/HasPluginConfig.php
Normal file
144
Xboard/app/Traits/HasPluginConfig.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Plugin;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
trait HasPluginConfig
|
||||
{
|
||||
/**
|
||||
* 缓存的插件配置
|
||||
*/
|
||||
protected ?array $pluginConfig = null;
|
||||
|
||||
/**
|
||||
* 插件代码
|
||||
*/
|
||||
protected ?string $pluginCode = null;
|
||||
|
||||
/**
|
||||
* 插件启用状态(仅当前对象生命周期内缓存)
|
||||
*/
|
||||
protected ?bool $pluginEnabled = null;
|
||||
|
||||
/**
|
||||
* 获取插件配置
|
||||
*/
|
||||
public function getConfig(?string $key = null, $default = null): mixed
|
||||
{
|
||||
$config = $this->getPluginConfig();
|
||||
|
||||
if ($key) {
|
||||
return $config[$key] ?? $default;
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整的插件配置
|
||||
*/
|
||||
protected function getPluginConfig(): array
|
||||
{
|
||||
if ($this->pluginConfig === null) {
|
||||
$pluginCode = $this->getPluginCode();
|
||||
|
||||
\Log::channel('daily')->info('Telegram Login: 获取插件配置', [
|
||||
'plugin_code' => $pluginCode
|
||||
]);
|
||||
|
||||
$this->pluginConfig = Cache::remember(
|
||||
"plugin_config_{$pluginCode}",
|
||||
3600,
|
||||
function () use ($pluginCode) {
|
||||
$plugin = Plugin::where('code', $pluginCode)
|
||||
->where('is_enabled', true)
|
||||
->first();
|
||||
|
||||
if (!$plugin || !$plugin->config) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return json_decode($plugin->config, true) ?? [];
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return $this->pluginConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件代码
|
||||
*/
|
||||
public function getPluginCode(): string
|
||||
{
|
||||
if ($this->pluginCode === null) {
|
||||
$this->pluginCode = $this->autoDetectPluginCode();
|
||||
}
|
||||
|
||||
return $this->pluginCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置插件代码(如果自动检测不准确可以手动设置)
|
||||
*/
|
||||
public function setPluginCode(string $pluginCode): void
|
||||
{
|
||||
$this->pluginCode = $pluginCode;
|
||||
$this->pluginConfig = null; // 重置配置缓存
|
||||
$this->pluginEnabled = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动检测插件代码
|
||||
*/
|
||||
protected function autoDetectPluginCode(): string
|
||||
{
|
||||
$reflection = new \ReflectionClass($this);
|
||||
$namespace = $reflection->getNamespaceName();
|
||||
|
||||
// 从命名空间提取插件代码
|
||||
// 例如: Plugin\TelegramLogin\Controllers => telegram_login
|
||||
if (preg_match('/^Plugin\\\\(.+?)\\\\/', $namespace, $matches)) {
|
||||
return $this->convertToKebabCase($matches[1]);
|
||||
}
|
||||
|
||||
throw new \RuntimeException('Unable to detect plugin code from namespace: ' . $namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 StudlyCase 转换为 kebab-case
|
||||
*/
|
||||
protected function convertToKebabCase(string $string): string
|
||||
{
|
||||
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $string));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查插件是否启用
|
||||
*/
|
||||
public function isPluginEnabled(): bool
|
||||
{
|
||||
if ($this->pluginEnabled !== null) {
|
||||
return $this->pluginEnabled;
|
||||
}
|
||||
|
||||
$pluginCode = $this->getPluginCode();
|
||||
$isEnabled = Plugin::where('code', $pluginCode)->value('is_enabled');
|
||||
$this->pluginEnabled = (bool) $isEnabled;
|
||||
|
||||
return $this->pluginEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除插件配置缓存
|
||||
*/
|
||||
public function clearConfigCache(): void
|
||||
{
|
||||
$pluginCode = $this->getPluginCode();
|
||||
Cache::forget("plugin_config_{$pluginCode}");
|
||||
$this->pluginConfig = null;
|
||||
$this->pluginEnabled = null;
|
||||
}
|
||||
}
|
||||
68
Xboard/app/Traits/QueryOperators.php
Normal file
68
Xboard/app/Traits/QueryOperators.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Contracts\Database\Query\Expression;
|
||||
|
||||
trait QueryOperators
|
||||
{
|
||||
/**
|
||||
* 获取查询运算符映射
|
||||
*
|
||||
* @param string $operator
|
||||
* @return string
|
||||
*/
|
||||
protected function getQueryOperator(string $operator): string
|
||||
{
|
||||
return match (strtolower($operator)) {
|
||||
'eq' => '=',
|
||||
'gt' => '>',
|
||||
'gte' => '>=',
|
||||
'lt' => '<',
|
||||
'lte' => '<=',
|
||||
'like' => 'like',
|
||||
'notlike' => 'not like',
|
||||
'null' => 'null',
|
||||
'notnull' => 'notnull',
|
||||
default => 'like'
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询值格式化
|
||||
*
|
||||
* @param string $operator
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
protected function formatQueryValue(string $operator, mixed $value): mixed
|
||||
{
|
||||
return match (strtolower($operator)) {
|
||||
'like', 'notlike' => "%{$value}%",
|
||||
'null', 'notnull' => null,
|
||||
default => $value
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用查询条件
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $field
|
||||
* @param string $operator
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
protected function applyQueryCondition($query, array|Expression|string $field, string $operator, mixed $value): void
|
||||
{
|
||||
$queryOperator = $this->getQueryOperator($operator);
|
||||
|
||||
if ($queryOperator === 'null') {
|
||||
$query->whereNull($field);
|
||||
} elseif ($queryOperator === 'notnull') {
|
||||
$query->whereNotNull($field);
|
||||
} else {
|
||||
$query->where($field, $queryOperator, $this->formatQueryValue($operator, $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user