From 6643e832ae233eb9862eaddf39a8091bf7a50b9a Mon Sep 17 00:00:00 2001 From: Erika Heidi Date: Thu, 13 Jul 2023 10:42:22 -0300 Subject: [PATCH] Adding fallback mechanism --- src/Stencil.php | 25 ++++++++++++++++++++++++- tests/Assets/Fallback/othertemplate.tpl | 3 +++ tests/Pest.php | 4 +++- tests/StencilTest.php | 11 ++++++++++- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/Assets/Fallback/othertemplate.tpl diff --git a/src/Stencil.php b/src/Stencil.php index 25c31da..3ca18e0 100644 --- a/src/Stencil.php +++ b/src/Stencil.php @@ -5,12 +5,23 @@ class Stencil { public string $stencilDir; + public array $fallbackDirs = []; public function __construct(string $stencilDir) { $this->stencilDir = $stencilDir; } + /** + * Additional template dirs to look for + * @param array $fallbackDirs + * @return void + */ + public function fallbackTo(array $fallbackDirs) + { + $this->fallbackDirs = $fallbackDirs; + } + /** * @throws FileNotFoundException */ @@ -46,9 +57,21 @@ public function getTemplate(string $template): string $templateFile = $this->stencilDir . '/' . $template . '.tpl'; if (!is_file($templateFile)) { - throw new FileNotFoundException("Template file not found."); + $templateFile = $this->locateFallbackTemplate($template); } return file_get_contents($templateFile); } + + public function locateFallbackTemplate(string $template): string + { + foreach ($this->fallbackDirs as $fallbackDir) { + $templateFile = $fallbackDir . '/' . $template . '.tpl'; + if (is_file($templateFile)) { + return $templateFile; + } + } + + throw new FileNotFoundException("Template file not found."); + } } diff --git a/tests/Assets/Fallback/othertemplate.tpl b/tests/Assets/Fallback/othertemplate.tpl new file mode 100644 index 0000000..f509413 --- /dev/null +++ b/tests/Assets/Fallback/othertemplate.tpl @@ -0,0 +1,3 @@ +## This is my Other Template + +My name is {{ name }} and I am a {{ description }}. \ No newline at end of file diff --git a/tests/Pest.php b/tests/Pest.php index dcb4123..a40851f 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -43,5 +43,7 @@ function getStencil(): Stencil { - return new Stencil(__DIR__ . '/Assets'); + $stencil = new Stencil(__DIR__ . '/Assets'); + $stencil->fallbackTo([__DIR__ . '/Assets/Fallback']); + return $stencil; } diff --git a/tests/StencilTest.php b/tests/StencilTest.php index 13d84bf..9886b4c 100644 --- a/tests/StencilTest.php +++ b/tests/StencilTest.php @@ -1,13 +1,22 @@ stencilDir)->toBeString(); }); +it('falls back to secondary tpl dir when tpl not found in default location', function () { + $stencil = getStencil(); + $parsedContent = $stencil->applyTemplate('othertemplate', []); + expect($parsedContent)->toBeString(); + $this->assertStringContainsString("This is my Other Template", $parsedContent); +}); + it('throws exception if template not found', function () { $stencil = getStencil(); - $this->expectException(\Minicli\FileNotFoundException::class); + $this->expectException(FileNotFoundException::class); $stencil->applyTemplate('notfound', []); });