Skip to content

Commit

Permalink
Compiler, CompilerExtension: shows suggestions for unexpected config …
Browse files Browse the repository at this point in the history
…items and extensions
  • Loading branch information
dg committed Nov 13, 2015
1 parent 274cab5 commit 333d42f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/DI/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ public function processExtensions()
$extra = implode("', '", array_keys($extra));
throw new Nette\DeprecatedException("Extensions '$extra' were added while container was being compiled.");

} elseif ($extra = array_diff_key($this->config, self::$reserved, $this->extensions)) {
$extra = implode("', '", array_keys($extra));
throw new Nette\InvalidStateException("Found sections '$extra' in configuration, but corresponding extensions are missing.");
} elseif ($extra = key(array_diff_key($this->config, self::$reserved, $this->extensions))) {
$hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys(self::$reserved + $this->extensions), $extra);
throw new Nette\InvalidStateException(
"Found section '$extra' in configuration, but corresponding extension is missing"
. ($hint ? ", did you mean '$hint'?" : '.')
);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/DI/CompilerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public function validateConfig(array $expected, array $config = NULL, $name = NU
}
if ($extra = array_diff_key((array) $config, $expected)) {
$name = $name ?: $this->name;
$extra = implode(", $name.", array_keys($extra));
throw new Nette\InvalidStateException("Unknown configuration option $name.$extra.");
$hint = Nette\Utils\ObjectMixin::getSuggestion(array_keys($expected), key($extra));
$extra = $hint ? key($extra) : implode(", $name.", array_keys($extra));
throw new Nette\InvalidStateException("Unknown configuration option $name.$extra" . ($hint ? ", did you mean $name.$hint?" : '.'));
}
return Config\Helpers::merge($config, $expected);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/DI/CompilerExtension.validateConfig.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ test(function () {
Assert::exception(function () {
$ext = new MyExtension;
$ext->validateConfig(['a' => 1, 'b' => 1], ['c' => 1]);
}, Nette\InvalidStateException::class, 'Unknown configuration option my.c.');
}, Nette\InvalidStateException::class, 'Unknown configuration option my.c, did you mean my.a?');

Assert::exception(function () {
$ext = new MyExtension;
$ext->validateConfig(['a' => 1, 'b' => 1], ['c' => 1, 'd' => 1], 'name');
}, Nette\InvalidStateException::class, 'Unknown configuration option name.c, name.d.');
}, Nette\InvalidStateException::class, 'Unknown configuration option name.c, did you mean name.a?');

Assert::exception(function () {
$ext = new MyExtension;
$ext->setConfig(['c' => 1, 'd' => 1]);
$ext->validateConfig(['a' => 1, 'b' => 1]);
}, Nette\InvalidStateException::class, 'Unknown configuration option my.c, my.d.');
}, Nette\InvalidStateException::class, 'Unknown configuration option my.c, did you mean my.a?');

0 comments on commit 333d42f

Please sign in to comment.