1
|
|
|
<?php |
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
|
|
|
/** @var PathInterface */ |
15
|
|
|
private $pathStart; |
16
|
|
|
|
17
|
|
|
/** @var PathInterface */ |
18
|
|
|
private $pathComposerJson; |
19
|
|
|
|
20
|
|
|
/** @var array<string> */ |
21
|
|
|
private $privateEntries; |
22
|
|
|
|
23
|
|
|
/** @var array<string> */ |
24
|
|
|
private $publicEntries; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $mode; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $version; |
31
|
|
|
|
32
|
|
|
/** @var bool */ |
33
|
|
|
private $enabledCache; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array<string> $privateEntries |
37
|
|
|
* @param array<string> $publicEntries |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
string $version, |
41
|
|
|
PathInterface $pathStart, |
42
|
|
|
PathInterface $pathComposerJson, |
43
|
|
|
array $privateEntries, |
44
|
|
|
array $publicEntries, |
45
|
|
|
string $mode = self::MODE_PUBLIC, |
46
|
|
|
bool $enabledCache = false |
47
|
|
|
) { |
48
|
|
|
$this->version = $version; |
49
|
|
|
$this->pathStart = $pathStart; |
50
|
|
|
$this->pathComposerJson = $pathComposerJson; |
51
|
|
|
$this->privateEntries = $privateEntries; |
52
|
|
|
$this->publicEntries = $publicEntries; |
53
|
|
|
$this->mode = $mode; |
54
|
|
|
$this->enabledCache = $enabledCache; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getStartPath(): PathInterface |
58
|
|
|
{ |
59
|
|
|
return $this->pathStart; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array<string> |
64
|
|
|
*/ |
65
|
|
|
public function getPrivateEntries(): array |
66
|
|
|
{ |
67
|
|
|
return $this->privateEntries; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array<string> |
72
|
|
|
*/ |
73
|
|
|
public function getPublicEntries(): array |
74
|
|
|
{ |
75
|
|
|
return $this->publicEntries; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getMode(): string |
79
|
|
|
{ |
80
|
|
|
return $this->mode; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getPathComposerJson(): PathInterface |
84
|
|
|
{ |
85
|
|
|
return $this->pathComposerJson; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
public function print(): string |
90
|
|
|
{ |
91
|
|
|
//todo: automatic dump config |
92
|
|
|
|
93
|
|
|
$prettyPrintPrivateEntries = $this->populateOutputVarFromArray($this->getPrivateEntries()); |
94
|
|
|
$prettyPrintPublicEntries = $this->populateOutputVarFromArray($this->getPublicEntries()); |
95
|
|
|
|
96
|
|
|
return |
97
|
|
|
'' . PHP_EOL . |
98
|
|
|
'|Dump config:' . PHP_EOL . |
99
|
|
|
'|> Version: ' . $this->getVersion() . PHP_EOL . |
100
|
|
|
'|> Cache: ' . ($this->enabledCache() === true ? "TRUE": "FALSE") . PHP_EOL . |
101
|
|
|
'|> Path start: ' . $this->pathStart->get() . PHP_EOL . |
102
|
|
|
'|> Composer Json path: ' . $this->pathComposerJson->get() . PHP_EOL . |
103
|
|
|
'|> Mode: ' . $this->getMode() . PHP_EOL . |
104
|
|
|
'|> Private entries: ' . $prettyPrintPrivateEntries . PHP_EOL . |
105
|
|
|
'|' . PHP_EOL . |
106
|
|
|
'|> Public entries: ' . $prettyPrintPublicEntries . PHP_EOL . |
107
|
|
|
''; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array<string> $entries |
112
|
|
|
*/ |
113
|
|
|
private function populateOutputVarFromArray(array $entries): string |
114
|
|
|
{ |
115
|
|
|
$prettyPrintNamespaceToValidate = "\n"; |
116
|
|
|
foreach ($entries as $namespace) { |
117
|
|
|
$prettyPrintNamespaceToValidate .= "| >" . $namespace; |
118
|
|
|
} |
119
|
|
|
return $prettyPrintNamespaceToValidate; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public static function loadFromFile(PathInterface $path): self |
123
|
|
|
{ |
124
|
|
|
$content = \safe\file_get_contents($path->get()); |
125
|
|
|
$arrayConfig = \safe\json_decode($content, true); |
126
|
|
|
|
127
|
|
|
$self = new self( |
128
|
|
|
$arrayConfig['version'], |
129
|
|
|
new FileSystemPath($arrayConfig['start-path']), |
130
|
|
|
new FileSystemPath($arrayConfig['composer-json-path']), |
131
|
|
|
$arrayConfig['private-entries'], |
132
|
|
|
$arrayConfig['public-entries'], |
133
|
|
|
$arrayConfig['mode'], |
134
|
|
|
$arrayConfig['cache'] |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$self->validateLoadedConfig(); |
138
|
|
|
|
139
|
|
|
return $self; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function validateLoadedConfig(): void |
143
|
|
|
{ |
144
|
|
|
//todo: adds more complex validation structure |
145
|
|
|
|
146
|
|
|
Assert::inArray($this->getMode(), [self::MODE_PUBLIC, self::MODE_MAKE_VENDOR_PRIVATE], "Mode not valid"); |
147
|
|
|
Assert::eq('0.1.0', $this->getVersion(), "Version not valid"); |
148
|
|
|
Assert::directory($this->getStartPath()->get(), "Start directory not valid"); |
149
|
|
|
Assert::directory($this->getPathComposerJson()->get(), "Composer json directory not valid"); |
150
|
|
|
Assert::boolean($this->enabledCache(), "Cache flag must be boolean"); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private function getVersion(): string |
154
|
|
|
{ |
155
|
|
|
//todo: use https://github.com/nikolaposa/version |
156
|
|
|
return $this->version; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function enabledCache(): bool |
160
|
|
|
{ |
161
|
|
|
return $this->enabledCache; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|