Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 4f38cd7

Browse files
committed
Allow path providers that don't add path elements each time
1 parent f843523 commit 4f38cd7

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

AutoRoute/BuilderContext.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ public function getFullPath()
103103
{
104104
$paths = array();
105105
foreach ($this->routeStacks as $routeStack) {
106-
$paths[] = $routeStack->getPath();
106+
$path = $routeStack->getPath();
107+
if (!empty($path)) {
108+
$paths[] = $path;
109+
}
107110
}
108111

109112
$path = implode('/', $paths);

AutoRoute/RouteStack.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
class RouteStack
1212
{
13-
protected $pathElements;
13+
protected $pathElements = array();
1414
protected $routes = array();
1515
protected $context;
1616
protected $existingRoute;
@@ -90,6 +90,10 @@ public function getPathElements()
9090
*/
9191
public function getPaths()
9292
{
93+
if (empty($this->pathElements)) {
94+
return array();
95+
}
96+
9397
$tmp = array();
9498

9599
foreach ($this->pathElements as $pathElement) {
@@ -135,7 +139,11 @@ public function getFullPath()
135139
$fullPath = $this->getPath();
136140

137141
if ($parentPath) {
138-
$fullPath = $parentPath.'/'.$fullPath;
142+
if (empty($fullPath)) {
143+
$fullPath = $parentPath;
144+
} else {
145+
$fullPath = $parentPath.'/'.$fullPath;
146+
}
139147
}
140148

141149
return $fullPath;

Tests/Unit/AutoRoute/BuilderContextTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ public function testStageAndCommitRouteStack()
2525
$this->assertCount(1, $this->builderContext->getRouteStacks());
2626
}
2727

28+
public function testIgnoreEmptyPath()
29+
{
30+
$this->routeStack->expects($this->at(3))
31+
->method('getPath')
32+
->will($this->returnValue('route'));
33+
34+
$this->routeStack->expects($this->at(4))
35+
->method('getPath')
36+
->will($this->returnValue(''));
37+
38+
$this->routeStack->expects($this->at(5))
39+
->method('getPath')
40+
->will($this->returnValue('foo/bar'));
41+
42+
$this->routeStack->expects($this->exactly(3))
43+
->method('isClosed')
44+
->will($this->returnValue(true));
45+
46+
for ($i = 0; $i < 3; $i++) {
47+
$this->builderContext->stageRouteStack($this->routeStack);
48+
$this->builderContext->commitRouteStack();
49+
}
50+
51+
$this->assertCount(3, $this->builderContext->getRouteStacks());
52+
$this->assertEquals('route/foo/bar', $this->builderContext->getFullPath());
53+
}
54+
2855
/**
2956
* @expectedException \RuntimeException
3057
*/

Tests/Unit/AutoRoute/RouteStackTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public function setUp()
1616
$this->route2 = new \stdClass;
1717
}
1818

19+
public function testGetEmptyPath()
20+
{
21+
$this->assertEmpty($this->routeStack->getPaths());
22+
$this->assertEquals('', $this->routeStack->getFullPath());
23+
}
24+
1925
public function testAddPathElement()
2026
{
2127
$this->routeStack->addPathElements(array('foo', 'bar'));

0 commit comments

Comments
 (0)