Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions src/Application/PresenterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,59 @@ public function getPresenterClass(& $name)

/**
* Sets mapping as pairs [module => mask]
* @param array
* @return self
*/
public function setMapping(array $mapping)
{
$prefixPattern = '\\\\?([\w\\\\]*\\\\)?';
$modulePattern = '[\w\\\\]*?\*\w*?\\\\';
$presenterPattern = '([\w\\\\]*\*\w*)';

foreach ($mapping as $module => $mask) {
if (!preg_match('#^\\\\?([\w\\\\]*\\\\)?(\w*\*\w*?\\\\)?([\w\\\\]*\*\w*)\z#', $mask, $m)) {
throw new Nette\InvalidStateException("Invalid mapping mask '$mask'.");
if (is_array($mask)) {

$prefixPart = '';
$modulePart = '';

if (count($mask) === 3) {
$prefixPart = $mask[0];
$modulePart = $mask[1];
$presenterPart = $mask[2];
} elseif (count($mask) === 2) {
if (preg_match("#^{$prefixPattern}\\z#", $mask[0])) {
$prefixPart = $mask[0];
$modulePart = '';
} elseif (preg_match("#^{$modulePattern}\\z#", $mask[0])) {
$prefixPart = '';
$modulePart = $mask[0];
}
$presenterPart = $mask[1];
} elseif (count($mask) === 1) {
$presenterPart = $mask[0];
} else {
throw new Nette\InvalidStateException("Invalid length of mask array.");
}

if (!preg_match("#^{$prefixPattern}\\z#", $prefixPart)) {
throw new Nette\InvalidStateException("Invalid prefix part of mask.");
}

if (!preg_match("#^({$modulePattern})?\\z#", $modulePart)) {
throw new Nette\InvalidStateException("Invalid module part of mask.");
}

if (!preg_match("#^{$presenterPattern}\\z#", $presenterPart)) {
throw new Nette\InvalidStateException("Invalid presenter part of mask.");
}

$this->mapping[$module] = [$prefixPart, $modulePart, $presenterPart];
} else {
if (!preg_match("#^{$prefixPattern}({$modulePattern})?{$presenterPattern}\\z#", $mask, $m)) {
throw new Nette\InvalidStateException("Invalid mapping mask '$mask'.");
}
$this->mapping[$module] = [$m[1], $m[2] ?: '*Module\\', $m[3]];
}
$this->mapping[$module] = [$m[1], $m[2] ?: '*Module\\', $m[3]];
}
return $this;
}
Expand Down
74 changes: 74 additions & 0 deletions tests/Application/PresenterFactory.formatPresenterClass.array.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* Test: Nette\Application\PresenterFactory.
*/

use Nette\Application\PresenterFactory;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test(function () {
$factory = new PresenterFactory;
$factory->setMapping([
'*' => ['App\\', 'Module\*\\', 'Presenter\*'],
]);
Assert::same('App\Module\Jupiter\Presenter\RedDwarf', $factory->formatPresenterClass('Jupiter:RedDwarf'));
Assert::same('App\Module\Universe\Module\Jupiter\Presenter\RedDwarf', $factory->formatPresenterClass('Universe:Jupiter:RedDwarf'));
});

test(function () {
$factory = new PresenterFactory;
$factory->setMapping([
'*' => ['App\\', '*\\', '*'],
]);
Assert::same('App\Universe\Jupiter\RedDwarf', $factory->formatPresenterClass('Universe:Jupiter:RedDwarf'));
});

test(function () {
$factory = new PresenterFactory;
$factory->setMapping([
'*' => ['*\\', '*'],
]);
Assert::same('Jupiter\RedDwarf', $factory->formatPresenterClass('Jupiter:RedDwarf'));
});

test(function () {
$factory = new PresenterFactory;
$factory->setMapping([
'*' => ['App\\', '*'],
]);
Assert::same('App\RedDwarf', $factory->formatPresenterClass('RedDwarf'));
});

test(function () {
$factory = new PresenterFactory;
$factory->setMapping([
'*' => ['App\\', 'Module\*Module\\', 'Presenter\*Presenter'],
]);
Assert::same(
'App\Module\JupiterModule\Presenter\RedDwarfPresenter',
$factory->formatPresenterClass('Jupiter:RedDwarf')
);
Assert::same(
'App\Module\UniverseModule\Module\JupiterModule\Presenter\RedDwarfPresenter',
$factory->formatPresenterClass('Universe:Jupiter:RedDwarf')
);
});


test(function () {
$factory = new PresenterFactory;
Assert::exception(
function () use ($factory) {
$factory->setMapping([
'*' => ['Lister', 'Rimmer', 'Cat', 'Kryton'],
]);
},
Nette\InvalidStateException::class,
'Invalid length of mask array.'
);
});