BiliHelper-personal/plugin/CheckUpdate/CheckUpdate.php
2022-06-03 23:34:44 +08:00

140 lines
3.7 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php declare(strict_types=1);
/**
* Website: https://mudew.com/
* Author: Lkeme
* License: The MIT License
* Email: Useri@live.cn
* Updated: 2022 ~ 2023
*
* _____ _ _ _ _ _ _____ _ _____ _____ _____
* | _ \ | | | | | | | | | | | ____| | | | _ \ | ____| | _ \ & l、
* | |_| | | | | | | | | |_| | | |__ | | | |_| | | |__ | |_| | (゚、 。
* | _ { | | | | | | | _ | | __| | | | ___/ | __| | _ /   \、゙ ~ヽ *
* | |_| | | | | |___ | | | | | | | |___ | |___ | | | |___ | | \ \  じしf_, )
* |_____/ |_| |_____| |_| |_| |_| |_____| |_____| |_| |_____| |_| \_\
*/
use Bhp\Cache\Cache;
use Bhp\Log\Log;
use Bhp\Notice\Notice;
use Bhp\Plugin\BasePluginRW;
use Bhp\Plugin\Plugin;
use Bhp\Request\Request;
use Bhp\TimeLock\TimeLock;
use Bhp\Util\Resource\Resource;
class CheckUpdate extends BasePluginRW
{
/**
* 插件信息
* @var array|string[]
*/
protected ?array $info = [
'hook' => __CLASS__, // hook
'name' => 'CheckUpdate', // 插件名称
'version' => '0.0.1', // 插件版本
'desc' => '检查版本更新', // 插件描述
'author' => 'Lkeme',// 作者
'priority' => 1000, // 插件优先级
'cycle' => '24(小时)', // 运行周期
];
/**
* @param Plugin $plugin
*/
public function __construct(Plugin &$plugin)
{
//
TimeLock::initTimeLock();
//
Cache::initCache();
//
Log::info('加载CheckUpdate插件');
// $this::class
$plugin->register($this, 'execute');
}
/**
* @use 执行
* @return void
*/
public function execute(): void
{
if (TimeLock::getTimes() > time()) return;
//
$this->_checkUpdate();
//
TimeLock::setTimes(24 * 60 * 60);
}
/**
* @return void
*/
protected function _checkUpdate(): void
{
//
Log::info('开始检查项目更新');
// resource object
$offline = $this->fetchOfflineVersion();
//
Log::info('拉取线上最新配置');
// object
$online = $this->fetchOnlineVersion();
// 比较版本
if ($this->compareVersion($offline->get('version'), $online->version)) {
// TODO 完善消息 支持markdown
$time = $online->time;
$version = $online->version;
$des = $online->des;
$info = "请注意版本变动更新哦~\n\n版本号: $version\n\n更新日志: $des\n\n更新时间: $time\n\n";
Log::notice($info);
Notice::push('update', $info);
} else {
Log::info('程序已是最新版本');
}
}
/**
* @use 拉取本地版本
* @return void
*/
protected function fetchOfflineVersion(): Resource
{
$this->loadResource('version.json', 'json');
return $this->resource;
}
/**
* @use 拉取线上版本
* @return object
*/
protected function fetchOnlineVersion(): object
{
$url = $this->resource->get('raw_url');
$payload = [];
return Request::getJson(false, 'other', $url, $payload);
}
/**
* @use 比较版本号
* @param string $off
* @param string $on
* @return bool
*/
protected static function compareVersion(string $off, string $on): bool
{
// true 有更新 false 无更新
return !($off == $on);
}
/**
* @use 重写系统路径
* @param string $filename
* @return string
*/
protected function getFilePath(string $filename): string
{
return str_replace("\\", "/", APP_RESOURCES_PATH . $filename);
}
}