Skip to content

Commit

Permalink
added feature "link aliases"
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 20, 2024
1 parent 1f4839d commit 97f3280
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Application/LinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(

/**
* Generates URL to presenter.
* @param string $destination in format "[//] [[[module:]presenter:]action | signal! | this] [#fragment]"
* @param string $destination in format "[//] [[[module:]presenter:]action | signal! | this | @alias] [#fragment]"
* @throws UI\InvalidLinkException
*/
public function link(
Expand All @@ -54,7 +54,7 @@ public function link(


/**
* @param string $destination in format "[[[module:]presenter:]action | signal! | this]"
* @param string $destination in format "[[[module:]presenter:]action | signal! | this | @alias]"
* @param string $mode forward|redirect|link
* @throws UI\InvalidLinkException
* @internal
Expand Down Expand Up @@ -85,6 +85,13 @@ public function createRequest(
$path = 'this';
}

if ($path[0] === '@') {
if (!$this->presenterFactory instanceof PresenterFactory) {
throw new \LogicException('Link aliasing requires PresenterFactory service.');
}
$path = ':' . $this->presenterFactory->getAlias(substr($path, 1));
}

$current = false;
[$presenter, $action] = Helpers::splitName($path);
if ($presenter === '') {
Expand Down Expand Up @@ -223,7 +230,7 @@ public function createRequest(


/**
* Parse destination in format "[//] [[[module:]presenter:]action | signal! | this] [?query] [#fragment]"
* Parse destination in format "[//] [[[module:]presenter:]action | signal! | this | @alias] [?query] [#fragment]"
* @throws UI\InvalidLinkException
* @return array{absolute: bool, path: string, signal: bool, args: ?array, fragment: string}
* @internal
Expand Down
17 changes: 17 additions & 0 deletions src/Application/PresenterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class PresenterFactory implements IPresenterFactory
'Nette' => ['NetteModule\\', '*\\', '*Presenter'],
];

private array $aliases = [];
private array $cache = [];

/** @var callable */
Expand Down Expand Up @@ -117,4 +118,20 @@ public function formatPresenterClass(string $presenter): string

return $mapping[0];
}


/**
* Sets pairs [alias => destination]
*/
public function setAliases(array $aliases): static
{
$this->aliases = $aliases;
return $this;
}


public function getAlias(string $alias): string
{
return $this->aliases[$alias] ?? throw new Nette\InvalidStateException("Link alias '$alias' was not found.");
}
}
5 changes: 5 additions & 0 deletions src/Bridges/ApplicationDI/ApplicationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function getConfigSchema(): Nette\Schema\Schema
Expect::string(),
Expect::arrayOf('string|array'),
),
'aliases' => Expect::arrayOf('string'),
'scanDirs' => Expect::anyOf(
Expect::arrayOf('string')->default($this->scanDirs)->mergeDefaults(),
false,
Expand Down Expand Up @@ -101,6 +102,10 @@ public function loadConfiguration(): void
]);
}

if ($config->aliases) {
$presenterFactory->addSetup('setAliases', [$config->aliases]);
}

$builder->addDefinition($this->prefix('linkGenerator'))
->setFactory(Nette\Application\LinkGenerator::class, [
1 => new Definitions\Statement([new Definitions\Statement('@Nette\Http\IRequest::getUrl'), 'withoutUserInfo']),
Expand Down
58 changes: 58 additions & 0 deletions tests/Routers/link-aliases.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Test: Nette\Application\UI\Component::redirect()
*/

declare(strict_types=1);

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

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


class TestPresenter extends Application\UI\Presenter
{
}


$factory = new PresenterFactory;
$factory->setAliases([
'a' => 'Test:a',
'b' => 'Test:b',
]);

Assert::same('Test:a', $factory->getAlias('a'));
Assert::same('Test:b', $factory->getAlias('b'));
Assert::exception(
fn() => $factory->getAlias('c'),
Nette\InvalidStateException::class,
"Link alias 'c' was not found.",
);



// link generator
$generator = new Application\LinkGenerator(
new Application\Routers\SimpleRouter,
new Http\UrlScript('http://localhost'),
$factory,
);

Assert::same('http://localhost/?action=a&presenter=Test', $generator->link('@a'));


// presenter
$presenter = new TestPresenter;
$presenter->injectPrimary(
new Http\Request(new Http\UrlScript('http://localhost')),
new Http\Response,
$factory,
new Application\Routers\SimpleRouter,
);


Assert::same('/?action=a&presenter=Test', $presenter->link('@a'));

0 comments on commit 97f3280

Please sign in to comment.