Skip to content

Commit

Permalink
Fix #102: Use parameter bag (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamwin committed Jun 20, 2021
1 parent 42b4841 commit 748f848
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 73 deletions.
92 changes: 42 additions & 50 deletions src/Route.php
Expand Up @@ -13,21 +13,38 @@
*/
final class Route
{
private ?string $name = null;
public const NAME = 'name';
public const METHODS = 'methods';
public const PATTERN = 'pattern';
public const HOST = 'host';
public const DEFAULTS = 'defaults';
public const OVERRIDE = 'override';

/** @var string[] */
private array $methods;
private string $pattern;
private ?string $host = null;
private bool $override = false;
private ?MiddlewareDispatcher $dispatcher;
private array $parameters;
private bool $actionAdded = false;
private ?MiddlewareDispatcher $dispatcher;
private array $middlewareDefinitions = [];
private array $disabledMiddlewareDefinitions = [];
private array $defaults = [];

private function __construct(?MiddlewareDispatcher $dispatcher = null)
{
$this->dispatcher = $dispatcher;
// Set default parameters
$this->parameters = [
self::OVERRIDE => false,
self::DEFAULTS => [],
];
}

private function setParameter(string $parameter, $value): void
{
$this->parameters[$parameter] = $value;
}

public function getParameter(string $parameter, $default = null)
{
return array_key_exists($parameter, $this->parameters) ? $this->parameters[$parameter] : $default;
}

public function injectDispatcher(MiddlewareDispatcher $dispatcher): void
Expand Down Expand Up @@ -152,30 +169,30 @@ public static function methods(
?MiddlewareDispatcher $dispatcher = null
): self {
$route = new self($dispatcher);
$route->methods = $methods;
$route->pattern = $pattern;
$route->setParameter(self::METHODS, $methods);
$route->setParameter(self::PATTERN, $pattern);

return $route;
}

public function name(string $name): self
{
$route = clone $this;
$route->name = $name;
$route->setParameter(self::NAME, $name);
return $route;
}

public function pattern(string $pattern): self
{
$new = clone $this;
$new->pattern = $pattern;
return $new;
$route = clone $this;
$route->setParameter(self::PATTERN, $pattern);
return $route;
}

public function host(string $host): self
{
$route = clone $this;
$route->host = rtrim($host, '/');
$route->setParameter(self::HOST, rtrim($host, '/'));
return $route;
}

Expand All @@ -187,7 +204,7 @@ public function host(string $host): self
public function override(): self
{
$route = clone $this;
$route->override = true;
$route->setParameter(self::OVERRIDE, true);
return $route;
}

Expand All @@ -201,7 +218,7 @@ public function override(): self
public function defaults(array $defaults): self
{
$route = clone $this;
$route->defaults = $defaults;
$route->setParameter(self::DEFAULTS, $defaults);
return $route;
}

Expand Down Expand Up @@ -276,49 +293,24 @@ public function __toString(): string
{
$result = '';

if ($this->name !== null) {
$result .= '[' . $this->name . '] ';
if ($this->getParameter(self::NAME) !== null) {
$result .= '[' . $this->getParameter(self::NAME) . '] ';
}

if ($this->methods !== []) {
$result .= implode(',', $this->methods) . ' ';
if ($this->getParameter(self::METHODS) !== []) {
$result .= implode(',', $this->getParameter(self::METHODS)) . ' ';
}
if ($this->host !== null && strrpos($this->pattern, $this->host) === false) {
$result .= $this->host;
if ($this->getParameter(self::HOST) !== null && strrpos($this->getParameter(self::PATTERN), $this->getParameter(self::HOST)) === false) {
$result .= $this->getParameter(self::HOST);
}
$result .= $this->pattern;
$result .= $this->getParameter(self::PATTERN);

return $result;
}

public function getName(): string
{
return $this->name ?? (implode(', ', $this->methods) . ' ' . $this->host . $this->pattern);
}

public function getMethods(): array
{
return $this->methods;
}

public function getPattern(): string
{
return $this->pattern;
}

public function getHost(): ?string
{
return $this->host;
}

public function isOverride(): bool
{
return $this->override;
}

public function getDefaults(): array
public function getDefaultName(): string
{
return $this->defaults;
return implode(', ', $this->getParameter(self::METHODS)) . ' ' . $this->getParameter(self::HOST) . $this->getParameter(self::PATTERN);
}

public function hasMiddlewares(): bool
Expand Down
16 changes: 8 additions & 8 deletions src/RouteCollection.php
Expand Up @@ -92,9 +92,9 @@ private function injectItem($route): void
return;
}

$this->items[] = $route->getName();
$routeName = $route->getName();
if (isset($this->routes[$routeName]) && !$route->isOverride()) {
$routeName = $route->getParameter(Route::NAME, $route->getDefaultName());
$this->items[] = $routeName;
if (isset($this->routes[$routeName]) && !$route->getParameter(Route::OVERRIDE)) {
throw new InvalidArgumentException("A route with name '$routeName' already exists.");
}
$this->routes[$routeName] = $route;
Expand Down Expand Up @@ -127,16 +127,16 @@ private function injectGroup(Group $group, array &$tree, string $prefix = ''): v
}

/** @var Route $modifiedItem */
$modifiedItem = $item->pattern($prefix . $item->getPattern());
$modifiedItem = $item->pattern($prefix . $item->getParameter(Route::PATTERN));

if (empty($tree[$group->getPrefix()])) {
$tree[] = $modifiedItem->getName();
$tree[] = $modifiedItem->getParameter(Route::NAME, $modifiedItem->getDefaultName());
} else {
$tree[$group->getPrefix()][] = $modifiedItem->getName();
$tree[$group->getPrefix()][] = $modifiedItem->getParameter(Route::NAME, $modifiedItem->getDefaultName());
}

$routeName = $modifiedItem->getName();
if (isset($this->routes[$routeName]) && !$modifiedItem->isOverride()) {
$routeName = $modifiedItem->getParameter(Route::NAME, $modifiedItem->getDefaultName());
if (isset($this->routes[$routeName]) && !$modifiedItem->getParameter(Route::OVERRIDE)) {
throw new InvalidArgumentException("A route with name '$routeName' already exists.");
}
$this->routes[$routeName] = $modifiedItem;
Expand Down
2 changes: 1 addition & 1 deletion tests/RouteCollectionTest.php
Expand Up @@ -39,7 +39,7 @@ public function testRouteOverride(): void

$routeCollection = new RouteCollection($group);
$route = $routeCollection->getRoute('my-route');
$this->assertSame('/{id}', $route->getPattern());
$this->assertSame('/{id}', $route->getParameter(Route::PATTERN));
}

public function testRouteWithoutAction(): void
Expand Down
28 changes: 14 additions & 14 deletions tests/RouteTest.php
Expand Up @@ -25,21 +25,21 @@ public function testName(): void
{
$route = Route::get('/')->name('test.route');

$this->assertSame('test.route', $route->getName());
$this->assertSame('test.route', $route->getParameter(Route::NAME, $route->getDefaultName()));
}

public function testNameDefault(): void
{
$route = Route::get('/');

$this->assertSame('GET /', $route->getName());
$this->assertSame('GET /', $route->getParameter(Route::NAME, $route->getDefaultName()));
}

public function testMethods(): void
{
$route = Route::methods([Method::POST, Method::HEAD], '/');

$this->assertSame([Method::POST, Method::HEAD], $route->getMethods());
$this->assertSame([Method::POST, Method::HEAD], $route->getParameter(Route::METHODS));
}

public const PATCH = 'PATCH';
Expand All @@ -50,77 +50,77 @@ public function testGetMethod(): void
{
$route = Route::get('/');

$this->assertSame([Method::GET], $route->getMethods());
$this->assertSame([Method::GET], $route->getParameter(Route::METHODS));
}

public function testPostMethod(): void
{
$route = Route::post('/');

$this->assertSame([Method::POST], $route->getMethods());
$this->assertSame([Method::POST], $route->getParameter(Route::METHODS));
}

public function testPutMethod(): void
{
$route = Route::put('/');

$this->assertSame([Method::PUT], $route->getMethods());
$this->assertSame([Method::PUT], $route->getParameter(Route::METHODS));
}

public function testDeleteMethod(): void
{
$route = Route::delete('/');

$this->assertSame([Method::DELETE], $route->getMethods());
$this->assertSame([Method::DELETE], $route->getParameter(Route::METHODS));
}

public function testPatchMethod(): void
{
$route = Route::patch('/');

$this->assertSame([Method::PATCH], $route->getMethods());
$this->assertSame([Method::PATCH], $route->getParameter(Route::METHODS));
}

public function testHeadMethod(): void
{
$route = Route::head('/');

$this->assertSame([Method::HEAD], $route->getMethods());
$this->assertSame([Method::HEAD], $route->getParameter(Route::METHODS));
}

public function testOptionsMethod(): void
{
$route = Route::options('/');

$this->assertSame([Method::OPTIONS], $route->getMethods());
$this->assertSame([Method::OPTIONS], $route->getParameter(Route::METHODS));
}

public function testPattern(): void
{
$route = Route::get('/test')->pattern('/test2');

$this->assertSame('/test2', $route->getPattern());
$this->assertSame('/test2', $route->getParameter(Route::PATTERN));
}

public function testHost(): void
{
$route = Route::get('/')->host('https://yiiframework.com/');

$this->assertSame('https://yiiframework.com', $route->getHost());
$this->assertSame('https://yiiframework.com', $route->getParameter(Route::HOST));
}

public function testDefaults(): void
{
$route = Route::get('/{language}')->defaults(['language' => 'en']);

$this->assertSame(['language' => 'en'], $route->getDefaults());
$this->assertSame(['language' => 'en'], $route->getParameter(Route::DEFAULTS));
}

public function testOverride(): void
{
$route = Route::get('/')->override();

$this->assertTrue($route->isOverride());
$this->assertTrue($route->getParameter(Route::OVERRIDE));
}

public function testToString(): void
Expand Down

0 comments on commit 748f848

Please sign in to comment.