From 2aaa820965fb12e7ee286575df93d33644580777 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Wed, 21 Sep 2011 23:40:12 -0700 Subject: [PATCH 01/10] Add a 'MustacheLoader' filesystem loader. --- Mustache.php | 5 ++- MustacheLoader.php | 85 ++++++++++++++++++++++++++++++++++++++ test/fixtures/bar.mustache | 1 + test/fixtures/foo.mustache | 1 + 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 MustacheLoader.php create mode 100644 test/fixtures/bar.mustache create mode 100644 test/fixtures/foo.mustache diff --git a/Mustache.php b/Mustache.php index 022488b..2bd9566 100644 --- a/Mustache.php +++ b/Mustache.php @@ -819,7 +819,10 @@ class Mustache { * @return string */ protected function _getPartial($tag_name) { - if (is_array($this->_partials) && isset($this->_partials[$tag_name])) { + if ( + (is_array($this->_partials) || $this->_partials instanceof ArrayAccess) + && isset($this->_partials[$tag_name]) + ) { return $this->_partials[$tag_name]; } diff --git a/MustacheLoader.php b/MustacheLoader.php new file mode 100644 index 0000000..9c4b386 --- /dev/null +++ b/MustacheLoader.php @@ -0,0 +1,85 @@ +baseDir = $baseDir; + $this->extension = $extension; + } + + /** + * @param string $offset Name of partial + * @return boolean + */ + public function offsetExists($offset) { + return (isset($this->partialsCache[$offset]) || file_exists($this->pathName($offset))); + } + + /** + * @throws InvalidArgumentException if the given partial doesn't exist + * @param string $offset Name of partial + * @return string Partial template contents + */ + public function offsetGet($offset) { + if (!$this->offsetExists($offset)) { + throw new InvalidArgumentException('Partial does not exist: ' . $offset); + } + + if (!isset($this->partialsCache[$offset])) { + $this->partialsCache[$offset] = file_get_contents($this->pathName($offset)); + } + + return $this->partialsCache[$offset]; + } + + /** + * MustacheLoader is an immutable filesystem loader. offsetSet throws a LogicException if called. + * + * @throws LogicException + * @return void + */ + public function offsetSet($offset, $value) { + throw new LogicException('Unable to set offset: MustacheLoader is an immutable ArrayAccess object.'); + } + + /** + * MustacheLoader is an immutable filesystem loader. offsetUnset throws a LogicException if called. + * + * @throws LogicException + * @return void + */ + public function offsetUnset($offset) { + throw new LogicException('Unable to unset offset: MustacheLoader is an immutable ArrayAccess object.'); + } + + /** + * An internal helper for generating path names. + * + * @param string $file Partial name + * @return string File path + */ + protected function pathName($file) { + return $this->baseDir . '/' . $file . '.' . $this->extension; + } +} diff --git a/test/fixtures/bar.mustache b/test/fixtures/bar.mustache new file mode 100644 index 0000000..139594d --- /dev/null +++ b/test/fixtures/bar.mustache @@ -0,0 +1 @@ +{{# truthy }}{{ bar }}{{/ truthy }} \ No newline at end of file diff --git a/test/fixtures/foo.mustache b/test/fixtures/foo.mustache new file mode 100644 index 0000000..008c714 --- /dev/null +++ b/test/fixtures/foo.mustache @@ -0,0 +1 @@ +{{ foo }} \ No newline at end of file From 5bc11ef1832f6e0a1ad2dd47207970fcc63ea3e3 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Thu, 22 Sep 2011 09:26:52 -0700 Subject: [PATCH 02/10] Test for the mustache loader. --- test/MustacheLoaderTest.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 test/MustacheLoaderTest.php diff --git a/test/MustacheLoaderTest.php b/test/MustacheLoaderTest.php new file mode 100644 index 0000000..ebb5745 --- /dev/null +++ b/test/MustacheLoaderTest.php @@ -0,0 +1,30 @@ +assertEquals(file_get_contents(dirname(__FILE__).'/fixtures/foo.mustache'), $loader['foo']); + $this->assertEquals(file_get_contents(dirname(__FILE__).'/fixtures/bar.mustache'), $loader['bar']); + } + + public function testMustacheUsesFilesystemLoader() { + $template = '{{> foo }}{{> bar }}'; + $data = array( + 'truthy' => true, + 'foo' => 'FOO', + 'bar' => 'BAR' + ); + $output = '{{ foo }}{{ bar }}'; + $m = new Mustache(); + $partials = new MustacheLoader(dirname(__FILE__).'/fixtures'); + $m->render($template, $data, $partials); + $this->assertEquals($output, $output); + } +} \ No newline at end of file From 69cce225e17c5b77dc8ffdc3e1494647d7a61860 Mon Sep 17 00:00:00 2001 From: chrisbarr Date: Wed, 26 Oct 2011 15:45:47 +0200 Subject: [PATCH 03/10] Incorrect variable name in example --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 830df90..730264f 100644 --- a/README.markdown +++ b/README.markdown @@ -44,7 +44,7 @@ Along with the associated Mustache class: Render it like so: render($template); ?> From f77cf3729de0e46d6187da2353a4ab438657b6fe Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 3 Jan 2012 14:00:12 -0800 Subject: [PATCH 04/10] Add an i18n example. --- examples/i18n/I18n.php | 20 ++++++++++++++++++++ examples/i18n/i18n.mustache | 1 + examples/i18n/i18n.txt | 1 + 3 files changed, 22 insertions(+) create mode 100644 examples/i18n/I18n.php create mode 100644 examples/i18n/i18n.mustache create mode 100644 examples/i18n/i18n.txt diff --git a/examples/i18n/I18n.php b/examples/i18n/I18n.php new file mode 100644 index 0000000..883cc9c --- /dev/null +++ b/examples/i18n/I18n.php @@ -0,0 +1,20 @@ + 'Hola.', + 'My name is {{ name }}.' => 'Me llamo {{ name }}.', + ); + + public function __trans($text) { + return isset($this->dictionary[$text]) ? $this->dictionary[$text] : $text; + } +} \ No newline at end of file diff --git a/examples/i18n/i18n.mustache b/examples/i18n/i18n.mustache new file mode 100644 index 0000000..eef169b --- /dev/null +++ b/examples/i18n/i18n.mustache @@ -0,0 +1 @@ +{{#__}}Hello.{{/__}} {{#__}}My name is {{ name }}.{{/__}} \ No newline at end of file diff --git a/examples/i18n/i18n.txt b/examples/i18n/i18n.txt new file mode 100644 index 0000000..650a71a --- /dev/null +++ b/examples/i18n/i18n.txt @@ -0,0 +1 @@ +Hola. Me llamo Bob. \ No newline at end of file From 3968949c0a20c6bccdca0e8c54033f4fe2bd4e64 Mon Sep 17 00:00:00 2001 From: Mogria Date: Sun, 8 Jan 2012 17:09:19 +0100 Subject: [PATCH 05/10] Added a PHP script to create new examples (tests) via command line Example of use: $ php create_example.php my_new_example /path/to/repo/examples/my_new_example /path/to/repo/examples/my_new_example/my_new_example.mustache /path/to/repo/examples/my_new_example/my_new_example.txt /path/to/repo/examples/my_new_example/MyNewExample.php $ cd examples/my_new_example/ $ ls my_new_example.mustache MyNewExample.php my_new_example.txt $ cat MyNewExample.php string + * AString -> a_string + * SomeStrings -> some_strings + * AStringMore -> a_string_more + * + * @param string $name + * @access public + * @return string + */ +function getLowerCaseName($name) { + return preg_replace_callback("/([A-Z])/", create_function ( + '$match', + 'return "_" . strtolower($match[1]);' + ), lcfirst($name)); +} + +/** + * transform a string to Uppercase (camelcase) + * Examples + * string -> String + * a_string -> AString + * some_strings -> SomeStrings + * a_string_more -> AStringMore -> a_string_more + * + * @param string $name + * @access public + * @return string + */ +function getUpperCaseName($name) { + return preg_replace_callback("/_([a-z])/", create_function ( + '$match', + 'return strtoupper($match{1});' + ), ucfirst($name)); +} + + +/** + * return the given value and echo it out appending "\n" + * + * @param mixed $value + * @access public + * @return mixed + */ +function out($value) { + echo $value . "\n"; + return $value; +} + +/** + * create Path for certain files in an example + * returns the directory name if only $directory is given. + * if an extension is given a complete filename is returned. + * the returned filename will be echoed out + * + * @param string $directory directory without / at the end + * @param string $filename filename without path and extension + * @param string $extension extension of the file without "." + * @access public + * @return string + */ +function buildPath($directory, $filename = null, $extension = null) { + return out(EXAMPLE_PATH . DS . $directory. + ($extension !== null && $filename !== null ? DS . $filename. "." . $extension : "")); +} + +/** + * creates the directory for the example + * the script die()'s if mkdir() fails + * + * @param string $directory + * @access public + * @return void + */ +function createDirectory($directory) { + if(!@mkdir(buildPath($directory))) { + die("FAILED to create directory\n"); + } +} + +/** + * create a file for the example with the given $content + * the script die()'s if fopen() fails + * + * @param string $directory directory without / at the end + * @param string $filename filename without path and extension + * @param string $extension extension of the file without "." + * @param string $content the content of the file + * @access public + * @return void + */ +function createFile($directory, $filename, $extension, $content = "") { + $handle = @fopen(buildPath($directory, $filename, $extension), "w"); + if($handle) { + fwrite($handle, $content); + fclose($handle); + } else { + die("FAILED to create file\n"); + } +} + + +/** + * routine to create the example directory and 3 files + * + * if the $example_name is "SomeThing" the following files will be created + * examples/some_thing + * examples/some_thing/some_thing.mustache + * examples/some_thing/some_thing.txt + * examples/some_thing/SomeThing.php + * + * @param mixed $example_name + * @access public + * @return void + */ +function main($example_name) { + $lowercase = getLowerCaseName($example_name); + $uppercase = getUpperCaseName($example_name); + createDirectory($lowercase); + createFile($lowercase, $lowercase, "mustache"); + createFile($lowercase, $lowercase, "txt"); + createFile($lowercase, $uppercase, "php", << 1) { + // get the name of the example + $example_name = $argv[1]; + + main($example_name); + +} else { + echo USAGE; +} From 5bf3ee00003bbe6559a4b4fe1b8d5dfd41bcb481 Mon Sep 17 00:00:00 2001 From: Mogria Date: Sun, 8 Jan 2012 18:59:35 +0100 Subject: [PATCH 06/10] create_example.php: added a `#!`-line and moved to `bin` directory --- create_example.php => bin/create_example.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) rename create_example.php => bin/create_example.php (95%) mode change 100644 => 100755 diff --git a/create_example.php b/bin/create_example.php old mode 100644 new mode 100755 similarity index 95% rename from create_example.php rename to bin/create_example.php index f5181d2..f895baa --- a/create_example.php +++ b/bin/create_example.php @@ -1,9 +1,10 @@ +#!/usr/bin/env php Date: Sun, 8 Jan 2012 16:08:50 -0800 Subject: [PATCH 07/10] Fix undefined `self` in the i18n example. --- examples/i18n/I18n.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/i18n/I18n.php b/examples/i18n/I18n.php index 883cc9c..980e649 100644 --- a/examples/i18n/I18n.php +++ b/examples/i18n/I18n.php @@ -6,7 +6,7 @@ class I18n extends Mustache { public $name = 'Bob'; // Add a {{#__}} lambda for i18n - public $__ = array(self, '__trans'); + public $__ = array(__CLASS__, '__trans'); // A *very* small i18n dictionary :) private $dictionary = array( From 38c5e9e8ea5ae8f25e6063c2fe843b103fe05c03 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 8 Jan 2012 16:19:58 -0800 Subject: [PATCH 08/10] Fix MustacheLoader test, add a second loader test. --- test/MustacheLoaderTest.php | 42 +++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/test/MustacheLoaderTest.php b/test/MustacheLoaderTest.php index ebb5745..2674a0f 100644 --- a/test/MustacheLoaderTest.php +++ b/test/MustacheLoaderTest.php @@ -15,16 +15,46 @@ class MustacheLoaderTest extends PHPUnit_Framework_TestCase { } public function testMustacheUsesFilesystemLoader() { - $template = '{{> foo }}{{> bar }}'; + $template = '{{> foo }} {{> bar }}'; $data = array( 'truthy' => true, 'foo' => 'FOO', - 'bar' => 'BAR' + 'bar' => 'BAR', ); - $output = '{{ foo }}{{ bar }}'; + $output = 'FOO BAR'; $m = new Mustache(); $partials = new MustacheLoader(dirname(__FILE__).'/fixtures'); - $m->render($template, $data, $partials); - $this->assertEquals($output, $output); + $this->assertEquals($output, $m->render($template, $data, $partials)); } -} \ No newline at end of file + + public function testMustacheUsesDifferentLoadersToo() { + $template = '{{> foo }} {{> bar }}'; + $data = array( + 'truthy' => true, + 'foo' => 'FOO', + 'bar' => 'BAR', + ); + $output = 'FOO BAR'; + $m = new Mustache(); + $partials = new DifferentMustacheLoader(); + $this->assertEquals($output, $m->render($template, $data, $partials)); + } +} + +class DifferentMustacheLoader implements ArrayAccess { + protected $partials = array( + 'foo' => '{{ foo }}', + 'bar' => '{{# truthy }}{{ bar }}{{/ truthy }}', + ); + + public function offsetExists($offset) { + return isset($this->partials[$offset]); + } + + public function offsetGet($offset) { + return $this->partials[$offset]; + } + + public function offsetSet($offset, $value) {} + public function offsetUnset($offset) {} +} From 236ba060caffca08b40b50c84df7957796739cce Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 8 Jan 2012 16:29:58 -0800 Subject: [PATCH 09/10] Code style updates. --- Mustache.php | 5 +- bin/create_example.php | 103 +++++++++++++++++++++-------------------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/Mustache.php b/Mustache.php index ce2739f..c87be98 100644 --- a/Mustache.php +++ b/Mustache.php @@ -819,10 +819,7 @@ class Mustache { * @return string */ protected function _getPartial($tag_name) { - if ( - (is_array($this->_partials) || $this->_partials instanceof ArrayAccess) - && isset($this->_partials[$tag_name]) - ) { + if ((is_array($this->_partials) || $this->_partials instanceof ArrayAccess) && isset($this->_partials[$tag_name])) { return $this->_partials[$tag_name]; } diff --git a/bin/create_example.php b/bin/create_example.php index f895baa..1f0b894 100755 --- a/bin/create_example.php +++ b/bin/create_example.php @@ -2,12 +2,15 @@ a_string * SomeStrings -> some_strings * AStringMore -> a_string_more - * - * @param string $name + * + * @param string $name * @access public * @return string */ function getLowerCaseName($name) { - return preg_replace_callback("/([A-Z])/", create_function ( - '$match', - 'return "_" . strtolower($match[1]);' - ), lcfirst($name)); + return preg_replace_callback("/([A-Z])/", create_function ( + '$match', + 'return "_" . strtolower($match[1]);' + ), lcfirst($name)); } /** @@ -49,29 +52,29 @@ function getLowerCaseName($name) { * a_string -> AString * some_strings -> SomeStrings * a_string_more -> AStringMore -> a_string_more - * - * @param string $name + * + * @param string $name * @access public * @return string */ function getUpperCaseName($name) { - return preg_replace_callback("/_([a-z])/", create_function ( - '$match', - 'return strtoupper($match{1});' - ), ucfirst($name)); + return preg_replace_callback("/_([a-z])/", create_function ( + '$match', + 'return strtoupper($match{1});' + ), ucfirst($name)); } /** * return the given value and echo it out appending "\n" - * - * @param mixed $value + * + * @param mixed $value * @access public * @return mixed */ function out($value) { - echo $value . "\n"; - return $value; + echo $value . "\n"; + return $value; } /** @@ -79,7 +82,7 @@ function out($value) { * returns the directory name if only $directory is given. * if an extension is given a complete filename is returned. * the returned filename will be echoed out - * + * * @param string $directory directory without / at the end * @param string $filename filename without path and extension * @param string $extension extension of the file without "." @@ -87,28 +90,28 @@ function out($value) { * @return string */ function buildPath($directory, $filename = null, $extension = null) { - return out(EXAMPLE_PATH . DS . $directory. - ($extension !== null && $filename !== null ? DS . $filename. "." . $extension : "")); + return out(EXAMPLE_PATH . DS . $directory. + ($extension !== null && $filename !== null ? DS . $filename. "." . $extension : "")); } /** * creates the directory for the example * the script die()'s if mkdir() fails - * - * @param string $directory + * + * @param string $directory * @access public * @return void */ function createDirectory($directory) { - if(!@mkdir(buildPath($directory))) { - die("FAILED to create directory\n"); - } + if(!@mkdir(buildPath($directory))) { + die("FAILED to create directory\n"); + } } /** * create a file for the example with the given $content * the script die()'s if fopen() fails - * + * * @param string $directory directory without / at the end * @param string $filename filename without path and extension * @param string $extension extension of the file without "." @@ -117,13 +120,13 @@ function createDirectory($directory) { * @return void */ function createFile($directory, $filename, $extension, $content = "") { - $handle = @fopen(buildPath($directory, $filename, $extension), "w"); - if($handle) { - fwrite($handle, $content); - fclose($handle); - } else { - die("FAILED to create file\n"); - } + $handle = @fopen(buildPath($directory, $filename, $extension), "w"); + if($handle) { + fwrite($handle, $content); + fclose($handle); + } else { + die("FAILED to create file\n"); + } } @@ -135,18 +138,18 @@ function createFile($directory, $filename, $extension, $content = "") { * examples/some_thing/some_thing.mustache * examples/some_thing/some_thing.txt * examples/some_thing/SomeThing.php - * - * @param mixed $example_name + * + * @param mixed $example_name * @access public * @return void */ function main($example_name) { - $lowercase = getLowerCaseName($example_name); - $uppercase = getUpperCaseName($example_name); - createDirectory($lowercase); - createFile($lowercase, $lowercase, "mustache"); - createFile($lowercase, $lowercase, "txt"); - createFile($lowercase, $uppercase, "php", << 1) { - // get the name of the example - $example_name = $argv[1]; - - main($example_name); + // get the name of the example + $example_name = $argv[1]; + + main($example_name); } else { - echo USAGE; + echo USAGE; } From 1764e6270bb42ee8dcfee0d12de2318b1556000f Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 8 Jan 2012 16:34:52 -0800 Subject: [PATCH 10/10] Version bump (0.9.0) --- Mustache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index c87be98..d0ff274 100644 --- a/Mustache.php +++ b/Mustache.php @@ -14,7 +14,7 @@ */ class Mustache { - const VERSION = '0.8.1'; + const VERSION = '0.9.0'; const SPEC_VERSION = '1.1.2'; /**