From 17ed4adf26913c1ecd28d136805f2f42491a558f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 10 Jan 2023 02:10:13 +0100 Subject: [PATCH] RouteList: parameter $flag changed to $oneWay (BC break) --- .phpstorm.meta.php | 8 -------- readme.md | 8 ++++---- src/Routing/RouteList.php | 18 ++++++++++-------- src/Routing/Router.php | 4 ++-- tests/RouteList/addRoute.phpt | 4 ++-- tests/RouteList/cache.phpt | 2 +- tests/RouteList/oneWay.phpt | 6 +++--- 7 files changed, 22 insertions(+), 28 deletions(-) delete mode 100644 .phpstorm.meta.php diff --git a/.phpstorm.meta.php b/.phpstorm.meta.php deleted file mode 100644 index cd9d3a75..00000000 --- a/.phpstorm.meta.php +++ /dev/null @@ -1,8 +0,0 @@ -addRoute('product-info', [...], $router::ONE_WAY); +$router->addRoute('product-info', [...], oneWay: true); // new URL /product/123 $router->addRoute('product/', [...]); ``` @@ -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). diff --git a/src/Routing/RouteList.php b/src/Routing/RouteList.php index daf77ba7..e47d133a 100644 --- a/src/Routing/RouteList.php +++ b/src/Routing/RouteList.php @@ -19,6 +19,8 @@ class RouteList implements Router { use Nette\SmartObject; + private const OneWay = 'oneWay'; + protected ?self $parent; /** @var array of [Router, flags] */ @@ -119,7 +121,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(); @@ -169,9 +171,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; } @@ -180,9 +182,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; } @@ -202,9 +204,9 @@ protected function modify(int $index, ?Router $router): void } - public function addRoute(string $mask, array $metadata = [], int $flags = 0): static + public function addRoute(string $mask, array $metadata = [], bool $oneWay = false): static { - $this->add(new Route($mask, $metadata), $flags); + $this->add(new Route($mask, $metadata), $oneWay); return $this; } @@ -250,7 +252,7 @@ public function getRouters(): array /** - * @return int[] + * @return bool[] */ public function getFlags(): array { diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 2ea81a95..61a2a04f 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -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. diff --git a/tests/RouteList/addRoute.phpt b/tests/RouteList/addRoute.phpt index e3b63d94..f44e2c37 100644 --- a/tests/RouteList/addRoute.phpt +++ b/tests/RouteList/addRoute.phpt @@ -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']); diff --git a/tests/RouteList/cache.phpt b/tests/RouteList/cache.phpt index 1b5ef75f..a0f618e6 100644 --- a/tests/RouteList/cache.phpt +++ b/tests/RouteList/cache.phpt @@ -14,7 +14,7 @@ $list = new RouteList; $list->add(new Route('bar', ['presenter' => 'bar'])); $list->add(new Route('', ['presenter' => 'foo'])); $list->add(new Route('/', ['presenter' => 'xxx'])); -$list->add(new Route('oneway'), $list::ONE_WAY); +$list->add(new Route('oneway'), oneWay: true); [$r1, $r2, $r3, $r4] = $list->getRouters(); diff --git a/tests/RouteList/oneWay.phpt b/tests/RouteList/oneWay.phpt index 8c824e5f..88084a07 100644 --- a/tests/RouteList/oneWay.phpt +++ b/tests/RouteList/oneWay.phpt @@ -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']);