Skip to content

Commit

Permalink
Fix index filename loading for directories
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Aug 18, 2014
1 parent 357c67e commit e833f30
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/Dispatcher/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use \Gt\Api\ApiFactory;
use \Gt\Database\DatabaseFactory;
use \Gt\Response\NotFoundException;
use \Gt\Core\Path;

abstract class Dispatcher {

Expand Down Expand Up @@ -84,6 +85,7 @@ public function process() {
// Returning early will cause Start to create a Redirect object.
return $fixedUri;
}

// Get the requested filename, or index filename if none set.
$filename = $this->getFilename(
$this->request->uri,
Expand Down Expand Up @@ -126,7 +128,12 @@ public function process() {
*/
public function getFilename($uri, $indexFilename) {
$filename = basename($uri);
if(empty($filename) || substr($uri, -1) === "/") {
if(empty($filename)) {
$filename = $indexFilename;
}

$pvPath = Path::get(Path::PAGEVIEW) . $uri;
if(is_dir($pvPath)) {
$filename = $indexFilename;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ Feature: Test that users can surf between pages

Scenario: Access directory as file
Given I go to "/directory"
Then the response status code should be 302
# Then I should see "MultiPage Test"
# And I should see "This is the index of the directory"
Then I should see "MultiPage Test"
And I should see "This is the index of the directory"

Scenario: Access nested file
Given I go to "/directory/page-inside-directory"
Expand Down
12 changes: 10 additions & 2 deletions test/Unit/Dispatcher/PageDispatcher.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,20 @@ public function testCreateResponseContentThrowsTypeException() {
* @dataProvider data_uris
*/
public function testGetFilenameRequestedFromUri($uri) {
$filename = $this->dispatcher->getFilename($uri, "index");

if(substr($uri, -1) === "/") {
if(strlen($uri) > 1) {
$dirPath = $this->pageViewDir . $uri;

if(!is_dir($dirPath)) {
mkdir($dirPath, 0775, true);
}
}

$filename = $this->dispatcher->getFilename($uri, "index");
$this->assertEquals("index", $filename);
}
else {
$filename = $this->dispatcher->getFilename($uri, "index");
$this->assertEquals(basename($uri), $filename);
}
}
Expand Down

0 comments on commit e833f30

Please sign in to comment.