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

Move parameters group into current route #128

Merged
merged 17 commits into from
Nov 22, 2021
Merged
30 changes: 27 additions & 3 deletions src/CurrentRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Router;

use Psr\Http\Message\UriInterface;
use RuntimeException;
use LogicException;

/**
* Holds information about current route e.g. matched last.
Expand All @@ -24,6 +24,11 @@ final class CurrentRoute implements CurrentRouteInterface
*/
private ?UriInterface $uri = null;

/**
* Current Route parameters
samdark marked this conversation as resolved.
Show resolved Hide resolved
*/
private array $parameters = [];

/**
* Returns the current route name.
*
Expand Down Expand Up @@ -63,7 +68,7 @@ public function setRoute(RouteParametersInterface $route): void
$this->route = $route;
return;
}
throw new RuntimeException('Can not set route since it was already set.');
throw new LogicException('Can not set route since it was already set.');
}

/**
Expand All @@ -75,6 +80,25 @@ public function setUri(UriInterface $uri): void
$this->uri = $uri;
return;
}
throw new RuntimeException('Can not set URI since it was already set.');
throw new LogicException('Can not set URI since it was already set.');
}

public function getParameters(): array
{
return $this->parameters;
}
rustamwin marked this conversation as resolved.
Show resolved Hide resolved

public function getParameter(string $name, $default = null): ?string
{
return $this->parameters[$name] ?? $default;
}
rustamwin marked this conversation as resolved.
Show resolved Hide resolved

public function setParameters(array $parameters)
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
{
if ($this->parameters === []) {
$this->parameters = $parameters;
return;
}
throw new LogicException('Can not set parameters since it was already set.');
}
}
17 changes: 17 additions & 0 deletions src/CurrentRouteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ public function getRoute(): ?RouteParametersInterface;
* @return UriInterface|null The current URI.
*/
public function getUri(): ?UriInterface;

/**
* Returns the current route parameters.
*
* @return array
*/
public function getParameters(): array;
rustamwin marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the current route parameter.
*
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function getParameter(string $name, $default = null);
}
5 changes: 1 addition & 4 deletions src/Middleware/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}

$this->currentRoute->setRoute($result->route());

foreach ($result->parameters() as $parameter => $value) {
$request = $request->withAttribute($parameter, $value);
}
$this->currentRoute->setParameters($result->parameters());

return $result->withDispatcher($this->dispatcher)->process($request, $handler);
}
Expand Down
71 changes: 71 additions & 0 deletions tests/CurrentRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Router\Tests;

use LogicException;
use Nyholm\Psr7\Uri;
use PHPUnit\Framework\TestCase;
use Yiisoft\Router\CurrentRoute;
Expand Down Expand Up @@ -37,4 +38,74 @@ public function testGetCurrentUri(): void

$this->assertSame($uri, $currentRoute->getUri());
}

public function testGetParameters(): void
{
$parameters = [
'test' => 'test',
'foo' => 'bar',
];
$currentRoute = new CurrentRoute();
$currentRoute->setParameters($parameters);

$this->assertSame($parameters, $currentRoute->getParameters());
}

public function testGetParameter(): void
{
$parameters = [
'test' => 'test',
'foo' => 'bar',
];
$currentRoute = new CurrentRoute();
$currentRoute->setParameters($parameters);

$this->assertSame('bar', $currentRoute->getParameter('foo'));
}

public function testGetParameterWithDefault()
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
{
$currentRoute = new CurrentRoute();
$currentRoute->setParameters(['test' => 1]);

$this->assertSame('bar', $currentRoute->getParameter('foo', 'bar'));
}

public function testGetParameterWithNonExist()
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
$currentRoute = new CurrentRoute();
$currentRoute->setParameters(['test' => 1]);

$this->assertNull($currentRoute->getParameter('foo'));
}
rustamwin marked this conversation as resolved.
Show resolved Hide resolved

public function testSetRouteTwice()
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Can not set route since it was already set.');

$currentRoute = new CurrentRoute();
$currentRoute->setRoute(Route::get('')->name('test'));
$currentRoute->setRoute(Route::get('/home')->name('home'));
}

public function testSetUriTwice()
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Can not set URI since it was already set.');

$currentRoute = new CurrentRoute();
$currentRoute->setUri(new Uri(''));
$currentRoute->setUri(new Uri('home'));
}

public function testSetParametersTwice()
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Can not set parameters since it was already set.');

$currentRoute = new CurrentRoute();
$currentRoute->setParameters(['foo' => 'bar']);
$currentRoute->setParameters(['id' => 1]);
}
}
10 changes: 10 additions & 0 deletions tests/Middleware/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ public function testGetCurrentUri(): void
$this->assertSame($request->getUri(), $currentRoute->getUri());
}

public function testGetParameters(): void
{
$currentRoute = new CurrentRoute();
$request = new ServerRequest('GET', '/');

$this->processWithRouter($request, $currentRoute);

$this->assertSame(['parameter' => 'value'], $currentRoute->getParameters());
}

private function getMatcher(): UrlMatcherInterface
{
$middleware = $this->createRouteMiddleware();
Expand Down