ExtensionRollbackCommand.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Dcat\Admin\Console;
  3. use Dcat\Admin\Admin;
  4. use Illuminate\Console\Command;
  5. class ExtensionRollbackCommand extends Command
  6. {
  7. protected $signature = 'admin:ext-rollback
  8. {name : The name of the extension. Eg: author-name/extension-name}
  9. {ver : If this parameter is specified, the process will stop on the specified version, if not, it will completely rollback the extension. Example: 1.3.9}
  10. {--force : Force rollback}';
  11. protected $description = 'Rollback an existing extension';
  12. public function handle()
  13. {
  14. $name = $this->argument('name');
  15. if (! Admin::extension()->has($name)) {
  16. throw new \InvalidArgumentException('Extension not found');
  17. }
  18. $stopOnVersion = ltrim(($this->argument('ver') ?: null), 'v');
  19. if ($stopOnVersion) {
  20. if (! Admin::extension()->versionManager()->hasDatabaseVersion($name, $stopOnVersion)) {
  21. throw new \InvalidArgumentException('Extension version not found');
  22. }
  23. $confirmQuestion = 'Please confirm that you wish to revert the extension to version '.$stopOnVersion.'. This may result in changes to your database and potential data loss.';
  24. } else {
  25. $confirmQuestion = 'Please confirm that you wish to completely rollback this extension. This may result in potential data loss.';
  26. }
  27. if ($this->option('force') || $this->confirm($confirmQuestion)) {
  28. try {
  29. Admin::extension()
  30. ->updateManager()
  31. ->setOutPut($this->output)
  32. ->rollback($name, $stopOnVersion);
  33. } catch (\Throwable $exception) {
  34. $lastVersion = Admin::extension()->versionManager()->getCurrentVersion($name);
  35. $this->output->writeln(sprintf('<comment>An exception occurred during the rollback and the process has been stopped. The extension was rolled back to version v%s.</comment>', $lastVersion));
  36. throw $exception;
  37. }
  38. }
  39. }
  40. }