|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiGen\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use ApiGen\Configuration\Exceptions\ConfigurationException; |
|
6
|
|
|
use ApiGen\Configuration\Theme\ThemeConfigFactory; |
|
7
|
|
|
use ApiGen\Utils\FileSystem; |
|
8
|
|
|
use ReflectionProperty; |
|
9
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11
|
|
|
|
|
12
|
|
|
final class ConfigurationOptionsResolver |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
public const ACCESS_LEVEL_PROTECTED = 'protected'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
public const ACCESS_LEVEL_PRIVATE = 'private'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public const ACCESS_LEVEL_PUBLIC = 'public'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var mixed[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private $defaults = [ |
|
33
|
|
|
// required |
|
34
|
|
|
ConfigurationOptions::SOURCE => [], |
|
35
|
|
|
ConfigurationOptions::DESTINATION => null, |
|
36
|
|
|
// file finder |
|
37
|
|
|
ConfigurationOptions::EXCLUDE => [], |
|
38
|
|
|
ConfigurationOptions::EXTENSIONS => ['php'], |
|
39
|
|
|
// template parameters |
|
40
|
|
|
ConfigurationOptions::TITLE => '', |
|
41
|
|
|
ConfigurationOptions::GOOGLE_ANALYTICS => '', |
|
42
|
|
|
// filtering generated content |
|
43
|
|
|
ConfigurationOptions::ACCESS_LEVELS => [self::ACCESS_LEVEL_PUBLIC, self::ACCESS_LEVEL_PROTECTED], |
|
44
|
|
|
ConfigurationOptions::ANNOTATION_GROUPS => [], |
|
45
|
|
|
ConfigurationOptions::BASE_URL => '', |
|
46
|
|
|
ConfigurationOptions::CONFIG => '', |
|
47
|
|
|
ConfigurationOptions::FORCE_OVERWRITE => false, |
|
48
|
|
|
ConfigurationOptions::TEMPLATE_CONFIG => null, |
|
49
|
|
|
// helpers - @todo: remove |
|
50
|
|
|
ConfigurationOptions::VISIBILITY_LEVELS => [], |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var ThemeConfigFactory |
|
55
|
|
|
*/ |
|
56
|
|
|
private $themeConfigFactory; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var OptionsResolver |
|
60
|
|
|
*/ |
|
61
|
|
|
private $resolver; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var OptionsResolverFactory |
|
65
|
|
|
*/ |
|
66
|
|
|
private $optionsResolverFactory; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var FileSystem |
|
70
|
|
|
*/ |
|
71
|
|
|
private $fileSystem; |
|
72
|
|
|
|
|
73
|
|
|
public function __construct( |
|
74
|
|
|
ThemeConfigFactory $themeConfigFactory, |
|
75
|
|
|
OptionsResolverFactory $optionsResolverFactory, |
|
76
|
|
|
FileSystem $fileSystem |
|
77
|
|
|
) { |
|
78
|
|
|
$this->themeConfigFactory = $themeConfigFactory; |
|
79
|
|
|
$this->optionsResolverFactory = $optionsResolverFactory; |
|
80
|
|
|
$this->fileSystem = $fileSystem; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param mixed[] $options |
|
85
|
|
|
* @return mixed[] |
|
86
|
|
|
*/ |
|
87
|
|
|
public function resolve(array $options): array |
|
88
|
|
|
{ |
|
89
|
|
|
$this->resolver = $this->optionsResolverFactory->create(); |
|
90
|
|
|
$this->setDefaults(); |
|
91
|
|
|
$this->setRequired(); |
|
92
|
|
|
$this->setAllowedValues(); |
|
93
|
|
|
$this->setNormalizers(); |
|
94
|
|
|
|
|
95
|
|
|
return $this->resolver->resolve($options); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function setDefaults(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->resolver->setDefaults($this->defaults); |
|
101
|
|
|
$this->resolver->setDefaults([ |
|
102
|
|
|
ConfigurationOptions::VISIBILITY_LEVELS => function (Options $options) { |
|
103
|
|
|
return $this->getAccessLevelForReflections($options[ConfigurationOptions::ACCESS_LEVELS]); |
|
104
|
|
|
}, |
|
105
|
|
|
ConfigurationOptions::TEMPLATE => function (Options $options) { |
|
106
|
|
|
$config = $options[ConfigurationOptions::TEMPLATE_CONFIG]; |
|
107
|
|
|
if ($config === '') { |
|
108
|
|
|
$config = getcwd() . '/packages/ThemeDefault/src/config.neon'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->themeConfigFactory->create($config) |
|
112
|
|
|
->getOptions(); |
|
113
|
|
|
} |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param mixed[] $options |
|
119
|
|
|
*/ |
|
120
|
|
|
private function getAccessLevelForReflections(array $options): int |
|
121
|
|
|
{ |
|
122
|
|
|
$accessLevel = null; |
|
123
|
|
|
|
|
124
|
|
|
if (in_array(self::ACCESS_LEVEL_PUBLIC, $options)) { |
|
125
|
|
|
$accessLevel |= ReflectionProperty::IS_PUBLIC; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (in_array(self::ACCESS_LEVEL_PROTECTED, $options)) { |
|
129
|
|
|
$accessLevel |= ReflectionProperty::IS_PROTECTED; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (in_array(self::ACCESS_LEVEL_PRIVATE, $options)) { |
|
133
|
|
|
$accessLevel |= ReflectionProperty::IS_PRIVATE; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $accessLevel; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
private function setRequired(): void |
|
140
|
|
|
{ |
|
141
|
|
|
$this->resolver->setRequired([ConfigurationOptions::SOURCE, ConfigurationOptions::DESTINATION]); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
private function setAllowedValues(): void |
|
145
|
|
|
{ |
|
146
|
|
|
$this->resolver->addAllowedValues(ConfigurationOptions::DESTINATION, function ($destination) { |
|
147
|
|
|
return $this->allowedValuesForDestination($destination); |
|
148
|
|
|
}); |
|
149
|
|
|
|
|
150
|
|
|
$this->resolver->addAllowedValues(ConfigurationOptions::SOURCE, function ($source) { |
|
151
|
|
|
return $this->allowedValuesForSource($source); |
|
152
|
|
|
}); |
|
153
|
|
|
|
|
154
|
|
|
$this->resolver->addAllowedValues(ConfigurationOptions::TEMPLATE_CONFIG, function ($value) { |
|
155
|
|
|
if ($value && ! is_file($value)) { |
|
156
|
|
|
throw new ConfigurationException(sprintf( |
|
157
|
|
|
'Template config "%s" was not found.', $value |
|
158
|
|
|
)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return true; |
|
162
|
|
|
}); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
private function setNormalizers(): void |
|
166
|
|
|
{ |
|
167
|
|
|
$this->resolver->setNormalizer(ConfigurationOptions::ANNOTATION_GROUPS, function (Options $options, $value) { |
|
168
|
|
|
if ($value === '') { |
|
169
|
|
|
return []; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $value; |
|
173
|
|
|
}); |
|
174
|
|
|
|
|
175
|
|
|
$this->resolver->setNormalizer(ConfigurationOptions::DESTINATION, function (Options $options, $value) { |
|
176
|
|
|
return $this->fileSystem->getAbsolutePath($value); |
|
177
|
|
|
}); |
|
178
|
|
|
|
|
179
|
|
|
$this->resolver->setNormalizer(ConfigurationOptions::BASE_URL, function (Options $options, $value) { |
|
180
|
|
|
return rtrim((string) $value, '/'); |
|
181
|
|
|
}); |
|
182
|
|
|
|
|
183
|
|
|
$this->resolver->setNormalizer(ConfigurationOptions::SOURCE, function (Options $options, $value) { |
|
184
|
|
|
if (! is_array($value)) { |
|
185
|
|
|
$value = [$value]; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
foreach ($value as $key => $source) { |
|
189
|
|
|
$value[$key] = $this->fileSystem->getAbsolutePath($source); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $value; |
|
193
|
|
|
}); |
|
194
|
|
|
|
|
195
|
|
|
$this->resolver->setNormalizer(ConfigurationOptions::TEMPLATE_CONFIG, function (Options $options, $value) { |
|
196
|
|
|
if ($value === null) { |
|
197
|
|
|
return ''; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return $this->fileSystem->getAbsolutePath($value); |
|
201
|
|
|
}); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
private function allowedValuesForDestination(?string $destination): bool |
|
205
|
|
|
{ |
|
206
|
|
|
if (! $destination) { |
|
207
|
|
|
throw new ConfigurationException( |
|
208
|
|
|
'Destination is not set. Use "--destination <directory>" or config to set it.' |
|
209
|
|
|
); |
|
210
|
|
|
} elseif (! is_dir($destination)) { |
|
211
|
|
|
mkdir($destination, 0755, true); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
if (! is_writable($destination)) { |
|
215
|
|
|
throw new ConfigurationException(sprintf( |
|
216
|
|
|
'Destination "%s" is not writable.', |
|
217
|
|
|
$destination |
|
218
|
|
|
)); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return true; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param string[] $source |
|
226
|
|
|
*/ |
|
227
|
|
|
private function allowedValuesForSource(array $source): bool |
|
228
|
|
|
{ |
|
229
|
|
|
foreach ($source as $singleSource) { |
|
230
|
|
|
$this->ensureSourceExists($singleSource); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return true; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
private function ensureSourceExists(string $singleSource): void |
|
237
|
|
|
{ |
|
238
|
|
|
if (! file_exists($singleSource)) { |
|
239
|
|
|
throw new ConfigurationException(sprintf( |
|
240
|
|
|
'Source "%s" does not exist', |
|
241
|
|
|
$singleSource |
|
242
|
|
|
)); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|