Skip to content

Commit

Permalink
RouteList: array access is deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 8, 2024
1 parent cdc2776 commit 17957ee
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
10 changes: 10 additions & 0 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,6 +141,7 @@ 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');
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Bridges.DI/RoutingExtension.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ test('', function () {
$container = new Container1;
$router = $container->getService('router');
Assert::type(Nette\Application\Routers\RouteList::class, $router);
Assert::same('index.php', $router[0]->getMask());
Assert::same('item/<id>', $router[1]->getMask());
$routes = $router->getRouters();
Assert::same('index.php', $routes[0]->getMask());
Assert::same('item/<id>', $routes[1]->getMask());

Assert::type(Nette\Application\Routers\RouteList::class, $router);
Assert::type(Nette\Application\Routers\Route::class, $router[0]);
Assert::type(Nette\Application\Routers\Route::class, $routes[0]);
});
3 changes: 1 addition & 2 deletions tests/Routers/RouteList.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

declare(strict_types=1);

use Nette\Application\Routers\Route;
use Nette\Application\Routers\RouteList;
use Tester\Assert;

Expand All @@ -17,7 +16,7 @@ require __DIR__ . '/Route.php';


$list = new RouteList;
$list[] = new Route('<presenter>/<action=default>/<id= \d{1,3}>');
$list->addRoute('<presenter>/<action=default>/<id= \d{1,3}>');


Assert::same('http://example.com/front.homepage/', testRouteOut($list, ['presenter' => 'Front:Homepage']));
Expand Down
5 changes: 2 additions & 3 deletions tests/Routers/RouteList.modules.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

declare(strict_types=1);

use Nette\Application\Routers\Route;
use Nette\Application\Routers\RouteList;


Expand All @@ -16,12 +15,12 @@ require __DIR__ . '/Route.php';


$list = new RouteList;
$list[] = new Route('auth/<presenter>[/<action>]', [
$list->addRoute('auth/<presenter>[/<action>]', [
'module' => 'Auth',
'presenter' => 'Homepage',
'action' => 'default',
]);
$list[] = new Route('<presenter>[/<action>]', [
$list->addRoute('<presenter>[/<action>]', [
'module' => 'Default',
'presenter' => 'Homepage',
'action' => 'default',
Expand Down

0 comments on commit 17957ee

Please sign in to comment.