1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Console\Commands\Traits; |
4
|
|
|
|
5
|
|
|
use Artisan; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
trait PrettyCommandOutput |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Run a SSH command. |
14
|
|
|
* |
15
|
|
|
* @param string $command The SSH command that needs to be run |
16
|
|
|
* @param bool $beforeNotice Information for the user before the command is run |
17
|
|
|
* @param bool $afterNotice Information for the user after the command is run |
18
|
|
|
* |
19
|
|
|
* @return mixed Command-line output |
20
|
|
|
*/ |
21
|
|
|
public function executeProcess($command, $beforeNotice = false, $afterNotice = false) |
22
|
|
|
{ |
23
|
|
|
$this->echo('info', $beforeNotice ? ' '.$beforeNotice : implode(' ', $command)); |
|
|
|
|
24
|
|
|
|
25
|
|
|
// make sure the command is an array as per Symphony 4.3+ requirement |
26
|
|
|
$command = is_string($command) ? explode(' ', $command) : $command; |
|
|
|
|
27
|
|
|
|
28
|
|
|
$process = new Process($command, null, null, null, $this->option('timeout')); |
|
|
|
|
29
|
|
|
$process->run(function ($type, $buffer) { |
30
|
|
|
if (Process::ERR === $type) { |
31
|
|
|
$this->echo('comment', $buffer); |
32
|
|
|
} else { |
33
|
|
|
$this->echo('line', $buffer); |
34
|
|
|
} |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
// executes after the command finishes |
38
|
|
|
if (! $process->isSuccessful()) { |
39
|
|
|
throw new ProcessFailedException($process); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($this->progressBar) { |
43
|
|
|
$this->progressBar->advance(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($afterNotice) { |
47
|
|
|
$this->echo('info', $afterNotice); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Run an artisan command. |
53
|
|
|
* |
54
|
|
|
* @param string $command The artisan command to be run. |
55
|
|
|
* @param array $arguments Key-value array of arguments to the artisan command. |
56
|
|
|
* @param bool $beforeNotice Information for the user before the command is run |
57
|
|
|
* @param bool $afterNotice Information for the user after the command is run |
58
|
|
|
* |
59
|
|
|
* @return mixed Command-line output |
60
|
|
|
*/ |
61
|
|
|
public function executeArtisanProcess($command, $arguments = [], $beforeNotice = false, $afterNotice = false) |
62
|
|
|
{ |
63
|
|
|
$beforeNotice = $beforeNotice ? ' '.$beforeNotice : 'php artisan '.implode(' ', (array) $command).' '.implode(' ', $arguments); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->echo('info', $beforeNotice); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
Artisan::call($command, $arguments); |
69
|
|
|
} catch (Exception $e) { |
|
|
|
|
70
|
|
|
throw new ProcessFailedException($e); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($this->progressBar) { |
74
|
|
|
$this->progressBar->advance(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($afterNotice) { |
78
|
|
|
$this->echo('info', $afterNotice); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Write text to the screen for the user to see. |
84
|
|
|
* |
85
|
|
|
* @param string $type line, info, comment, question, error |
86
|
|
|
* @param string $content |
87
|
|
|
*/ |
88
|
|
|
public function echo($type, $content) |
89
|
|
|
{ |
90
|
|
|
if ($this->option('debug') == false) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// skip empty lines |
95
|
|
|
if (trim($content)) { |
96
|
|
|
$this->{$type}($content); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Write a title inside a box. |
102
|
|
|
* |
103
|
|
|
* @param string $content |
104
|
|
|
*/ |
105
|
|
|
public function box($content) |
106
|
|
|
{ |
107
|
|
|
for ($i = 0, $line = ''; $i < strlen($content); ++$i, $line .= '─'); |
108
|
|
|
|
109
|
|
|
$this->line(''); |
|
|
|
|
110
|
|
|
$this->info("┌───{$line}───┐"); |
|
|
|
|
111
|
|
|
$this->info("│ $content │"); |
112
|
|
|
$this->info("└───{$line}───┘"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Write a title inside a box. |
117
|
|
|
* |
118
|
|
|
* @param string $content |
119
|
|
|
*/ |
120
|
|
|
public function note($content) |
121
|
|
|
{ |
122
|
|
|
$this->line("│ $content"); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|