InstallFromLocal.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Dcat\Admin\Http\Forms;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Contracts\LazyRenderable;
  5. use Dcat\Admin\Exception\RuntimeException;
  6. use Dcat\Admin\Traits\LazyWidget;
  7. use Dcat\Admin\Widgets\Form;
  8. class InstallFromLocal extends Form implements LazyRenderable
  9. {
  10. use LazyWidget;
  11. public function handle(array $input)
  12. {
  13. $file = $input['extension'];
  14. if (! $file) {
  15. return $this->response()->error('Invalid arguments.');
  16. }
  17. try {
  18. $path = $this->getFilePath($file);
  19. $manager = Admin::extension();
  20. $allNames = $manager->all()->keys()->toArray();
  21. $manager->extract($path);
  22. $manager->load();
  23. $newAllNames = $manager->all()->keys()->toArray();
  24. $diff = array_diff($newAllNames, $allNames);
  25. if (! $diff) {
  26. return $this->response()->error(trans('admin.invalid_extension_package'));
  27. }
  28. $manager
  29. ->updateManager()
  30. ->update(current($diff));
  31. return $this->response()
  32. ->success(implode('<br>', $manager->updateManager()->notes))
  33. ->refresh();
  34. } catch (\Throwable $e) {
  35. Admin::reportException($e);
  36. return $this->response()->error($e->getMessage());
  37. } finally {
  38. if (! empty($path)) {
  39. @unlink($path);
  40. }
  41. }
  42. }
  43. public function form()
  44. {
  45. $this->file('extension')
  46. ->required()
  47. ->disk($this->disk())
  48. ->accept('zip,arc,rar,tar.gz', 'application/zip')
  49. ->autoUpload();
  50. }
  51. protected function getFilePath($file)
  52. {
  53. $root = config("filesystems.disks.{$this->disk()}.root");
  54. if (! $root) {
  55. throw new RuntimeException(sprintf('Missing \'root\' for disk [%s].', $this->disk()));
  56. }
  57. return rtrim($root, '/').'/'.$file;
  58. }
  59. protected function disk()
  60. {
  61. return config('admin.extension.disk') ?: 'local';
  62. }
  63. }