-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathNeonAdapter.php
More file actions
157 lines (137 loc) · 3.89 KB
/
NeonAdapter.php
File metadata and controls
157 lines (137 loc) · 3.89 KB
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
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\DI\Config\Adapters;
use Nette;
use Nette\DI\Config\Helpers;
use Nette\DI\Definitions\Reference;
use Nette\DI\Definitions\Statement;
use Nette\Neon;
/**
* Reading and generating NEON files.
*/
final class NeonAdapter implements Nette\DI\Config\Adapter
{
use Nette\SmartObject;
private const PREVENT_MERGING_SUFFIX = '!';
/**
* Reads configuration from NEON file.
*/
public function load(string $file): array
{
$input = Nette\Utils\FileSystem::read($file);
if (substr($input, 0, 3) === "\u{FEFF}") { // BOM
$input = substr($input, 3);
}
$decoder = new Neon\Decoder;
$node = $decoder->parseToNode($input);
$traverser = new Neon\Traverser;
$node = $traverser->traverse($node, [$this, 'removeUnderscoreVisitor']);
return $this->process((array) $node->toValue());
}
/** @throws Nette\InvalidStateException */
public function process(array $arr): array
{
$res = [];
foreach ($arr as $key => $val) {
if (is_string($key) && substr($key, -1) === self::PREVENT_MERGING_SUFFIX) {
if (!is_array($val) && $val !== null) {
throw new Nette\DI\InvalidConfigurationException(sprintf(
"Replacing operator is available only for arrays, item '%s' is not array.",
$key
));
}
$key = substr($key, 0, -1);
$val[Helpers::PREVENT_MERGING] = true;
}
if (is_array($val)) {
$val = $this->process($val);
} elseif ($val instanceof Neon\Entity) {
if ($val->value === Neon\Neon::CHAIN) {
$tmp = null;
foreach ($this->process($val->attributes) as $st) {
$tmp = new Statement(
$tmp === null ? $st->getEntity() : [$tmp, ltrim(implode('::', (array) $st->getEntity()), ':')],
$st->arguments
);
}
$val = $tmp;
} else {
$tmp = $this->process([$val->value]);
if (is_string($tmp[0]) && strpos($tmp[0], '?') !== false) {
throw new Nette\DI\InvalidConfigurationException('Operator ? is deprecated in config file.');
}
$val = new Statement($tmp[0], $this->process($val->attributes));
}
}
$res[$key] = $val;
}
return $res;
}
/**
* Generates configuration in NEON format.
*/
public function dump(array $data): string
{
array_walk_recursive(
$data,
function (&$val): void {
if ($val instanceof Statement) {
$val = self::statementToEntity($val);
}
}
);
return "# generated by Nette\n\n" . Neon\Neon::encode($data, Neon\Neon::BLOCK);
}
private static function statementToEntity(Statement $val): Neon\Entity
{
array_walk_recursive(
$val->arguments,
function (&$val): void {
if ($val instanceof Statement) {
$val = self::statementToEntity($val);
} elseif ($val instanceof Reference) {
$val = '@' . $val->getValue();
}
}
);
$entity = $val->getEntity();
if ($entity instanceof Reference) {
$entity = '@' . $entity->getValue();
} elseif (is_array($entity)) {
if ($entity[0] instanceof Statement) {
return new Neon\Entity(
Neon\Neon::CHAIN,
[
self::statementToEntity($entity[0]),
new Neon\Entity('::' . $entity[1], $val->arguments),
]
);
} elseif ($entity[0] instanceof Reference) {
$entity = '@' . $entity[0]->getValue() . '::' . $entity[1];
} elseif (is_string($entity[0])) {
$entity = $entity[0] . '::' . $entity[1];
}
}
return new Neon\Entity($entity, $val->arguments);
}
public function removeUnderscoreVisitor(Neon\Node $node)
{
if (!$node instanceof Neon\Node\EntityNode) {
return;
}
$index = false;
foreach ($node->attributes as $i => $attr) {
if ($index) {
$attr->key = $attr->key ?? new Neon\Node\LiteralNode((string) $i);
}
if ($attr->value instanceof Neon\Node\LiteralNode && $attr->value->value === '_') {
unset($node->attributes[$i]);
$index = true;
}
}
}
}