Merge pull request #85 from bobthecow/feature/5.2-support

Make this hotness work with PHP 5.2
This commit is contained in:
Justin Hileman
2012-03-17 23:17:41 -07:00
35 changed files with 353 additions and 492 deletions
+2 -2
View File
@@ -11,7 +11,7 @@ A quick example:
```php
<?php
$m = new Mustache;
$m = new Mustache_Mustache;
echo $m->render('Hello {{planet}}', array('planet' => 'World!')); // "Hello World!"
```
@@ -48,7 +48,7 @@ And render it:
```php
<?php
$m = new Mustache;
$m = new Mustache_Mustache;
$chris = new Chris;
echo $m->render($template, $chris);
```
+1 -1
View File
@@ -13,7 +13,7 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=5.2.4"
},
"autoload": {
"psr-0": { "Mustache": "src/" }
+10 -8
View File
@@ -9,30 +9,32 @@
* file that was distributed with this source code.
*/
namespace Mustache;
/**
* Mustache class autoloader.
*/
class Autoloader {
class Mustache_Autoloader {
private $baseDir;
/**
* Autoloader constructor.
*
* @param string $baseDir Mustache library base directory (default: __DIR__.'/..')
* @param string $baseDir Mustache library base directory (default: dirname(__FILE__).'/..')
*/
public function __construct($baseDir = null) {
$this->baseDir = rtrim($baseDir, '/') ?: __DIR__.'/..';
if ($baseDir === null) {
$this->baseDir = dirname(__FILE__).'/..';
} else {
$this->baseDir = rtrim($baseDir, '/');
}
}
/**
* Register a new instance as an SPL autoloader.
*
* @param string $baseDir Mustache library base directory (default: __DIR__.'/..')
* @param string $baseDir Mustache library base directory (default: dirname(__FILE__).'/..')
*
* @return \Mustache\Autoloader Registered Autoloader instance
* @return Mustache_Autoloader Registered Autoloader instance
*/
static public function register($baseDir = null) {
$loader = new self($baseDir);
@@ -55,7 +57,7 @@ class Autoloader {
return;
}
$file = sprintf('%s/%s.php', $this->baseDir, str_replace('\\', '/', $class));
$file = sprintf('%s/%s.php', $this->baseDir, str_replace('_', '/', $class));
if (is_file($file)) {
require $file;
}
+30 -32
View File
@@ -9,14 +9,12 @@
* file that was distributed with this source code.
*/
namespace Mustache;
/**
* Mustache Compiler class.
*
* This class is responsible for turning a Mustache token parse tree into normal PHP source code.
*/
class Compiler {
class Mustache_Compiler {
private $sections;
private $source;
@@ -46,7 +44,7 @@ class Compiler {
/**
* Helper function for walking the Mustache token parse tree.
*
* @throws \InvalidArgumentException upon encountering unknown token types.
* @throws InvalidArgumentException upon encountering unknown token types.
*
* @param array $tree Parse tree of Mustache tokens
* @param int $level (default: 0)
@@ -57,46 +55,46 @@ class Compiler {
$code = '';
$level++;
foreach ($tree as $node) {
switch (is_string($node) ? 'text' : $node[Tokenizer::TYPE]) {
case Tokenizer::T_SECTION:
switch (is_string($node) ? 'text' : $node[Mustache_Tokenizer::TYPE]) {
case Mustache_Tokenizer::T_SECTION:
$code .= $this->section(
$node[Tokenizer::NODES],
$node[Tokenizer::NAME],
$node[Tokenizer::INDEX],
$node[Tokenizer::END],
$node[Tokenizer::OTAG],
$node[Tokenizer::CTAG],
$node[Mustache_Tokenizer::NODES],
$node[Mustache_Tokenizer::NAME],
$node[Mustache_Tokenizer::INDEX],
$node[Mustache_Tokenizer::END],
$node[Mustache_Tokenizer::OTAG],
$node[Mustache_Tokenizer::CTAG],
$level
);
break;
case Tokenizer::T_INVERTED:
case Mustache_Tokenizer::T_INVERTED:
$code .= $this->invertedSection(
$node[Tokenizer::NODES],
$node[Tokenizer::NAME],
$node[Mustache_Tokenizer::NODES],
$node[Mustache_Tokenizer::NAME],
$level
);
break;
case Tokenizer::T_PARTIAL:
case Tokenizer::T_PARTIAL_2:
case Mustache_Tokenizer::T_PARTIAL:
case Mustache_Tokenizer::T_PARTIAL_2:
$code .= $this->partial(
$node[Tokenizer::NAME],
isset($node[Tokenizer::INDENT]) ? $node[Tokenizer::INDENT] : '',
$node[Mustache_Tokenizer::NAME],
isset($node[Mustache_Tokenizer::INDENT]) ? $node[Mustache_Tokenizer::INDENT] : '',
$level
);
break;
case Tokenizer::T_UNESCAPED:
case Tokenizer::T_UNESCAPED_2:
$code .= $this->variable($node[Tokenizer::NAME], false, $level);
case Mustache_Tokenizer::T_UNESCAPED:
case Mustache_Tokenizer::T_UNESCAPED_2:
$code .= $this->variable($node[Mustache_Tokenizer::NAME], false, $level);
break;
case Tokenizer::T_COMMENT:
case Mustache_Tokenizer::T_COMMENT:
break;
case Tokenizer::T_ESCAPED:
$code .= $this->variable($node[Tokenizer::NAME], true, $level);
case Mustache_Tokenizer::T_ESCAPED:
$code .= $this->variable($node[Mustache_Tokenizer::NAME], true, $level);
break;
@@ -105,7 +103,7 @@ class Compiler {
break;
default:
throw new \InvalidArgumentException('Unknown node type: '.json_encode($node));
throw new InvalidArgumentException('Unknown node type: '.json_encode($node));
}
}
@@ -114,8 +112,8 @@ class Compiler {
const KLASS = '<?php
class %s extends \Mustache\Template {
public function renderInternal(\Mustache\Context $context, $indent = \'\', $escape = false) {
class %s extends Mustache_Template {
public function renderInternal(Mustache_Context $context, $indent = \'\', $escape = false) {
$buffer = \'\';
%s
@@ -149,7 +147,7 @@ class Compiler {
';
const SECTION = '
private function section%s(\Mustache\Context $context, $indent, $value) {
private function section%s(Mustache_Context $context, $indent, $value) {
$buffer = \'\';
if (!is_string($value) && is_callable($value)) {
$source = %s;
@@ -329,9 +327,9 @@ class Compiler {
*
* The return value will be one of `find`, `findDot` or `last`.
*
* @see \Mustache\Context::find
* @see \Mustache\Context::findDot
* @see \Mustache\Context::last
* @see Mustache_Context::find
* @see Mustache_Context::findDot
* @see Mustache_Context::last
*
* @param string $id Variable name
*
+2 -4
View File
@@ -9,12 +9,10 @@
* file that was distributed with this source code.
*/
namespace Mustache;
/**
* Mustache Template rendering Context.
*/
class Context {
class Mustache_Context {
private $stack = array();
/**
@@ -118,7 +116,7 @@ class Context {
/**
* Helper function to find a variable in the Context stack.
*
* @see \Mustache\Context::find
* @see Mustache_Context::find
*
* @param string $id Variable name
* @param array $stack Context stack
+12 -14
View File
@@ -9,27 +9,25 @@
* file that was distributed with this source code.
*/
namespace Mustache;
/**
* A collection of helpers for a Mustache instance.
*/
class HelperCollection {
class Mustache_HelperCollection {
private $helpers = array();
/**
* Helper Collection constructor.
*
* Optionally accepts an array (or \Traversable) of `$name => $helper` pairs.
* Optionally accepts an array (or Traversable) of `$name => $helper` pairs.
*
* @throws \InvalidArgumentException if the $helpers argument isn't an array or \Traversable
* @throws InvalidArgumentException if the $helpers argument isn't an array or Traversable
*
* @param array|Traversable $helpers (default: null)
*/
public function __construct($helpers = null) {
if ($helpers !== null) {
if (!is_array($helpers) && !$helpers instanceof \Traversable) {
throw new \InvalidArgumentException('HelperCollection constructor expects an array of helpers');
if (!is_array($helpers) && !$helpers instanceof Traversable) {
throw new InvalidArgumentException('HelperCollection constructor expects an array of helpers');
}
foreach ($helpers as $name => $helper) {
@@ -41,7 +39,7 @@ class HelperCollection {
/**
* Magic mutator.
*
* @see \Mustache\HelperCollection::add
* @see Mustache_HelperCollection::add
*
* @param string $name
* @param mixed $helper
@@ -63,7 +61,7 @@ class HelperCollection {
/**
* Magic accessor.
*
* @see \Mustache\HelperCollection::get
* @see Mustache_HelperCollection::get
*
* @param string $name
*
@@ -82,7 +80,7 @@ class HelperCollection {
*/
public function get($name) {
if (!$this->has($name)) {
throw new \InvalidArgumentException('Unknown helper: '.$name);
throw new InvalidArgumentException('Unknown helper: '.$name);
}
return $this->helpers[$name];
@@ -91,7 +89,7 @@ class HelperCollection {
/**
* Magic isset().
*
* @see \Mustache\HelperCollection::has
* @see Mustache_HelperCollection::has
*
* @param string $name
*
@@ -115,7 +113,7 @@ class HelperCollection {
/**
* Magic unset().
*
* @see \Mustache\HelperCollection::remove
* @see Mustache_HelperCollection::remove
*
* @param string $name
*/
@@ -126,13 +124,13 @@ class HelperCollection {
/**
* Check whether a given helper is present in the collection.
*
* @throws \InvalidArgumentException if the requested helper is not present.
* @throws InvalidArgumentException if the requested helper is not present.
*
* @param string $name
*/
public function remove($name) {
if (!$this->has($name)) {
throw new \InvalidArgumentException('Unknown helper: '.$name);
throw new InvalidArgumentException('Unknown helper: '.$name);
}
unset($this->helpers[$name]);
+1 -3
View File
@@ -9,12 +9,10 @@
* file that was distributed with this source code.
*/
namespace Mustache;
/**
* Mustache Template Loader interface.
*/
interface Loader {
interface Mustache_Loader {
/**
* Load a Template by name.
+3 -8
View File
@@ -9,11 +9,6 @@
* file that was distributed with this source code.
*/
namespace Mustache\Loader;
use Mustache\Loader;
use Mustache\Loader\MutableLoader;
/**
* Mustache Template array Loader implementation.
*
@@ -26,13 +21,13 @@ use Mustache\Loader\MutableLoader;
*
* $tpl = $loader->load('foo'); // '{{ bar }}'
*
* The ArrayLoader is used internally as a partials loader by \Mustache\Mustache instance when an array of partials
* The ArrayLoader is used internally as a partials loader by Mustache_Mustache instance when an array of partials
* is set. It can also be used as a quick-and-dirty Template loader.
*
* @implements Loader
* @implements MutableLoader
*/
class ArrayLoader implements Loader, MutableLoader {
class Mustache_Loader_ArrayLoader implements Mustache_Loader, Mustache_Loader_MutableLoader {
/**
* ArrayLoader constructor.
@@ -52,7 +47,7 @@ class ArrayLoader implements Loader, MutableLoader {
*/
public function load($name) {
if (!isset($this->templates[$name])) {
throw new \InvalidArgumentException('Template '.$name.' not found.');
throw new InvalidArgumentException('Template '.$name.' not found.');
}
return $this->templates[$name];
+10 -14
View File
@@ -9,28 +9,24 @@
* file that was distributed with this source code.
*/
namespace Mustache\Loader;
use Mustache\Loader;
/**
* Mustache Template filesystem Loader implementation.
*
* An ArrayLoader instance loads Mustache Template source from the filesystem by name:
*
* $loader = new FilesystemLoader(__DIR__.'/views');
* $tpl = $loader->load('foo'); // equivalent to `file_get_contents(__DIR__.'/views/foo.mustache');
* $loader = new FilesystemLoader(dirname(__FILE__).'/views');
* $tpl = $loader->load('foo'); // equivalent to `file_get_contents(dirname(__FILE__).'/views/foo.mustache');
*
* This is probably the most useful Mustache Loader implementation. It can be used for partials and normal Templates:
*
* $m = new Mustache(array(
* 'loader' => new FilesystemLoader(__DIR__.'/views'),
* 'partials_loader' => new FilesystemLoader(__DIR__.'/views/partials'),
* 'loader' => new FilesystemLoader(dirname(__FILE__).'/views'),
* 'partials_loader' => new FilesystemLoader(dirname(__FILE__).'/views/partials'),
* ));
*
* @implements Loader
*/
class FilesystemLoader implements Loader {
class Mustache_Loader_FilesystemLoader implements Mustache_Loader {
private $baseDir;
private $extension = '.mustache';
private $templates = array();
@@ -45,7 +41,7 @@ class FilesystemLoader implements Loader {
* 'extension' => '.ms',
* );
*
* @throws \RuntimeException if $baseDir does not exist.
* @throws RuntimeException if $baseDir does not exist.
*
* @param string $baseDir Base directory containing Mustache template files.
* @param array $options Array of Loader options (default: array())
@@ -54,7 +50,7 @@ class FilesystemLoader implements Loader {
$this->baseDir = rtrim(realpath($baseDir), '/');
if (!is_dir($this->baseDir)) {
throw new \RuntimeException('FilesystemLoader baseDir must be a directory: '.$baseDir);
throw new RuntimeException('FilesystemLoader baseDir must be a directory: '.$baseDir);
}
if (isset($options['extension'])) {
@@ -65,7 +61,7 @@ class FilesystemLoader implements Loader {
/**
* Load a Template by name.
*
* $loader = new FilesystemLoader(__DIR__.'/views');
* $loader = new FilesystemLoader(dirname(__FILE__).'/views');
* $loader->load('admin/dashboard'); // loads "./views/admin/dashboard.mustache";
*
* @param string $name
@@ -83,7 +79,7 @@ class FilesystemLoader implements Loader {
/**
* Helper function for loading a Mustache file by name.
*
* @throws \InvalidArgumentException if a template file is not found.
* @throws InvalidArgumentException if a template file is not found.
*
* @param string $name
*
@@ -93,7 +89,7 @@ class FilesystemLoader implements Loader {
$fileName = $this->getFileName($name);
if (!file_exists($fileName)) {
throw new \InvalidArgumentException('Template '.$name.' not found.');
throw new InvalidArgumentException('Template '.$name.' not found.');
}
return file_get_contents($fileName);
+1 -3
View File
@@ -9,12 +9,10 @@
* file that was distributed with this source code.
*/
namespace Mustache\Loader;
/**
* Mustache Template mutable Loader interface.
*/
interface MutableLoader {
interface Mustache_Loader_MutableLoader {
/**
* Set an associative array of Template sources for this loader.
+1 -5
View File
@@ -9,10 +9,6 @@
* file that was distributed with this source code.
*/
namespace Mustache\Loader;
use Mustache\Loader;
/**
* Mustache Template string Loader implementation.
*
@@ -29,7 +25,7 @@ use Mustache\Loader;
*
* @implements Loader
*/
class StringLoader implements Loader {
class Mustache_Loader_StringLoader implements Mustache_Loader {
/**
* Load a Template by source.
+53 -59
View File
@@ -9,12 +9,6 @@
* file that was distributed with this source code.
*/
namespace Mustache;
use Mustache\Loader\ArrayLoader;
use Mustache\Loader\MutableLoader;
use Mustache\Loader\StringLoader;
/**
* A Mustache implementation in PHP.
*
@@ -27,7 +21,7 @@ use Mustache\Loader\StringLoader;
*
* @author Justin Hileman {@link http://justinhileman.com}
*/
class Mustache {
class Mustache_Mustache {
const VERSION = '2.0.0-dev';
const SPEC_VERSION = '1.1.2';
@@ -50,20 +44,20 @@ class Mustache {
*
* $options = array(
* // The class prefix for compiled templates. Defaults to '__Mustache_'
* 'template_class_prefix' => '\My\Namespace\Template\',
* 'template_class_prefix' => '__MyTemplates_',
*
* // A cache directory for compiled templates. Mustache will not cache templates unless this is set
* 'cache' => __DIR__.'/tmp/cache/mustache',
* 'cache' => dirname(__FILE__).'/tmp/cache/mustache',
*
* // A Mustache template loader instance. Uses a StringLoader if not specified
* 'loader' => new \Mustache\Loader\FilesystemLoader(__DIR__.'/views'),
* 'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'),
*
* // A Mustache loader instance for partials.
* 'partials_loader' => new \Mustache\Loader\FilesystemLoader(__DIR__.'/views/partials'),
* 'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views/partials'),
*
* // An array of Mustache partials. Useful for quick-and-dirty string template loading, but not as
* // efficient or lazy as a Filesystem (or database) loader.
* 'partials' => array('foo' => file_get_contents(__DIR__.'/views/partials/foo.mustache')),
* 'partials' => array('foo' => file_get_contents(dirname(__FILE__).'/views/partials/foo.mustache')),
*
* // An array of 'helpers'. Helpers can be global variables or objects, closures (e.g. for higher order
* // sections), or any other valid Mustache context value. They will be prepended to the context stack,
@@ -126,8 +120,8 @@ class Mustache {
*
* Equivalent to calling `$mustache->loadTemplate($template)->render($data);`
*
* @see \Mustache\Mustache::loadTemplate
* @see \Mustache\Template::render
* @see Mustache_Mustache::loadTemplate
* @see Mustache_Template::render
*
* @param string $template
* @param mixed $data
@@ -159,9 +153,9 @@ class Mustache {
/**
* Set the Mustache template Loader instance.
*
* @param \Mustache\Loader $loader
* @param Mustache_Loader $loader
*/
public function setLoader(Loader $loader) {
public function setLoader(Mustache_Loader $loader) {
$this->loader = $loader;
}
@@ -171,11 +165,11 @@ class Mustache {
* If no Loader instance has been explicitly specified, this method will instantiate and return
* a StringLoader instance.
*
* @return \Mustache\Loader
* @return Mustache_Loader
*/
public function getLoader() {
if (!isset($this->loader)) {
$this->loader = new StringLoader;
$this->loader = new Mustache_Loader_StringLoader;
}
return $this->loader;
@@ -184,9 +178,9 @@ class Mustache {
/**
* Set the Mustache partials Loader instance.
*
* @param \Mustache\Loader $partialsLoader
* @param Mustache_Loader $partialsLoader
*/
public function setPartialsLoader(Loader $partialsLoader) {
public function setPartialsLoader(Mustache_Loader $partialsLoader) {
$this->partialsLoader = $partialsLoader;
}
@@ -196,11 +190,11 @@ class Mustache {
* If no Loader instance has been explicitly specified, this method will instantiate and return
* an ArrayLoader instance.
*
* @return \Mustache\Loader
* @return Mustache_Loader
*/
public function getPartialsLoader() {
if (!isset($this->partialsLoader)) {
$this->partialsLoader = new ArrayLoader;
$this->partialsLoader = new Mustache_Loader_ArrayLoader;
}
return $this->partialsLoader;
@@ -209,14 +203,14 @@ class Mustache {
/**
* Set partials for the current partials Loader instance.
*
* @throws \RuntimeException If the current Loader instance is immutable
* @throws RuntimeException If the current Loader instance is immutable
*
* @param array $partials (default: array())
*/
public function setPartials(array $partials = array()) {
$loader = $this->getPartialsLoader();
if (!$loader instanceof MutableLoader) {
throw new \RuntimeException('Unable to set partials on an immutable Mustache Loader instance');
if (!$loader instanceof Mustache_Loader_MutableLoader) {
throw new RuntimeException('Unable to set partials on an immutable Mustache Loader instance');
}
$loader->setTemplates($partials);
@@ -229,13 +223,13 @@ class Mustache {
* any other valid Mustache context value. They will be prepended to the context stack, so they will be available in
* any template loaded by this Mustache instance.
*
* @throws \InvalidArgumentException if $helpers is not an array or \Traversable
* @throws InvalidArgumentException if $helpers is not an array or Traversable
*
* @param array|Traversable $helpers
*/
public function setHelpers($helpers) {
if (!is_array($helpers) && !$helpers instanceof \Traversable) {
throw new \InvalidArgumentException('setHelpers expects an array of helpers');
if (!is_array($helpers) && !$helpers instanceof Traversable) {
throw new InvalidArgumentException('setHelpers expects an array of helpers');
}
$this->getHelpers()->clear();
@@ -248,13 +242,13 @@ class Mustache {
/**
* Get the current set of Mustache helpers.
*
* @see \Mustache\Mustache::setHelpers
* @see Mustache_Mustache::setHelpers
*
* @return \Mustache\HelperCollection
* @return Mustache_HelperCollection
*/
public function getHelpers() {
if (!isset($this->helpers)) {
$this->helpers = new HelperCollection;
$this->helpers = new Mustache_HelperCollection;
}
return $this->helpers;
@@ -263,7 +257,7 @@ class Mustache {
/**
* Add a new Mustache helper.
*
* @see \Mustache\Mustache::setHelpers
* @see Mustache_Mustache::setHelpers
*
* @param string $name
* @param mixed $helper
@@ -275,7 +269,7 @@ class Mustache {
/**
* Get a Mustache helper by name.
*
* @see \Mustache\Mustache::setHelpers
* @see Mustache_Mustache::setHelpers
*
* @param string $name
*
@@ -288,7 +282,7 @@ class Mustache {
/**
* Check whether this Mustache instance has a helper.
*
* @see \Mustache\Mustache::setHelpers
* @see Mustache_Mustache::setHelpers
*
* @param string $name
*
@@ -301,7 +295,7 @@ class Mustache {
/**
* Remove a helper by name.
*
* @see \Mustache\Mustache::setHelpers
* @see Mustache_Mustache::setHelpers
*
* @param string $name
*/
@@ -312,9 +306,9 @@ class Mustache {
/**
* Set the Mustache Tokenizer instance.
*
* @param \Mustache\Tokenizer $tokenizer
* @param Mustache_Tokenizer $tokenizer
*/
public function setTokenizer(Tokenizer $tokenizer) {
public function setTokenizer(Mustache_Tokenizer $tokenizer) {
$this->tokenizer = $tokenizer;
}
@@ -323,11 +317,11 @@ class Mustache {
*
* If no Tokenizer instance has been explicitly specified, this method will instantiate and return a new one.
*
* @return \Mustache\Tokenizer
* @return Mustache_Tokenizer
*/
public function getTokenizer() {
if (!isset($this->tokenizer)) {
$this->tokenizer = new Tokenizer;
$this->tokenizer = new Mustache_Tokenizer;
}
return $this->tokenizer;
@@ -336,9 +330,9 @@ class Mustache {
/**
* Set the Mustache Parser instance.
*
* @param \Mustache\Parser $parser
* @param Mustache_Parser $parser
*/
public function setParser(Parser $parser) {
public function setParser(Mustache_Parser $parser) {
$this->parser = $parser;
}
@@ -347,11 +341,11 @@ class Mustache {
*
* If no Parser instance has been explicitly specified, this method will instantiate and return a new one.
*
* @return \Mustache\Parser
* @return Mustache_Parser
*/
public function getParser() {
if (!isset($this->parser)) {
$this->parser = new Parser;
$this->parser = new Mustache_Parser;
}
return $this->parser;
@@ -360,9 +354,9 @@ class Mustache {
/**
* Set the Mustache Compiler instance.
*
* @param \Mustache\Compiler $compiler
* @param Mustache_Compiler $compiler
*/
public function setCompiler(Compiler $compiler) {
public function setCompiler(Mustache_Compiler $compiler) {
$this->compiler = $compiler;
}
@@ -371,11 +365,11 @@ class Mustache {
*
* If no Compiler instance has been explicitly specified, this method will instantiate and return a new one.
*
* @return \Mustache\Compiler
* @return Mustache_Compiler
*/
public function getCompiler() {
if (!isset($this->compiler)) {
$this->compiler = new Compiler;
$this->compiler = new Mustache_Compiler;
}
return $this->compiler;
@@ -403,7 +397,7 @@ class Mustache {
*
* @param string $name
*
* @return \Mustache\Template
* @return Mustache_Template
*/
public function loadTemplate($name) {
return $this->loadSource($this->getLoader()->load($name));
@@ -417,7 +411,7 @@ class Mustache {
*
* @param string $name
*
* @return \Mustache\Template
* @return Mustache_Template
*/
public function loadPartial($name) {
return $this->loadSource($this->getPartialsLoader()->load($name));
@@ -432,7 +426,7 @@ class Mustache {
* @param string $source
* @param string $delims (default: null)
*
* @return \Mustache\Template
* @return Mustache_Template
*/
public function loadLambda($source, $delims = null) {
if ($delims !== null) {
@@ -445,13 +439,13 @@ class Mustache {
/**
* Instantiate and return a Mustache Template instance by source.
*
* @see \Mustache\Mustache::loadTemplate
* @see \Mustache\Mustache::loadPartial
* @see \Mustache\Mustache::loadLambda
* @see Mustache_Mustache::loadTemplate
* @see Mustache_Mustache::loadPartial
* @see Mustache_Mustache::loadLambda
*
* @param string $source
*
* @return \Mustache\Template
* @return Mustache_Template
*/
private function loadSource($source) {
$className = $this->getTemplateClassName($source);
@@ -478,7 +472,7 @@ class Mustache {
/**
* Helper method to tokenize a Mustache template.
*
* @see \Mustache\Tokenizer::scan
* @see Mustache_Tokenizer::scan
*
* @param string $source
*
@@ -491,7 +485,7 @@ class Mustache {
/**
* Helper method to parse a Mustache template.
*
* @see \Mustache\Parser::parse
* @see Mustache_Parser::parse
*
* @param string $source
*
@@ -504,7 +498,7 @@ class Mustache {
/**
* Helper method to compile a Mustache template.
*
* @see \Mustache\Compiler::compile
* @see Mustache_Compiler::compile
*
* @param string $source
*
@@ -533,7 +527,7 @@ class Mustache {
/**
* Helper method to dump a generated Mustache Template subclass to the file cache.
*
* @throws \RuntimeException if unable to write to $fileName.
* @throws RuntimeException if unable to write to $fileName.
*
* @param string $fileName
* @param string $source
@@ -552,6 +546,6 @@ class Mustache {
}
}
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $fileName));
throw new RuntimeException(sprintf('Failed to write cache file "%s".', $fileName));
}
}
+15 -17
View File
@@ -9,14 +9,12 @@
* file that was distributed with this source code.
*/
namespace Mustache;
/**
* Mustache Parser class.
*
* This class is responsible for turning a set of Mustache tokens into a parse tree.
*/
class Parser {
class Mustache_Parser {
/**
* Process an array of Mustache tokens and convert them into a parse tree.
@@ -26,20 +24,20 @@ class Parser {
* @return array Mustache token parse tree
*/
public function parse(array $tokens = array()) {
return $this->buildTree(new \ArrayIterator($tokens));
return $this->buildTree(new ArrayIterator($tokens));
}
/**
* Helper method for recursively building a parse tree.
*
* @throws \LogicException when nesting errors or mismatched section tags are encountered.
* @throws LogicException when nesting errors or mismatched section tags are encountered.
*
* @param \ArrayIterator $tokens Stream of Mustache tokens
* @param ArrayIterator $tokens Stream of Mustache tokens
* @param array $parent Parent token (default: null)
*
* @return array Mustache Token parse tree
*/
private function buildTree(\ArrayIterator $tokens, array $parent = null) {
private function buildTree(ArrayIterator $tokens, array $parent = null) {
$nodes = array();
do {
@@ -49,23 +47,23 @@ class Parser {
if ($token === null) {
continue;
} elseif (is_array($token)) {
switch ($token[Tokenizer::TYPE]) {
case Tokenizer::T_SECTION:
case Tokenizer::T_INVERTED:
switch ($token[Mustache_Tokenizer::TYPE]) {
case Mustache_Tokenizer::T_SECTION:
case Mustache_Tokenizer::T_INVERTED:
$nodes[] = $this->buildTree($tokens, $token);
break;
case Tokenizer::T_END_SECTION:
case Mustache_Tokenizer::T_END_SECTION:
if (!isset($parent)) {
throw new \LogicException('Unexpected closing tag: /'. $token[Tokenizer::NAME]);
throw new LogicException('Unexpected closing tag: /'. $token[Mustache_Tokenizer::NAME]);
}
if ($token[Tokenizer::NAME] !== $parent[Tokenizer::NAME]) {
throw new \LogicException('Nesting error: ' . $parent[Tokenizer::NAME] . ' vs. ' . $token[Tokenizer::NAME]);
if ($token[Mustache_Tokenizer::NAME] !== $parent[Mustache_Tokenizer::NAME]) {
throw new LogicException('Nesting error: ' . $parent[Mustache_Tokenizer::NAME] . ' vs. ' . $token[Mustache_Tokenizer::NAME]);
}
$parent[Tokenizer::END] = $token[Tokenizer::INDEX];
$parent[Tokenizer::NODES] = $nodes;
$parent[Mustache_Tokenizer::END] = $token[Mustache_Tokenizer::INDEX];
$parent[Mustache_Tokenizer::NODES] = $nodes;
return $parent;
break;
@@ -81,7 +79,7 @@ class Parser {
} while ($tokens->valid());
if (isset($parent)) {
throw new \LogicException('Missing closing tag: ' . $parent[Tokenizer::NAME]);
throw new LogicException('Missing closing tag: ' . $parent[Mustache_Tokenizer::NAME]);
}
return $nodes;
+11 -13
View File
@@ -9,37 +9,35 @@
* file that was distributed with this source code.
*/
namespace Mustache;
/**
* Abstract Mustache Template class.
*
* @abstract
*/
abstract class Template {
abstract class Mustache_Template {
/**
* @var \Mustache\Mustache
* @var Mustache_Mustache
*/
protected $mustache;
/**
* Mustache Template constructor.
*
* @param \Mustache\Mustache $mustache
* @param Mustache_Mustache $mustache
*/
public function __construct(Mustache $mustache) {
public function __construct(Mustache_Mustache $mustache) {
$this->mustache = $mustache;
}
/**
* Mustache Template instances can be treated as a function and rendered by simply calling them:
*
* $m = new Mustache;
* $m = new Mustache_Mustache;
* $tpl = $m->loadTemplate('Hello, {{ name }}!');
* echo $tpl(array('name' => 'World')); // "Hello, World!"
*
* @see \Mustache\Template::render
* @see Mustache_Template::render
*
* @param mixed $context Array or object rendering context (default: array())
*
@@ -67,11 +65,11 @@ abstract class Template {
*
* @abstract
*
* @param \Mustache\Context $context
* @param Mustache_Context $context
*
* @return string Rendered template
*/
abstract public function renderInternal(Context $context, $indent = '', $escape = false);
abstract public function renderInternal(Mustache_Context $context, $indent = '', $escape = false);
/**
* Tests whether a value should be iterated over (e.g. in a section context).
@@ -104,7 +102,7 @@ abstract class Template {
*/
protected function isIterable($value) {
if (is_object($value)) {
return $value instanceof \Traversable;
return $value instanceof Traversable;
} elseif (is_array($value)) {
$i = 0;
foreach ($value as $k => $v) {
@@ -126,10 +124,10 @@ abstract class Template {
*
* @param mixed $context Optional first context frame (default: null)
*
* @return \Mustache\Context
* @return Mustache_Context
*/
protected function prepareContextStack($context = null) {
$stack = new Context;
$stack = new Mustache_Context;
$helpers = $this->mustache->getHelpers();
if (!$helpers->isEmpty()) {
+1 -3
View File
@@ -9,14 +9,12 @@
* file that was distributed with this source code.
*/
namespace Mustache;
/**
* Mustache Tokenizer class.
*
* This class is responsible for turning raw template source into a set of Mustache tokens.
*/
class Tokenizer {
class Mustache_Tokenizer {
// Finite state machine states
const IN_TEXT = 0;
+7 -11
View File
@@ -9,29 +9,25 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test;
use Mustache\Autoloader;
/**
* @group unit
*/
class AutoloaderTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_AutoloaderTest extends PHPUnit_Framework_TestCase {
public function testRegister() {
$loader = Autoloader::register();
$loader = Mustache_Autoloader::register();
$this->assertTrue(spl_autoload_unregister(array($loader, 'autoload')));
}
public function testAutoloader() {
$loader = new Autoloader(__DIR__.'/../../fixtures/autoloader');
$loader = new Mustache_Autoloader(dirname(__FILE__).'/../../fixtures/autoloader');
$this->assertNull($loader->autoload('NonMustacheClass'));
$this->assertFalse(class_exists('NonMustacheClass'));
$loader->autoload('Mustache\Foo');
$this->assertTrue(class_exists('Mustache\Foo'));
$loader->autoload('Mustache_Foo');
$this->assertTrue(class_exists('Mustache_Foo'));
$loader->autoload('\Mustache\Bar');
$this->assertTrue(class_exists('Mustache\Bar'));
$loader->autoload('\Mustache_Bar');
$this->assertTrue(class_exists('Mustache_Bar'));
}
}
+12 -17
View File
@@ -9,21 +9,16 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test;
use Mustache\Compiler;
use Mustache\Tokenizer;
/**
* @group unit
*/
class CompilerTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_CompilerTest extends \PHPUnit_Framework_TestCase {
/**
* @dataProvider getCompileValues
*/
public function testCompile($source, array $tree, $name, $customEscaper, $charset, $expected) {
$compiler = new Compiler;
$compiler = new Mustache_Compiler;
$compiled = $compiler->compile($source, $tree, $name, $customEscaper, $charset);
foreach ($expected as $contains) {
@@ -34,20 +29,20 @@ class CompilerTest extends \PHPUnit_Framework_TestCase {
public function getCompileValues() {
return array(
array('', array(), 'Banana', false, 'ISO-8859-1', array(
"\nclass Banana extends \Mustache\Template",
"\nclass Banana extends Mustache_Template",
'return htmlspecialchars($buffer, ENT_COMPAT, \'ISO-8859-1\');',
'return $buffer;',
)),
array('', array('TEXT'), 'Monkey', false, 'UTF-8', array(
"\nclass Monkey extends \Mustache\Template",
"\nclass Monkey extends Mustache_Template",
'return htmlspecialchars($buffer, ENT_COMPAT, \'UTF-8\');',
'$buffer .= $indent . \'TEXT\';',
'return $buffer;',
)),
array('', array('TEXT'), 'Monkey', true, 'ISO-8859-1', array(
"\nclass Monkey extends \Mustache\Template",
"\nclass Monkey extends Mustache_Template",
'$buffer .= $indent . \'TEXT\';',
'return call_user_func($this->mustache->getEscape(), $buffer);',
'return $buffer;',
@@ -59,12 +54,12 @@ class CompilerTest extends \PHPUnit_Framework_TestCase {
'foo',
"\n",
array(
Tokenizer::TYPE => Tokenizer::T_ESCAPED,
Tokenizer::NAME => 'name',
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name',
),
array(
Tokenizer::TYPE => Tokenizer::T_ESCAPED,
Tokenizer::NAME => '.',
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => '.',
),
"'bar'",
),
@@ -72,7 +67,7 @@ class CompilerTest extends \PHPUnit_Framework_TestCase {
false,
'UTF-8',
array(
"\nclass Monkey extends \Mustache\Template",
"\nclass Monkey extends Mustache_Template",
'$buffer .= $indent . \'foo\'',
'$buffer .= "\n"',
'$value = $context->find(\'name\');',
@@ -90,7 +85,7 @@ class CompilerTest extends \PHPUnit_Framework_TestCase {
* @expectedException InvalidArgumentException
*/
public function testCompilerThrowsUnknownNodeTypeException() {
$compiler = new Compiler;
$compiler->compile('', array(array(Tokenizer::TYPE => 'invalid')), 'SomeClass');
$compiler = new Mustache_Compiler;
$compiler->compile('', array(array(Mustache_Tokenizer::TYPE => 'invalid')), 'SomeClass');
}
}
+12 -16
View File
@@ -9,44 +9,40 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test;
use Mustache\Context;
/**
* @group unit
*/
class ContextTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
$one = new Context;
$one = new Mustache_Context;
$this->assertSame('', $one->find('foo'));
$this->assertSame('', $one->find('bar'));
$two = new Context(array(
$two = new Mustache_Context(array(
'foo' => 'FOO',
'bar' => '<BAR>'
));
$this->assertEquals('FOO', $two->find('foo'));
$this->assertEquals('<BAR>', $two->find('bar'));
$obj = new \StdClass;
$obj = new StdClass;
$obj->name = 'NAME';
$three = new Context($obj);
$three = new Mustache_Context($obj);
$this->assertSame($obj, $three->last());
$this->assertEquals('NAME', $three->find('name'));
}
public function testPushPopAndLast() {
$context = new Context;
$context = new Mustache_Context;
$this->assertFalse($context->last());
$dummy = new TestDummy;
$dummy = new Mustache_Test_TestDummy;
$context->push($dummy);
$this->assertSame($dummy, $context->last());
$this->assertSame($dummy, $context->pop());
$this->assertFalse($context->last());
$obj = new \StdClass;
$obj = new StdClass;
$context->push($dummy);
$this->assertSame($dummy, $context->last());
$context->push($obj);
@@ -57,11 +53,11 @@ class ContextTest extends \PHPUnit_Framework_TestCase {
}
public function testFind() {
$context = new Context;
$context = new Mustache_Context;
$dummy = new TestDummy;
$dummy = new Mustache_Test_TestDummy;
$obj = new \StdClass;
$obj = new StdClass;
$obj->name = 'obj';
$arr = array(
@@ -98,7 +94,7 @@ class ContextTest extends \PHPUnit_Framework_TestCase {
}
}
class TestDummy {
class Mustache_Test_TestDummy {
public $name = 'dummy';
public function __invoke() {
+4 -8
View File
@@ -9,21 +9,17 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test\Functional;
use Mustache\Mustache;
/**
* @group magic_methods
* @group functional
*/
class CallTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_CallTest extends PHPUnit_Framework_TestCase {
public function testCallEatsContext() {
$m = new Mustache;
$m = new Mustache_Mustache;
$tpl = $m->loadTemplate('{{# foo }}{{ label }}: {{ name }}{{/ foo }}');
$foo = new ClassWithCall();
$foo = new Mustache_Test_Functional_ClassWithCall();
$foo->name = 'Bob';
$data = array('label' => 'name', 'foo' => $foo);
@@ -32,7 +28,7 @@ class CallTest extends \PHPUnit_Framework_TestCase {
}
}
class ClassWithCall {
class Mustache_Test_Functional_ClassWithCall {
public $name;
public function __call($method, $args) {
return 'unknown value';
@@ -9,15 +9,11 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test\Functional;
use Mustache\Mustache;
/**
* @group examples
* @group functional
*/
class ExamplesTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_ExamplesTest extends PHPUnit_Framework_TestCase {
/**
* Test everything in the `examples` directory.
@@ -30,7 +26,7 @@ class ExamplesTest extends \PHPUnit_Framework_TestCase {
* @param string $expected
*/
public function testExamples($context, $source, $partials, $expected) {
$mustache = new Mustache(array(
$mustache = new Mustache_Mustache(array(
'partials' => $partials
));
$this->assertEquals($expected, $mustache->loadTemplate($source)->render($context));
@@ -50,7 +46,7 @@ class ExamplesTest extends \PHPUnit_Framework_TestCase {
* @return array
*/
public function getExamples() {
$path = realpath(__DIR__.'/../../../../examples');
$path = realpath(dirname(__FILE__).'/../../../../examples');
$examples = array();
$handle = opendir($path);
@@ -9,26 +9,22 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test\Functional;
use Mustache\Mustache;
/**
* @group lambdas
* @group functional
*/
class HigherOrderSectionsTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_HigherOrderSectionsTest extends PHPUnit_Framework_TestCase {
private $mustache;
public function setUp() {
$this->mustache = new Mustache;
$this->mustache = new Mustache_Mustache;
}
public function testAnonymousFunctionSectionCallback() {
$tpl = $this->mustache->loadTemplate('{{#wrapper}}{{name}}{{/wrapper}}');
$foo = new Foo;
$foo = new Mustache_Test_Functional_Foo;
$foo->name = 'Mario';
$foo->wrapper = function($text) {
return sprintf('<div class="anonymous">%s</div>', $text);
@@ -41,7 +37,7 @@ class HigherOrderSectionsTest extends \PHPUnit_Framework_TestCase {
$one = $this->mustache->loadTemplate('{{name}}');
$two = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
$foo = new Foo;
$foo = new Mustache_Test_Functional_Foo;
$foo->name = 'Luigi';
$this->assertEquals($foo->name, $one->render($foo));
@@ -51,7 +47,7 @@ class HigherOrderSectionsTest extends \PHPUnit_Framework_TestCase {
public function testRuntimeSectionCallback() {
$tpl = $this->mustache->loadTemplate('{{#doublewrap}}{{name}}{{/doublewrap}}');
$foo = new Foo;
$foo = new Mustache_Test_Functional_Foo;
$foo->doublewrap = array($foo, 'wrapWithBoth');
$this->assertEquals(sprintf('<strong><em>%s</em></strong>', $foo->name), $tpl->render($foo));
@@ -60,7 +56,7 @@ class HigherOrderSectionsTest extends \PHPUnit_Framework_TestCase {
public function testStaticSectionCallback() {
$tpl = $this->mustache->loadTemplate('{{#trimmer}} {{name}} {{/trimmer}}');
$foo = new Foo;
$foo = new Mustache_Test_Functional_Foo;
$foo->trimmer = array(get_class($foo), 'staticTrim');
$this->assertEquals($foo->name, $tpl->render($foo));
@@ -69,7 +65,7 @@ class HigherOrderSectionsTest extends \PHPUnit_Framework_TestCase {
public function testViewArraySectionCallback() {
$tpl = $this->mustache->loadTemplate('{{#trim}} {{name}} {{/trim}}');
$foo = new Foo;
$foo = new Mustache_Test_Functional_Foo;
$data = array(
'name' => 'Bob',
@@ -95,19 +91,19 @@ class HigherOrderSectionsTest extends \PHPUnit_Framework_TestCase {
public function testMonsters() {
$tpl = $this->mustache->loadTemplate('{{#title}}{{title}} {{/title}}{{name}}');
$frank = new Monster();
$frank = new Mustache_Test_Functional_Monster();
$frank->title = 'Dr.';
$frank->name = 'Frankenstein';
$this->assertEquals('Dr. Frankenstein', $tpl->render($frank));
$dracula = new Monster();
$dracula = new Mustache_Test_Functional_Monster();
$dracula->title = 'Count';
$dracula->name = 'Dracula';
$this->assertEquals('Count Dracula', $tpl->render($dracula));
}
}
class Foo {
class Mustache_Test_Functional_Foo {
public $name = 'Justin';
public $lorem = 'Lorem ipsum dolor sit amet,';
public $wrap;
@@ -135,7 +131,7 @@ class Foo {
}
}
class Monster {
class Mustache_Test_Functional_Monster {
public $title;
public $name;
}
@@ -9,20 +9,16 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test\Functional;
use Mustache\Mustache;
/**
* @group mustache_injection
* @group functional
*/
class MustacheInjectionTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_TestCase {
private $mustache;
public function setUp() {
$this->mustache = new Mustache;
$this->mustache = new Mustache_Mustache;
}
// interpolation
@@ -9,23 +9,18 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test\Functional;
use Mustache\Mustache;
use Mustache\Loader\StringLoader;
/**
* A PHPUnit test case wrapping the Mustache Spec
*
* @group mustache-spec
* @group functional
*/
class MustacheSpecTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCase {
private static $mustache;
public static function setUpBeforeClass() {
self::$mustache = new Mustache;
self::$mustache = new Mustache_Mustache;
}
/**
@@ -33,7 +28,7 @@ class MustacheSpecTest extends \PHPUnit_Framework_TestCase {
* simply to provide a 'skipped' test if the `spec` submodule isn't initialized.
*/
public function testSpecInitialized() {
if (!file_exists(__DIR__.'/../../../../vendor/spec/specs/')) {
if (!file_exists(dirname(__FILE__).'/../../../../vendor/spec/specs/')) {
$this->markTestSkipped('Mustache spec submodule not initialized: run "git submodule update --init"');
}
}
@@ -161,13 +156,13 @@ class MustacheSpecTest extends \PHPUnit_Framework_TestCase {
* @return array
*/
private function loadSpec($name) {
$filename = __DIR__ . '/../../../../vendor/spec/specs/' . $name . '.yml';
$filename = dirname(__FILE__) . '/../../../../vendor/spec/specs/' . $name . '.yml';
if (!file_exists($filename)) {
return array();
}
$data = array();
$yaml = new \sfYamlParser;
$yaml = new sfYamlParser;
$file = file_get_contents($filename);
// @hack: pre-process the 'lambdas' spec so the Symfony YAML parser doesn't complain.
@@ -9,24 +9,20 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test\Functional;
use Mustache\Mustache;
/**
* @group sections
* @group functional
*/
class ObjectSectionTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_ObjectSectionTest extends PHPUnit_Framework_TestCase {
private $mustache;
public function setUp() {
$this->mustache = new Mustache;
$this->mustache = new Mustache_Mustache;
}
public function testBasicObject() {
$tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
$this->assertEquals('Foo', $tpl->render(new Alpha));
$this->assertEquals('Foo', $tpl->render(new Mustache_Test_Functional_Alpha));
}
/**
@@ -34,7 +30,7 @@ class ObjectSectionTest extends \PHPUnit_Framework_TestCase {
*/
public function testObjectWithGet() {
$tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
$this->assertEquals('Foo', $tpl->render(new Beta));
$this->assertEquals('Foo', $tpl->render(new Mustache_Test_Functional_Beta));
}
/**
@@ -42,32 +38,32 @@ class ObjectSectionTest extends \PHPUnit_Framework_TestCase {
*/
public function testSectionObjectWithGet() {
$tpl = $this->mustache->loadTemplate('{{#bar}}{{#foo}}{{name}}{{/foo}}{{/bar}}');
$this->assertEquals('Foo', $tpl->render(new Gamma));
$this->assertEquals('Foo', $tpl->render(new Mustache_Test_Functional_Gamma));
}
public function testSectionObjectWithFunction() {
$tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
$alpha = new Alpha;
$alpha->foo = new Delta;
$alpha = new Mustache_Test_Functional_Alpha;
$alpha->foo = new Mustache_Test_Functional_Delta;
$this->assertEquals('Foo', $tpl->render($alpha));
}
}
class Alpha {
class Mustache_Test_Functional_Alpha {
public $foo;
public function __construct() {
$this->foo = new \StdClass();
$this->foo = new StdClass();
$this->foo->name = 'Foo';
$this->foo->number = 1;
}
}
class Beta {
class Mustache_Test_Functional_Beta {
protected $_data = array();
public function __construct() {
$this->_data['foo'] = new \StdClass();
$this->_data['foo'] = new StdClass();
$this->_data['foo']->name = 'Foo';
$this->_data['foo']->number = 1;
}
@@ -81,15 +77,15 @@ class Beta {
}
}
class Gamma {
class Mustache_Test_Functional_Gamma {
public $bar;
public function __construct() {
$this->bar = new Beta();
$this->bar = new Mustache_Test_Functional_Beta;
}
}
class Delta {
class Mustache_Test_Functional_Delta {
protected $_name = 'Foo';
public function name() {
+5 -9
View File
@@ -9,16 +9,12 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test;
use Mustache\HelperCollection;
class HelperCollectionTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_HelperCollectionTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
$foo = function() { echo 'foo'; };
$bar = 'BAR';
$helpers = new HelperCollection(array(
$helpers = new Mustache_HelperCollection(array(
'foo' => $foo,
'bar' => $bar,
));
@@ -31,7 +27,7 @@ class HelperCollectionTest extends \PHPUnit_Framework_TestCase {
$foo = function() { echo 'foo'; };
$bar = 'BAR';
$helpers = new HelperCollection;
$helpers = new Mustache_HelperCollection;
$this->assertTrue($helpers->isEmpty());
$this->assertFalse($helpers->has('foo'));
$this->assertFalse($helpers->has('bar'));
@@ -56,7 +52,7 @@ class HelperCollectionTest extends \PHPUnit_Framework_TestCase {
$foo = function() { echo 'foo'; };
$bar = 'BAR';
$helpers = new HelperCollection;
$helpers = new Mustache_HelperCollection;
$this->assertTrue($helpers->isEmpty());
$this->assertFalse($helpers->has('foo'));
$this->assertFalse($helpers->has('bar'));
@@ -93,7 +89,7 @@ class HelperCollectionTest extends \PHPUnit_Framework_TestCase {
$this->setExpectedException($exception);
}
$helpers = new HelperCollection($helpers);
$helpers = new Mustache_HelperCollection($helpers);
foreach ($actions as $method => $args) {
call_user_func_array(array($helpers, $method), $args);
@@ -9,16 +9,12 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test\Loader;
use Mustache\Loader\ArrayLoader;
/**
* @group unit
*/
class ArrayLoaderTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_Loader_ArrayLoaderTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
$loader = new ArrayLoader(array(
$loader = new Mustache_Loader_ArrayLoader(array(
'foo' => 'bar'
));
@@ -26,7 +22,7 @@ class ArrayLoaderTest extends \PHPUnit_Framework_TestCase {
}
public function testSetAndLoadTemplates() {
$loader = new ArrayLoader(array(
$loader = new Mustache_Loader_ArrayLoader(array(
'foo' => 'bar'
));
$this->assertEquals('bar', $loader->load('foo'));
@@ -46,7 +42,7 @@ class ArrayLoaderTest extends \PHPUnit_Framework_TestCase {
* @expectedException \InvalidArgumentException
*/
public function testMissingTemplatesThrowExceptions() {
$loader = new ArrayLoader;
$loader = new Mustache_Loader_ArrayLoader;
$loader->load('not_a_real_template');
}
}
@@ -9,24 +9,20 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test\Loader;
use Mustache\Loader\FilesystemLoader;
/**
* @group unit
*/
class FilesystemLoaderTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_Loader_FilesystemLoaderTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
$baseDir = realpath(__DIR__.'/../../../fixtures/templates');
$loader = new FilesystemLoader($baseDir, array('extension' => '.ms'));
$baseDir = realpath(dirname(__FILE__).'/../../../fixtures/templates');
$loader = new Mustache_Loader_FilesystemLoader($baseDir, array('extension' => '.ms'));
$this->assertEquals('alpha contents', $loader->load('alpha'));
$this->assertEquals('beta contents', $loader->load('beta.ms'));
}
public function testLoadTemplates() {
$baseDir = realpath(__DIR__.'/../../../fixtures/templates');
$loader = new FilesystemLoader($baseDir);
$baseDir = realpath(dirname(__FILE__).'/../../../fixtures/templates');
$loader = new Mustache_Loader_FilesystemLoader($baseDir);
$this->assertEquals('one contents', $loader->load('one'));
$this->assertEquals('two contents', $loader->load('two.mustache'));
}
@@ -35,15 +31,15 @@ class FilesystemLoaderTest extends \PHPUnit_Framework_TestCase {
* @expectedException \RuntimeException
*/
public function testMissingBaseDirThrowsException() {
$loader = new FilesystemLoader(__DIR__.'/not_a_directory');
$loader = new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/not_a_directory');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testMissingTemplateThrowsException() {
$baseDir = realpath(__DIR__.'/../../../fixtures/templates');
$loader = new FilesystemLoader($baseDir);
$baseDir = realpath(dirname(__FILE__).'/../../../fixtures/templates');
$loader = new Mustache_Loader_FilesystemLoader($baseDir);
$loader->load('fake');
}
@@ -9,16 +9,12 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test\Loader;
use Mustache\Loader\StringLoader;
/**
* @group unit
*/
class StringLoaderTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_Loader_StringLoaderTest extends PHPUnit_Framework_TestCase {
public function testLoadTemplates() {
$loader = new StringLoader;
$loader = new Mustache_Loader_StringLoader;
$this->assertEquals('foo', $loader->load('foo'));
$this->assertEquals('{{ bar }}', $loader->load('{{ bar }}'));
+18 -51
View File
@@ -9,19 +9,10 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test;
use Mustache\Compiler;
use Mustache\Mustache;
use Mustache\Loader\StringLoader;
use Mustache\Loader\ArrayLoader;
use Mustache\Parser;
use Mustache\Tokenizer;
/**
* @group unit
*/
class MustacheTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_MustacheTest extends PHPUnit_Framework_TestCase {
private static $tempDir;
@@ -33,9 +24,9 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
}
public function testConstructor() {
$loader = new StringLoader;
$partialsLoader = new ArrayLoader;
$mustache = new Mustache(array(
$loader = new Mustache_Loader_StringLoader;
$partialsLoader = new Mustache_Loader_ArrayLoader;
$mustache = new Mustache_Mustache(array(
'template_class_prefix' => '__whot__',
'cache' => self::$tempDir,
'loader' => $loader,
@@ -67,7 +58,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
$data = array('bar' => 'baz');
$output = 'TEH OUTPUT';
$template = $this->getMockBuilder('Mustache\Template')
$template = $this->getMockBuilder('Mustache_Template')
->disableOriginalConstructor()
->getMock();
@@ -84,11 +75,11 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
}
public function testSettingServices() {
$loader = new StringLoader;
$tokenizer = new Tokenizer;
$parser = new Parser;
$compiler = new Compiler;
$mustache = new Mustache;
$loader = new Mustache_Loader_StringLoader;
$tokenizer = new Mustache_Tokenizer;
$parser = new Mustache_Parser;
$compiler = new Mustache_Compiler;
$mustache = new Mustache_Mustache;
$this->assertNotSame($loader, $mustache->getLoader());
$mustache->setLoader($loader);
@@ -115,7 +106,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
* @group functional
*/
public function testCache() {
$mustache = new Mustache(array(
$mustache = new Mustache_Mustache(array(
'template_class_prefix' => '__whot__',
'cache' => self::$tempDir,
));
@@ -126,7 +117,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
$fileName = self::$tempDir . '/' . $className . '.php';
$this->assertInstanceOf($className, $template);
$this->assertFileExists($fileName);
$this->assertContains("\nclass $className extends \Mustache\Template", file_get_contents($fileName));
$this->assertContains("\nclass $className extends Mustache_Template", file_get_contents($fileName));
}
/**
@@ -134,7 +125,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
* @dataProvider getBadEscapers
*/
public function testNonCallableEscapeThrowsException($escape) {
new Mustache(array('escape' => $escape));
new Mustache_Mustache(array('escape' => $escape));
}
public function getBadEscapers() {
@@ -144,25 +135,12 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
);
}
/**
* @group functional
* @expectedException \RuntimeException
*/
public function testCacheFailsThrowException() {
global $mustacheFilesystemRenameHax;
$mustacheFilesystemRenameHax = true;
$mustache = new Mustache(array('cache' => self::$tempDir));
$mustache->loadTemplate('{{ foo }}');
}
/**
* @expectedException \RuntimeException
*/
public function testImmutablePartialsLoadersThrowException() {
$mustache = new Mustache(array(
'partials_loader' => new StringLoader,
$mustache = new Mustache_Mustache(array(
'partials_loader' => new Mustache_Loader_StringLoader,
));
$mustache->setPartials(array('foo' => '{{ foo }}'));
@@ -171,7 +149,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
public function testHelpers() {
$foo = function() { return 'foo'; };
$bar = 'BAR';
$mustache = new Mustache(array('helpers' => array(
$mustache = new Mustache_Mustache(array('helpers' => array(
'foo' => $foo,
'bar' => $bar,
)));
@@ -207,7 +185,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
* @expectedException \InvalidArgumentException
*/
public function testSetHelpersThrowsExceptions() {
$mustache = new Mustache;
$mustache = new Mustache_Mustache;
$mustache->setHelpers('monkeymonkeymonkey');
}
@@ -232,7 +210,7 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
}
}
class MustacheStub extends Mustache {
class MustacheStub extends Mustache_Mustache {
public $source;
public $template;
public function loadTemplate($source) {
@@ -241,14 +219,3 @@ class MustacheStub extends Mustache {
return $this->template;
}
}
// It's prob'ly best if you ignore this bit.
namespace Mustache;
function rename($a, $b) {
global $mustacheFilesystemRenameHax;
return ($mustacheFilesystemRenameHax) ? false : \rename($a, $b);
}
+43 -48
View File
@@ -9,22 +9,17 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test;
use Mustache\Parser;
use Mustache\Tokenizer;
/**
* @group unit
*/
class ParserTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_ParserTest extends PHPUnit_Framework_TestCase {
/**
* @dataProvider getTokenSets
*/
public function testParse($tokens, $expected)
{
$parser = new Parser;
$parser = new Mustache_Parser;
$this->assertEquals($expected, $parser->parse($tokens));
}
@@ -43,12 +38,12 @@ class ParserTest extends \PHPUnit_Framework_TestCase {
array(
array(array(
Tokenizer::TYPE => Tokenizer::T_ESCAPED,
Tokenizer::NAME => 'name'
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name'
)),
array(array(
Tokenizer::TYPE => Tokenizer::T_ESCAPED,
Tokenizer::NAME => 'name'
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name'
)),
),
@@ -56,32 +51,32 @@ class ParserTest extends \PHPUnit_Framework_TestCase {
array(
'foo',
array(
Tokenizer::TYPE => Tokenizer::T_INVERTED,
Tokenizer::INDEX => 123,
Tokenizer::NAME => 'parent'
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_INVERTED,
Mustache_Tokenizer::INDEX => 123,
Mustache_Tokenizer::NAME => 'parent'
),
array(
Tokenizer::TYPE => Tokenizer::T_ESCAPED,
Tokenizer::NAME => 'name'
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name'
),
array(
Tokenizer::TYPE => Tokenizer::T_END_SECTION,
Tokenizer::INDEX => 456,
Tokenizer::NAME => 'parent'
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_END_SECTION,
Mustache_Tokenizer::INDEX => 456,
Mustache_Tokenizer::NAME => 'parent'
),
'bar',
),
array(
'foo',
array(
Tokenizer::TYPE => Tokenizer::T_INVERTED,
Tokenizer::NAME => 'parent',
Tokenizer::INDEX => 123,
Tokenizer::END => 456,
Tokenizer::NODES => array(
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_INVERTED,
Mustache_Tokenizer::NAME => 'parent',
Mustache_Tokenizer::INDEX => 123,
Mustache_Tokenizer::END => 456,
Mustache_Tokenizer::NODES => array(
array(
Tokenizer::TYPE => Tokenizer::T_ESCAPED,
Tokenizer::NAME => 'name'
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name'
),
),
),
@@ -97,7 +92,7 @@ class ParserTest extends \PHPUnit_Framework_TestCase {
* @expectedException \LogicException
*/
public function testParserThrowsExceptions($tokens) {
$parser = new Parser;
$parser = new Mustache_Parser;
$parser->parse($tokens);
}
@@ -107,9 +102,9 @@ class ParserTest extends \PHPUnit_Framework_TestCase {
array(
array(
array(
Tokenizer::TYPE => Tokenizer::T_SECTION,
Tokenizer::NAME => 'parent',
Tokenizer::INDEX => 123,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_SECTION,
Mustache_Tokenizer::NAME => 'parent',
Mustache_Tokenizer::INDEX => 123,
),
),
),
@@ -118,9 +113,9 @@ class ParserTest extends \PHPUnit_Framework_TestCase {
array(
array(
array(
Tokenizer::TYPE => Tokenizer::T_INVERTED,
Tokenizer::NAME => 'parent',
Tokenizer::INDEX => 123,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_INVERTED,
Mustache_Tokenizer::NAME => 'parent',
Mustache_Tokenizer::INDEX => 123,
),
),
),
@@ -129,9 +124,9 @@ class ParserTest extends \PHPUnit_Framework_TestCase {
array(
array(
array(
Tokenizer::TYPE => Tokenizer::T_END_SECTION,
Tokenizer::NAME => 'parent',
Tokenizer::INDEX => 123,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_END_SECTION,
Mustache_Tokenizer::NAME => 'parent',
Mustache_Tokenizer::INDEX => 123,
),
),
),
@@ -140,24 +135,24 @@ class ParserTest extends \PHPUnit_Framework_TestCase {
array(
array(
array(
Tokenizer::TYPE => Tokenizer::T_SECTION,
Tokenizer::NAME => 'parent',
Tokenizer::INDEX => 123,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_SECTION,
Mustache_Tokenizer::NAME => 'parent',
Mustache_Tokenizer::INDEX => 123,
),
array(
Tokenizer::TYPE => Tokenizer::T_SECTION,
Tokenizer::NAME => 'child',
Tokenizer::INDEX => 123,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_SECTION,
Mustache_Tokenizer::NAME => 'child',
Mustache_Tokenizer::INDEX => 123,
),
array(
Tokenizer::TYPE => Tokenizer::T_END_SECTION,
Tokenizer::NAME => 'parent',
Tokenizer::INDEX => 123,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_END_SECTION,
Mustache_Tokenizer::NAME => 'parent',
Mustache_Tokenizer::INDEX => 123,
),
array(
Tokenizer::TYPE => Tokenizer::T_END_SECTION,
Tokenizer::NAME => 'child',
Tokenizer::INDEX => 123,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_END_SECTION,
Mustache_Tokenizer::NAME => 'child',
Mustache_Tokenizer::INDEX => 123,
),
),
),
+8 -14
View File
@@ -9,28 +9,22 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test;
use Mustache\Context;
use Mustache\Mustache;
use Mustache\Template;
/**
* @group unit
*/
class TemplateTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_TemplateTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
$mustache = new Mustache;
$template = new TemplateStub($mustache);
$mustache = new Mustache_Mustache;
$template = new Mustache_Test_TemplateStub($mustache);
$this->assertSame($mustache, $template->getMustache());
}
public function testRendering() {
$rendered = '<< wheee >>';
$mustache = new Mustache;
$template = new TemplateStub($mustache);
$mustache = new Mustache_Mustache;
$template = new Mustache_Test_TemplateStub($mustache);
$template->rendered = $rendered;
$context = new Context;
$context = new Mustache_Context;
$this->assertEquals($rendered, $template());
$this->assertEquals($rendered, $template->render());
@@ -39,14 +33,14 @@ class TemplateTest extends \PHPUnit_Framework_TestCase {
}
}
class TemplateStub extends Template {
class Mustache_Test_TemplateStub extends Mustache_Template {
public $rendered;
public function getMustache() {
return $this->mustache;
}
public function renderInternal(Context $context, $indent = '', $escape = false) {
public function renderInternal(Mustache_Context $context, $indent = '', $escape = false) {
return $this->rendered;
}
}
+37 -41
View File
@@ -9,20 +9,16 @@
* file that was distributed with this source code.
*/
namespace Mustache\Test;
use Mustache\Tokenizer;
/**
* @group unit
*/
class TokenizerTest extends \PHPUnit_Framework_TestCase {
class Mustache_Test_TokenizerTest extends PHPUnit_Framework_TestCase {
/**
* @dataProvider getTokens
*/
public function testScan($text, $delimiters, $expected) {
$tokenizer = new Tokenizer;
$tokenizer = new Mustache_Tokenizer;
$this->assertSame($expected, $tokenizer->scan($text, $delimiters));
}
@@ -45,11 +41,11 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase {
null,
array(
array(
Tokenizer::TYPE => Tokenizer::T_ESCAPED,
Tokenizer::NAME => 'name',
Tokenizer::OTAG => '{{',
Tokenizer::CTAG => '}}',
Tokenizer::INDEX => 10,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name',
Mustache_Tokenizer::OTAG => '{{',
Mustache_Tokenizer::CTAG => '}}',
Mustache_Tokenizer::INDEX => 10,
)
)
),
@@ -65,11 +61,11 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase {
'<<< >>>',
array(
array(
Tokenizer::TYPE => Tokenizer::T_ESCAPED,
Tokenizer::NAME => 'name',
Tokenizer::OTAG => '<<<',
Tokenizer::CTAG => '>>>',
Tokenizer::INDEX => 12,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name',
Mustache_Tokenizer::OTAG => '<<<',
Mustache_Tokenizer::CTAG => '>>>',
Mustache_Tokenizer::INDEX => 12,
)
)
),
@@ -79,42 +75,42 @@ class TokenizerTest extends \PHPUnit_Framework_TestCase {
null,
array(
array(
Tokenizer::TYPE => Tokenizer::T_UNESCAPED,
Tokenizer::NAME => 'a',
Tokenizer::OTAG => '{{',
Tokenizer::CTAG => '}}',
Tokenizer::INDEX => 8,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_UNESCAPED,
Mustache_Tokenizer::NAME => 'a',
Mustache_Tokenizer::OTAG => '{{',
Mustache_Tokenizer::CTAG => '}}',
Mustache_Tokenizer::INDEX => 8,
),
"\n",
array(
Tokenizer::TYPE => Tokenizer::T_SECTION,
Tokenizer::NAME => 'b',
Tokenizer::OTAG => '{{',
Tokenizer::CTAG => '}}',
Tokenizer::INDEX => 18,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_SECTION,
Mustache_Tokenizer::NAME => 'b',
Mustache_Tokenizer::OTAG => '{{',
Mustache_Tokenizer::CTAG => '}}',
Mustache_Tokenizer::INDEX => 18,
),
null,
array(
Tokenizer::TYPE => Tokenizer::T_ESCAPED,
Tokenizer::NAME => 'c',
Tokenizer::OTAG => '|',
Tokenizer::CTAG => '|',
Tokenizer::INDEX => 37,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'c',
Mustache_Tokenizer::OTAG => '|',
Mustache_Tokenizer::CTAG => '|',
Mustache_Tokenizer::INDEX => 37,
),
array(
Tokenizer::TYPE => Tokenizer::T_END_SECTION,
Tokenizer::NAME => 'b',
Tokenizer::OTAG => '|',
Tokenizer::CTAG => '|',
Tokenizer::INDEX => 37,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_END_SECTION,
Mustache_Tokenizer::NAME => 'b',
Mustache_Tokenizer::OTAG => '|',
Mustache_Tokenizer::CTAG => '|',
Mustache_Tokenizer::INDEX => 37,
),
"\n",
array(
Tokenizer::TYPE => Tokenizer::T_UNESCAPED,
Tokenizer::NAME => 'd',
Tokenizer::OTAG => '|',
Tokenizer::CTAG => '|',
Tokenizer::INDEX => 51,
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_UNESCAPED,
Mustache_Tokenizer::NAME => 'd',
Mustache_Tokenizer::OTAG => '|',
Mustache_Tokenizer::CTAG => '|',
Mustache_Tokenizer::INDEX => 51,
),
)
+3 -3
View File
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
require __DIR__.'/../src/Mustache/Autoloader.php';
\Mustache\Autoloader::register();
require dirname(__FILE__).'/../src/Mustache/Autoloader.php';
Mustache_Autoloader::register();
require __DIR__.'/../vendor/yaml/lib/sfYamlParser.php';
require dirname(__FILE__).'/../vendor/yaml/lib/sfYamlParser.php';
+1 -3
View File
@@ -9,8 +9,6 @@
* file that was distributed with this source code.
*/
namespace Mustache;
class Bar {
class Mustache_Bar {
// nada
}
+1 -3
View File
@@ -9,8 +9,6 @@
* file that was distributed with this source code.
*/
namespace Mustache;
class Foo {
class Mustache_Foo {
// nada
}