Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php namespace com\github\mustache;

use util\Objects;
use text\StreamTokenizer;
use com\github\mustache\templates\Templates;
use com\github\mustache\templates\Source;
use com\github\mustache\templates\NotFound;

/**
* File-based template loading loads templates from the file system.
*
* @test xp://com.github.mustache.unittest.FileBasedTemplateLoaderTest
*/
abstract class FileBasedTemplateLoader implements TemplateLoader, WithListing {
abstract class FileBasedTemplateLoader implements Templates, WithListing {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WithListing interface should be merged into the Templates class.

protected $base, $extensions, $listing;

/**
Expand Down Expand Up @@ -53,9 +57,10 @@ protected abstract function inputStreamFor($name);
public function load($name) {
$variants= $this->variantsOf($name);
foreach ($variants as $variant) {
if ($stream= $this->inputStreamFor($variant)) return $stream;
if ($stream= $this->inputStreamFor($variant)) return new Source(new StreamTokenizer($stream));
}
throw new TemplateNotFoundException('Cannot find template ['.implode(', ', $variants).'] in '.Objects::stringOf($this->base));

return new NotFound('Cannot find template ['.implode(', ', $variants).'] in '.Objects::stringOf($this->base));
}

/**
Expand Down
25 changes: 14 additions & 11 deletions src/main/php/com/github/mustache/InMemory.class.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php namespace com\github\mustache;

use io\streams\MemoryInputStream;
use text\StringTokenizer;
use com\github\mustache\templates\Templates;
use com\github\mustache\templates\Source;
use com\github\mustache\templates\NotFound;

/**
* Template loading
*
* @test xp://com.github.mustache.unittest.InMemoryTest
*/
class InMemory implements TemplateLoader, WithListing {
class InMemory implements Templates, WithListing {
protected $templates, $listing;

/**
Expand Down Expand Up @@ -54,18 +57,18 @@ public function add($name, $bytes) {
return $this;
}

/**
* Load a template by a given name
*
* @param string $name The template name without file extension
* @return io.streams.InputStream
* @throws com.github.mustache.TemplateNotFoundException
*/
/**
* Load a template by a given name
*
* @param string $name The template name, not including the file extension
* @return com.github.mustache.TemplateSource
*/
public function load($name) {
if (isset($this->templates[$name])) {
return new MemoryInputStream($this->templates[$name]);
return new Source(new StringTokenizer($this->templates[$name]));
} else {
return new NotFound('Cannot find template '.$name);
}
throw new TemplateNotFoundException('Cannot find template '.$name);
}

/**
Expand Down
41 changes: 22 additions & 19 deletions src/main/php/com/github/mustache/MustacheEngine.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php namespace com\github\mustache;

use text\StreamTokenizer;
use com\github\mustache\templates\Input;
use com\github\mustache\templates\Templates;
use com\github\mustache\templates\FromLoader;
use text\StringTokenizer;

/**
Expand All @@ -23,8 +25,7 @@
* @see http://mustache.github.io/mustache.5.html
*/
class MustacheEngine {
protected $templates;
protected $parser;
protected $templates, $parser;
public $helpers= [];

/**
Expand All @@ -38,18 +39,22 @@ public function __construct() {
/**
* Sets template loader to be used
*
* @param com.github.mustache.TemplateLoader $l
* @param com.github.mustache.templates.Templates|com.github.mustache.TemplateLoader $l
* @return self this
*/
public function withTemplates(TemplateLoader $l) {
$this->templates= $l;
public function withTemplates($l) {
if ($l instanceof Templates) {
$this->templates= $l;
} else {
$this->templates= new FromLoader($l);
}
return $this;
}

/**
* Sets template loader to be used
* Gets used template loader
*
* @return com.github.mustache.TemplateLoader
* @return com.github.mustache.Templates
*/
public function getTemplates() {
return $this->templates;
Expand Down Expand Up @@ -92,15 +97,15 @@ public function withHelpers(array $helpers) {
/**
* Compile a template.
*
* @param string $template The template, as a string
* @param string|com.github.mustache.templates.Input $template The template
* @param string $start Initial start tag, defaults to "{{"
* @param string $end Initial end tag, defaults to "}}"
* @param string $indent Indenting level, defaults to no indenting
* @return com.github.mustache.Template
*/
public function compile($template, $start= '{{', $end= '}}', $indent= '') {
return new Template('<string>', $this->parser->parse(
new StringTokenizer($template),
$template instanceof Input ? $template->tokens() : new StringTokenizer($template),
$start,
$end,
$indent
Expand All @@ -118,7 +123,7 @@ public function compile($template, $start= '{{', $end= '}}', $indent= '') {
*/
public function load($name, $start= '{{', $end= '}}', $indent= '') {
return new Template($name, $this->parser->parse(
new StreamTokenizer($this->templates->load($name)),
$this->templates->load($name)->tokens(),
$start,
$end,
$indent
Expand All @@ -142,22 +147,20 @@ public function evaluate(Template $template, $arg) {
}

/**
* Render a template - like evaluate(), but will compile if necessary.
* Render a template, compiling it from source
*
* @param var $template The template, either as string or as compiled Template instance
* @param string|com.github.mustache.templates.Input $template
* @param var $arg Either a view context, or a Context instance
* @param string $start Initial start tag, defaults to "{{"
* @param string $end Initial end tag, defaults to "}}"
* @param string $indent Indenting level, defaults to no indenting
* @return string The rendered output
*/
public function render($template, $arg, $start= '{{', $end= '}}', $indent= '') {
if ($template instanceof Template) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was dropped, use evaluate() if necessary.

$target= $template;
} else {
$target= $this->compile($template, $start, $end, $indent);
}
return $this->evaluate($target, $arg);
return $this->evaluate(
$this->compile($template, $start, $end, $indent),
$arg
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/php/com/github/mustache/TemplateLoader.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* Template loading
*
* @deprecated Use com.github.mustache.templates.Templates instead!
* @test xp://com.github.mustache.unittest.TemplateTransformationTest
*/
interface TemplateLoader {
Expand Down
31 changes: 31 additions & 0 deletions src/main/php/com/github/mustache/templates/FromLoader.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php namespace com\github\mustache\templates;

use text\StreamTokenizer;

/**
* Adapter for TemplateLoaders
*
* @deprecated Template loaders were replaced by `Templates`.
*/
class FromLoader implements Templates {
private $loader;

/** @param com.github.mustache.TemplateLoader $loader */
public function __construct($loader) {
$this->loader= $loader;
}

/**
* Load a template by a given name
*
* @param string $name The template name, not including the file extension
* @return com.github.mustache.TemplateSource
*/
public function load($name) {
try {
return new Source(new StreamTokenizer($this->loader->load($name)));
} catch (TemplateNotFoundException $e) {
return new NotFound($e->getMessage());
}
}
}
15 changes: 15 additions & 0 deletions src/main/php/com/github/mustache/templates/Input.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace com\github\mustache\templates;

interface Input {

/** @return bool */
public function exists();

/**
* Returns tokens
*
* @return text.Tokenizer
* @throws com.github.mustache.TemplateNotFoundException
*/
public function tokens();
}
23 changes: 23 additions & 0 deletions src/main/php/com/github/mustache/templates/NotFound.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php namespace com\github\mustache\templates;

use com\github\mustache\TemplateNotFoundException;

class NotFound implements Input {
private $reason;

/** @param string $reason */
public function __construct($reason) {
$this->reason= $reason;
}

/** @return bool */
public function exists() { return false; }

/**
* Returns tokens
*
* @return text.Tokenizer
* @throws com.github.mustache.TemplateNotFoundException
*/
public function tokens() { throw new TemplateNotFoundException($this->reason); }
}
21 changes: 21 additions & 0 deletions src/main/php/com/github/mustache/templates/Source.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php namespace com\github\mustache\templates;

class Source implements Input {
private $tokens;

/** @param text.Tokenizer $tokens */
public function __construct($tokens) {
$this->tokens= $tokens;
}

/** @return bool */
public function exists() { return true; }

/**
* Returns tokens
*
* @return text.Tokenizer
* @throws com.github.mustache.TemplateNotFoundException
*/
public function tokens() { return $this->tokens; }
}
18 changes: 18 additions & 0 deletions src/main/php/com/github/mustache/templates/Templates.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php namespace com\github\mustache\templates;

/**
* Template loading
*
* @test xp://com.github.mustache.unittest.TemplateTransformationTest
*/
interface Templates {

/**
* Load a template by a given name
*
* @param string $name The template name, not including the file extension
* @return com.github.mustache.templates.Source
*/
public function load($name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use com\github\mustache\FilesIn;
use com\github\mustache\TemplateNotFoundException;
use lang\System;
use io\streams\Streams;
use io\Folder;
use io\File;
use io\FileUtil;
Expand Down Expand Up @@ -43,13 +42,13 @@ public function load_from_default_class_loader() {
$loader= new FilesIn(self::$temp);
$this->assertEquals(
'Mustache template {{id}}',
Streams::readAll($loader->load('test'))
$loader->load('test')->tokens()->nextToken("\n")
);
}

#[@test, @expect(TemplateNotFoundException::class)]
#[@test]
public function load_non_existant() {
(new FilesIn(self::$temp))->load('@non.existant@');
$this->assertFalse((new FilesIn(self::$temp))->load('@non.existant@')->exists());
}

#[@test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?php namespace com\github\mustache\unittest;

use com\github\mustache\InMemory;
use com\github\mustache\TemplateNotFoundException;
use io\streams\Streams;

class InMemoryTest extends \unittest\TestCase {

#[@test]
public function load() {
$content= 'Mustache template {{id}}';
$loader= new InMemory(['test' => $content]);
$this->assertEquals($content, Streams::readAll($loader->load('test')));
$this->assertEquals($content, $loader->load('test')->tokens()->nextToken("\n"));
}

#[@test, @expect(TemplateNotFoundException::class)]
#[@test]
public function load_non_existant() {
(new InMemory())->load('@non.existant@');
$this->assertFalse((new InMemory())->load('@non.existant@')->exists());
}

#[@test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php namespace com\github\mustache\unittest;

use com\github\mustache\ResourcesIn;
use com\github\mustache\TemplateNotFoundException;
use lang\ClassLoader;
use io\streams\Streams;

class ResourcesInTest extends \unittest\TestCase {

Expand All @@ -12,13 +10,13 @@ public function load_from_default_class_loader() {
$loader= new ResourcesIn(ClassLoader::getDefault());
$this->assertEquals(
'Mustache template {{id}}',
Streams::readAll($loader->load('com/github/mustache/unittest/template'))
$loader->load('com/github/mustache/unittest/template')->tokens()->nextToken("\n")
);
}

#[@test, @expect(TemplateNotFoundException::class)]
#[@test]
public function load_non_existant() {
(new ResourcesIn(ClassLoader::getDefault()))->load('@non.existant@');
$this->assertFalse((new ResourcesIn(ClassLoader::getDefault()))->load('@non.existant@')->exists());
}

#[@test]
Expand Down