-
-
Notifications
You must be signed in to change notification settings - Fork 470
/
Copy pathConfig.php
387 lines (342 loc) · 11.2 KB
/
Config.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
* This file is part of the Zephir.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Zephir;
use ArrayAccess;
use JsonSerializable;
use ReturnTypeWillChange;
use function array_key_exists;
use function array_values;
use function count;
use function current;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function is_array;
use function json_decode;
use function json_encode;
use function json_last_error;
use function key;
use function preg_match;
use const JSON_ERROR_CTRL_CHAR;
use const JSON_ERROR_DEPTH;
use const JSON_ERROR_NONE;
use const JSON_ERROR_STATE_MISMATCH;
use const JSON_ERROR_SYNTAX;
use const JSON_ERROR_UTF8;
use const JSON_PRETTY_PRINT;
/**
* Manages compiler global configuration.
*/
class Config implements ArrayAccess, JsonSerializable
{
/**
* Is config changed?
*/
protected bool $changed = false;
/**
* Default configuration for project.
*/
private array $container = [
'stubs' => [
'path' => 'ide/%version%/%namespace%/',
'stubs-run-after-generate' => false,
'banner' => '',
],
'api' => [
'path' => 'doc/%version%',
'theme' => [
'name' => 'zephir',
'options' => [
'github' => null,
'analytics' => null,
'main_color' => '#3E6496',
'link_color' => '#3E6496',
'link_hover_color' => '#5F9AE7',
],
],
],
'warnings' => [
'unused-variable' => true,
'unused-variable-external' => false,
'possible-wrong-parameter' => true,
'possible-wrong-parameter-undefined' => false,
'nonexistent-function' => true,
'nonexistent-class' => true,
'non-valid-isset' => true,
'non-array-update' => true,
'non-valid-objectupdate' => true,
'non-valid-fetch' => true,
'invalid-array-index' => true,
'non-array-append' => true,
'invalid-return-type' => true,
'unreachable-code' => true,
'nonexistent-constant' => true,
'not-supported-magic-constant' => true,
'non-valid-decrement' => true,
'non-valid-increment' => true,
'non-valid-clone' => true,
'non-valid-new' => true,
'non-array-access' => true,
'invalid-reference' => true,
'invalid-typeof-comparison' => true,
'conditional-initialization' => true,
],
'optimizations' => [
'static-type-inference' => true,
'static-type-inference-second-pass' => true,
'local-context-pass' => true,
'constant-folding' => true,
'static-constant-class-folding' => true,
'call-gatherer-pass' => true,
'check-invalid-reads' => false,
'internal-call-transformation' => false,
],
'extra' => [
'indent' => 'spaces',
'export-classes' => false,
],
'namespace' => '',
'name' => '',
'description' => '',
'author' => 'Phalcon Team',
'version' => '0.0.1',
'verbose' => false,
'requires' => [
'extensions' => [],
],
];
/**
* @throws Exception
*/
public function __construct()
{
$this->populate();
}
/**
* Returns JSON representation of the project config.
*/
public function __toString()
{
return (string)json_encode($this, JSON_PRETTY_PRINT);
}
/**
* Writes the configuration if it has been changed.
*/
public function dumpToFile(): void
{
file_put_contents('config.json', $this);
}
/**
* Factory method to create a Config instance from the $_SERVER['argv'].
*/
public static function fromServer(): self
{
$config = new self();
/**
* Change configurations flags
*/
if ($_SERVER['argc'] >= 2) {
$argv = $_SERVER['argv'];
for ($i = 1; $i < $_SERVER['argc']; ++$i) {
$parameter = $argv[$i];
if (preg_match('/^-fno-([a-z0-9\-]+)$/', $parameter, $matches)) {
$config->set($matches[1], false, 'optimizations');
unset($argv[$i]);
continue;
}
if (preg_match('/^-f([a-z0-9\-]+)$/', $parameter, $matches)) {
$config->set($matches[1], true, 'optimizations');
unset($argv[$i]);
continue;
}
if (preg_match('/^-W([a-z0-9\-]+)$/', $parameter, $matches)) {
$config->set($matches[1], false, 'warnings');
unset($argv[$i]);
continue;
}
if (preg_match('/^-w([a-z0-9\-]+)$/', $parameter, $matches)) {
$config->set($matches[1], true, 'warnings');
unset($argv[$i]);
continue;
}
if (preg_match('/^--([a-z0-9\-]+)$/', $parameter, $matches)) {
// Only known options
if (null !== $config->get($matches[1], 'extra')) {
$config->set($matches[1], true, 'extra');
unset($argv[$i]);
}
continue;
}
if (preg_match('/^--([a-z0-9\-]+)=(.*)$/', $parameter, $matches)) {
// Only known options
if (null !== $config->get($matches[1], 'extra')) {
$config->set($matches[1], $matches[2], 'extra');
unset($argv[$i]);
}
continue;
}
switch ($parameter) {
case '-q':
case '--quiet':
$config->set('silent', true);
break;
case '-v':
case '--verbose':
$config->set('verbose', true);
break;
case '-V':
$config->set('verbose', false);
break;
default:
break;
}
}
$_SERVER['argv'] = array_values($argv);
$_SERVER['argc'] = count($argv);
}
return $config;
}
/**
* Retrieves a configuration setting.
*
* @param mixed $key
* @param mixed $namespace
*
* @return mixed|null
*/
public function get($key, $namespace = null)
{
return null !== $namespace ? $this->offsetGet([$namespace => $key]) : $this->offsetGet($key);
}
/**
* Specify data which should be serialized to JSON.
*/
public function jsonSerialize(): array
{
return $this->container;
}
/**
* Allows to check whether a $key is defined.
*
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset): bool
{
return isset($this->container[$offset]) || array_key_exists($offset, $this->container);
}
/**
* Gets a $key from the internal container.
*
* @param mixed $offset
*
* @return mixed|null
*/
#[ReturnTypeWillChange]
public function offsetGet($offset)
{
if (!is_array($offset)) {
return $this->offsetExists($offset) ? $this->container[$offset] : null;
}
$namespace = key($offset);
$offset = current($offset);
if (!$this->offsetExists($namespace) || !is_array($this->container[$namespace])) {
return null;
}
if (isset($this->container[$namespace][$offset]) || array_key_exists($offset, $this->container[$namespace])) {
return $this->container[$namespace][$offset];
}
return null;
}
/**
* Sets a configuration value.
*
* @param mixed $offset
* @param mixed $value
*/
#[ReturnTypeWillChange]
public function offsetSet($offset, $value): void
{
if (!is_array($offset)) {
$this->container[$offset] = $value;
return;
}
$namespace = key($offset);
$offset = current($offset);
if (!array_key_exists($namespace, $this->container)) {
$this->container[$namespace] = [];
}
$this->container[$namespace][$offset] = $value;
}
/**
* Unsets a $key from internal container.
*
* @param mixed $offset
*
* @deprecated
*
*/
#[ReturnTypeWillChange]
public function offsetUnset($offset): void
{
unset($this->container[$offset]);
}
/**
* Changes a configuration setting.
*
* @param mixed $key
* @param mixed $value
* @param mixed $namespace
*/
public function set($key, $value, $namespace = null): void
{
null !== $namespace ? $this->offsetSet([$namespace => $key], $value) : $this->offsetSet($key, $value);
}
/**
* Populate project configuration.
*
* @throws Exception
*/
protected function populate(): void
{
if (!file_exists('config.json')) {
return;
}
$config = json_decode(file_get_contents('config.json'), true);
$message = 'The config.json file is invalid';
switch (json_last_error()) {
case JSON_ERROR_NONE:
foreach ($config as $key => $configSection) {
$this->offsetSet($key, $configSection);
}
return;
case JSON_ERROR_DEPTH:
$message = "$message: Maximum stack depth exceeded";
break;
case JSON_ERROR_STATE_MISMATCH:
$message = "$message: Underflow or the modes mismatch";
break;
case JSON_ERROR_CTRL_CHAR:
$message = "$message: Unexpected control character found";
break;
case JSON_ERROR_SYNTAX:
$message = "$message: Syntax error, malformed JSON";
break;
case JSON_ERROR_UTF8:
$message = "$message: Malformed UTF-8 characters, possibly incorrectly encoded";
break;
default:
break;
}
throw new Exception($message);
}
}