Skip to content

Commit

Permalink
RouteList: parameter $flag changed to $oneWay (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 10, 2023
1 parent fe8076e commit 985fede
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 28 deletions.
8 changes: 0 additions & 8 deletions .phpstorm.meta.php

This file was deleted.

8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,14 @@ Global filters give you the ability to adjust the behavior of the route in absol
If a parameter has a custom filter defined and a global filter exists at the same time, custom `FILTER_IN` is executed before the global and vice versa global `FILTER_OUT` is executed before the custom. Thus, inside the global filter are the values of the parameters `controller` resp. `action` written in PascalCase resp. camelCase style.


ONE_WAY flag
OneWay flag
------------

One-way routes are used to preserve the functionality of old URLs that the application no longer generates but still accepts. We flag them with `ONE_WAY`:
One-way routes are used to preserve the functionality of old URLs that the application no longer generates but still accepts. We flag them with `OneWay`:

```php
// old URL /product-info?id=123
$router->addRoute('product-info', [...], $router::ONE_WAY);
$router->addRoute('product-info', [...], oneWay: true);
// new URL /product/123
$router->addRoute('product/<id>', [...]);
```
Expand Down Expand Up @@ -450,7 +450,7 @@ SEO and Canonization

The framework increases SEO (search engine optimization) by preventing duplication of content at different URLs. If multiple addresses link to a same destination, eg `/index` and `/index.html`, the framework determines the first one as primary (canonical) and redirects the others to it using HTTP code 301. Thanks to this, search engines will not index pages twice and do not break their page rank. .

This process is called canonization. The canonical URL is the one generated by the router, i.e. by the first matching route in the [collection ](#route-collection) without the ONE_WAY flag. Therefore, in the collection, we list **primary routes first**.
This process is called canonization. The canonical URL is the one generated by the router, i.e. by the first matching route in the [collection ](#route-collection) without the OneWay flag. Therefore, in the collection, we list **primary routes first**.

Canonization is performed by the controller, more in the chapter [canonization ](controllers#Canonization).

Expand Down
18 changes: 10 additions & 8 deletions src/Routing/RouteList.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class RouteList implements Router
{
use Nette\SmartObject;

private const OneWay = 'oneWay';

protected ?self $parent;

/** @var array of [Router, flags] */
Expand Down Expand Up @@ -120,7 +122,7 @@ public function warmupCache(): void
$candidates = [];
$routers = [];
foreach ($this->list as [$router, $flags]) {
if ($flags & self::ONE_WAY) {
if ($flags[self::OneWay]) {
continue;
} elseif ($router instanceof self) {
$router->warmupCache();
Expand Down Expand Up @@ -170,9 +172,9 @@ public function warmupCache(): void
/**
* Adds a router.
*/
public function add(Router $router, int $flags = 0): static
public function add(Router $router, bool $oneWay = false): static
{
$this->list[] = [$router, $flags];
$this->list[] = [$router, [self::OneWay => $oneWay]];
$this->ranks = null;
return $this;
}
Expand All @@ -181,9 +183,9 @@ public function add(Router $router, int $flags = 0): static
/**
* Prepends a router.
*/
public function prepend(Router $router, int $flags = 0): void
public function prepend(Router $router, bool $oneWay = false): void
{
array_splice($this->list, 0, 0, [[$router, $flags]]);
array_splice($this->list, 0, 0, [[$router, [self::OneWay => $oneWay]]]);
$this->ranks = null;
}

Expand All @@ -207,9 +209,9 @@ public function addRoute(
#[Language('TEXT')]
string $mask,
array $metadata = [],
int $flags = 0,
bool $oneWay = false,
) {
$this->add(new Route($mask, $metadata), $flags);
$this->add(new Route($mask, $metadata), $oneWay);
return $this;
}

Expand Down Expand Up @@ -255,7 +257,7 @@ public function getRouters(): array


/**
* @return int[]
* @return bool[]
*/
public function getFlags(): array
{
Expand Down
4 changes: 2 additions & 2 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
interface Router
{
/** for back compatibility */
public const ONE_WAY = 0b0001;
/** @deprecated */
public const ONE_WAY = true;

/**
* Maps HTTP request to an array.
Expand Down
4 changes: 2 additions & 2 deletions tests/RouteList/addRoute.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require __DIR__ . '/../bootstrap.php';


$list = new RouteList;
$list->addRoute('foo', ['route' => 'foo'], RouteList::ONE_WAY);
$list->addRoute('bar', ['route' => 'bar'], RouteList::ONE_WAY);
$list->addRoute('foo', ['route' => 'foo'], oneWay: true);
$list->addRoute('bar', ['route' => 'bar'], oneWay: true);
$list->addRoute('hello', ['route' => 'hello']);


Expand Down
2 changes: 1 addition & 1 deletion tests/RouteList/cache.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $list = new RouteList;
$list->add(new Route('bar', ['presenter' => 'bar']));
$list->add(new Route('<foo>', ['presenter' => 'foo']));
$list->add(new Route('<presenter>/<action>', ['presenter' => 'xxx']));
$list->add(new Route('oneway'), $list::ONE_WAY);
$list->add(new Route('oneway'), oneWay: true);

[$r1, $r2, $r3, $r4] = $list->getRouters();

Expand Down
6 changes: 3 additions & 3 deletions tests/RouteList/oneWay.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ require __DIR__ . '/../bootstrap.php';


$list = new RouteList;
$list->add(new Route('foo', ['route' => 'foo']), RouteList::ONE_WAY);
$list->addRoute('bar', ['route' => 'bar'], RouteList::ONE_WAY);
$list->add(new Route('foo', ['route' => 'foo']), oneWay: true);
$list->addRoute('bar', ['route' => 'bar'], oneWay: true);
$list->add(new Route('hello', ['route' => 'hello']));

Assert::same([RouteList::ONE_WAY, RouteList::ONE_WAY, 0], $list->getFlags());
Assert::same([['oneWay' => true], ['oneWay' => true], ['oneWay' => false]], $list->getFlags());

testRouteIn($list, '/foo', ['route' => 'foo', 'test' => 'testvalue']);
testRouteIn($list, '/bar', ['route' => 'bar', 'test' => 'testvalue']);
Expand Down

0 comments on commit 985fede

Please sign in to comment.