Skip to content

Commit

Permalink
Merge pull request #2 from minicli/fallback-tpldir
Browse files Browse the repository at this point in the history
Adding fallback mechanism
  • Loading branch information
erikaheidi authored Jul 13, 2023
2 parents e3d0d06 + 6643e83 commit 3102f84
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/Stencil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.");
}
}
3 changes: 3 additions & 0 deletions tests/Assets/Fallback/othertemplate.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## This is my Other Template

My name is {{ name }} and I am a {{ description }}.
4 changes: 3 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@

function getStencil(): Stencil
{
return new Stencil(__DIR__ . '/Assets');
$stencil = new Stencil(__DIR__ . '/Assets');
$stencil->fallbackTo([__DIR__ . '/Assets/Fallback']);
return $stencil;
}
11 changes: 10 additions & 1 deletion tests/StencilTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?php

use Minicli\FileNotFoundException;

it('sets the template dir', function () {
$stencil = getStencil();
expect($stencil->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', []);
});

Expand Down

0 comments on commit 3102f84

Please sign in to comment.