From 39dad1c350fbe9bd399bb8e89c2f6fda0889f65e Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Sat, 1 Nov 2014 12:16:09 +0000 Subject: [PATCH] Refactor header-footer loading --- src/Dispatcher/PageDispatcher.php | 51 +++++++++++-------- .../test/Acceptance/Features/render.feature | 1 + 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/Dispatcher/PageDispatcher.php b/src/Dispatcher/PageDispatcher.php index bd11b7cb..f15e4647 100644 --- a/src/Dispatcher/PageDispatcher.php +++ b/src/Dispatcher/PageDispatcher.php @@ -71,25 +71,11 @@ public function loadSource($path, $pathFile) { switch($specialName) { case "header": - if(empty($headerSource)) { - $headerSource = file_get_contents($fullPath); - - if(strpos($extension, "htm") !== 0) { - $headerSource = Transformer::toHtml( - $headerSource, $extension); - } - } + $this->readSourceContent($fullPath, $headerSource, true); break; case "footer": - if(empty($footerSource)) { - $footerSource = file_get_contents($fullPath); - - if(strpos($extension, "htm") !== 0) { - $footerSource = Transformer::toHtml( - $footerSource, $extension); - } - } + $this->readSourceContent($fullPath, $footerSource, true); break; } } @@ -115,11 +101,7 @@ public function loadSource($path, $pathFile) { if(strcasecmp($fileBase, $pathFileBase) === 0) { $fullPath = implode("/", [$path, $fileName]); - $source .= file_get_contents($fullPath); - } - - if(strpos($extension, "htm") !== 0) { - $source = Transformer::toHtml($source, $extension); + $this->readSourceContent($fullPath, $source); } } @@ -132,6 +114,33 @@ public function loadSource($path, $pathFile) { throw new NotFoundException(implode("/", [$path, $pathFile])); } +/** + * If $out variable is empty, reads the source file provided, transforming + * where necessary, providing $out with the HTML source. + * + * @param string $fullPath Absolute path to source file on disk + * @param string &$out Reference to variable to write HTML source to + * @param boolean $replaceOut When true, $out's contents will be replaced. When + * false, if $out has contents already it will be ignored + */ +private function readSourceContent($fullPath, &$out, $replaceOut = false) { + if($replaceOut + && !empty($out)) { + return; + } + + $source = file_get_contents($fullPath); + $extension = pathinfo($fullPath, PATHINFO_EXTENSION); + if(strpos($extension, "htm") !== 0) { + $source = Transformer::toHtml($source, $extension); + } + + $out = $source; +} + +/** + * + */ public function createResponseContent($html, $config) { if(!is_string($html)) { throw new \Gt\Core\Exception\InvalidArgumentTypeException( diff --git a/test/Acceptance/HeaderFooter/test/Acceptance/Features/render.feature b/test/Acceptance/HeaderFooter/test/Acceptance/Features/render.feature index 0f050f6d..c388b367 100644 --- a/test/Acceptance/HeaderFooter/test/Acceptance/Features/render.feature +++ b/test/Acceptance/HeaderFooter/test/Acceptance/Features/render.feature @@ -25,4 +25,5 @@ Feature: Test that HTML headers and footers are included Given I go to "/directory-with-own-header" Then I should see "This page should have its own header" And I should see "Test Website (overridden header)" + And I should not see "Test Website (base header)" And I should see "This is the footer" \ No newline at end of file