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

Handle base path by routeCollector instead of RouteCollectorProxy #2844

Merged
merged 3 commits into from Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Slim/Routing/Dispatcher.php
Expand Up @@ -38,8 +38,10 @@ protected function createDispatcher(): FastRouteDispatcher
}

$routeDefinitionCallback = function (RouteCollector $r) {
$basePath = $this->routeCollector->getBasePath();

foreach ($this->routeCollector->getRoutes() as $route) {
$r->addRoute($route->getMethods(), $route->getPattern(), $route->getIdentifier());
$r->addRoute($route->getMethods(), $basePath . $route->getPattern(), $route->getIdentifier());
}
};

Expand Down
18 changes: 10 additions & 8 deletions Slim/Routing/RouteCollectorProxy.php
Expand Up @@ -43,27 +43,27 @@ class RouteCollectorProxy implements RouteCollectorProxyInterface
/**
* @var string
*/
protected $basePath;
protected $groupPattern;

/**
* @param ResponseFactoryInterface $responseFactory
* @param CallableResolverInterface $callableResolver
* @param RouteCollectorInterface|null $routeCollector
* @param ContainerInterface|null $container
* @param string $basePath
* @param string $groupPattern
*/
public function __construct(
ResponseFactoryInterface $responseFactory,
CallableResolverInterface $callableResolver,
?ContainerInterface $container = null,
?RouteCollectorInterface $routeCollector = null,
string $basePath = ''
string $groupPattern = ''
) {
$this->responseFactory = $responseFactory;
$this->callableResolver = $callableResolver;
$this->container = $container;
$this->routeCollector = $routeCollector ?? new RouteCollector($responseFactory, $callableResolver, $container);
$this->basePath = $basePath;
$this->groupPattern = $groupPattern;
}

/**
Expand Down Expand Up @@ -103,15 +103,16 @@ public function getRouteCollector(): RouteCollectorInterface
*/
public function getBasePath(): string
{
return $this->basePath;
return $this->routeCollector->getBasePath();
}

/**
* {@inheritdoc}
*/
public function setBasePath(string $basePath): RouteCollectorProxyInterface
{
$this->basePath = $basePath;
$this->routeCollector->setBasePath($basePath);

return $this;
}

Expand Down Expand Up @@ -176,7 +177,7 @@ public function any(string $pattern, $callable): RouteInterface
*/
public function map(array $methods, string $pattern, $callable): RouteInterface
{
$pattern = $this->basePath . $pattern;
$pattern = $this->groupPattern . $pattern;

return $this->routeCollector->map($methods, $pattern, $callable);
}
Expand All @@ -186,7 +187,8 @@ public function map(array $methods, string $pattern, $callable): RouteInterface
*/
public function group(string $pattern, $callable): RouteGroupInterface
{
$pattern = $this->basePath . $pattern;
$pattern = $this->groupPattern . $pattern;

return $this->routeCollector->group($pattern, $callable);
}

Expand Down
7 changes: 3 additions & 4 deletions tests/Routing/RouteCollectorProxyTest.php
Expand Up @@ -100,16 +100,15 @@ public function testGetSetBasePath()
$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);
$callableResolverProphecy = $this->prophesize(CallableResolverInterface::class);
$containerProphecy = $this->prophesize(ContainerInterface::class);
$routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class);

$routeCollectorProxy = new RouteCollectorProxy(
$responseFactoryProphecy->reveal(),
$callableResolverProphecy->reveal(),
$containerProphecy->reveal(),
$routeCollectorProphecy->reveal(),
$basePath
$containerProphecy->reveal()
);

$routeCollectorProxy->setBasePath($basePath);

$this->assertEquals($basePath, $routeCollectorProxy->getBasePath());

$newBasePath = '/new/base/path';
Expand Down