ModuleInstall.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Modules\Develop\Support;
  3. use Catch\CatchAdmin;
  4. use Catch\Exceptions\FailedException;
  5. use Catch\Facade\Zipper;
  6. use Illuminate\Support\Facades\File;
  7. /**
  8. * module install
  9. */
  10. class ModuleInstall
  11. {
  12. const NORMAL_INSTALL = 1;
  13. const ZIP_INSTALL = 2;
  14. public function __construct(protected readonly int|string $type){}
  15. /**
  16. *
  17. * @param array $params
  18. */
  19. public function install(array $params): void
  20. {
  21. try {
  22. if ($this->type === self::NORMAL_INSTALL) {
  23. $this->installWithTitle($params['title']);
  24. }
  25. if ($this->type == self::ZIP_INSTALL) {
  26. $this->installWithZip($params['title'], $params['file']);
  27. }
  28. } catch (\Exception $e) {
  29. if ($this->type == self::ZIP_INSTALL) {
  30. CatchAdmin::deleteModulePath($params['title']);
  31. }
  32. throw new FailedException('安装失败: ' . $e->getMessage());
  33. }
  34. }
  35. /**
  36. *
  37. * @param string $title
  38. */
  39. protected function installWithTitle(string $title): void
  40. {
  41. try {
  42. $installer = CatchAdmin::getModuleInstaller($title);
  43. $installer->install();
  44. } catch (\Exception|\Throwable $e) {
  45. // CatchAdmin::deleteModulePath($title);
  46. throw new FailedException('安装失败: ' . $e->getMessage());
  47. }
  48. }
  49. /**
  50. * get
  51. *
  52. * @param string $title
  53. * @param string $zip
  54. */
  55. protected function installWithZip(string $title, string $zip): void
  56. {
  57. $zipRepository = Zipper::make($zip)->getRepository();
  58. $zipRepository->getArchive()->extractTo(CatchAdmin::getModulePath($title));
  59. $this->installWithTitle($title);
  60. File::delete($zip);
  61. }
  62. }