-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathConfirmableTrait.php
More file actions
56 lines (45 loc) · 1.38 KB
/
ConfirmableTrait.php
File metadata and controls
56 lines (45 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace Illuminate\Console;
use function Laravel\Prompts\confirm;
trait ConfirmableTrait
{
/**
* Confirm before proceeding with the action.
*
* This method only asks for confirmation in production.
*
* @template TReturn of bool = bool
*
* @param string $warning
* @param (\Closure(): TReturn)|TReturn|null $callback
* @return (TReturn is false ? true : bool)
*/
public function confirmToProceed($warning = 'Application In Production', $callback = null)
{
$callback = is_null($callback) ? $this->getDefaultConfirmCallback() : $callback;
$shouldConfirm = value($callback);
if ($shouldConfirm) {
if ($this->hasOption('force') && $this->option('force')) {
return true;
}
$this->components->alert($warning);
$confirmed = confirm('Are you sure you want to run this command?', default: false);
if (! $confirmed) {
$this->components->warn('Command cancelled.');
return false;
}
}
return true;
}
/**
* Get the default confirmation callback.
*
* @return \Closure(): bool
*/
protected function getDefaultConfirmCallback()
{
return function () {
return $this->getLaravel()->environment() === 'production';
};
}
}