Skip to content
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

LinkGenerator: allow semicolon destination prefix #256

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1610b52
opened 3.1-dev
dg Jan 5, 2020
34f5736
requires PHP 7.2
dg Apr 20, 2020
dd5caee
removed some deprecated stuff
dg Jan 5, 2020
a6a190a
Presenter::getContext() triggers error message
dg Feb 11, 2020
faeab27
Link: added isLinkCurrent() & getComponent() [Closes #229]
dg Jan 3, 2020
deaf3ad
Template divided into hierarchy Template > DefaultTemplate
dg Mar 4, 2020
44d2867
Template, DefaultTemplate: magic access via __get/__set replaced with…
dg Mar 4, 2020
0d7b0fc
DefaultTemplate: added default properites and @methods
dg Mar 4, 2020
6cfc530
TemplateFactory: sets only properties that exist
dg Mar 4, 2020
7d02fc1
TemplateFactory: requires ITemplate instead of Template
dg Mar 28, 2020
4dfb020
Control, Presenter::createTemplate() tries to create custom Template …
dg Jan 4, 2020
e8b5a52
Control, Presenter::createTemplate() added parameter $class for custo…
solcik Jan 9, 2020
d0b4da7
Presenter::sendTemplate() added parameter $template
dg Jan 6, 2020
1809630
UIMacros: overloaded macro {templatePrint}
dg Jan 8, 2020
c83bd5d
interface Nette\Application\IRouter is alias for Nette\Routing\Router
dg Mar 10, 2020
711f0f3
Route, SimpleRouter: flags is deprecated
dg Mar 10, 2020
378b9f1
RouteList: Countable and IteratorAggregate are deprecated
dg Mar 10, 2020
ecf7bf3
Template: passes the template to the latte instead of just parameters.
dg Mar 31, 2020
269831a
UIMacros: modifiers are deprecated in {control}, {link} and {plink}
dg May 26, 2020
1e11287
Presenter: life-time of flash message is always 30sec
dg Jun 9, 2020
b6546d4
ApplicationExtension: catchExceptions is always true on production mo…
dg Jun 16, 2020
0ef0688
RoutingExtension: removed option 'routeClass' (BC break)
dg Jul 10, 2020
4d274c9
RoutingExtension: creates service 'routes' only when routes are defin…
dg Jul 10, 2020
04319e2
RoutingExtension: creates service 'routes' when no other exists WIP
dg Jul 12, 2020
bc0fbef
LinkGenerator: allow semicolon destination prefix
dakujem Mar 24, 2020
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.1', '7.2', '7.3', '7.4']
php: ['7.2', '7.3', '7.4']

fail-fast: false

Expand Down
2 changes: 2 additions & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@
exitPoint(\Nette\Application\UI\Presenter::sendResponse());
exitPoint(\Nette\Application\UI\Presenter::sendTemplate());
exitPoint(\Nette\Application\UI\Presenter::terminate());

override(\Nette\Application\UI\Control::createTemplate(0), map(['' => '@']));
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: php
php:
- 7.1
- 7.2
- 7.3
- 7.4
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": ">=7.1",
"php": ">=7.2",
"nette/component-model": "^3.0",
"nette/http": "^3.0.2",
"nette/routing": "~3.0.0",
Expand All @@ -31,15 +31,15 @@
"nette/forms": "^3.0",
"nette/robot-loader": "^3.2",
"nette/security": "^3.0",
"latte/latte": "^2.6.2",
"latte/latte": "^2.7",
"tracy/tracy": "^2.6",
"mockery/mockery": "^1.0",
"phpstan/phpstan-nette": "^0.12"
},
"conflict": {
"nette/di": "<3.0-stable",
"nette/forms": "<3.0",
"latte/latte": "<2.6",
"latte/latte": "<2.7.1",
"tracy/tracy": "<2.5"
},
"autoload": {
Expand All @@ -52,7 +52,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
"dev-master": "3.1-dev"
}
}
}
7 changes: 7 additions & 0 deletions src/Application/LinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public function link(string $dest, array $params = []): string
}
[, $presenter, $action, $frag] = $m;

if ($presenter[0] === ':') { // absolute
$presenter = substr($presenter, 1);
if (!$presenter) {
throw new UI\InvalidLinkException("Missing presenter name in '$dest'.");
}
}

try {
$class = $this->presenterFactory ? $this->presenterFactory->getPresenterClass($presenter) : null;
} catch (InvalidPresenterException $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/Application/MicroPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function run(Application\Request $request): Application\IResponse
public function createTemplate(string $class = null, callable $latteFactory = null): Application\UI\ITemplate
{
$latte = $latteFactory ? $latteFactory() : $this->getContext()->getByType(Nette\Bridges\ApplicationLatte\ILatteFactory::class)->create();
$template = $class ? new $class : new Nette\Bridges\ApplicationLatte\Template($latte);
$template = $class ? new $class : new Nette\Bridges\ApplicationLatte\DefaultTemplate($latte);

$template->setParameters($this->request->getParameters());
$template->presenter = $this;
Expand Down
3 changes: 1 addition & 2 deletions src/Application/Routers/CliRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
namespace Nette\Application\Routers;

use Nette;
use Nette\Application;


/**
* The unidirectional router for CLI. (experimental)
*/
final class CliRouter implements Application\IRouter
final class CliRouter implements Nette\Routing\Router
{
use Nette\SmartObject;

Expand Down
20 changes: 9 additions & 11 deletions src/Application/Routers/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* The bidirectional route is responsible for mapping
* HTTP request to an array for dispatch and vice-versa.
*/
class Route extends Nette\Routing\Route implements Nette\Application\IRouter
class Route extends Nette\Routing\Route implements Nette\Routing\Router
{
private const
PRESENTER_KEY = 'presenter',
Expand All @@ -40,9 +40,6 @@ class Route extends Nette\Routing\Route implements Nette\Application\IRouter
],
];

/** @deprecated */
public static $styles = [];

/** @var int */
private $flags;

Expand All @@ -69,12 +66,11 @@ public function __construct(string $mask, $metadata = [], int $flags = 0)
];
}

$this->defaultMeta = $this->defaultMeta + self::UI_META;
if (self::$styles) {
trigger_error('Route::$styles is deprecated.', E_USER_DEPRECATED);
array_replace_recursive($this->defaultMeta, self::$styles);
if ($flags) {
trigger_error(__METHOD__ . '() parameter $flags is deprecated, use RouteList::addRoute(..., ..., $flags) instead.', E_USER_DEPRECATED);
}

$this->defaultMeta = $this->defaultMeta + self::UI_META;
$this->flags = $flags;
parent::__construct($mask, $metadata);
}
Expand Down Expand Up @@ -149,11 +145,10 @@ public function getConstantParameters(): array
}


/**
* Returns flags.
*/
/** @deprecated */
public function getFlags(): int
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return $this->flags;
}

Expand Down Expand Up @@ -210,3 +205,6 @@ public static function path2presenter(string $s): string
return $s;
}
}


interface_exists(Nette\Application\IRouter::class);
11 changes: 9 additions & 2 deletions src/Application/Routers/RouteList.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* The router broker.
*/
class RouteList extends Nette\Routing\RouteList implements Nette\Application\IRouter, \ArrayAccess, \Countable, \IteratorAggregate
class RouteList extends Nette\Routing\RouteList implements Nette\Routing\Router, \ArrayAccess, \Countable, \IteratorAggregate
{
private const PRESENTER_KEY = 'presenter';

Expand Down Expand Up @@ -93,8 +93,10 @@ public function getModule(): ?string
}


/** @deprecated */
public function count(): int
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return count($this->getRouters());
}

Expand Down Expand Up @@ -132,7 +134,7 @@ public function offsetGet($index)
*/
public function offsetExists($index): bool
{
return is_int($index) && $index >= 0 && $index < $this->count();
return is_int($index) && $index >= 0 && $index < count($this->getRouters());
}


Expand All @@ -149,8 +151,13 @@ public function offsetUnset($index): void
}


/** @deprecated */
public function getIterator(): \ArrayIterator
{
trigger_error(__METHOD__ . '() is deprecated, use getRouters().', E_USER_DEPRECATED);
return new \ArrayIterator($this->getRouters());
}
}


interface_exists(Nette\Application\IRouter::class);
39 changes: 9 additions & 30 deletions src/Application/Routers/SimpleRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
/**
* The bidirectional route for trivial routing via query parameters.
*/
final class SimpleRouter extends Nette\Routing\SimpleRouter implements Nette\Application\IRouter
final class SimpleRouter extends Nette\Routing\SimpleRouter implements Nette\Routing\Router
{
private const
PRESENTER_KEY = 'presenter',
MODULE_KEY = 'module';

/** @var string */
private $module = '';

/** @var int */
private $flags;

Expand All @@ -43,31 +40,16 @@ public function __construct($defaults = [], int $flags = 0)
}

if (isset($defaults[self::MODULE_KEY])) {
trigger_error(__METHOD__ . '() parameter module is deprecated, use RouteList::withModule() instead.', E_USER_DEPRECATED);
$this->module = $defaults[self::MODULE_KEY] . ':';
unset($defaults[self::MODULE_KEY]);
throw new Nette\DeprecatedException(__METHOD__ . '() parameter module is deprecated, use RouteList::withModule() instead.');
} elseif ($flags) {
trigger_error(__METHOD__ . '() parameter $flags is deprecated, use RouteList::add(..., $flags) instead.', E_USER_DEPRECATED);
}

$this->flags = $flags;
parent::__construct($defaults);
}


/**
* Maps HTTP request to an array.
*/
public function match(Nette\Http\IRequest $httpRequest): ?array
{
$params = parent::match($httpRequest);
$presenter = $params[self::PRESENTER_KEY] ?? null;
if (is_string($presenter)) {
$params[self::PRESENTER_KEY] = $this->module . $presenter;
}

return $params;
}


/**
* Constructs absolute URL from array.
*/
Expand All @@ -76,20 +58,17 @@ public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?stri
if ($this->flags & self::ONE_WAY) {
return null;
}

if (strncmp($params[self::PRESENTER_KEY], $this->module, strlen($this->module)) !== 0) {
return null;
}
$params[self::PRESENTER_KEY] = substr($params[self::PRESENTER_KEY], strlen($this->module));
return parent::constructUrl($params, $refUrl);
}


/**
* Returns flags.
*/
/** @deprecated */
public function getFlags(): int
{
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
return $this->flags;
}
}


interface_exists(Nette\Application\IRouter::class);
24 changes: 3 additions & 21 deletions src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,6 @@ final public function getParameterId(string $name): string
}


/** @deprecated */
final public function getParam($name = null, $default = null)
{
trigger_error(__METHOD__ . '() is deprecated; use getParameter() or getParameters() instead.', E_USER_DEPRECATED);
return func_num_args() ? $this->getParameter($name, $default) : $this->getParameters();
}


/********************* interface ISignalReceiver ****************d*g**/


Expand Down Expand Up @@ -287,21 +279,11 @@ public function isLinkCurrent(string $destination = null, $args = []): bool
* @param array|mixed $args
* @throws Nette\Application\AbortException
*/
public function redirect(/*int $code, string */$destination, $args = []): void
public function redirect($destination, $args = []): void
{
if (is_numeric($destination)) {
trigger_error(__METHOD__ . '() first parameter $code is deprecated; use redirectPermanent() for 301 redirect.', E_USER_DEPRECATED);
[$code, $destination, $args] = func_get_args() + [null, null, []];
if (func_num_args() > 3 || !is_array($args)) {
$args = array_slice(func_get_args(), 2);
}
} else {
$code = null;
$args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1);
}

$args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1);
$presenter = $this->getPresenter();
$presenter->redirectUrl($presenter->createRequest($this, $destination, $args, 'redirect'), $code);
$presenter->redirectUrl($presenter->createRequest($this, $destination, $args, 'redirect'));
}


Expand Down
27 changes: 24 additions & 3 deletions src/Application/UI/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Control is renderable Presenter component.
*
* @property-read ITemplate|Nette\Bridges\ApplicationLatte\Template|\stdClass $template
* @property-read ITemplate|Nette\Bridges\ApplicationLatte\DefaultTemplate|\stdClass $template
*/
abstract class Control extends Component implements IRenderable
{
Expand Down Expand Up @@ -51,10 +51,31 @@ final public function getTemplate(): ITemplate
}


protected function createTemplate(): ITemplate
/**
* @param string $class
*/
protected function createTemplate(/*string $class = null*/): ITemplate
{
$class = func_num_args() ? func_get_arg(0) : $this->formatTemplateClass(); // back compatibility
$templateFactory = $this->templateFactory ?: $this->getPresenter()->getTemplateFactory();
return $templateFactory->createTemplate($this);
return $templateFactory->createTemplate($this, $class);
}


public function formatTemplateClass(): ?string
{
$class = preg_replace('#Presenter$|Control$#', 'Template', static::class);
if ($class === static::class || !class_exists($class)) {
return null;
} elseif (!is_a($class, ITemplate::class, true)) {
trigger_error(sprintf(
'%s: class %s was found but does not implement the %s, so it will not be used for the template.',
static::class, $class, ITemplate::class
), E_USER_NOTICE);
return null;
} else {
return $class;
}
}


Expand Down
18 changes: 18 additions & 0 deletions src/Application/UI/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public function __construct(Component $component, string $destination, array $pa
}


/**
* Returns link component.
*/
public function getComponent(): Component
{
return $this->component;
}


/**
* Returns link destination.
*/
Expand Down Expand Up @@ -80,6 +89,15 @@ public function getParameters(): array
}


/**
* Determines whether this links to the current page.
*/
public function isLinkCurrent(): bool
{
return $this->component->isLinkCurrent($this->destination, $this->params);
}


/**
* Converts link to URL.
*/
Expand Down
Loading