Skip to content

Nested mapping #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"nette/forms": "^3.2",
"nette/robot-loader": "^4.0",
"nette/security": "^3.2",
"latte/latte": "^2.10.2 || ^3.0.18",
"latte/latte": "^3.0.18",
"tracy/tracy": "^2.9",
"mockery/mockery": "^2.0",
"phpstan/phpstan-nette": "^1.0",
Expand All @@ -42,7 +42,7 @@
"nette/di": "<3.2",
"nette/forms": "<3.2",
"nette/schema": "<1.3",
"latte/latte": "<2.7.1 || >=3.0.0 <3.0.18 || >=3.1",
"latte/latte": "<3.0.18",
"tracy/tracy": "<2.9"
},
"autoload": {
Expand All @@ -55,7 +55,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
"dev-master": "4.0-dev"
}
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Nette Application MVC
=====================

[![Downloads this Month](https://img.shields.io/packagist/dm/nette/application.svg)](https://packagist.org/packages/nette/application)
[![Tests](https://github.com/nette/application/actions/workflows/tests.yml/badge.svg?branch=v3.2)](https://github.com/nette/application/actions)
[![Tests](https://github.com/nette/application/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/nette/application/actions)
[![Latest Stable Version](https://poser.pugx.org/nette/application/v/stable)](https://github.com/nette/application/releases)
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/nette/application/blob/master/license.md)

Expand Down
1 change: 1 addition & 0 deletions src/Application/LinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public static function parseDestination(string $destination): array
}

if (!empty($matches['query'])) {
trigger_error("Link format is obsolete, use arguments instead of query string in '$destination'.", E_USER_DEPRECATED);
parse_str(substr($matches['query'], 1), $args);
}

Expand Down
28 changes: 20 additions & 8 deletions src/Application/PresenterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PresenterFactory implements IPresenterFactory
{
/** @var array[] of module => splited mask */
private array $mapping = [
'*' => ['', '*Module\\', '*Presenter'],
'*' => ['App\\UI\\', '*\\', '**Presenter'],
'Nette' => ['NetteModule\\', '*\\', '*Presenter'],
];

Expand Down Expand Up @@ -107,14 +107,26 @@ public function formatPresenterClass(string $presenter): string
if (!Nette\Utils\Strings::match($presenter, '#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#D')) {
throw new InvalidPresenterException("Presenter name must be alphanumeric string, '$presenter' is invalid.");
}
$parts = explode(':', $presenter);
$mapping = isset($parts[1], $this->mapping[$parts[0]])
? $this->mapping[array_shift($parts)]
: $this->mapping['*'];
$parts = explode(':', $presenter);

while ($part = array_shift($parts)) {
$mapping[0] .= strtr($mapping[$parts ? 1 : 2], ['**' => "$part\\$part", '*' => $part]);
}
$mapping = $this->mapping['*'];

$key = $presenter;
while (strrpos($key, ':') != false) {
$key = substr($key, 0, strrpos($key, ':'));

if (isset($this->mapping[$key])) {
$mapping = $this->mapping[$key];

$parts = array_slice($parts, substr_count($key, ':') + 1);
break;
}

}

while ($part = array_shift($parts)) {
$mapping[0] .= strtr($mapping[$parts ? 1 : 2], ['**' => "$part\\$part", '*' => $part]);
}

return $mapping[0];
}
Expand Down
10 changes: 5 additions & 5 deletions src/Application/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
/**
* Presenter request.
*
* @property string $presenterName
* @property array $parameters
* @property array $post
* @property array $files
* @property string|null $method
* @property-deprecated string $presenterName
* @property-deprecated array $parameters
* @property-deprecated array $post
* @property-deprecated array $files
* @property-deprecated string|null $method
*/
final class Request
{
Expand Down
3 changes: 0 additions & 3 deletions src/Application/Routers/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,3 @@ public static function path2presenter(string $s): string
return $s;
}
}


interface_exists(Nette\Application\IRouter::class);
13 changes: 10 additions & 3 deletions src/Application/Routers/RouteList.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public function getModule(): ?string
*/
public function offsetSet($index, $router): void
{
if ($router instanceof Route) {
trigger_error('Usage `$router[] = new Route(...)` is deprecated, use `$router->addRoute(...)`.', E_USER_DEPRECATED);
} else {
$class = getclass($router);
trigger_error("Usage `\$router[] = new $class` is deprecated, use `\$router->add(new $class)`.", E_USER_DEPRECATED);
}

if ($index === null) {
$this->add($router);
} else {
Expand All @@ -109,6 +116,7 @@ public function offsetSet($index, $router): void
*/
public function offsetGet($index): Nette\Routing\Router
{
trigger_error('Usage `$route = $router[...]` is deprecated, use `$router->getRouters()`.', E_USER_DEPRECATED);
if (!$this->offsetExists($index)) {
throw new Nette\OutOfRangeException('Offset invalid or out of range');
}
Expand All @@ -122,6 +130,7 @@ public function offsetGet($index): Nette\Routing\Router
*/
public function offsetExists($index): bool
{
trigger_error('Usage `isset($router[...])` is deprecated.', E_USER_DEPRECATED);
return is_int($index) && $index >= 0 && $index < count($this->getRouters());
}

Expand All @@ -132,13 +141,11 @@ public function offsetExists($index): bool
*/
public function offsetUnset($index): void
{
trigger_error('Usage `unset($router[$index])` is deprecated, use `$router->modify($index, null)`.', E_USER_DEPRECATED);
if (!$this->offsetExists($index)) {
throw new Nette\OutOfRangeException('Offset invalid or out of range');
}

$this->modify($index, null);
}
}


interface_exists(Nette\Application\IRouter::class);
3 changes: 0 additions & 3 deletions src/Application/Routers/SimpleRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,3 @@ public function __construct(array|string $defaults = [])
parent::__construct($defaults);
}
}


interface_exists(Nette\Application\IRouter::class);
93 changes: 49 additions & 44 deletions src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
* other child components, and interact with user. Components have properties
* for storing their status, and responds to user command.
*
* @property-read Presenter $presenter
* @property-read bool $linkCurrent
* @property-deprecated Presenter $presenter
* @property-deprecated bool $linkCurrent
*/
abstract class Component extends Nette\ComponentModel\Container implements SignalReceiver, StatePersistent, \ArrayAccess
{
Expand Down Expand Up @@ -71,18 +71,27 @@ public function getUniqueId(): string
}


public function addComponent(
Nette\ComponentModel\IComponent $component,
?string $name,
?string $insertBefore = null,
): static
{
if (!$component instanceof SignalReceiver && !$component instanceof StatePersistent) {
throw new Nette\InvalidStateException("Component '$name' of type " . get_debug_type($component) . ' is not intended to be used in the Presenter.');
}

return parent::addComponent($component, $name, $insertBefore = null);
}


protected function createComponent(string $name): ?Nette\ComponentModel\IComponent
{
if (method_exists($this, $method = 'createComponent' . $name)) {
(new AccessPolicy($this, new \ReflectionMethod($this, $method)))->checkAccess();
}
$res = parent::createComponent($name);
if ($res && !$res instanceof SignalReceiver && !$res instanceof StatePersistent) {
$type = $res::class;
trigger_error("It seems that component '$name' of type $type is not intended to be used in the Presenter.");
(new AccessPolicy($this, $rm = new \ReflectionMethod($this, $method)))->checkAccess();
$this->checkRequirements($rm);
}

return $res;
return parent::createComponent($name);
}


Expand Down Expand Up @@ -161,9 +170,9 @@ public function loadState(array $params): void
));
}

$this->$name = $params[$name];
$this->$name = &$params[$name];
} else {
$params[$name] = $this->$name ?? null;
$params[$name] = &$this->$name;
}
}

Expand Down Expand Up @@ -225,6 +234,7 @@ public function saveStatePartial(array &$params, ComponentReflection $reflection
final public function getParameter(string $name): mixed
{
if (func_num_args() > 1) {
trigger_error(__METHOD__ . '() parameter $default is deprecated, use operator ??', E_USER_DEPRECATED);
$default = func_get_arg(1);
}
return $this->params[$name] ?? $default ?? null;
Expand All @@ -236,7 +246,7 @@ final public function getParameter(string $name): mixed
*/
final public function getParameters(): array
{
return $this->params;
return array_map(fn($item) => $item, $this->params);
}


Expand Down Expand Up @@ -281,49 +291,49 @@ public static function formatSignalMethod(string $signal): string
/**
* Generates URL to presenter, action or signal.
* @param string $destination in format "[//] [[[module:]presenter:]action | signal! | this] [#fragment]"
* @param array|mixed $args
* @param mixed ...$args
* @throws InvalidLinkException
*/
public function link(string $destination, $args = []): string
public function link(string $destination, ...$args): string
{
try {
$args = func_num_args() < 3 && is_array($args)
? $args
: array_slice(func_get_args(), 1);
$args = count($args) === 1 && is_array($args[0] ?? null)
? $args[0]
: $args;
return $this->getPresenter()->getLinkGenerator()->link($destination, $args, $this, 'link');

} catch (InvalidLinkException $e) {
return $this->getPresenter()->handleInvalidLink($e);
return $this->getPresenter()->processInvalidLink($e);
}
}


/**
* Returns destination as Link object.
* @param string $destination in format "[//] [[[module:]presenter:]action | signal! | this] [#fragment]"
* @param array|mixed $args
* @param mixed ...$args
*/
public function lazyLink(string $destination, $args = []): Link
public function lazyLink(string $destination, ...$args): Link
{
$args = func_num_args() < 3 && is_array($args)
? $args
: array_slice(func_get_args(), 1);
$args = count($args) === 1 && is_array($args[0] ?? null)
? $args[0]
: $args;
return new Link($this, $destination, $args);
}


/**
* Determines whether it links to the current page.
* @param ?string $destination in format "[[[module:]presenter:]action | signal! | this]"
* @param array|mixed $args
* @param mixed ...$args
* @throws InvalidLinkException
*/
public function isLinkCurrent(?string $destination = null, $args = []): bool
public function isLinkCurrent(?string $destination = null, ...$args): bool
{
if ($destination !== null) {
$args = func_num_args() < 3 && is_array($args)
? $args
: array_slice(func_get_args(), 1);
$args = count($args) === 1 && is_array($args[0] ?? null)
? $args[0]
: $args;
$this->getPresenter()->getLinkGenerator()->createRequest($this, $destination, $args, 'test');
}

Expand All @@ -334,15 +344,14 @@ public function isLinkCurrent(?string $destination = null, $args = []): bool
/**
* Redirect to another presenter, action or signal.
* @param string $destination in format "[//] [[[module:]presenter:]action | signal! | this] [#fragment]"
* @param array|mixed $args
* @return never
* @param mixed ...$args
* @throws Nette\Application\AbortException
*/
public function redirect(string $destination, $args = []): void
public function redirect(string $destination, ...$args): never
{
$args = func_num_args() < 3 && is_array($args)
? $args
: array_slice(func_get_args(), 1);
$args = count($args) === 1 && is_array($args[0] ?? null)
? $args[0]
: $args;
$presenter = $this->getPresenter();
$presenter->saveGlobalState();
$presenter->redirectUrl($presenter->getLinkGenerator()->link($destination, $args, $this, 'redirect'));
Expand All @@ -352,15 +361,14 @@ public function redirect(string $destination, $args = []): void
/**
* Permanently redirects to presenter, action or signal.
* @param string $destination in format "[//] [[[module:]presenter:]action | signal! | this] [#fragment]"
* @param array|mixed $args
* @return never
* @param mixed ...$args
* @throws Nette\Application\AbortException
*/
public function redirectPermanent(string $destination, $args = []): void
public function redirectPermanent(string $destination, ...$args): never
{
$args = func_num_args() < 3 && is_array($args)
? $args
: array_slice(func_get_args(), 1);
$args = count($args) === 1 && is_array($args[0] ?? null)
? $args[0]
: $args;
$presenter = $this->getPresenter();
$presenter->redirectUrl(
$presenter->getLinkGenerator()->link($destination, $args, $this, 'redirect'),
Expand All @@ -378,6 +386,3 @@ public function error(string $message = '', int $httpCode = Nette\Http\IResponse
throw new Nette\Application\BadRequestException($message, $httpCode);
}
}


class_exists(PresenterComponent::class);
Loading