Skip to content

Commit 2371723

Browse files
committed
Implemented ShellAdapter
1 parent 0cd663b commit 2371723

3 files changed

Lines changed: 94 additions & 23 deletions

File tree

src/Adapter/Shell/ShellAdapter.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Sal\Seven\Adapter\Shell;
4+
5+
use Psr\Log\LoggerInterface;
6+
use Psr\Log\NullLogger;
7+
use Sal\Seven\Model\CommandResult;
8+
use Symfony\Component\Process\Exception\ProcessTimedOutException;
9+
use Symfony\Component\Process\Process;
10+
11+
class ShellAdapter implements ShellAdapterInterface
12+
{
13+
private LoggerInterface $logger;
14+
15+
public function __construct(
16+
?LoggerInterface $logger = null,
17+
) {
18+
$this->logger = $logger ?? new NullLogger();
19+
}
20+
21+
public function setLogger(LoggerInterface $logger): void
22+
{
23+
$this->logger = $logger;
24+
}
25+
26+
public function getLogger(): LoggerInterface
27+
{
28+
return $this->logger;
29+
}
30+
31+
/**
32+
* Runs a command on the local shell providing $pipedInput in command STDIN.
33+
* The process will be killed when $timeout seconds are reached.
34+
* If the timeout is null, then no timeout is set to the process.
35+
*
36+
* @param mixed[] $command
37+
*
38+
* @return CommandResult the command result
39+
*
40+
* @throws \RuntimeException
41+
* @throws ProcessTimedOutException
42+
*/
43+
public function runCommand(
44+
array $command,
45+
?string $pipedInput = null,
46+
?int $timeout = null,
47+
?\Closure $outCallback = null,
48+
): CommandResult {
49+
$proc = new Process($command);
50+
51+
$this->logger->debug($proc->getCommandLine());
52+
53+
if (null !== $pipedInput) {
54+
$proc->setInput($pipedInput);
55+
}
56+
57+
$proc->setTimeout($timeout);
58+
$code = $proc->run($outCallback);
59+
60+
return new CommandResult(
61+
$code,
62+
$proc->getOutput(),
63+
$proc->getErrorOutput(),
64+
$proc->getCommandLine(),
65+
);
66+
}
67+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Sal\Seven\Adapter\Shell;
4+
5+
use Psr\Log\LoggerAwareInterface;
6+
use Sal\Seven\Model\CommandResult;
7+
use Symfony\Component\Process\Exception\ProcessTimedOutException;
8+
9+
interface ShellAdapterInterface extends LoggerAwareInterface
10+
{
11+
/**
12+
* @param mixed[] $command
13+
*
14+
* @return CommandResult the command result
15+
*
16+
* @throws \RuntimeException
17+
* @throws ProcessTimedOutException
18+
*/
19+
public function runCommand(
20+
array $command,
21+
?string $pipedInput = null,
22+
?int $timeout = null,
23+
?\Closure $outCallback = null,
24+
): CommandResult;
25+
}

src/Adapter/Ssh/SshAdapterInterface.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace Sal\Seven\Adapter\Ssh;
44

5-
use Psr\Log\LoggerAwareInterface;
6-
use Sal\Seven\Model\CommandResult;
5+
use Sal\Seven\Adapter\Shell\ShellAdapterInterface;
76
use Sal\Seven\Model\File;
87
use Symfony\Component\Process\Exception\ProcessTimedOutException;
98

109
/**
1110
* @author Luca Saladino <sal65535@protonmail.com>
1211
*/
13-
interface SshAdapterInterface extends LoggerAwareInterface
12+
interface SshAdapterInterface extends ShellAdapterInterface
1413
{
1514
public function setHost(string $host): void;
1615

@@ -26,26 +25,6 @@ public function getTimeout(): ?int;
2625

2726
public function waitForSshLogin(): void;
2827

29-
/**
30-
* Runs a command via SSH providing $pipedInput in command STDIN.
31-
* The process will be killed when $timeout seconds are reached.
32-
* If the timeout is null, then no timeout is set to the process.
33-
* Executes: ssh -o <option> user@host $command | $pipedInput.
34-
*
35-
* @param mixed[] $command
36-
*
37-
* @return CommandResult the command result
38-
*
39-
* @throws \RuntimeException
40-
* @throws ProcessTimedOutException
41-
*/
42-
public function runCommand(
43-
array $command,
44-
?string $pipedInput = null,
45-
?int $timeout = null,
46-
?\Closure $outCallback = null,
47-
): CommandResult;
48-
4928
/**
5029
* Uploads a file via SCP killing the process when $timeout seconds are reached.
5130
* If the timeout is null, then no timeout is set to the upload process.

0 commit comments

Comments
 (0)