1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Console\Output; |
9
|
|
|
|
10
|
|
|
use Deployer\Deployer; |
11
|
|
|
use Deployer\Host\Host; |
12
|
|
|
use Deployer\Task\Task; |
13
|
|
|
use Symfony\Component\Console\Helper\FormatterHelper; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
class Informer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var OutputWatcher |
20
|
|
|
*/ |
21
|
|
|
private $output; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int|double |
25
|
|
|
*/ |
26
|
|
|
private $startTime; |
27
|
|
|
|
28
|
15 |
|
public function __construct(OutputWatcher $output) |
29
|
|
|
{ |
30
|
15 |
|
$this->output = $output; |
31
|
15 |
|
} |
32
|
|
|
|
33
|
12 |
|
public function startTask(Task $task) |
34
|
|
|
{ |
35
|
12 |
|
$this->startTime = round(microtime(true) * 1000); |
36
|
|
|
if ( |
37
|
12 |
|
$this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL && |
38
|
12 |
|
!$task->isShallow() |
39
|
|
|
) { |
40
|
12 |
|
$this->output->writeln("➤ Executing task <info>{$task->getName()}</info>"); |
41
|
12 |
|
$this->output->setWasWritten(false); |
42
|
|
|
} |
43
|
12 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Print task was ok. |
47
|
|
|
*/ |
48
|
13 |
|
public function endTask(Task $task) |
49
|
|
|
{ |
50
|
13 |
|
if ($task->isShallow()) { |
51
|
2 |
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
13 |
|
$endTime = round(microtime(true) * 1000); |
55
|
13 |
|
$millis = $endTime - $this->startTime; |
56
|
13 |
|
$seconds = floor($millis / 1000); |
57
|
13 |
|
$millis = $millis - $seconds * 1000; |
58
|
13 |
|
$taskTime = ($seconds > 0 ? "{$seconds}s " : "") . "{$millis}ms"; |
59
|
|
|
|
60
|
|
|
$shouldReplaceTaskMark = |
61
|
13 |
|
$this->output->isDecorated() && |
62
|
13 |
|
$this->output->getVerbosity() == OutputInterface::VERBOSITY_NORMAL && |
63
|
13 |
|
!$this->output->getWasWritten(); |
64
|
|
|
|
65
|
13 |
|
if ($shouldReplaceTaskMark) { |
66
|
|
|
$this->output->writeln("\r\033[K\033[1A\r<info>✔</info>"); |
67
|
|
|
} else { |
68
|
13 |
|
if ($this->output->getVerbosity() == OutputInterface::VERBOSITY_NORMAL) { |
69
|
11 |
|
$this->output->writeln("<info>✔</info> Ok"); |
70
|
|
|
} else { |
71
|
2 |
|
$this->output->writeln("<info>✔</info> Ok [$taskTime]"); |
72
|
|
|
} |
73
|
|
|
} |
74
|
13 |
|
} |
75
|
|
|
|
76
|
10 |
|
public function endOnHost(string $hostname) |
77
|
|
|
{ |
78
|
10 |
|
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
79
|
1 |
|
$this->output->writeln("<info>•</info> done on [$hostname]"); |
80
|
|
|
} |
81
|
10 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Print error. |
85
|
|
|
* |
86
|
|
|
* @param bool $nonFatal |
87
|
|
|
*/ |
88
|
2 |
|
public function taskError($nonFatal = true) |
89
|
|
|
{ |
90
|
2 |
|
if ($nonFatal) { |
91
|
1 |
|
$this->output->writeln("<fg=yellow>✘</fg=yellow> Some errors occurred!"); |
92
|
|
|
} else { |
93
|
1 |
|
$this->output->writeln("<fg=red>✘</fg=red> <options=underscore>Some errors occurred!</options=underscore>"); |
94
|
|
|
} |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \Throwable $exception |
99
|
|
|
* @param Host $host |
100
|
|
|
*/ |
101
|
|
|
public function taskException($exception, $host = null) |
102
|
|
|
{ |
103
|
|
|
/** @var FormatterHelper $formatter */ |
104
|
|
|
$formatter = Deployer::get()->getHelper('formatter'); |
105
|
|
|
$messages = array_filter(array_map('trim', explode("\n", $exception->getMessage())), function ($line) { |
106
|
|
|
return !empty($line); |
107
|
|
|
}); |
108
|
|
|
$exceptionClass = get_class($exception); |
109
|
|
|
|
110
|
|
|
if (empty($host)) { |
111
|
|
|
array_unshift($messages, "[$exceptionClass]"); |
112
|
|
|
} else { |
113
|
|
|
array_unshift($messages, "[$exceptionClass] on [{$host->getHostname()}]"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->output->writeln($formatter->formatBlock($messages, 'error', true)); |
117
|
|
|
$this->output->writeln(''); |
118
|
|
|
|
119
|
|
|
if (OutputInterface::VERBOSITY_VERBOSE <= $this->output->getVerbosity()) { |
120
|
|
|
$this->output->writeln('<comment>Exception trace:</comment>', OutputInterface::VERBOSITY_QUIET); |
121
|
|
|
|
122
|
|
|
// exception related properties |
123
|
|
|
$trace = $exception->getTrace(); |
124
|
|
|
array_unshift($trace, [ |
125
|
|
|
'function' => '', |
126
|
|
|
'file' => $exception->getFile() !== null ? $exception->getFile() : 'n/a', |
127
|
|
|
'line' => $exception->getLine() !== null ? $exception->getLine() : 'n/a', |
128
|
|
|
'args' => [], |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
for ($i = 0, $count = count($trace); $i < $count; ++$i) { |
132
|
|
|
$class = isset($trace[$i]['class']) ? $trace[$i]['class'] : ''; |
133
|
|
|
$type = isset($trace[$i]['type']) ? $trace[$i]['type'] : ''; |
134
|
|
|
$function = $trace[$i]['function']; |
135
|
|
|
$file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a'; |
136
|
|
|
$line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a'; |
137
|
|
|
|
138
|
|
|
$this->output->writeln(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line), OutputInterface::VERBOSITY_QUIET); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->output->writeln('', OutputInterface::VERBOSITY_QUIET); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|