Skip to content

Commit

Permalink
Merge pull request #2844 from piotr-cz/bugfix/basepath-handling
Browse files Browse the repository at this point in the history
Handle base path by routeCollector instead of RouteCollectorProxy
  • Loading branch information
l0gicgate committed Sep 25, 2019
2 parents b63b7d4 + 6912d53 commit d084972
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
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

0 comments on commit d084972

Please sign in to comment.