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\ProcessRunner; |
12
|
|
|
|
13
|
|
|
use Deployer\Host\Host; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
class Printer |
17
|
|
|
{ |
18
|
|
|
private OutputInterface $output; |
19
|
|
|
|
20
|
|
|
public function __construct(OutputInterface $output) |
21
|
|
|
{ |
22
|
|
|
$this->output = $output; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function command(Host $host, string $type, string $command): void |
26
|
|
|
{ |
27
|
|
|
// -v for run command |
28
|
|
|
if ($this->output->isVerbose()) { |
29
|
|
|
$this->output->writeln("[$host] <fg=green;options=bold>$type</> $command"); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns a callable for use with the symfony Process->run($callable) method. |
35
|
|
|
* |
36
|
|
|
* @return callable A function expecting a int $type (e.g. Process::OUT or Process::ERR) and string $buffer parameters. |
37
|
|
|
*/ |
38
|
|
|
public function callback(Host $host, bool $forceOutput): callable |
39
|
|
|
{ |
40
|
|
|
return function ($type, $buffer) use ($forceOutput, $host) { |
41
|
|
|
if ($this->output->isVerbose() || $forceOutput) { |
42
|
|
|
$this->printBuffer($type, $host, $buffer); |
43
|
|
|
} |
44
|
|
|
}; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $type Process::OUT or Process::ERR |
49
|
|
|
*/ |
50
|
|
|
public function printBuffer(string $type, Host $host, string $buffer): void |
51
|
|
|
{ |
52
|
|
|
foreach (explode("\n", rtrim($buffer)) as $line) { |
53
|
|
|
$this->writeln($type, $host, $line); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function writeln(string $type, Host $host, string $line): void |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
// Omit empty lines |
60
|
|
|
if (empty($line)) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->output->writeln("[$host] $line"); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.