Confirmable.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Concerns;
  12. use Closure;
  13. use Composer\InstalledVersions;
  14. use function Hyperf\Support\value;
  15. trait Confirmable
  16. {
  17. /**
  18. * Confirm before proceeding with the action.
  19. *
  20. * This method only asks for confirmation in production.
  21. */
  22. public function confirmToProceed(string $warning = 'Application In Production!', null|bool|Closure $callback = null): bool
  23. {
  24. $callback ??= $this->isShouldConfirm();
  25. $shouldConfirm = value($callback);
  26. if ($shouldConfirm) {
  27. if ($this->input->getOption('force')) {
  28. return true;
  29. }
  30. $this->alert($warning);
  31. $confirmed = $this->confirm('Do you really wish to run this command?');
  32. if (! $confirmed) {
  33. $this->comment('Command Cancelled!');
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39. protected function isShouldConfirm(): bool
  40. {
  41. return is_callable(['Composer\InstalledVersions', 'getRootPackage'])
  42. && (InstalledVersions::getRootPackage()['dev'] ?? false) === false;
  43. }
  44. }