From 19fb6eac2195fa1a93cab26633c6a9fe86a558d8 Mon Sep 17 00:00:00 2001 From: Matt Hackmann Date: Tue, 27 Oct 2015 20:00:28 -0700 Subject: [PATCH 1/3] template file extension now configurable --- src/Dust/Dust.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) mode change 100644 => 100755 src/Dust/Dust.php diff --git a/src/Dust/Dust.php b/src/Dust/Dust.php old mode 100644 new mode 100755 index a051b55..92b7279 --- a/src/Dust/Dust.php +++ b/src/Dust/Dust.php @@ -18,7 +18,10 @@ class Dust implements \Serializable { public $autoloaderOverride; - public function __construct($parser = null, $evaluator = null) { + // Default template extension + private $_extension = 'dust'; + + public function __construct($parser = null, $evaluator = null, $options = null) { if ($parser === null) $parser = new Parse\Parser(); if ($evaluator === null) $evaluator = new Evaluate\Evaluator($this); $this->parser = $parser; @@ -47,6 +50,14 @@ public function __construct($parser = null, $evaluator = null) { "contextDump" => new Helper\ContextDump() ]; $this->automaticFilters = [$this->filters['h']]; + + if (is_array($options)) { + if (isset($options['extension'])) { + $extension = $options['extension']; + $this->_extension = $extension{0} === '.' ? substr($extension, 1) : $extension; + } + } + } public function compile($source, $name = null) { @@ -62,7 +73,10 @@ public function compileFn($source, $name = null) { public function resolveAbsoluteDustFilePath($path, $basePath = null) { //add extension if necessary - if (substr_compare($path, '.dust', -5, 5) !== 0) $path .= '.dust'; + $ext = explode('.', $path); + if (end($ext) !== $this->_extension) { + $path .= '.' . $this->_extension; + } if ($basePath != null) { $possible = realpath($basePath . '/' . $path); if ($possible !== false) return $possible; From 97d156ce24aba1443f83d1f964320578f0928928 Mon Sep 17 00:00:00 2001 From: Matt Hackmann Date: Tue, 27 Oct 2015 20:17:33 -0700 Subject: [PATCH 2/3] updated tests for custom extension --- tests/Dust/FilesystemTest.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) mode change 100644 => 100755 tests/Dust/FilesystemTest.php diff --git a/tests/Dust/FilesystemTest.php b/tests/Dust/FilesystemTest.php old mode 100644 new mode 100755 index b828c31..4ceba12 --- a/tests/Dust/FilesystemTest.php +++ b/tests/Dust/FilesystemTest.php @@ -3,7 +3,7 @@ class FilesystemTest extends DustTestBase { public static $dir; - + public static function setUpBeforeClass() { FilesystemTest::$dir = sys_get_temp_dir(); if (FilesystemTest::$dir[strlen(FilesystemTest::$dir) - 1] != '/') FilesystemTest::$dir .= '/'; @@ -18,8 +18,14 @@ public static function setUpBeforeClass() { //add child template $fileRes = file_put_contents(FilesystemTest::$dir . '/childTemplate.dust', '{>baseTemplate/}{dust->compileFile(FilesystemTest::$dir . '/childTemplate'); $expected = "before\noneDefault...newTwo...newThree\nafter"; $this->assertEquals($expected, $this->dust->renderTemplate($compiled, (object)[])); } - + public function testIncludedDirectories() { //add the dir $this->dust->includedDirectories[] = FilesystemTest::$dir; //now have something call the child $this->assertTemplate("Begin - before\noneDefault...newTwo...newThree\nafter - End", 'Begin - {>childTemplate/} - End', (object)[]); } - + + public function testCustomFileExtension() { + $dust = new Dust(null, null, [ 'extension' => 'tl' ]); + $dust->includedDirectories[] = FilesystemTest::$dir; + $template = $dust->compile('{>simpleTemplate/}'); + $render = $dust->renderTemplate($template, (object)[]); + $this->assertEquals($render, "line1\nline2"); + } + } \ No newline at end of file From e52f101c26fd70334fd2481892d5b193e2d60be2 Mon Sep 17 00:00:00 2001 From: Matt Hackmann Date: Tue, 27 Oct 2015 20:30:43 -0700 Subject: [PATCH 3/3] added helper name to unable to find helper exception (#10) --- src/Dust/Evaluate/Evaluator.php | 38 ++++++++++++++++----------------- tests/Dust/StandardTest.php | 20 +++++++++++------ 2 files changed, 33 insertions(+), 25 deletions(-) mode change 100644 => 100755 src/Dust/Evaluate/Evaluator.php mode change 100644 => 100755 tests/Dust/StandardTest.php diff --git a/src/Dust/Evaluate/Evaluator.php b/src/Dust/Evaluate/Evaluator.php old mode 100644 new mode 100755 index 097c51e..8ab9715 --- a/src/Dust/Evaluate/Evaluator.php +++ b/src/Dust/Evaluate/Evaluator.php @@ -5,23 +5,23 @@ use Dust\Filter; class Evaluator { public $dust; - + public $options; - + public function __construct(\Dust\Dust $dust, $options = null) { if ($options === null) $options = new EvaluatorOptions(); $this->dust = $dust; $this->options = $options; } - + public function error(Ast\Ast $ast = null, $message = null) { throw new EvaluateException($ast, $message); } - + public function evaluate(Ast\Body $source, $state) { return trim($this->evaluateBody($source, new Context($this, null, new State($state)), new Chunk($this))->out); } - + public function evaluateBody(Ast\Body $body, Context $ctx, Chunk $chunk) { //go ahead and set the file path on the current context $ctx->currentFilePath = $body->filePath; @@ -35,7 +35,7 @@ public function evaluateBody(Ast\Body $body, Context $ctx, Chunk $chunk) { } return $chunk; } - + public function evaluateSection(Ast\Section $section, Context $ctx, Chunk $chunk) { //stuff that doesn't need resolution if ($section->type == '+') { @@ -63,7 +63,7 @@ public function evaluateSection(Ast\Section $section, Context $ctx, Chunk $chunk } //do we have the helper? if (!isset($this->dust->helpers[$section->identifier->key])) { - $this->error($section->identifier, 'Unable to find helper'); + $this->error($section->identifier, 'Unable to find helper: ' . $section->identifier->key); } $helper = $this->dust->helpers[$section->identifier->key]; //build state w/ no current value @@ -135,7 +135,7 @@ public function evaluateSection(Ast\Section $section, Context $ctx, Chunk $chunk } return $chunk; } - + public function evaluateElseBody(Ast\Section $section, Context $ctx, Chunk $chunk) { if ($section->bodies != null && count($section->bodies) > 0) { foreach ($section->bodies as $value) { @@ -146,7 +146,7 @@ public function evaluateElseBody(Ast\Section $section, Context $ctx, Chunk $chun } return $chunk; } - + public function evaluatePartial(Ast\Partial $partial, Context $ctx, Chunk $chunk) { $partialName = $partial->key; if ($partialName == null) $partialName = $this->toDustString($this->normalizeResolved($ctx, $partial->inline, $chunk)); @@ -179,7 +179,7 @@ public function evaluatePartial(Ast\Partial $partial, Context $ctx, Chunk $chunk //render the partial then return $this->evaluateBody($partialBody, $ctx->pushState($state), $chunk); } - + public function evaluateSpecial(Ast\Special $spl, Context $ctx, Chunk $chunk) { switch ($spl->key) { case 'n': @@ -202,7 +202,7 @@ public function evaluateSpecial(Ast\Special $spl, Context $ctx, Chunk $chunk) { } return $chunk; } - + public function evaluateReference(Ast\Reference $ref, Context $ctx, Chunk $chunk) { //resolve $resolved = $this->normalizeResolved($ctx, $ctx->resolve($ref->identifier), $chunk); @@ -229,12 +229,12 @@ public function evaluateReference(Ast\Reference $ref, Context $ctx, Chunk $chunk } return $chunk; } - + public function evaluateBuffer(Ast\Buffer $buffer, Context $ctx, Chunk $chunk) { $chunk->write($buffer->contents); return $chunk; } - + public function evaluateParameters(array $params, Context $ctx) { $ret = []; foreach ($params as $value) { @@ -251,7 +251,7 @@ public function evaluateParameters(array $params, Context $ctx) { } return $ret; } - + public function normalizeResolved(Context $ctx, $resolved, Chunk $chunk, Ast\Section $section = null) { $handledSpecial = true; while ($handledSpecial) { @@ -274,14 +274,14 @@ public function normalizeResolved(Context $ctx, $resolved, Chunk $chunk, Ast\Sec } return $resolved; } - + public function isEmpty($val) { //numeric not empty if (is_numeric($val)) return false; //otherwise, normal empty check return empty($val); } - + public function exists($val) { //object exists if (is_object($val)) return true; @@ -296,14 +296,14 @@ public function exists($val) { //nulls do not exist return !is_null($val); } - + public function toDustString($val) { if (is_bool($val)) return $val ? 'true' : 'false'; if (is_array($val)) return implode(',', $val); if (is_object($val) && !method_exists($val, '__toString')) return get_class($val); return (string) $val; } - + public function handleCallback(Context $ctx, $callback, Chunk $chunk, Ast\Section $section = null) { //reset "this" on closures if ($callback instanceof \Closure) { @@ -341,5 +341,5 @@ public function handleCallback(Context $ctx, $callback, Chunk $chunk, Ast\Sectio //invoke return call_user_func_array($callback, $args); } - + } \ No newline at end of file diff --git a/tests/Dust/StandardTest.php b/tests/Dust/StandardTest.php old mode 100644 new mode 100755 index 464f473..02a3589 --- a/tests/Dust/StandardTest.php +++ b/tests/Dust/StandardTest.php @@ -28,15 +28,15 @@ public function testContextTypes() { //class $this->assertTemplate($expected, $template, new StoogesContext()); } - + public function testArrayAccess() { $this->assertTemplate('123', '{#items}{.}{/items}', (object)["items" => new \ArrayObject([1, 2, 3])]); } - + public function testStringIndex() { $this->assertTemplate('a => b,2 => c,foo => blah', '{#items}{$idx} => {.}{@sep},{/sep}{/items}', ["items" => ["a" => 'b', 2 => 'c', "foo" => 'blah']]); } - + public function testAutoloaderOverride() { //override $autoloaderInvoked = false; @@ -53,12 +53,12 @@ public function testAutoloaderOverride() { ]); $this->assertTrue($autoloaderInvoked); } - + public function testCustomFilter() { $this->dust->filters['stripTags'] = new StripTagsFilter(); $this->assertTemplate('Value: foo, bar', 'Value: {contents|stripTags}', (object)["contents" => '
foo,
bar
']); } - + public function testCustomHelper() { //from manual $this->dust->helpers['substr'] = function (Evaluate\Chunk $chunk, Evaluate\Context $ctx, Evaluate\Bodies $bodies, Evaluate\Parameters $params) { @@ -77,5 +77,13 @@ public function testCustomHelper() { //test some things (kinda taken from PHP manual) $this->assertTemplate('bcdef,bcd,abcd,abcdef,bc', '{@substr str="abcdef" begin=1 /},' . '{@substr str="abcdef" begin=1 len=3 /},' . '{@substr str="abcdef" len=4 /},' . '{@substr str="abcdef" len=8 /},' . '{@substr str="abcdef" begin=1 end=3 /}', (object)[]); } - + + public function testCustomHelperNotFound() { + try { + $this->assertTemplate('NULL', '{@customHelper param=myParam/}', (object)[]); + } catch (Evaluate\EvaluateException $exc) { + $this->assertEquals('Unable to find helper: customHelper', $exc->getMessage()); + } + } + } \ No newline at end of file