Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea78a08

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

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

src/Application/PresenterFactory.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,16 @@ public function getPresenterClass(& $name)
9999
public function setMapping(array $mapping)
100100
{
101101
foreach ($mapping as $module => $mask) {
102-
if (!preg_match('#^\\\\?([\w\\\\]*\\\\)?(\w*\*\w*?\\\\)?([\w\\\\]*\*\w*)\z#', $mask, $m)) {
103-
throw new Nette\InvalidStateException("Invalid mapping mask '$mask'.");
102+
if (is_string($mask)) {
103+
if (!preg_match('#^\\\\?([\w\\\\]*\\\\)?(\w*\*\w*?\\\\)?([\w\\\\]*\*\w*)\z#', $mask, $m)) {
104+
throw new Nette\InvalidStateException("Invalid mapping mask '$mask'.");
105+
}
106+
$this->mapping[$module] = array($m[1], $m[2] ?: '*Module\\', $m[3]);
107+
} elseif (is_array($mask) && count($mask) === 3) {
108+
$this->mapping[$module] = array($mask[0] ? $mask[0] . '\\' : '', $mask[1] . '\\', $mask[2]);
109+
} else {
110+
throw new Nette\InvalidStateException("Invalid mapping mask for module $module.");
104111
}
105-
$this->mapping[$module] = array($m[1], $m[2] ?: '*Module\\', $m[3]);
106112
}
107113
return $this;
108114
}

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(array(
57+
'*' => array('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(array(
67+
'*' => array('', '*', '*'),
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(array(
77+
'*' => array('*', '*'),
78+
));
79+
},
80+
'Nette\InvalidStateException',
81+
'Invalid mapping mask for module *.'
82+
);

0 commit comments

Comments
 (0)