1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace NamespaceProtector\Config; |
4
|
|
|
|
5
|
|
|
use NamespaceProtector\Common\FileSystemPath; |
6
|
|
|
use NamespaceProtector\Common\PathInterface; |
7
|
|
|
use Webmozart\Assert\Assert; |
8
|
|
|
|
9
|
|
|
final class Config |
10
|
|
|
{ |
11
|
|
|
public const MODE_MAKE_VENDOR_PRIVATE = 'MODE_MAKE_VENDOR_PRIVATE'; |
12
|
|
|
public const MODE_PUBLIC = 'PUBLIC'; |
13
|
|
|
|
14
|
|
|
public const PLOTTER_TERMINAL = 'plotter-terminal'; |
15
|
|
|
public const PLOTTER_PNG = 'plotter-png'; |
16
|
|
|
|
17
|
|
|
/** @var PathInterface */ |
18
|
|
|
private $pathStart; |
19
|
|
|
|
20
|
|
|
/** @var PathInterface */ |
21
|
|
|
private $pathComposerJson; |
22
|
|
|
|
23
|
|
|
/** @var array<string> */ |
24
|
|
|
private $privateEntries; |
25
|
|
|
|
26
|
|
|
/** @var array<string> */ |
27
|
|
|
private $publicEntries; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $mode; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $version; |
34
|
|
|
|
35
|
|
|
/** @var bool */ |
36
|
|
|
private $enabledCache; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
private $plotter; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array<string> $privateEntries |
43
|
|
|
* @param array<string> $publicEntries |
44
|
|
|
*/ |
45
|
|
|
private function __construct( |
46
|
|
|
string $version, |
47
|
|
|
PathInterface $pathStart, |
48
|
|
|
PathInterface $pathComposerJson, |
49
|
|
|
array $privateEntries, |
50
|
|
|
array $publicEntries, |
51
|
|
|
string $mode = self::MODE_PUBLIC, |
52
|
|
|
bool $enabledCache = false, |
53
|
|
|
string $plotter = self::PLOTTER_TERMINAL |
54
|
|
|
) { |
55
|
|
|
$this->version = $version; |
56
|
|
|
$this->pathStart = $pathStart; |
57
|
|
|
$this->pathComposerJson = $pathComposerJson; |
58
|
|
|
$this->privateEntries = $privateEntries; |
59
|
|
|
$this->publicEntries = $publicEntries; |
60
|
|
|
$this->mode = $mode; |
61
|
|
|
$this->enabledCache = $enabledCache; |
62
|
|
|
$this->plotter = $plotter; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getStartPath(): PathInterface |
66
|
|
|
{ |
67
|
|
|
return $this->pathStart; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array<string> |
72
|
|
|
*/ |
73
|
|
|
public function getPrivateEntries(): array |
74
|
|
|
{ |
75
|
|
|
return $this->privateEntries; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array<string> |
80
|
|
|
*/ |
81
|
|
|
public function getPublicEntries(): array |
82
|
|
|
{ |
83
|
|
|
return $this->publicEntries; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getMode(): string |
87
|
|
|
{ |
88
|
|
|
return $this->mode; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getPlotter(): string |
92
|
|
|
{ |
93
|
|
|
return $this->plotter; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getPathComposerJson(): PathInterface |
97
|
|
|
{ |
98
|
|
|
return $this->pathComposerJson; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function print(): string |
102
|
|
|
{ |
103
|
|
|
//todo: automatic dump config |
104
|
|
|
|
105
|
|
|
$prettyPrintPrivateEntries = $this->populateOutputVarFromArray($this->getPrivateEntries()); |
106
|
|
|
$prettyPrintPublicEntries = $this->populateOutputVarFromArray($this->getPublicEntries()); |
107
|
|
|
|
108
|
|
|
return |
109
|
|
|
'' . PHP_EOL . |
110
|
|
|
'|Dump config:' . PHP_EOL . |
111
|
|
|
'|> Version: ' . $this->getVersion() . PHP_EOL . |
112
|
|
|
'|> Cache: ' . ($this->enabledCache() === true ? 'TRUE' : 'FALSE') . PHP_EOL . |
113
|
|
|
'|> Plotter: ' . $this->getPlotter() . PHP_EOL . |
114
|
|
|
'|> Path start: ' . $this->pathStart->get() . PHP_EOL . |
115
|
|
|
'|> Composer Json path: ' . $this->pathComposerJson->get() . PHP_EOL . |
116
|
|
|
'|> Mode: ' . $this->getMode() . PHP_EOL . |
117
|
|
|
'|> Private entries: ' . $prettyPrintPrivateEntries . PHP_EOL . |
118
|
|
|
'|' . PHP_EOL . |
119
|
|
|
'|> Public entries: ' . $prettyPrintPublicEntries . PHP_EOL . |
120
|
|
|
''; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array<string> $entries |
125
|
|
|
*/ |
126
|
|
|
private function populateOutputVarFromArray(array $entries): string |
127
|
|
|
{ |
128
|
|
|
$prettyPrintNamespaceToValidate = "\n"; |
129
|
|
|
foreach ($entries as $namespace) { |
130
|
|
|
$prettyPrintNamespaceToValidate .= '| >' . $namespace . \PHP_EOL; |
131
|
|
|
} |
132
|
|
|
return $prettyPrintNamespaceToValidate; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public static function loadFromFile(PathInterface $path): self |
136
|
|
|
{ |
137
|
|
|
$content = \safe\file_get_contents($path->get()); |
138
|
|
|
$arrayConfig = \safe\json_decode($content, true); |
139
|
|
|
|
140
|
|
|
$self = new self( |
141
|
|
|
$arrayConfig['version'], |
142
|
|
|
new FileSystemPath($arrayConfig['start-path'] ?? '.'), |
143
|
|
|
new FileSystemPath($arrayConfig['composer-json-path'] ?? '.'), |
144
|
|
|
$arrayConfig['private-entries'] ?? [], |
145
|
|
|
$arrayConfig['public-entries'] ?? [], |
146
|
|
|
$arrayConfig['mode'] ?? self::MODE_PUBLIC, |
147
|
|
|
$arrayConfig['cache'] ?? false, |
148
|
|
|
$arrayConfig['plotter'] ?? self::PLOTTER_TERMINAL, |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
$self->validateLoadedConfig(); |
152
|
|
|
|
153
|
|
|
return $self; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** @param array<string,string> $parameters */ |
157
|
|
|
public static function fromConfigWithOverride(self $config, array $parameters): self |
158
|
|
|
{ |
159
|
|
|
$self = new self( |
160
|
|
|
$config->getVersion(), |
161
|
|
|
$config->getStartPath(), |
162
|
|
|
$config->getPathComposerJson(), |
163
|
|
|
$config->getPrivateEntries(), |
164
|
|
|
$config->getPublicEntries(), |
165
|
|
|
$config->getMode(), |
166
|
|
|
$config->enabledCache(), |
167
|
|
|
$parameters['plotter'] ?? $config->getPlotter(), |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$self->validateLoadedConfig(); |
171
|
|
|
|
172
|
|
|
return $self; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
private function validateLoadedConfig(): void |
176
|
|
|
{ |
177
|
|
|
Assert::inArray($this->getMode(), [self::MODE_PUBLIC, self::MODE_MAKE_VENDOR_PRIVATE], 'Mode not valid'); |
178
|
|
|
Assert::eq('0.1.0', $this->getVersion(), 'Version not valid'); |
179
|
|
|
Assert::directory($this->getStartPath()->get(), 'Start directory not valid'); |
180
|
|
|
Assert::directory($this->getPathComposerJson()->get(), 'Composer json directory not valid'); |
181
|
|
|
Assert::boolean($this->enabledCache(), 'Cache flag must be boolean'); |
182
|
|
|
Assert::inArray($this->getPlotter(), [self::PLOTTER_TERMINAL, self::PLOTTER_PNG], 'Plotter not valid'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
private function getVersion(): string |
186
|
|
|
{ |
187
|
|
|
//todo: use https://github.com/nikolaposa/version |
188
|
|
|
return $this->version; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function enabledCache(): bool |
192
|
|
|
{ |
193
|
|
|
return $this->enabledCache; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|