1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Deployer\Component\ProcessRunner; |
12
|
|
|
|
13
|
|
|
use Deployer\Host\Host; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
class Printer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var OutputInterface |
20
|
14 |
|
*/ |
21
|
|
|
private $output; |
22
|
14 |
|
|
23
|
14 |
|
public function __construct(OutputInterface $output) |
24
|
|
|
{ |
25
|
10 |
|
$this->output = $output; |
26
|
|
|
} |
27
|
|
|
|
28
|
10 |
|
public function command(Host $host, string $type, string $command): void |
29
|
6 |
|
{ |
30
|
|
|
// -v for run command |
31
|
10 |
|
if ($this->output->isVerbose()) { |
32
|
|
|
$this->output->writeln("[$host] <fg=green;options=bold>$type</> $command"); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns a callable for use with the symfony Process->run($callable) method. |
38
|
|
|
* |
39
|
10 |
|
* @return callable A function expecting a int $type (e.g. Process::OUT or Process::ERR) and string $buffer parameters. |
40
|
|
|
*/ |
41
|
|
|
public function callback(Host $host, bool $forceOutput): callable |
42
|
9 |
|
{ |
43
|
5 |
|
return function ($type, $buffer) use ($forceOutput, $host) { |
44
|
|
|
if ($this->output->isVerbose() || $forceOutput) { |
45
|
10 |
|
$this->printBuffer($type, $host, $buffer); |
46
|
|
|
} |
47
|
|
|
}; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $type Process::OUT or Process::ERR |
52
|
|
|
*/ |
53
|
5 |
|
public function printBuffer(string $type, Host $host, string $buffer): void |
54
|
|
|
{ |
55
|
5 |
|
foreach (explode("\n", rtrim($buffer)) as $line) { |
56
|
5 |
|
$this->writeln($type, $host, $line); |
57
|
|
|
} |
58
|
5 |
|
} |
59
|
|
|
|
60
|
|
|
public function writeln(string $type, Host $host, string $line): void |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
// Omit empty lines |
63
|
|
|
if (empty($line)) { |
64
|
|
|
return; |
65
|
5 |
|
} |
66
|
|
|
|
67
|
5 |
|
$this->output->writeln("[$host] $line"); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.