Skip to content

Commit 07bce43

Browse files
committed
Container: shows suggestions for missing components
1 parent 533063e commit 07bce43

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
],
1616
"require": {
1717
"php": ">=5.3.1",
18-
"nette/utils": "~2.2"
18+
"nette/utils": "^2.3.5"
1919
},
2020
"require-dev": {
2121
"nette/tester": "~1.3"

src/ComponentModel/Container.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ public function getComponent($name, $need = TRUE)
159159
}
160160

161161
} elseif ($need) {
162-
throw new Nette\InvalidArgumentException("Component with name '$name' does not exist.");
162+
$hint = Nette\Utils\ObjectMixin::getSuggestion(array_merge(
163+
array_keys($this->components),
164+
array_map('lcfirst', preg_filter('#^createComponent([A-Z0-9].*)#', '$1', get_class_methods($this)))
165+
), $name);
166+
throw new Nette\InvalidArgumentException("Component with name '$name' does not exist" . ($hint ? ", did you mean '$hint'?" : '.'));
163167
}
164168
}
165169

tests/ComponentModel/Container.factory.addComponent.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ Assert::same('b', $a->getComponent('b')->getName());
2828

2929
Assert::exception(function () use ($a) {
3030
$a->getComponent('B')->getName();
31-
}, 'InvalidArgumentException', "Component with name 'B' does not exist.");
31+
}, 'InvalidArgumentException', "Component with name 'B' does not exist, did you mean 'b'?");

tests/ComponentModel/Container.factory.addComponentWithreturn.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ Assert::same('b', $a->getComponent('b')->getName());
2929

3030
Assert::exception(function () use ($a) {
3131
$a->getComponent('B')->getName();
32-
}, 'InvalidArgumentException', "Component with name 'B' does not exist.");
32+
}, 'InvalidArgumentException', "Component with name 'B' does not exist, did you mean 'b'?");

tests/ComponentModel/Container.factory.return.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Assert::count(1, $a->getComponents());
3131

3232
Assert::exception(function () use ($a) {
3333
$a->getComponent('B')->getName();
34-
}, 'InvalidArgumentException', "Component with name 'B' does not exist.");
34+
}, 'InvalidArgumentException', "Component with name 'B' does not exist, did you mean 'b'?");
3535

3636

3737
$a->removeComponent($b);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\ComponentModel\Container suggestions.
5+
*/
6+
7+
use Nette\ComponentModel\Container;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
class TestContainer extends Container
15+
{
16+
17+
public function createComponentPublic()
18+
{
19+
}
20+
21+
public static function createComponentPublicStatic()
22+
{
23+
}
24+
25+
protected function createComponentProtected()
26+
{
27+
}
28+
29+
private function createComponentPrivate()
30+
{
31+
}
32+
33+
}
34+
35+
36+
$cont = new TestContainer;
37+
$cont->addComponent(new TestContainer, 'form');
38+
39+
Assert::exception(function () use ($cont) {
40+
$comp = $cont->getComponent('from');
41+
}, 'Nette\InvalidArgumentException', "Component with name 'from' does not exist, did you mean 'form'?");
42+
43+
Assert::exception(function () use ($cont) {
44+
$comp = $cont->getComponent('Public');
45+
}, 'Nette\InvalidArgumentException', "Component with name 'Public' does not exist, did you mean 'public'?");
46+
47+
Assert::exception(function () use ($cont) {
48+
$comp = $cont->getComponent('PublicStatic');
49+
}, 'Nette\InvalidArgumentException', "Component with name 'PublicStatic' does not exist, did you mean 'publicStatic'?");
50+
51+
Assert::exception(function () use ($cont) {
52+
$comp = $cont->getComponent('Protected');
53+
}, 'Nette\InvalidArgumentException', "Component with name 'Protected' does not exist, did you mean 'protected'?");
54+
55+
Assert::exception(function () use ($cont) { // suggest only non-private methods
56+
$comp = $cont->getComponent('Private');
57+
}, 'Nette\InvalidArgumentException', "Component with name 'Private' does not exist.");

0 commit comments

Comments
 (0)