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