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\Utility; |
9
|
|
|
|
10
|
|
|
use Deployer\Logger\Logger; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Process\Process; |
13
|
|
|
|
14
|
|
|
class ProcessOutputPrinter |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var OutputInterface |
18
|
|
|
*/ |
19
|
|
|
private $output; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Logger |
23
|
|
|
*/ |
24
|
|
|
private $logger; |
25
|
|
|
|
26
|
9 |
|
public function __construct(OutputInterface $output, Logger $logger) |
27
|
|
|
{ |
28
|
9 |
|
$this->output = $output; |
29
|
9 |
|
$this->logger = $logger; |
30
|
9 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns a callable for use with the symfony Process->run($callable) method. |
34
|
|
|
* |
35
|
|
|
* @return callable A function expecting a int $type (e.g. Process::OUT or Process::ERR) and string $buffer parameters. |
36
|
|
|
*/ |
37
|
9 |
|
public function callback(string $hostname) |
38
|
|
|
{ |
39
|
|
|
return function ($type, $buffer) use ($hostname) { |
40
|
9 |
|
foreach (explode("\n", rtrim($buffer)) as $line) { |
41
|
9 |
|
$this->writeln($type, $hostname, $line); |
42
|
|
|
} |
43
|
9 |
|
}; |
44
|
|
|
} |
45
|
|
|
|
46
|
9 |
|
public function command(string $hostname, string $command) |
47
|
|
|
{ |
48
|
9 |
|
$this->logger->log("[$hostname] > $command"); |
49
|
|
|
|
50
|
9 |
|
if ($this->output->isVeryVerbose()) { |
51
|
1 |
|
$this->output->writeln("[$hostname] <fg=cyan>></fg=cyan> $command"); |
52
|
|
|
} |
53
|
9 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param int $type Process::OUT or Process::ERR |
57
|
|
|
* @param string $hostname for debugging |
58
|
|
|
* @param string $line to print |
59
|
|
|
*/ |
60
|
9 |
|
public function writeln($type, $hostname, $line) |
61
|
|
|
{ |
62
|
9 |
|
$line = $this->filterOutput($line); |
63
|
|
|
|
64
|
|
|
// Omit empty lines |
65
|
9 |
|
if (empty($line)) { |
66
|
3 |
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
if ($type === Process::ERR) { |
70
|
6 |
|
$this->logger->log("[$hostname] < [error] $line"); |
71
|
|
|
} else { |
72
|
9 |
|
$this->logger->log("[$hostname] < $line"); |
73
|
|
|
} |
74
|
|
|
|
75
|
9 |
|
if ($this->output->isDecorated()) { |
76
|
|
|
if ($type === Process::ERR) { |
77
|
|
|
$line = "[$hostname] \033[0;31m<\e[0m $line"; |
78
|
|
|
} else { |
79
|
|
|
$line = "[$hostname] \033[0;90m< $line\033[0m"; |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
9 |
|
$line = "[$hostname] < $line"; |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
if ($this->output->isDebug()) { |
86
|
1 |
|
$this->output->writeln($line, OutputInterface::OUTPUT_RAW); |
87
|
|
|
} |
88
|
9 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* This filtering used only in Ssh\Client, but for simplify putted here. |
92
|
|
|
* |
93
|
|
|
* @param string $output |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
9 |
|
public function filterOutput($output) |
97
|
|
|
{ |
98
|
9 |
|
return preg_replace('/\[exit_code:(.*?)\]/', '', $output); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|