Skip to content

Commit

Permalink
Fix #183: Move URI prefix to RouteCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamwin committed Feb 25, 2023
1 parent 04e3a88 commit 860fe9e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
final class RouteCollection implements RouteCollectionInterface
{
private string $uriPrefix = '';

/**
* @psalm-var Items
*/
Expand Down Expand Up @@ -55,6 +57,16 @@ public function getRouteTree(bool $routeAsString = true): array
return $this->buildTree($this->items, $routeAsString);
}

public function getUriPrefix(): string
{
return $this->uriPrefix;
}

public function setUriPrefix(string $name): void
{
$this->uriPrefix = $name;
}

private function ensureItemsInjected(): void
{
if ($this->items === []) {
Expand Down Expand Up @@ -83,10 +95,11 @@ private function injectItems(array $items): void
private function injectItem(Group|Route $route): void
{
if ($route instanceof Group) {
$this->injectGroup($route, $this->items);
$this->injectGroup($route, $this->items, $this->uriPrefix);
return;
}

$route = $route->pattern($this->uriPrefix . $route->getData('pattern'));
$routeName = $route->getData('name');
$this->items[] = $routeName;
if (isset($this->routes[$routeName]) && !$route->getData('override')) {
Expand Down
11 changes: 11 additions & 0 deletions src/RouteCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

interface RouteCollectionInterface
{
/**
* Returns URI prefix.
* @see setUriPrefix()
*/
public function getUriPrefix(): string;

/**
* Sets the URI prefix so that all routes are registered to this path after the domain part.
*/
public function setUriPrefix(string $prefix): void;

/**
* @return Route[]
*/
Expand Down
4 changes: 0 additions & 4 deletions src/UrlGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ public function generateFromCurrent(
?string $fallbackRouteName = null
): string;

public function getUriPrefix(): string;

public function setUriPrefix(string $name): void;

/**
* Set default argument value.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@

final class RouteCollectionTest extends TestCase
{
public function testUriPrefix(): void
{
$route1 = Route::get('/')->name('route1');
$route2 = Route::get('/{id}')->name('route2');

$group = Group::create()->routes($route1);

$collector = new RouteCollector();
$collector->addGroup($group);
$collector->addRoute($route2);

$routeCollection = new RouteCollection($collector);
$routeCollection->setUriPrefix('/api');

$this->assertStringStartsWith('/api', $routeCollection->getRoute('route1')->getData('pattern'));
$this->assertStringStartsWith('/api', $routeCollection->getRoute('route2')->getData('pattern'));
}

public function testAddRouteWithDuplicateName(): void
{
$listRoute = Route::get('/')->name('my-route');
Expand Down

0 comments on commit 860fe9e

Please sign in to comment.