1 | <?php |
||
14 | class ProcessOutputPrinter |
||
15 | { |
||
16 | /** |
||
17 | * @var OutputInterface |
||
18 | */ |
||
19 | private $output; |
||
20 | |||
21 | /** |
||
22 | * @var Logger |
||
23 | */ |
||
24 | private $logger; |
||
25 | |||
26 | 1 | public function __construct(OutputInterface $output, Logger $logger) |
|
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 | 1 | public function callback(string $hostname) |
|
45 | |||
46 | 1 | public function command(string $hostname, string $command) |
|
47 | { |
||
48 | 1 | $this->logger->log("[$hostname] > $command"); |
|
49 | |||
50 | 1 | if ($this->output->isVeryVerbose()) { |
|
51 | $this->output->writeln("[$hostname] <fg=cyan>></fg=cyan> $command"); |
||
52 | } |
||
53 | 1 | } |
|
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 | 1 | public function writeln($type, $hostname, $line) |
|
61 | { |
||
62 | 1 | $line = $this->filterOutput($line); |
|
63 | |||
64 | // Omit empty lines |
||
65 | 1 | if (empty($line)) { |
|
66 | return; |
||
67 | } |
||
68 | |||
69 | 1 | if ($type === Process::ERR) { |
|
70 | $this->logger->log("[$hostname] < [error] $line"); |
||
71 | } else { |
||
72 | 1 | $this->logger->log("[$hostname] < $line"); |
|
73 | } |
||
74 | |||
75 | 1 | 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 | 1 | $line = "[$hostname] < $line"; |
|
83 | } |
||
84 | |||
85 | 1 | if ($this->output->isDebug()) { |
|
86 | $this->output->writeln($line, OutputInterface::OUTPUT_RAW); |
||
87 | } |
||
88 | 1 | } |
|
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 | 1 | public function filterOutput($output) |
|
100 | } |
||
101 |