From e2818155c399b2760f167f8c6be4b53e84017b6c Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 14 Jun 2021 14:34:11 -0400 Subject: [PATCH] Support generating non-html files (#64) --- src/Page.php | 16 +++++++++++--- tests/PageTest.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tests/PageTest.php diff --git a/src/Page.php b/src/Page.php index 22fb9a0..ebfc8ae 100644 --- a/src/Page.php +++ b/src/Page.php @@ -45,7 +45,7 @@ protected function write($request) $html = $response->getContent(); - if (! $this->is404() && ! $this->files->exists($this->directory())) { + if (! $this->files->exists($this->directory())) { $this->files->makeDirectory($this->directory(), 0755, true); } @@ -56,7 +56,7 @@ protected function write($request) public function directory() { - return $this->config['destination'] . $this->url(); + return dirname($this->path()); } public function path() @@ -65,7 +65,17 @@ public function path() return $this->config['destination'] . '/404.html'; } - return $this->directory() . '/index.html'; + $url = $this->url(); + + $ext = pathinfo($url, PATHINFO_EXTENSION) ?: 'html'; + + $url = $this->config['destination'] . $url; + + if ($ext === 'html') { + $url .= '/index.html'; + } + + return $url; } public function url() diff --git a/tests/PageTest.php b/tests/PageTest.php new file mode 100644 index 0000000..2b4ad8d --- /dev/null +++ b/tests/PageTest.php @@ -0,0 +1,52 @@ +mock(Entry::class); + $entry->shouldReceive('urlWithoutRedirect')->andReturn('/foo/bar'); + + $page = $this->page($entry, ['destination' => '/path/to/static']); + + $this->assertEquals('/path/to/static/foo/bar/index.html', $page->path()); + $this->assertEquals('/path/to/static/foo/bar', $page->directory()); + } + + /** @test */ + public function it_gets_the_path_of_a_url_with_a_file_extension() + { + $entry = $this->mock(Entry::class); + $entry->shouldReceive('urlWithoutRedirect')->andReturn('/foo/bar/sitemap.xml'); + + $page = $this->page($entry, ['destination' => '/path/to/static']); + + $this->assertEquals('/path/to/static/foo/bar/sitemap.xml', $page->path()); + $this->assertEquals('/path/to/static/foo/bar', $page->directory()); + } + + /** @test */ + public function it_gets_the_path_of_the_404_url() + { + $entry = $this->mock(Entry::class); + $entry->shouldReceive('urlWithoutRedirect')->andReturn('/404'); + + $page = $this->page($entry, ['destination' => '/path/to/static']); + + $this->assertEquals('/path/to/static/404.html', $page->path()); + $this->assertEquals('/path/to/static', $page->directory()); + } + + private function page($entry, $config) + { + return new Page($this->mock(Filesystem::class), $config, $entry); + } +}