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