1
|
|
|
<?php |
2
|
|
|
namespace CfdiUtils\OpenSSL; |
3
|
|
|
|
4
|
|
|
use CfdiUtils\Utils\Internal\ShellExec; |
5
|
|
|
use CfdiUtils\Utils\Internal\ShellExecTemplate; |
6
|
|
|
|
7
|
|
|
class Caller |
8
|
|
|
{ |
9
|
|
|
const DEFAULT_OPENSSL_EXECUTABLE = 'openssl'; |
10
|
|
|
|
11
|
|
|
/** @var string */ |
12
|
|
|
private $executable; |
13
|
|
|
|
14
|
100 |
|
public function __construct(string $executable = '') |
15
|
|
|
{ |
16
|
100 |
|
$this->executable = $executable ?: static::DEFAULT_OPENSSL_EXECUTABLE; |
17
|
100 |
|
} |
18
|
|
|
|
19
|
17 |
|
public function getExecutable(): string |
20
|
|
|
{ |
21
|
17 |
|
return $this->executable; |
22
|
|
|
} |
23
|
|
|
|
24
|
13 |
|
public function call(string $template, array $arguments, array $environment = []): CallResponse |
25
|
|
|
{ |
26
|
|
|
try { |
27
|
|
|
// build command for shellExec |
28
|
13 |
|
array_unshift($arguments, $this->getExecutable()); |
29
|
13 |
|
$command = ($this->createShellExecTemplate())->create('? ' . $template, $arguments); |
30
|
|
|
|
31
|
|
|
// create ShellExec |
32
|
13 |
|
$shellExec = $this->createShellExec($command, $environment); |
33
|
2 |
|
} catch (\Throwable $exception) { |
34
|
2 |
|
throw new OpenSSLException('Unable to build command', 0, $exception); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// execute ShellExec |
38
|
11 |
|
$execution = $shellExec->run(); |
39
|
|
|
|
40
|
|
|
// build response |
41
|
11 |
|
$callResponse = new CallResponse( |
42
|
11 |
|
$execution->commandLine(), |
43
|
11 |
|
$execution->output(), |
44
|
11 |
|
$execution->errors(), |
45
|
11 |
|
$execution->exitStatus() |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
// eval response |
49
|
11 |
|
if ($callResponse->exitStatus() !== 0) { |
50
|
2 |
|
throw new OpenSSLCallerException($callResponse); |
51
|
|
|
} |
52
|
|
|
|
53
|
9 |
|
return $callResponse; |
54
|
|
|
} |
55
|
|
|
|
56
|
11 |
|
protected function createShellExec(array $command, array $environment): ShellExec |
57
|
|
|
{ |
58
|
11 |
|
return new ShellExec($command, $environment); |
59
|
|
|
} |
60
|
|
|
|
61
|
13 |
|
protected function createShellExecTemplate(): ShellExecTemplate |
62
|
|
|
{ |
63
|
13 |
|
return new ShellExecTemplate(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|