-
Notifications
You must be signed in to change notification settings - Fork 149
/
SampleManager.php
113 lines (92 loc) · 3.15 KB
/
SampleManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace OpenStack\Sample;
use RuntimeException;
class SampleManager
{
protected $basePath;
protected $paths = [];
protected $verbosity;
public function __construct($basePath, $verbosity)
{
$this->basePath = $basePath;
$this->verbosity = $verbosity;
}
public function deletePaths(): void
{
if (!empty($this->paths)) {
foreach ($this->paths as $path) {
unlink($path);
}
}
}
protected function getGlobalReplacements(): array
{
return [
'{userId}' => getenv('OS_USER_ID'),
'{username}' => getenv('OS_USERNAME'),
'{password}' => getenv('OS_PASSWORD'),
'{domainId}' => getenv('OS_DOMAIN_ID'),
'{authUrl}' => getenv('OS_AUTH_URL'),
'{tenantId}' => getenv('OS_TENANT_ID'),
'{region}' => getenv('OS_REGION'),
'{projectId}' => getenv('OS_PROJECT_ID'),
'{projectName}' => getenv('OS_PROJECT_NAME'),
];
}
public function getConnectionStr(): string
{
return str_replace('$openstack =', 'return', $this->getConnectionTemplate());
}
protected function getConnectionTemplate(): string
{
if ($this->verbosity === 1) {
$subst = <<<'EOL'
use OpenStack\Sample\DefaultLogger;
use GuzzleHttp\MessageFormatter;
$options = [
'debugLog' => true,
'logger' => new DefaultLogger(),
'messageFormatter' => new MessageFormatter(),
];
$openstack = $this->getOpenStack($options);
EOL;
} elseif ($this->verbosity === 2) {
$subst = <<<'EOL'
use OpenStack\Sample\DefaultLogger;
use GuzzleHttp\MessageFormatter;
$options = [
'debugLog' => true,
'logger' => new DefaultLogger(),
'messageFormatter' => new MessageFormatter(MessageFormatter::DEBUG),
];
$openstack = $this->getOpenStack($options);
EOL;
} else {
$subst = <<<'EOL'
use OpenStack\Integration\Utils;
$openstack = $this->getOpenStack();
EOL;
}
return $subst;
}
public function write(string $path, array $replacements): string
{
$replacements = array_merge($this->getGlobalReplacements(), $replacements);
$sampleFile = rtrim($this->basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $path;
if (!file_exists($sampleFile)) {
throw new RuntimeException(sprintf("%s does not exist", $sampleFile));
}
if(!is_readable($sampleFile)) {
throw new RuntimeException(sprintf("%s is not readable", $sampleFile));
}
$content = strtr(file_get_contents($sampleFile), $replacements);
$content = str_replace("'vendor/'", "'" . dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "vendor'", $content);
$subst = $this->getConnectionTemplate();
$content = preg_replace('/\([^)]+\)/', '', $content, 1);
$content = str_replace('$openstack = new OpenStack\OpenStack;', $subst, $content);
$tmp = tempnam(sys_get_temp_dir(), 'openstack');
file_put_contents($tmp, $content);
$this->paths[] = $tmp;
return $tmp;
}
}