Skip to content

Commit c0628d5

Browse files
authored
Merge pull request #31 from xp-forge/feature/templates-args
Make withTemplates() accept folder references and in-memory declarations
2 parents 984e979 + d339dcd commit c0628d5

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/main/php/com/handlebarsjs/HandlebarsEngine.class.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace com\handlebarsjs;
22

3-
use com\github\mustache\{Context, DataContext, Template};
3+
use com\github\mustache\{Context, DataContext, Template, FilesIn, InMemory};
4+
use io\{Folder, Path};
45
use lang\IllegalArgumentException;
56
use util\log\{LogCategory, LogLevel};
67

@@ -105,11 +106,17 @@ public function withLogger($logger) {
105106
/**
106107
* Sets template loader to be used
107108
*
108-
* @param com.github.mustache.templates.Templates|com.github.mustache.TemplateLoader $l
109+
* @param string|io.Folder|io.Path|[:string]|com.github.mustache.templates.Templates $arg
109110
* @return self this
110111
*/
111-
public function withTemplates($l) {
112-
$this->templates->delegate($l);
112+
public function withTemplates($arg) {
113+
if ($arg instanceof Folder || $arg instanceof Path || is_string($arg)) {
114+
$this->templates->delegate(new FilesIn($arg, ['.handlebars']));
115+
} else if (is_array($arg)) {
116+
$this->templates->delegate(new InMemory($arg));
117+
} else {
118+
$this->templates->delegate($arg);
119+
}
113120
return $this;
114121
}
115122

src/test/php/com/handlebarsjs/unittest/EngineTest.class.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
<?php namespace com\handlebarsjs\unittest;
22

3+
use com\github\mustache\{FilesIn, InMemory};
34
use com\handlebarsjs\{HandlebarsEngine, HandlebarsParser};
45
use io\streams\MemoryOutputStream;
5-
use lang\IllegalArgumentException;
6-
use test\{Assert, Expect, Test};
6+
use io\{Path, File, Files, Folder};
7+
use lang\{IllegalArgumentException, Environment};
8+
use test\{Assert, Expect, Test, Values};
79
use util\log\LogCategory;
810

911
class EngineTest {
1012

13+
/** @return iterable */
14+
private function files() {
15+
yield [function($temp) { return $temp->getURI(); }, 'string'];
16+
yield [function($temp) { return new Folder($temp->getURI()); }, 'io.Folder'];
17+
yield [function($temp) { return new Path($temp->getURI()); }, 'io.Path'];
18+
yield [function($temp) { return new FilesIn($temp, ['.handlebars']); }, 'com.github.mustache.FilesIn'];
19+
}
20+
1121
#[Test]
1222
public function can_create() {
1323
new HandlebarsEngine();
@@ -67,4 +77,38 @@ public function version() { return '1.0.0'; }
6777
});
6878
Assert::equals('1.0.0', $engine->parser()->version());
6979
}
80+
81+
#[Test]
82+
public function templates_initially_empty() {
83+
$engine= new HandlebarsEngine();
84+
Assert::equals([], $engine->templates()->listing()->templates());
85+
}
86+
87+
#[Test]
88+
public function templates_from_loader() {
89+
$engine= (new HandlebarsEngine())->withTemplates(new InMemory(['test' => 'Hello {{name}}']));
90+
Assert::equals(['test'], $engine->templates()->listing()->templates());
91+
}
92+
93+
#[Test]
94+
public function templates_from_map() {
95+
$engine= (new HandlebarsEngine())->withTemplates(['test' => 'Hello {{name}}']);
96+
Assert::equals(['test'], $engine->templates()->listing()->templates());
97+
}
98+
99+
#[Test, Values(from: 'files')]
100+
public function templates_from_files($arg) {
101+
$temp= new Folder(Environment::tempDir(), md5(self::class));
102+
$temp->create();
103+
104+
try {
105+
Files::write(new File($temp, 'test.handlebars'), 'Hello {{name}}');
106+
Files::write(new File($temp, 'translations.csv'), 'Not included in listing');
107+
108+
$engine= (new HandlebarsEngine())->withTemplates($arg($temp));
109+
Assert::equals(['test'], $engine->templates()->listing()->templates());
110+
} finally {
111+
$temp->unlink();
112+
}
113+
}
70114
}

0 commit comments

Comments
 (0)