ConfirmableTrait.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Illuminate\Console;
  3. use Closure;
  4. trait ConfirmableTrait
  5. {
  6. /**
  7. * Confirm before proceeding with the action.
  8. *
  9. * This method only asks for confirmation in production.
  10. *
  11. * @param string $warning
  12. * @param \Closure|bool|null $callback
  13. * @return bool
  14. */
  15. public function confirmToProceed($warning = 'Application In Production!', $callback = null)
  16. {
  17. $callback = is_null($callback) ? $this->getDefaultConfirmCallback() : $callback;
  18. $shouldConfirm = $callback instanceof Closure ? call_user_func($callback) : $callback;
  19. if ($shouldConfirm) {
  20. if ($this->option('force')) {
  21. return true;
  22. }
  23. $this->alert($warning);
  24. $confirmed = $this->confirm('Do you really wish to run this command?');
  25. if (! $confirmed) {
  26. $this->comment('Command Cancelled!');
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
  32. /**
  33. * Get the default confirmation callback.
  34. *
  35. * @return \Closure
  36. */
  37. protected function getDefaultConfirmCallback()
  38. {
  39. return function () {
  40. return $this->getLaravel()->environment() == 'production';
  41. };
  42. }
  43. }