Skip to content

Commit

Permalink
The _ are removed using Neon visitor [Closes #264]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 26, 2021
1 parent 4cf5bb3 commit 7d7a710
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
28 changes: 27 additions & 1 deletion src/DI/Config/Adapters/NeonAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ final class NeonAdapter implements Nette\DI\Config\Adapter
*/
public function load(string $file): array
{
return $this->process((array) Neon\Neon::decode(Nette\Utils\FileSystem::read($file)));
$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());
}


Expand Down Expand Up @@ -128,4 +136,22 @@ function (&$val): void {
}
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;
}
}
}
}
4 changes: 2 additions & 2 deletions src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ public static function escape($value)


/**
* Removes ... / _ and process constants recursively.
* Removes ... and process constants recursively.
*/
public static function filterArguments(array $args): array
{
foreach ($args as $k => $v) {
if ($v === '...' || $v === '_') {
if ($v === '...') {
unset($args[$k]);
} elseif (
PHP_VERSION_ID >= 80100
Expand Down
4 changes: 2 additions & 2 deletions tests/DI/Helpers.filterArguments.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ require __DIR__ . '/../bootstrap.php';
Assert::same([], Helpers::filterArguments([]));

Assert::same(
['a', 'b', 4 => ['c'], [1 => 'd']],
Helpers::filterArguments(['a', 'b', '...', '_', ['c', '...'], ['...', 'd']])
['a', 'b', 3 => ['c'], [1 => 'd']],
Helpers::filterArguments(['a', 'b', '...', ['c', '...'], ['...', 'd']])
);

Assert::same(
Expand Down

0 comments on commit 7d7a710

Please sign in to comment.