catchAdmin/modules/Develop/Support/ModuleInstall.php

71 lines
1.6 KiB
PHP
Raw Normal View History

2023-01-09 18:19:20 +08:00
<?php
namespace Modules\Develop\Support;
use Catch\CatchAdmin;
use Catch\Exceptions\FailedException;
use Catch\Facade\Zipper;
2023-01-10 17:46:05 +08:00
use Illuminate\Support\Facades\File;
2023-01-09 18:19:20 +08:00
/**
* module install
*/
class ModuleInstall
{
const NORMAL_INSTALL = 1;
const ZIP_INSTALL = 2;
public function __construct(protected readonly int|string $type){}
/**
*
* @param array $params
*/
public function install(array $params): void
{
try {
if ($this->type === self::NORMAL_INSTALL) {
$this->installWithTitle($params['title']);
}
if ($this->type == self::ZIP_INSTALL) {
$this->installWithZip($params['title'], $params['file']);
}
} catch (\Exception $e) {
if ($this->type == self::ZIP_INSTALL) {
CatchAdmin::deleteModulePath($params['title']);
}
throw new FailedException('安装失败: ' . $e->getMessage());
}
}
/**
*
* @param string $title
*/
protected function installWithTitle(string $title): void
{
$installer = CatchAdmin::getModuleInstaller($title);
$installer->install();
}
/**
* get
*
* @param string $title
* @param string $zip
*/
protected function installWithZip(string $title, string $zip): void
{
$zipRepository = Zipper::make($zip)->getRepository();
$zipRepository->getArchive()->extractTo(CatchAdmin::getModulePath($title));
$this->installWithTitle($title);
2023-01-10 17:46:05 +08:00
File::delete($zip);
2023-01-09 18:19:20 +08:00
}
}