Skip to content

Commit fa3ba64

Browse files
committed
PresenterFactory: added possibility to configure mapping via array; thanks @achse [Closes #101][Closes #119]
1 parent 3f8f9bd commit fa3ba64

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/Application/PresenterFactory.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,16 @@ public function getPresenterClass(& $name)
9696
public function setMapping(array $mapping)
9797
{
9898
foreach ($mapping as $module => $mask) {
99-
if (!preg_match('#^\\\\?([\w\\\\]*\\\\)?(\w*\*\w*?\\\\)?([\w\\\\]*\*\w*)\z#', $mask, $m)) {
100-
throw new Nette\InvalidStateException("Invalid mapping mask '$mask'.");
99+
if (is_string($mask)) {
100+
if (!preg_match('#^\\\\?([\w\\\\]*\\\\)?(\w*\*\w*?\\\\)?([\w\\\\]*\*\w*)\z#', $mask, $m)) {
101+
throw new Nette\InvalidStateException("Invalid mapping mask '$mask'.");
102+
}
103+
$this->mapping[$module] = [$m[1], $m[2] ?: '*Module\\', $m[3]];
104+
} elseif (is_array($mask) && count($mask) === 3) {
105+
$this->mapping[$module] = [$mask[0] ? $mask[0] . '\\' : '', $mask[1] . '\\', $mask[2]];
106+
} else {
107+
throw new Nette\InvalidStateException("Invalid mapping mask for module $module.");
101108
}
102-
$this->mapping[$module] = [$m[1], $m[2] ?: '*Module\\', $m[3]];
103109
}
104110
return $this;
105111
}

tests/Application/PresenterFactory.formatPresenterClass.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,34 @@ test(function () {
4949
Assert::same('My\App\BarPresenter', $factory->formatPresenterClass('Foo3:Bar'));
5050
Assert::same('My\App\BarModule\BazPresenter', $factory->formatPresenterClass('Foo3:Bar:Baz'));
5151
});
52+
53+
54+
test(function () {
55+
$factory = new PresenterFactory;
56+
$factory->setMapping([
57+
'*' => ['App', 'Module\*', 'Presenter\*'],
58+
]);
59+
Assert::same('App\Module\Foo\Presenter\Bar', $factory->formatPresenterClass('Foo:Bar'));
60+
Assert::same('App\Module\Universe\Module\Foo\Presenter\Bar', $factory->formatPresenterClass('Universe:Foo:Bar'));
61+
});
62+
63+
64+
test(function () {
65+
$factory = new PresenterFactory;
66+
$factory->setMapping([
67+
'*' => ['', '*', '*'],
68+
]);
69+
Assert::same('Module\Foo\Bar', $factory->formatPresenterClass('Module:Foo:Bar'));
70+
});
71+
72+
73+
Assert::exception(
74+
function () {
75+
$factory = new PresenterFactory;
76+
$factory->setMapping([
77+
'*' => ['*', '*'],
78+
]);
79+
},
80+
Nette\InvalidStateException::class,
81+
'Invalid mapping mask for module *.'
82+
);

0 commit comments

Comments
 (0)