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

Fix #183: Move URI prefix to RouteCollection #192

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 $prefix): void
{
$this->uriPrefix = $prefix;
}

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
12 changes: 12 additions & 0 deletions src/RouteCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

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
19 changes: 19 additions & 0 deletions tests/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@

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($prefix = '/api');

$this->assertSame($prefix, $routeCollection->getUriPrefix());
$this->assertStringStartsWith($prefix, $routeCollection->getRoute('route1')->getData('pattern'));
$this->assertStringStartsWith($prefix, $routeCollection->getRoute('route2')->getData('pattern'));
}

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