Skip to content

Commit 74036e4

Browse files
committed
Refactor logger callback print
1 parent 9e82d22 commit 74036e4

4 files changed

Lines changed: 15 additions & 28 deletions

File tree

src/Logger/Logger.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,27 @@ public function __construct(OutputInterface $output, HandlerInterface $log)
3434

3535
public function command(Host $host, string $type, string $command): void
3636
{
37-
// -v for run command
3837
if ($this->output->isVerbose()) {
3938
$this->output->writeln("[$host] <fg=green;options=bold>$type</> $command");
4039
}
4140
$this->fileLog->log("[{$host->getAlias()}] $type: $command");
4241
}
4342

44-
/**
45-
* Returns a callable for use with the symfony Process->run($callable) method.
46-
*
47-
* @return callable A function expecting a int $type (e.g. Process::OUT or Process::ERR) and string $buffer parameters.
48-
*/
49-
public function callback(Host $host, bool $forceOutput): callable
43+
public function print(Host $host, string $buffer, bool $force = false): void
5044
{
51-
return function ($type, $buffer) use ($forceOutput, $host) {
52-
if ($this->output->isVerbose() || $forceOutput) {
53-
$this->printBuffer($type, $host, $buffer);
45+
if ($this->output->isVerbose() || $force) {
46+
foreach (explode("\n", rtrim($buffer)) as $line) {
47+
if (empty($line)) {
48+
return;
49+
}
50+
$this->output->writeln("[$host] $line");
5451
}
55-
};
56-
}
57-
58-
/**
59-
* @param string $type Process::OUT or Process::ERR
60-
*/
61-
public function printBuffer(string $type, Host $host, string $buffer): void
62-
{
52+
}
6353
foreach (explode("\n", rtrim($buffer)) as $line) {
6454
if (empty($line)) {
6555
return;
6656
}
67-
$this->output->writeln("[$host] $line");
57+
$this->fileLog->log("[{$host->getAlias()}] $line");
6858
}
6959
}
7060

src/ProcessRunner/ProcessRunner.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ public function run(Host $host, string $command, RunParams $params): string
3636
{
3737
$this->logger->command($host, 'run', $command);
3838

39-
$terminalOutput = $this->logger->callback($host, $params->forceOutput);
40-
$callback = function ($type, $buffer) use ($terminalOutput) {
41-
$terminalOutput($type, $buffer);
42-
};
43-
4439
if (!empty($params->env)) {
4540
$env = env_stringify($params->env);
4641
$command = "export $env; $command";
@@ -56,6 +51,10 @@ public function run(Host $host, string $command, RunParams $params): string
5651
->setIdleTimeout($params->idleTimeout)
5752
->setWorkingDirectory($params->cwd ?? deployer_root());
5853

54+
$callback = function ($type, $buffer) use ($params, $host) {
55+
$this->logger->print($host, $buffer, $params->forceOutput);
56+
};
57+
5958
try {
6059
$process->mustRun($callback);
6160
return $process->getOutput();

src/Ssh/SshClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function run(Host $host, string $command, RunParams $params): string
6969
->setIdleTimeout($params->idleTimeout);
7070

7171
$callback = function ($type, $buffer) use ($params, $host) {
72-
$this->logger->callback($host, $params->forceOutput)($type, $buffer);
72+
$this->logger->print($host, $buffer, $params->forceOutput);
7373
};
7474

7575
try {

src/Utility/Rsync.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ function (string $value) {
106106
}
107107
return;
108108
}
109-
if ($this->output->isVerbose()) {
110-
$this->logger->printBuffer($type, $host, $buffer);
111-
}
109+
$this->logger->print($host, $buffer);
112110
};
113111

114112
$process = new Process($command);

0 commit comments

Comments
 (0)