ConfirmableTrait.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Command;
  12. use Closure;
  13. trait ConfirmableTrait
  14. {
  15. /**
  16. * Confirm before proceeding with the action.
  17. *
  18. * This method only asks for confirmation in production.
  19. *
  20. * @param string $warning
  21. * @param null|bool|\Closure $callback
  22. * @return bool
  23. */
  24. public function confirmToProceed($warning = 'Application In Production!', $callback = false)
  25. {
  26. $shouldConfirm = $callback instanceof Closure ? call_user_func($callback) : $callback;
  27. if ($shouldConfirm) {
  28. if ($this->input->getOption('force')) {
  29. return true;
  30. }
  31. $this->alert($warning);
  32. $confirmed = $this->confirm('Do you really wish to run this command?');
  33. if (! $confirmed) {
  34. $this->comment('Command Cancelled!');
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. }