2022-08-11 07:28:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ba\module;
|
|
|
|
|
|
|
|
use ba\Depend;
|
|
|
|
use think\Exception;
|
|
|
|
use think\facade\Config;
|
|
|
|
use FilesystemIterator;
|
|
|
|
use RecursiveDirectoryIterator;
|
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 模块管理类
|
|
|
|
*/
|
|
|
|
class Manage
|
|
|
|
{
|
2022-08-15 17:04:30 +00:00
|
|
|
public const UNINSTALLED = 0;
|
|
|
|
public const INSTALLED = 1;
|
|
|
|
public const WAIT_INSTALL = 2;
|
|
|
|
public const CONFLICT_PENDING = 3;
|
|
|
|
public const DEPENDENT_WAIT_INSTALL = 4;
|
|
|
|
public const DIRECTORY_OCCUPIED = 5;
|
2022-08-29 03:15:08 +00:00
|
|
|
public const DISABLE = 6;
|
2022-08-15 17:04:30 +00:00
|
|
|
|
2022-08-11 07:28:16 +00:00
|
|
|
/**
|
|
|
|
* @var Manage 对象实例
|
|
|
|
*/
|
|
|
|
protected static $instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string 安装目录
|
|
|
|
*/
|
|
|
|
protected $installDir = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string 备份目录
|
|
|
|
*/
|
|
|
|
protected $ebakDir = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string 模板唯一标识
|
|
|
|
*/
|
|
|
|
protected $uid = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string 模板根目录
|
|
|
|
*/
|
2022-08-15 17:18:58 +00:00
|
|
|
protected $modulesDir = null;
|
2022-08-11 07:28:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 初始化
|
|
|
|
* @access public
|
|
|
|
* @param string $uid
|
|
|
|
* @return Manage
|
|
|
|
*/
|
2022-09-11 06:59:34 +00:00
|
|
|
public static function instance(string $uid = ''): Manage
|
2022-08-11 07:28:16 +00:00
|
|
|
{
|
|
|
|
if (is_null(self::$instance)) {
|
|
|
|
self::$instance = new static($uid);
|
|
|
|
}
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct(string $uid)
|
|
|
|
{
|
2022-08-15 17:18:58 +00:00
|
|
|
$this->installDir = root_path() . 'modules' . DIRECTORY_SEPARATOR;
|
|
|
|
$this->ebakDir = $this->installDir . 'ebak' . DIRECTORY_SEPARATOR;
|
2022-08-11 07:28:16 +00:00
|
|
|
if (!is_dir($this->installDir)) {
|
|
|
|
mkdir($this->installDir, 0755, true);
|
|
|
|
}
|
|
|
|
if (!is_dir($this->ebakDir)) {
|
|
|
|
mkdir($this->ebakDir, 0755, true);
|
|
|
|
}
|
2022-09-11 06:59:34 +00:00
|
|
|
|
|
|
|
if ($uid) {
|
|
|
|
$this->uid = $uid;
|
|
|
|
$this->modulesDir = $this->installDir . $uid . DIRECTORY_SEPARATOR;
|
|
|
|
}
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 02:22:51 +00:00
|
|
|
public function getInstallState()
|
2022-08-14 12:29:06 +00:00
|
|
|
{
|
2022-08-15 17:18:58 +00:00
|
|
|
if (!is_dir($this->modulesDir)) {
|
2022-08-15 17:04:30 +00:00
|
|
|
return self::UNINSTALLED;
|
2022-08-14 12:29:06 +00:00
|
|
|
}
|
|
|
|
$info = $this->getInfo();
|
|
|
|
if ($info && isset($info['state'])) {
|
|
|
|
return $info['state'];
|
|
|
|
}
|
2022-08-15 04:02:19 +00:00
|
|
|
|
|
|
|
// 目录已存在,但非正常的模块
|
2022-08-15 17:18:58 +00:00
|
|
|
return dir_is_empty($this->modulesDir) ? self::UNINSTALLED : self::DIRECTORY_OCCUPIED;
|
2022-08-14 12:29:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 02:22:51 +00:00
|
|
|
public function download(string $token, int $orderId)
|
2022-09-05 09:21:16 +00:00
|
|
|
{
|
|
|
|
if (!$orderId) {
|
|
|
|
throw new Exception('Order not found');
|
|
|
|
}
|
|
|
|
// 下载
|
|
|
|
$sysVersion = Config::get('buildadmin.version');
|
|
|
|
$zipFile = Server::download($this->uid, $this->installDir, [
|
|
|
|
'sysVersion' => $sysVersion,
|
|
|
|
'ba-user-token' => $token,
|
|
|
|
'order_id' => $orderId,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// 解压
|
|
|
|
Server::unzip($zipFile);
|
|
|
|
|
|
|
|
// 删除下载的zip
|
|
|
|
@unlink($zipFile);
|
|
|
|
|
2022-09-06 02:22:51 +00:00
|
|
|
// 检查是否完整
|
|
|
|
$this->checkPackage();
|
|
|
|
|
2022-09-05 09:21:16 +00:00
|
|
|
// 设置为待安装状态
|
|
|
|
$this->setInfo([
|
|
|
|
'state' => self::WAIT_INSTALL,
|
|
|
|
]);
|
2022-09-06 02:22:51 +00:00
|
|
|
|
|
|
|
return $zipFile;
|
|
|
|
}
|
|
|
|
|
2022-09-11 06:59:34 +00:00
|
|
|
public function upload(string $file)
|
|
|
|
{
|
|
|
|
$file = path_transform(root_path() . 'public' . $file);
|
|
|
|
if (!is_file($file)) {
|
|
|
|
throw new Exception('Zip file not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
$copyTo = $this->installDir . 'uploadTemp' . date('YmdHis') . '.zip';
|
|
|
|
copy($file, $copyTo);
|
|
|
|
|
|
|
|
// 解压
|
|
|
|
$copyToDir = Server::unzip($copyTo);
|
|
|
|
$copyToDir .= DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
// 删除zip
|
|
|
|
@unlink($copyTo);
|
|
|
|
|
|
|
|
// 读取ini
|
|
|
|
$info = Server::getIni($copyToDir);
|
|
|
|
if (!isset($info['uid']) || !$info['uid']) {
|
|
|
|
throw new Exception('Basic configuration of the Module is incomplete');
|
|
|
|
}
|
|
|
|
$this->uid = $info['uid'];
|
|
|
|
$this->modulesDir = $this->installDir . $info['uid'] . DIRECTORY_SEPARATOR;
|
|
|
|
rename($copyToDir, $this->modulesDir);
|
|
|
|
|
|
|
|
// 检查是否完整
|
|
|
|
$this->checkPackage();
|
|
|
|
|
|
|
|
// 设置为待安装状态
|
|
|
|
$this->setInfo([
|
|
|
|
'state' => self::WAIT_INSTALL,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
2022-09-06 02:22:51 +00:00
|
|
|
public function update(string $token, int $orderId)
|
|
|
|
{
|
|
|
|
$state = $this->getInstallState();
|
|
|
|
if ($state != self::DISABLE) {
|
|
|
|
throw new Exception('Please disable the module before updating');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->download($token, $orderId);
|
2022-09-10 16:20:10 +00:00
|
|
|
|
|
|
|
// 执行更新脚本
|
|
|
|
Server::execEvent($this->uid, 'update');
|
2022-09-05 09:21:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 07:28:16 +00:00
|
|
|
/**
|
|
|
|
* 安装模板或案例
|
|
|
|
* @param string $token 用户token
|
|
|
|
* @param int $orderId 订单号
|
|
|
|
* @throws moduleException
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function install(string $token, int $orderId)
|
|
|
|
{
|
2022-09-06 02:22:51 +00:00
|
|
|
$state = $this->getInstallState();
|
2022-08-29 03:15:08 +00:00
|
|
|
if ($state == self::INSTALLED || $state == self::DIRECTORY_OCCUPIED || $state == self::DISABLE) {
|
2022-08-15 17:18:58 +00:00
|
|
|
throw new Exception('Module already exists');
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:04:30 +00:00
|
|
|
if ($state == self::UNINSTALLED) {
|
2022-09-06 02:22:51 +00:00
|
|
|
$this->download($token, $orderId);
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 导入sql
|
2022-08-15 17:18:58 +00:00
|
|
|
Server::importSql($this->modulesDir);
|
2022-08-11 07:28:16 +00:00
|
|
|
|
2022-09-10 16:20:10 +00:00
|
|
|
// 执行安装脚本
|
|
|
|
Server::execEvent($this->uid, 'install');
|
|
|
|
|
2022-08-11 07:28:16 +00:00
|
|
|
// 启用插件
|
2022-08-27 06:12:52 +00:00
|
|
|
$this->enable('install');
|
2022-08-29 17:12:42 +00:00
|
|
|
|
|
|
|
return $this->getInfo();
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 02:07:19 +00:00
|
|
|
public function uninstall()
|
|
|
|
{
|
|
|
|
$info = $this->getInfo();
|
|
|
|
if ($info['state'] != self::DISABLE) {
|
|
|
|
throw new moduleException('Please disable the module first', 0, [
|
|
|
|
'uid' => $this->uid,
|
|
|
|
]);
|
|
|
|
}
|
2022-09-10 16:20:10 +00:00
|
|
|
|
|
|
|
// 执行卸载脚本
|
|
|
|
Server::execEvent($this->uid, 'uninstall');
|
|
|
|
|
2022-09-05 02:07:19 +00:00
|
|
|
deldir($this->modulesDir);
|
|
|
|
}
|
|
|
|
|
2022-09-06 02:22:51 +00:00
|
|
|
/**
|
|
|
|
* 启禁用模块
|
|
|
|
*/
|
2022-09-02 07:59:06 +00:00
|
|
|
public function changeState(bool $state)
|
|
|
|
{
|
|
|
|
$info = $this->getInfo();
|
|
|
|
$canDisable = [
|
|
|
|
self::INSTALLED,
|
|
|
|
self::CONFLICT_PENDING,
|
|
|
|
self::DEPENDENT_WAIT_INSTALL,
|
|
|
|
];
|
|
|
|
if (!$state) {
|
|
|
|
if (!in_array($info['state'], $canDisable)) {
|
|
|
|
throw new moduleException('The current state of the module cannot be set to disabled', 0, [
|
|
|
|
'uid' => $this->uid,
|
|
|
|
'state' => $info['state'],
|
|
|
|
]);
|
|
|
|
}
|
2022-09-11 15:19:08 +00:00
|
|
|
return $this->disable();
|
2022-09-02 07:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($info['state'] != self::DISABLE) {
|
|
|
|
throw new moduleException('The current state of the module cannot be set to enabled', 0, [
|
|
|
|
'uid' => $this->uid,
|
|
|
|
'state' => $info['state'],
|
|
|
|
]);
|
|
|
|
}
|
2022-09-04 08:07:51 +00:00
|
|
|
$this->setInfo([
|
|
|
|
'state' => self::WAIT_INSTALL,
|
|
|
|
]);
|
2022-09-11 15:19:08 +00:00
|
|
|
return $info;
|
2022-09-02 07:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 06:12:52 +00:00
|
|
|
public function enable(string $trigger)
|
2022-08-11 07:28:16 +00:00
|
|
|
{
|
2022-08-29 19:01:25 +00:00
|
|
|
$this->conflictHandle($trigger);
|
|
|
|
$this->dependUpdateHandle();
|
2022-08-11 07:28:16 +00:00
|
|
|
|
|
|
|
// 执行启用脚本
|
|
|
|
Server::execEvent($this->uid, 'enable');
|
2022-08-17 08:12:32 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 07:59:06 +00:00
|
|
|
public function disable()
|
|
|
|
{
|
2022-09-05 09:21:16 +00:00
|
|
|
$upadte = request()->get("upadte/b", false);
|
|
|
|
$confirmConflict = request()->get("confirmConflict/b", false);
|
|
|
|
|
2022-09-04 07:43:59 +00:00
|
|
|
$info = $this->getInfo();
|
2022-09-02 19:55:06 +00:00
|
|
|
$zipFile = $this->ebakDir . $this->uid . '-install.zip';
|
2022-09-06 02:22:51 +00:00
|
|
|
$zipDir = false;
|
|
|
|
if (is_file($zipFile)) {
|
|
|
|
try {
|
|
|
|
$zipDir = Server::unzip($zipFile);
|
|
|
|
} catch (moduleException|Exception $e) {
|
|
|
|
// skip
|
|
|
|
}
|
2022-09-02 21:28:09 +00:00
|
|
|
}
|
2022-09-02 19:55:06 +00:00
|
|
|
|
2022-09-02 12:32:17 +00:00
|
|
|
$conflictFile = Server::getFileList($this->modulesDir, true);
|
2022-09-02 19:55:06 +00:00
|
|
|
$disableDependConflict = $this->disableDependConflictCheck($zipDir);
|
2022-09-02 12:32:17 +00:00
|
|
|
if (($disableDependConflict || $conflictFile) && !$confirmConflict) {
|
|
|
|
throw new moduleException('Module file updated', -1, [
|
|
|
|
'uid' => $this->uid,
|
|
|
|
'conflictFile' => $conflictFile,
|
|
|
|
'dependConflict' => (bool)$disableDependConflict,
|
|
|
|
]);
|
|
|
|
}
|
2022-09-02 19:55:06 +00:00
|
|
|
|
|
|
|
// 对冲突进行备份
|
2022-09-04 07:43:59 +00:00
|
|
|
$dependWaitInstall = [];
|
2022-09-02 19:55:06 +00:00
|
|
|
if (in_array('composer', $disableDependConflict)) {
|
2022-09-04 07:43:59 +00:00
|
|
|
$conflictFile[] = 'composer.json';
|
|
|
|
$dependWaitInstall[] = [
|
|
|
|
'pm' => false,
|
|
|
|
'command' => 'composer.update',
|
2022-09-09 08:16:13 +00:00
|
|
|
'type' => 'composer_dependent_wait_install',
|
2022-09-04 07:43:59 +00:00
|
|
|
];
|
2022-09-02 19:55:06 +00:00
|
|
|
}
|
|
|
|
if (in_array('npm', $disableDependConflict)) {
|
2022-09-04 07:43:59 +00:00
|
|
|
$conflictFile[] = 'web' . DIRECTORY_SEPARATOR . 'package.json';
|
|
|
|
$dependWaitInstall[] = [
|
|
|
|
'pm' => true,
|
|
|
|
'command' => 'web-install',
|
2022-09-09 08:16:13 +00:00
|
|
|
'type' => 'npm_dependent_wait_install',
|
2022-09-04 07:43:59 +00:00
|
|
|
];
|
2022-09-02 19:55:06 +00:00
|
|
|
}
|
|
|
|
if ($conflictFile) {
|
|
|
|
$ebakZip = $this->ebakDir . $this->uid . '-disable-' . date('YmdHis') . '.zip';
|
|
|
|
Server::createZip($conflictFile, $ebakZip);
|
|
|
|
}
|
|
|
|
|
2022-09-02 21:28:09 +00:00
|
|
|
// 删除模块文件
|
|
|
|
$protectedFiles = Server::getConfig($this->modulesDir, 'protectedFiles');
|
|
|
|
foreach ($protectedFiles as &$protectedFile) {
|
|
|
|
$protectedFile = path_transform(root_path() . $protectedFile);
|
|
|
|
}
|
|
|
|
$moduleFile = Server::getFileList($this->modulesDir);
|
|
|
|
foreach ($moduleFile as $item) {
|
|
|
|
$file = path_transform(root_path() . $item);
|
|
|
|
if (in_array($file, $protectedFiles)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (file_exists($file)) {
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
del_empty_dir(dirname($file));
|
|
|
|
}
|
|
|
|
|
2022-09-03 06:15:45 +00:00
|
|
|
// 恢复备份文件
|
|
|
|
if ($zipDir) {
|
|
|
|
foreach (
|
|
|
|
$iterator = new RecursiveIteratorIterator(
|
|
|
|
new RecursiveDirectoryIterator($zipDir, FilesystemIterator::SKIP_DOTS),
|
|
|
|
RecursiveIteratorIterator::SELF_FIRST
|
|
|
|
) as $item
|
|
|
|
) {
|
|
|
|
$ebakFile = path_transform(root_path() . $iterator->getSubPathName());
|
|
|
|
if ($item->isDir()) {
|
|
|
|
if (!is_dir($ebakFile)) {
|
|
|
|
mkdir($ebakFile, 0755, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
copy($item, $ebakFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 07:43:59 +00:00
|
|
|
// 删除解压后的备份文件
|
2022-09-02 19:55:06 +00:00
|
|
|
deldir($zipDir);
|
2022-09-04 07:43:59 +00:00
|
|
|
|
|
|
|
// 执行禁用脚本
|
|
|
|
Server::execEvent($this->uid, 'disable');
|
|
|
|
|
|
|
|
$this->setInfo([
|
|
|
|
'state' => self::DISABLE,
|
|
|
|
]);
|
|
|
|
|
2022-09-05 09:21:16 +00:00
|
|
|
if ($upadte) {
|
|
|
|
$token = request()->get("token/s", '');
|
2022-09-06 02:22:51 +00:00
|
|
|
$order = request()->get("order/d", 0);
|
2022-09-05 09:21:16 +00:00
|
|
|
$this->update($token, $order);
|
|
|
|
throw new moduleException('upadte', -3, [
|
|
|
|
'uid' => $this->uid,
|
2022-09-13 07:13:36 +00:00
|
|
|
'vitereload' => $info['vitereload'],
|
2022-09-05 09:21:16 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-09-04 07:43:59 +00:00
|
|
|
if ($dependWaitInstall) {
|
|
|
|
throw new moduleException('dependent wait install', -2, [
|
|
|
|
'uid' => $this->uid,
|
|
|
|
'wait_install' => $dependWaitInstall,
|
2022-09-13 07:13:36 +00:00
|
|
|
'vitereload' => $info['vitereload'],
|
2022-09-04 07:43:59 +00:00
|
|
|
]);
|
|
|
|
}
|
2022-09-11 15:19:08 +00:00
|
|
|
return $info;
|
2022-09-02 07:59:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 07:28:16 +00:00
|
|
|
/**
|
|
|
|
* 处理依赖和文件冲突,并完成与前端的冲突处理交互
|
2022-08-29 19:01:25 +00:00
|
|
|
* @throws moduleException|Exception
|
2022-08-11 07:28:16 +00:00
|
|
|
*/
|
2022-08-29 19:01:25 +00:00
|
|
|
public function conflictHandle(string $trigger): bool
|
2022-08-11 07:28:16 +00:00
|
|
|
{
|
2022-08-29 19:01:25 +00:00
|
|
|
$info = $this->getInfo();
|
|
|
|
if ($info['state'] != self::WAIT_INSTALL && $info['state'] != self::CONFLICT_PENDING) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-27 15:52:29 +00:00
|
|
|
$fileConflict = Server::getFileList($this->modulesDir, true);// 文件冲突
|
|
|
|
$dependConflict = Server::dependConflictCheck($this->modulesDir);// 依赖冲突
|
|
|
|
$installFiles = Server::getFileList($this->modulesDir);// 待安装文件
|
|
|
|
$depends = Server::getDepend($this->modulesDir);// 待安装依赖
|
2022-08-11 07:28:16 +00:00
|
|
|
|
|
|
|
$coverFiles = [];// 要覆盖的文件-备份
|
|
|
|
$discardFiles = [];// 抛弃的文件-复制时不覆盖
|
|
|
|
$dependObj = new Depend();
|
|
|
|
if ($fileConflict || $dependConflict) {
|
|
|
|
$extend = request()->post('extend/a', []);
|
|
|
|
if (!$extend) {
|
|
|
|
// 发现冲突->手动处理->转换为方便前端使用的格式
|
|
|
|
$fileConflictTemp = [];
|
|
|
|
foreach ($fileConflict as $key => $item) {
|
|
|
|
$fileConflictTemp[$key] = [
|
|
|
|
'newFile' => $this->uid . DIRECTORY_SEPARATOR . $item,
|
|
|
|
'oldFile' => $item,
|
|
|
|
'solution' => 'cover',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$dependConflictTemp = [];
|
|
|
|
foreach ($dependConflict as $env => $item) {
|
|
|
|
$dev = !(stripos($env, 'dev') === false);
|
|
|
|
foreach ($item as $depend => $v) {
|
|
|
|
$dependConflictTemp[] = [
|
|
|
|
'env' => $env,
|
|
|
|
'newDepend' => $depend . ' ' . $v,
|
|
|
|
'oldDepend' => $depend . ' ' . (stripos($env, 'require') === false ? $dependObj->hasNpmDependencies($depend, $dev) : $dependObj->hasComposerRequire($depend, $dev)),
|
|
|
|
'depend' => $depend,
|
|
|
|
'solution' => 'cover',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 02:04:53 +00:00
|
|
|
$this->setInfo([
|
|
|
|
'state' => self::CONFLICT_PENDING,
|
|
|
|
]);
|
2022-08-15 17:18:58 +00:00
|
|
|
throw new moduleException('Module file conflicts', -1, [
|
2022-08-11 07:28:16 +00:00
|
|
|
'fileConflict' => $fileConflictTemp,
|
|
|
|
'dependConflict' => $dependConflictTemp,
|
|
|
|
'uid' => $this->uid,
|
2022-08-17 02:04:53 +00:00
|
|
|
'state' => self::CONFLICT_PENDING,
|
2022-08-11 07:28:16 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理冲突
|
|
|
|
if ($fileConflict && isset($extend['fileConflict'])) {
|
|
|
|
foreach ($installFiles as $ikey => $installFile) {
|
|
|
|
if (isset($extend['fileConflict'][$installFile])) {
|
|
|
|
if ($extend['fileConflict'][$installFile] == 'discard') {
|
|
|
|
$discardFiles[] = $installFile;
|
|
|
|
unset($installFiles[$ikey]);
|
|
|
|
} else {
|
|
|
|
$coverFiles[] = $installFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($dependConflict && isset($extend['dependConflict'])) {
|
2022-08-27 15:52:29 +00:00
|
|
|
foreach ($depends as $fKey => $fItem) {
|
2022-08-11 07:28:16 +00:00
|
|
|
foreach ($fItem as $cKey => $cItem) {
|
2022-08-27 15:52:29 +00:00
|
|
|
if (isset($extend['dependConflict'][$fKey][$cKey])) {
|
|
|
|
if ($extend['dependConflict'][$fKey][$cKey] == 'discard') {
|
|
|
|
unset($depends[$fKey][$cKey]);
|
|
|
|
}
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 15:52:29 +00:00
|
|
|
// 如果有依赖更新,增加要备份的文件
|
|
|
|
if ($depends) {
|
|
|
|
foreach ($depends as $key => $item) {
|
|
|
|
if (!$item) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($key == 'require' || $key == 'require-dev') {
|
|
|
|
$coverFiles[] = 'composer.json';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($key == 'dependencies' || $key == 'devDependencies') {
|
|
|
|
$coverFiles[] = 'web' . DIRECTORY_SEPARATOR . 'package.json';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 备份将被覆盖的文件
|
|
|
|
if ($coverFiles) {
|
|
|
|
$ebakZip = $trigger == 'install' ? $this->ebakDir . $this->uid . '-install.zip' : $this->ebakDir . $this->uid . '-cover-' . date('YmdHis') . '.zip';
|
|
|
|
Server::createZip($coverFiles, $ebakZip);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($depends) {
|
2022-08-17 03:45:53 +00:00
|
|
|
$npm = false;
|
|
|
|
$composer = false;
|
2022-08-27 15:52:29 +00:00
|
|
|
foreach ($depends as $key => $item) {
|
2022-08-17 05:18:22 +00:00
|
|
|
if (!$item) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-15 17:04:30 +00:00
|
|
|
if ($key == 'require') {
|
2022-08-17 03:45:53 +00:00
|
|
|
$composer = true;
|
2022-08-15 17:04:30 +00:00
|
|
|
$dependObj->addComposerRequire($item, false, true);
|
|
|
|
} elseif ($key == 'require-dev') {
|
2022-08-17 03:45:53 +00:00
|
|
|
$composer = true;
|
2022-08-15 17:04:30 +00:00
|
|
|
$dependObj->addComposerRequire($item, true, true);
|
|
|
|
} elseif ($key == 'dependencies') {
|
2022-08-17 03:45:53 +00:00
|
|
|
$npm = true;
|
2022-08-15 17:04:30 +00:00
|
|
|
$dependObj->addNpmDependencies($item, false, true);
|
|
|
|
} elseif ($key == 'devDependencies') {
|
2022-08-17 03:45:53 +00:00
|
|
|
$npm = true;
|
2022-08-15 17:04:30 +00:00
|
|
|
$dependObj->addNpmDependencies($item, true, true);
|
|
|
|
}
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
2022-08-17 03:45:53 +00:00
|
|
|
if ($npm) {
|
|
|
|
$info['npm_dependent_wait_install'] = 1;
|
2022-08-17 05:18:22 +00:00
|
|
|
$info['state'] = self::DEPENDENT_WAIT_INSTALL;
|
2022-08-17 03:45:53 +00:00
|
|
|
}
|
|
|
|
if ($composer) {
|
|
|
|
$info['composer_dependent_wait_install'] = 1;
|
2022-08-17 05:18:22 +00:00
|
|
|
$info['state'] = self::DEPENDENT_WAIT_INSTALL;
|
|
|
|
}
|
|
|
|
if ($info['state'] != self::DEPENDENT_WAIT_INSTALL) {
|
|
|
|
// 无冲突
|
|
|
|
$this->setInfo([
|
|
|
|
'state' => self::INSTALLED,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$this->setInfo([], $info);
|
2022-08-17 03:45:53 +00:00
|
|
|
}
|
2022-08-17 05:18:22 +00:00
|
|
|
} else {
|
|
|
|
// 无冲突
|
|
|
|
$this->setInfo([
|
|
|
|
'state' => self::INSTALLED,
|
|
|
|
]);
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 复制文件
|
|
|
|
$overwriteDir = Server::getOverwriteDir();
|
|
|
|
foreach ($overwriteDir as $dirItem) {
|
2022-08-15 17:18:58 +00:00
|
|
|
$baseDir = $this->modulesDir . $dirItem;
|
2022-08-11 07:28:16 +00:00
|
|
|
$destDir = root_path() . $dirItem;
|
|
|
|
if (!is_dir($baseDir)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach (
|
|
|
|
$iterator = new RecursiveIteratorIterator(
|
|
|
|
new RecursiveDirectoryIterator($baseDir, FilesystemIterator::SKIP_DOTS),
|
|
|
|
RecursiveIteratorIterator::SELF_FIRST
|
|
|
|
) as $item
|
|
|
|
) {
|
|
|
|
$destDirItem = $destDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName();
|
|
|
|
if ($item->isDir()) {
|
|
|
|
if (!is_dir($destDirItem)) {
|
|
|
|
mkdir($destDirItem, 0755, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!in_array(str_replace(root_path(), '', $destDirItem), $discardFiles)) {
|
|
|
|
copy($item, $destDirItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 19:01:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dependUpdateHandle()
|
|
|
|
{
|
|
|
|
$info = $this->getInfo();
|
|
|
|
if ($info['state'] == self::DEPENDENT_WAIT_INSTALL) {
|
|
|
|
$waitInstall = [];
|
|
|
|
if (isset($info['composer_dependent_wait_install'])) {
|
|
|
|
$waitInstall[] = 'composer_dependent_wait_install';
|
|
|
|
}
|
|
|
|
if (isset($info['npm_dependent_wait_install'])) {
|
|
|
|
$waitInstall[] = 'npm_dependent_wait_install';
|
|
|
|
}
|
|
|
|
if ($waitInstall) {
|
|
|
|
throw new moduleException('dependent wait install', -2, [
|
|
|
|
'uid' => $this->uid,
|
|
|
|
'state' => self::DEPENDENT_WAIT_INSTALL,
|
|
|
|
'wait_install' => $waitInstall,
|
2022-09-13 07:13:36 +00:00
|
|
|
'vitereload' => $info['vitereload'],
|
2022-08-29 19:01:25 +00:00
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$this->setInfo([
|
|
|
|
'state' => self::INSTALLED,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dependentInstallComplete(string $type)
|
|
|
|
{
|
|
|
|
$info = $this->getInfo();
|
|
|
|
if ($info['state'] == self::DEPENDENT_WAIT_INSTALL) {
|
|
|
|
if ($type == 'npm') {
|
|
|
|
unset($info['npm_dependent_wait_install']);
|
|
|
|
}
|
|
|
|
if ($type == 'composer') {
|
|
|
|
unset($info['composer_dependent_wait_install']);
|
|
|
|
}
|
|
|
|
if ($type == 'all') {
|
|
|
|
unset($info['npm_dependent_wait_install'], $info['composer_dependent_wait_install']);
|
|
|
|
}
|
|
|
|
if (!isset($info['npm_dependent_wait_install']) && !isset($info['composer_dependent_wait_install'])) {
|
|
|
|
$info['state'] = self::INSTALLED;
|
|
|
|
}
|
|
|
|
$this->setInfo([], $info);
|
|
|
|
}
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 19:55:06 +00:00
|
|
|
public function disableDependConflictCheck(string $ebakDir): array
|
2022-09-02 12:32:17 +00:00
|
|
|
{
|
2022-09-02 21:28:09 +00:00
|
|
|
if (!$ebakDir) {
|
|
|
|
return [];
|
|
|
|
}
|
2022-09-02 12:32:17 +00:00
|
|
|
$dependFile = [
|
2022-09-02 19:55:06 +00:00
|
|
|
path_transform($ebakDir . DIRECTORY_SEPARATOR . 'composer.json') => [
|
2022-09-02 12:32:17 +00:00
|
|
|
'path' => path_transform(root_path() . 'composer.json'),
|
|
|
|
'type' => 'composer',
|
|
|
|
],
|
2022-09-02 19:55:06 +00:00
|
|
|
path_transform($ebakDir . DIRECTORY_SEPARATOR . 'web/package.json') => [
|
2022-09-02 12:32:17 +00:00
|
|
|
'path' => path_transform(root_path() . 'web/package.json'),
|
|
|
|
'type' => 'npm',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$conflict = [];
|
|
|
|
foreach ($dependFile as $key => $item) {
|
|
|
|
if (is_file($key) && (filesize($key) != filesize($item['path']) || md5_file($key) != md5_file($item['path']))) {
|
|
|
|
$conflict[] = $item['type'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $conflict;
|
|
|
|
}
|
|
|
|
|
2022-08-11 07:28:16 +00:00
|
|
|
public function checkPackage(): bool
|
|
|
|
{
|
2022-08-15 17:18:58 +00:00
|
|
|
if (!is_dir($this->modulesDir)) {
|
|
|
|
throw new Exception('Module package file does not exist');
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
$info = $this->getInfo();
|
|
|
|
$infoKeys = ['uid', 'title', 'intro', 'author', 'version', 'state'];
|
|
|
|
foreach ($infoKeys as $value) {
|
|
|
|
if (!array_key_exists($value, $info)) {
|
2022-08-15 17:18:58 +00:00
|
|
|
deldir($this->modulesDir);
|
|
|
|
throw new Exception('Basic configuration of the Module is incomplete');
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInfo()
|
|
|
|
{
|
2022-08-15 17:18:58 +00:00
|
|
|
return Server::getIni($this->modulesDir);
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setInfo(array $kv = [], array $arr = []): bool
|
|
|
|
{
|
|
|
|
if ($kv) {
|
|
|
|
$info = $this->getInfo();
|
|
|
|
foreach ($kv as $k => $v) {
|
|
|
|
$info[$k] = $v;
|
|
|
|
}
|
2022-08-15 17:18:58 +00:00
|
|
|
return Server::setIni($this->modulesDir, $info);
|
2022-08-11 07:28:16 +00:00
|
|
|
} elseif ($arr) {
|
2022-08-15 17:18:58 +00:00
|
|
|
return Server::setIni($this->modulesDir, $arr);
|
2022-08-11 07:28:16 +00:00
|
|
|
}
|
|
|
|
throw new Exception('Parameter error');
|
|
|
|
}
|
|
|
|
}
|