Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/main/php/com/handlebarsjs/HandlebarsEngine.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace com\handlebarsjs;

use com\github\mustache\{Context, DataContext, Template};
use com\github\mustache\{Context, DataContext, Template, FilesIn, InMemory};
use io\{Folder, Path};
use lang\IllegalArgumentException;
use util\log\{LogCategory, LogLevel};

Expand Down Expand Up @@ -105,11 +106,17 @@ public function withLogger($logger) {
/**
* Sets template loader to be used
*
* @param com.github.mustache.templates.Templates|com.github.mustache.TemplateLoader $l
* @param string|io.Folder|io.Path|[:string]|com.github.mustache.templates.Templates $arg
* @return self this
*/
public function withTemplates($l) {
$this->templates->delegate($l);
public function withTemplates($arg) {
if ($arg instanceof Folder || $arg instanceof Path || is_string($arg)) {
$this->templates->delegate(new FilesIn($arg, ['.handlebars']));
} else if (is_array($arg)) {
$this->templates->delegate(new InMemory($arg));
} else {
$this->templates->delegate($arg);
}
return $this;
}

Expand Down
48 changes: 46 additions & 2 deletions src/test/php/com/handlebarsjs/unittest/EngineTest.class.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<?php namespace com\handlebarsjs\unittest;

use com\github\mustache\{FilesIn, InMemory};
use com\handlebarsjs\{HandlebarsEngine, HandlebarsParser};
use io\streams\MemoryOutputStream;
use lang\IllegalArgumentException;
use test\{Assert, Expect, Test};
use io\{Path, File, Files, Folder};
use lang\{IllegalArgumentException, Environment};
use test\{Assert, Expect, Test, Values};
use util\log\LogCategory;

class EngineTest {

/** @return iterable */
private function files() {
yield [function($temp) { return $temp->getURI(); }, 'string'];
yield [function($temp) { return new Folder($temp->getURI()); }, 'io.Folder'];
yield [function($temp) { return new Path($temp->getURI()); }, 'io.Path'];
yield [function($temp) { return new FilesIn($temp, ['.handlebars']); }, 'com.github.mustache.FilesIn'];
}

#[Test]
public function can_create() {
new HandlebarsEngine();
Expand Down Expand Up @@ -67,4 +77,38 @@ public function version() { return '1.0.0'; }
});
Assert::equals('1.0.0', $engine->parser()->version());
}

#[Test]
public function templates_initially_empty() {
$engine= new HandlebarsEngine();
Assert::equals([], $engine->templates()->listing()->templates());
}

#[Test]
public function templates_from_loader() {
$engine= (new HandlebarsEngine())->withTemplates(new InMemory(['test' => 'Hello {{name}}']));
Assert::equals(['test'], $engine->templates()->listing()->templates());
}

#[Test]
public function templates_from_map() {
$engine= (new HandlebarsEngine())->withTemplates(['test' => 'Hello {{name}}']);
Assert::equals(['test'], $engine->templates()->listing()->templates());
}

#[Test, Values(from: 'files')]
public function templates_from_files($arg) {
$temp= new Folder(Environment::tempDir(), md5(self::class));
$temp->create();

try {
Files::write(new File($temp, 'test.handlebars'), 'Hello {{name}}');
Files::write(new File($temp, 'translations.csv'), 'Not included in listing');

$engine= (new HandlebarsEngine())->withTemplates($arg($temp));
Assert::equals(['test'], $engine->templates()->listing()->templates());
} finally {
$temp->unlink();
}
}
}
Loading