Update to proposed PSR-1 coding standard.

* 4 space (no tab) indent
 * K&R-style braces
This commit is contained in:
Justin Hileman
2012-05-01 22:03:39 -07:00
parent be5cfb3e9e
commit 2448992cc8
57 changed files with 3283 additions and 3009 deletions
+8 -4
View File
@@ -12,7 +12,8 @@
/**
* Mustache class autoloader.
*/
class Mustache_Autoloader {
class Mustache_Autoloader
{
private $baseDir;
@@ -21,7 +22,8 @@ class Mustache_Autoloader {
*
* @param string $baseDir Mustache library base directory (default: dirname(__FILE__).'/..')
*/
public function __construct($baseDir = null) {
public function __construct($baseDir = null)
{
if ($baseDir === null) {
$this->baseDir = dirname(__FILE__).'/..';
} else {
@@ -36,7 +38,8 @@ class Mustache_Autoloader {
*
* @return Mustache_Autoloader Registered Autoloader instance
*/
static public function register($baseDir = null) {
static public function register($baseDir = null)
{
$loader = new self($baseDir);
spl_autoload_register(array($loader, 'autoload'));
@@ -48,7 +51,8 @@ class Mustache_Autoloader {
*
* @param string $class
*/
public function autoload($class) {
public function autoload($class)
{
if ($class[0] === '\\') {
$class = substr($class, 1);
}
+31 -16
View File
@@ -14,7 +14,8 @@
*
* This class is responsible for turning a Mustache token parse tree into normal PHP source code.
*/
class Mustache_Compiler {
class Mustache_Compiler
{
private $sections;
private $source;
@@ -31,7 +32,8 @@ class Mustache_Compiler {
*
* @return string Generated PHP source code
*/
public function compile($source, array $tree, $name, $customEscape = false, $charset = 'UTF-8') {
public function compile($source, array $tree, $name, $customEscape = false, $charset = 'UTF-8')
{
$this->sections = array();
$this->source = $source;
$this->indentNextLine = true;
@@ -51,7 +53,8 @@ class Mustache_Compiler {
*
* @return string Generated PHP source code;
*/
private function walk(array $tree, $level = 0) {
private function walk(array $tree, $level = 0)
{
$code = '';
$level++;
foreach ($tree as $node) {
@@ -112,8 +115,10 @@ class Mustache_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
@@ -134,7 +139,8 @@ class Mustache_Compiler {
*
* @return string Generated PHP source code
*/
private function writeCode($tree, $name) {
private function writeCode($tree, $name)
{
$code = $this->walk($tree);
$sections = implode("\n", $this->sections);
@@ -178,7 +184,8 @@ class Mustache_Compiler {
*
* @return string Generated section PHP source code
*/
private function section($nodes, $id, $start, $end, $otag, $ctag, $level) {
private function section($nodes, $id, $start, $end, $otag, $ctag, $level)
{
$method = $this->getFindMethod($id);
$id = var_export($id, true);
$source = var_export(substr($this->source, $start, $end - $start), true);
@@ -214,7 +221,8 @@ class Mustache_Compiler {
*
* @return string Generated inverted section PHP source code
*/
private function invertedSection($nodes, $id, $level) {
private function invertedSection($nodes, $id, $level)
{
$method = $this->getFindMethod($id);
$id = var_export($id, true);
@@ -236,7 +244,8 @@ class Mustache_Compiler {
*
* @return string Generated partial call PHP source code
*/
private function partial($id, $indent, $level) {
private function partial($id, $indent, $level)
{
return sprintf(
$this->prepare(self::PARTIAL, $level),
var_export($id, true),
@@ -263,7 +272,8 @@ class Mustache_Compiler {
*
* @return string Generated variable interpolation PHP source
*/
private function variable($id, $escape, $level) {
private function variable($id, $escape, $level)
{
$method = $this->getFindMethod($id);
$id = ($method !== 'last') ? var_export($id, true) : '';
$value = $escape ? $this->getEscape() : '$value';
@@ -282,7 +292,8 @@ class Mustache_Compiler {
*
* @return string Generated output Buffer call PHP source
*/
private function text($text, $level) {
private function text($text, $level)
{
if ($text === "\n") {
$this->indentNextLine = true;
@@ -301,13 +312,14 @@ class Mustache_Compiler {
*
* @return string PHP source code snippet
*/
private function prepare($text, $bonus = 0, $prependNewline = true) {
private function prepare($text, $bonus = 0, $prependNewline = true)
{
$text = ($prependNewline ? "\n" : '').trim($text);
if ($prependNewline) {
$bonus++;
}
return preg_replace("/\n(\t\t)?/", "\n".str_repeat("\t", $bonus), $text);
return preg_replace("/\n( {8})?/", "\n".str_repeat(" ", $bonus * 4), $text);
}
const DEFAULT_ESCAPE = 'htmlspecialchars(%s, ENT_COMPAT, %s)';
@@ -318,7 +330,8 @@ class Mustache_Compiler {
*
* @return string Either a custom callback, or an inline call to `htmlspecialchars`
*/
private function getEscape($value = '$value') {
private function getEscape($value = '$value')
{
if ($this->customEscape) {
return sprintf(self::CUSTOM_ESCAPE, $value);
} else {
@@ -339,7 +352,8 @@ class Mustache_Compiler {
*
* @return string `find` method name
*/
private function getFindMethod($id) {
private function getFindMethod($id)
{
if ($id === '.') {
return 'last';
} elseif (strpos($id, '.') === false) {
@@ -356,7 +370,8 @@ class Mustache_Compiler {
*
* @return string "$indent . " or ""
*/
private function flushIndent() {
private function flushIndent()
{
if ($this->indentNextLine) {
$this->indentNextLine = false;
+16 -8
View File
@@ -12,7 +12,8 @@
/**
* Mustache Template rendering Context.
*/
class Mustache_Context {
class Mustache_Context
{
private $stack = array();
/**
@@ -20,7 +21,8 @@ class Mustache_Context {
*
* @param mixed $context Default rendering context (default: null)
*/
public function __construct($context = null) {
public function __construct($context = null)
{
if ($context !== null) {
$this->stack = array($context);
}
@@ -31,7 +33,8 @@ class Mustache_Context {
*
* @param mixed $value Object or array to use for context
*/
public function push($value) {
public function push($value)
{
array_push($this->stack, $value);
}
@@ -40,7 +43,8 @@ class Mustache_Context {
*
* @return mixed Last Context frame (object or array)
*/
public function pop() {
public function pop()
{
return array_pop($this->stack);
}
@@ -49,7 +53,8 @@ class Mustache_Context {
*
* @return mixed Last Context frame (object or array)
*/
public function last() {
public function last()
{
return end($this->stack);
}
@@ -68,7 +73,8 @@ class Mustache_Context {
*
* @return mixed Variable value, or '' if not found
*/
public function find($id) {
public function find($id)
{
return $this->findVariableInStack($id, $this->stack);
}
@@ -97,7 +103,8 @@ class Mustache_Context {
*
* @return mixed Variable value, or '' if not found
*/
public function findDot($id) {
public function findDot($id)
{
$chunks = explode('.', $id);
$first = array_shift($chunks);
$value = $this->findVariableInStack($first, $this->stack);
@@ -123,7 +130,8 @@ class Mustache_Context {
*
* @return mixed Variable value, or '' if not found
*/
private function findVariableInStack($id, array $stack) {
private function findVariableInStack($id, array $stack)
{
for ($i = count($stack) - 1; $i >= 0; $i--) {
if (is_object($stack[$i])) {
if (method_exists($stack[$i], $id)) {
+64 -32
View File
@@ -21,7 +21,8 @@
*
* @author Justin Hileman {@link http://justinhileman.com}
*/
class Mustache_Engine {
class Mustache_Engine
{
const VERSION = '2.0.0-dev';
const SPEC_VERSION = '1.1.2';
@@ -77,7 +78,8 @@ class Mustache_Engine {
*
* @param array $options (default: array())
*/
public function __construct(array $options = array()) {
public function __construct(array $options = array())
{
if (isset($options['template_class_prefix'])) {
$this->templateClassPrefix = $options['template_class_prefix'];
}
@@ -128,7 +130,8 @@ class Mustache_Engine {
*
* @return string Rendered template
*/
public function render($template, $data) {
public function render($template, $data)
{
return $this->loadTemplate($template)->render($data);
}
@@ -137,7 +140,8 @@ class Mustache_Engine {
*
* @return mixed Callable or null
*/
public function getEscape() {
public function getEscape()
{
return $this->escape;
}
@@ -146,7 +150,8 @@ class Mustache_Engine {
*
* @return string
*/
public function getCharset() {
public function getCharset()
{
return $this->charset;
}
@@ -155,7 +160,8 @@ class Mustache_Engine {
*
* @param Mustache_Loader $loader
*/
public function setLoader(Mustache_Loader $loader) {
public function setLoader(Mustache_Loader $loader)
{
$this->loader = $loader;
}
@@ -167,7 +173,8 @@ class Mustache_Engine {
*
* @return Mustache_Loader
*/
public function getLoader() {
public function getLoader()
{
if (!isset($this->loader)) {
$this->loader = new Mustache_Loader_StringLoader;
}
@@ -180,7 +187,8 @@ class Mustache_Engine {
*
* @param Mustache_Loader $partialsLoader
*/
public function setPartialsLoader(Mustache_Loader $partialsLoader) {
public function setPartialsLoader(Mustache_Loader $partialsLoader)
{
$this->partialsLoader = $partialsLoader;
}
@@ -192,7 +200,8 @@ class Mustache_Engine {
*
* @return Mustache_Loader
*/
public function getPartialsLoader() {
public function getPartialsLoader()
{
if (!isset($this->partialsLoader)) {
$this->partialsLoader = new Mustache_Loader_ArrayLoader;
}
@@ -207,7 +216,8 @@ class Mustache_Engine {
*
* @param array $partials (default: array())
*/
public function setPartials(array $partials = array()) {
public function setPartials(array $partials = array())
{
$loader = $this->getPartialsLoader();
if (!$loader instanceof Mustache_Loader_MutableLoader) {
throw new RuntimeException('Unable to set partials on an immutable Mustache Loader instance');
@@ -227,7 +237,8 @@ class Mustache_Engine {
*
* @param array|Traversable $helpers
*/
public function setHelpers($helpers) {
public function setHelpers($helpers)
{
if (!is_array($helpers) && !$helpers instanceof Traversable) {
throw new InvalidArgumentException('setHelpers expects an array of helpers');
}
@@ -246,7 +257,8 @@ class Mustache_Engine {
*
* @return Mustache_HelperCollection
*/
public function getHelpers() {
public function getHelpers()
{
if (!isset($this->helpers)) {
$this->helpers = new Mustache_HelperCollection;
}
@@ -262,7 +274,8 @@ class Mustache_Engine {
* @param string $name
* @param mixed $helper
*/
public function addHelper($name, $helper) {
public function addHelper($name, $helper)
{
$this->getHelpers()->add($name, $helper);
}
@@ -275,7 +288,8 @@ class Mustache_Engine {
*
* @return mixed Helper
*/
public function getHelper($name) {
public function getHelper($name)
{
return $this->getHelpers()->get($name);
}
@@ -288,7 +302,8 @@ class Mustache_Engine {
*
* @return boolean True if the helper is present
*/
public function hasHelper($name) {
public function hasHelper($name)
{
return $this->getHelpers()->has($name);
}
@@ -299,7 +314,8 @@ class Mustache_Engine {
*
* @param string $name
*/
public function removeHelper($name) {
public function removeHelper($name)
{
$this->getHelpers()->remove($name);
}
@@ -308,7 +324,8 @@ class Mustache_Engine {
*
* @param Mustache_Tokenizer $tokenizer
*/
public function setTokenizer(Mustache_Tokenizer $tokenizer) {
public function setTokenizer(Mustache_Tokenizer $tokenizer)
{
$this->tokenizer = $tokenizer;
}
@@ -319,7 +336,8 @@ class Mustache_Engine {
*
* @return Mustache_Tokenizer
*/
public function getTokenizer() {
public function getTokenizer()
{
if (!isset($this->tokenizer)) {
$this->tokenizer = new Mustache_Tokenizer;
}
@@ -332,7 +350,8 @@ class Mustache_Engine {
*
* @param Mustache_Parser $parser
*/
public function setParser(Mustache_Parser $parser) {
public function setParser(Mustache_Parser $parser)
{
$this->parser = $parser;
}
@@ -343,7 +362,8 @@ class Mustache_Engine {
*
* @return Mustache_Parser
*/
public function getParser() {
public function getParser()
{
if (!isset($this->parser)) {
$this->parser = new Mustache_Parser;
}
@@ -356,7 +376,8 @@ class Mustache_Engine {
*
* @param Mustache_Compiler $compiler
*/
public function setCompiler(Mustache_Compiler $compiler) {
public function setCompiler(Mustache_Compiler $compiler)
{
$this->compiler = $compiler;
}
@@ -367,7 +388,8 @@ class Mustache_Engine {
*
* @return Mustache_Compiler
*/
public function getCompiler() {
public function getCompiler()
{
if (!isset($this->compiler)) {
$this->compiler = new Mustache_Compiler;
}
@@ -382,7 +404,8 @@ class Mustache_Engine {
*
* @return string Mustache Template class name
*/
public function getTemplateClassName($source) {
public function getTemplateClassName($source)
{
return $this->templateClassPrefix . md5(sprintf(
'version:%s,escape:%s,charset:%s,source:%s',
self::VERSION,
@@ -399,7 +422,8 @@ class Mustache_Engine {
*
* @return Mustache_Template
*/
public function loadTemplate($name) {
public function loadTemplate($name)
{
return $this->loadSource($this->getLoader()->load($name));
}
@@ -413,7 +437,8 @@ class Mustache_Engine {
*
* @return Mustache_Template
*/
public function loadPartial($name) {
public function loadPartial($name)
{
try {
return $this->loadSource($this->getPartialsLoader()->load($name));
} catch (InvalidArgumentException $e) {
@@ -432,7 +457,8 @@ class Mustache_Engine {
*
* @return Mustache_Template
*/
public function loadLambda($source, $delims = null) {
public function loadLambda($source, $delims = null)
{
if ($delims !== null) {
$source = $delims . "\n" . $source;
}
@@ -451,7 +477,8 @@ class Mustache_Engine {
*
* @return Mustache_Template
*/
private function loadSource($source) {
private function loadSource($source)
{
$className = $this->getTemplateClassName($source);
if (!isset($this->templates[$className])) {
@@ -482,7 +509,8 @@ class Mustache_Engine {
*
* @return array Tokens
*/
private function tokenize($source) {
private function tokenize($source)
{
return $this->getTokenizer()->scan($source);
}
@@ -495,7 +523,8 @@ class Mustache_Engine {
*
* @return array Token tree
*/
private function parse($source) {
private function parse($source)
{
return $this->getParser()->parse($this->tokenize($source));
}
@@ -508,7 +537,8 @@ class Mustache_Engine {
*
* @return string generated Mustache template class code
*/
private function compile($source) {
private function compile($source)
{
$tree = $this->parse($source);
$name = $this->getTemplateClassName($source);
@@ -522,7 +552,8 @@ class Mustache_Engine {
*
* @return string Mustache Template class cache filename
*/
private function getCacheFilename($source) {
private function getCacheFilename($source)
{
if ($this->cache) {
return sprintf('%s/%s.php', $this->cache, $this->getTemplateClassName($source));
}
@@ -536,7 +567,8 @@ class Mustache_Engine {
* @param string $fileName
* @param string $source
*/
private function writeCacheFile($fileName, $source) {
private function writeCacheFile($fileName, $source)
{
if (!is_dir(dirname($fileName))) {
mkdir(dirname($fileName), 0777, true);
}
+24 -12
View File
@@ -12,7 +12,8 @@
/**
* A collection of helpers for a Mustache instance.
*/
class Mustache_HelperCollection {
class Mustache_HelperCollection
{
private $helpers = array();
/**
@@ -24,7 +25,8 @@ class Mustache_HelperCollection {
*
* @param array|Traversable $helpers (default: null)
*/
public function __construct($helpers = 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');
@@ -44,7 +46,8 @@ class Mustache_HelperCollection {
* @param string $name
* @param mixed $helper
*/
public function __set($name, $helper) {
public function __set($name, $helper)
{
$this->add($name, $helper);
}
@@ -54,7 +57,8 @@ class Mustache_HelperCollection {
* @param string $name
* @param mixed $helper
*/
public function add($name, $helper) {
public function add($name, $helper)
{
$this->helpers[$name] = $helper;
}
@@ -67,7 +71,8 @@ class Mustache_HelperCollection {
*
* @return mixed Helper
*/
public function __get($name) {
public function __get($name)
{
return $this->get($name);
}
@@ -78,7 +83,8 @@ class Mustache_HelperCollection {
*
* @return mixed Helper
*/
public function get($name) {
public function get($name)
{
if (!$this->has($name)) {
throw new InvalidArgumentException('Unknown helper: '.$name);
}
@@ -95,7 +101,8 @@ class Mustache_HelperCollection {
*
* @return boolean True if helper is present
*/
public function __isset($name) {
public function __isset($name)
{
return $this->has($name);
}
@@ -106,7 +113,8 @@ class Mustache_HelperCollection {
*
* @return boolean True if helper is present
*/
public function has($name) {
public function has($name)
{
return array_key_exists($name, $this->helpers);
}
@@ -117,7 +125,8 @@ class Mustache_HelperCollection {
*
* @param string $name
*/
public function __unset($name) {
public function __unset($name)
{
$this->remove($name);
}
@@ -128,7 +137,8 @@ class Mustache_HelperCollection {
*
* @param string $name
*/
public function remove($name) {
public function remove($name)
{
if (!$this->has($name)) {
throw new InvalidArgumentException('Unknown helper: '.$name);
}
@@ -141,7 +151,8 @@ class Mustache_HelperCollection {
*
* Removes all helpers from this collection
*/
public function clear() {
public function clear()
{
$this->helpers = array();
}
@@ -150,7 +161,8 @@ class Mustache_HelperCollection {
*
* @return boolean True if the collection is empty
*/
public function isEmpty() {
public function isEmpty()
{
return empty($this->helpers);
}
}
+3 -2
View File
@@ -12,7 +12,8 @@
/**
* Mustache Template Loader interface.
*/
interface Mustache_Loader {
interface Mustache_Loader
{
/**
* Load a Template by name.
@@ -21,5 +22,5 @@ interface Mustache_Loader {
*
* @return string Mustache Template source
*/
function load($name);
public function load($name);
}
+10 -5
View File
@@ -27,14 +27,16 @@
* @implements Loader
* @implements MutableLoader
*/
class Mustache_Loader_ArrayLoader implements Mustache_Loader, Mustache_Loader_MutableLoader {
class Mustache_Loader_ArrayLoader implements Mustache_Loader, Mustache_Loader_MutableLoader
{
/**
* ArrayLoader constructor.
*
* @param array $templates Associative array of Template source (default: array())
*/
public function __construct(array $templates = array()) {
public function __construct(array $templates = array())
{
$this->templates = $templates;
}
@@ -45,7 +47,8 @@ class Mustache_Loader_ArrayLoader implements Mustache_Loader, Mustache_Loader_Mu
*
* @return string Mustache Template source
*/
public function load($name) {
public function load($name)
{
if (!isset($this->templates[$name])) {
throw new InvalidArgumentException('Template '.$name.' not found.');
}
@@ -58,7 +61,8 @@ class Mustache_Loader_ArrayLoader implements Mustache_Loader, Mustache_Loader_Mu
*
* @param array $templates
*/
public function setTemplates(array $templates) {
public function setTemplates(array $templates)
{
$this->templates = $templates;
}
@@ -68,7 +72,8 @@ class Mustache_Loader_ArrayLoader implements Mustache_Loader, Mustache_Loader_Mu
* @param string $name
* @param string $template Mustache Template source
*/
public function setTemplate($name, $template) {
public function setTemplate($name, $template)
{
$this->templates[$name] = $template;
}
}
+10 -5
View File
@@ -26,7 +26,8 @@
*
* @implements Loader
*/
class Mustache_Loader_FilesystemLoader implements Mustache_Loader {
class Mustache_Loader_FilesystemLoader implements Mustache_Loader
{
private $baseDir;
private $extension = '.mustache';
private $templates = array();
@@ -46,7 +47,8 @@ class Mustache_Loader_FilesystemLoader implements Mustache_Loader {
* @param string $baseDir Base directory containing Mustache template files.
* @param array $options Array of Loader options (default: array())
*/
public function __construct($baseDir, array $options = array()) {
public function __construct($baseDir, array $options = array())
{
$this->baseDir = rtrim(realpath($baseDir), '/');
if (!is_dir($this->baseDir)) {
@@ -68,7 +70,8 @@ class Mustache_Loader_FilesystemLoader implements Mustache_Loader {
*
* @return string Mustache Template source
*/
public function load($name) {
public function load($name)
{
if (!isset($this->templates[$name])) {
$this->templates[$name] = $this->loadFile($name);
}
@@ -85,7 +88,8 @@ class Mustache_Loader_FilesystemLoader implements Mustache_Loader {
*
* @return string Mustache Template source
*/
private function loadFile($name) {
private function loadFile($name)
{
$fileName = $this->getFileName($name);
if (!file_exists($fileName)) {
@@ -102,7 +106,8 @@ class Mustache_Loader_FilesystemLoader implements Mustache_Loader {
*
* @return string Template file name
*/
private function getFileName($name) {
private function getFileName($name)
{
$fileName = $this->baseDir . '/' . $name;
if (substr($fileName, 0 - strlen($this->extension)) !== $this->extension) {
$fileName .= $this->extension;
+4 -3
View File
@@ -12,14 +12,15 @@
/**
* Mustache Template mutable Loader interface.
*/
interface Mustache_Loader_MutableLoader {
interface Mustache_Loader_MutableLoader
{
/**
* Set an associative array of Template sources for this loader.
*
* @param array $templates
*/
function setTemplates(array $templates);
public function setTemplates(array $templates);
/**
* Set a Template source by name.
@@ -27,5 +28,5 @@ interface Mustache_Loader_MutableLoader {
* @param string $name
* @param string $template Mustache Template source
*/
function setTemplate($name, $template);
public function setTemplate($name, $template);
}
+4 -2
View File
@@ -25,7 +25,8 @@
*
* @implements Loader
*/
class Mustache_Loader_StringLoader implements Mustache_Loader {
class Mustache_Loader_StringLoader implements Mustache_Loader
{
/**
* Load a Template by source.
@@ -34,7 +35,8 @@ class Mustache_Loader_StringLoader implements Mustache_Loader {
*
* @return string Mustache Template source
*/
public function load($name) {
public function load($name)
{
return $name;
}
}
+6 -3
View File
@@ -14,7 +14,8 @@
*
* This class is responsible for turning a set of Mustache tokens into a parse tree.
*/
class Mustache_Parser {
class Mustache_Parser
{
/**
* Process an array of Mustache tokens and convert them into a parse tree.
@@ -23,7 +24,8 @@ class Mustache_Parser {
*
* @return array Mustache token parse tree
*/
public function parse(array $tokens = array()) {
public function parse(array $tokens = array())
{
return $this->buildTree(new ArrayIterator($tokens));
}
@@ -37,7 +39,8 @@ class Mustache_Parser {
*
* @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 {
+12 -6
View File
@@ -14,7 +14,8 @@
*
* @abstract
*/
abstract class Mustache_Template {
abstract class Mustache_Template
{
/**
* @var Mustache_Engine
@@ -26,7 +27,8 @@ abstract class Mustache_Template {
*
* @param Mustache_Engine $mustache
*/
public function __construct(Mustache_Engine $mustache) {
public function __construct(Mustache_Engine $mustache)
{
$this->mustache = $mustache;
}
@@ -43,7 +45,8 @@ abstract class Mustache_Template {
*
* @return string Rendered template
*/
public function __invoke($context = array()) {
public function __invoke($context = array())
{
return $this->render($context);
}
@@ -54,7 +57,8 @@ abstract class Mustache_Template {
*
* @return string Rendered template
*/
public function render($context = array()) {
public function render($context = array())
{
return $this->renderInternal($this->prepareContextStack($context));
}
@@ -100,7 +104,8 @@ abstract class Mustache_Template {
*
* @return boolean True if the value is 'iterable'
*/
protected function isIterable($value) {
protected function isIterable($value)
{
if (is_object($value)) {
return $value instanceof Traversable;
} elseif (is_array($value)) {
@@ -126,7 +131,8 @@ abstract class Mustache_Template {
*
* @return Mustache_Context
*/
protected function prepareContextStack($context = null) {
protected function prepareContextStack($context = null)
{
$stack = new Mustache_Context;
$helpers = $this->mustache->getHelpers();
+14 -7
View File
@@ -84,7 +84,8 @@ class Mustache_Tokenizer {
*
* @return array Set of Mustache tokens
*/
public function scan($text, $delimiters = null) {
public function scan($text, $delimiters = null)
{
$this->reset();
if ($delimiters = trim($delimiters)) {
@@ -172,7 +173,8 @@ class Mustache_Tokenizer {
/**
* Helper function to reset tokenizer internal state.
*/
private function reset() {
private function reset()
{
$this->state = self::IN_TEXT;
$this->tagType = null;
$this->tag = null;
@@ -187,7 +189,8 @@ class Mustache_Tokenizer {
/**
* Flush the current buffer to a token.
*/
private function flushBuffer() {
private function flushBuffer()
{
if (!empty($this->buffer)) {
$this->tokens[] = array(self::TYPE => self::T_TEXT, self::VALUE => $this->buffer);
$this->buffer = '';
@@ -199,7 +202,8 @@ class Mustache_Tokenizer {
*
* @return boolean True if the current line is all whitespace
*/
private function lineIsWhitespace() {
private function lineIsWhitespace()
{
$tokensCount = count($this->tokens);
for ($j = $this->lineStart; $j < $tokensCount; $j++) {
$token = $this->tokens[$j];
@@ -222,7 +226,8 @@ class Mustache_Tokenizer {
*
* @param bool $noNewLine Suppress the newline? (default: false)
*/
private function filterLine($noNewLine = false) {
private function filterLine($noNewLine = false)
{
$this->flushBuffer();
if ($this->seenTag && $this->lineIsWhitespace()) {
$tokensCount = count($this->tokens);
@@ -251,7 +256,8 @@ class Mustache_Tokenizer {
*
* @return int New index value
*/
private function changeDelimiters($text, $index) {
private function changeDelimiters($text, $index)
{
$startIndex = strpos($text, '=', $index) + 1;
$close = '='.$this->ctag;
$closeIndex = strpos($text, $close, $index);
@@ -272,7 +278,8 @@ class Mustache_Tokenizer {
*
* @return boolean True if this is a closing section tag
*/
private function tagChange($tag, $text, $index) {
private function tagChange($tag, $text, $index)
{
return substr($text, $index, strlen($tag)) === $tag;
}
}
+6 -3
View File
@@ -12,13 +12,16 @@
/**
* @group unit
*/
class Mustache_Test_AutoloaderTest extends PHPUnit_Framework_TestCase {
public function testRegister() {
class Mustache_Test_AutoloaderTest extends PHPUnit_Framework_TestCase
{
public function testRegister()
{
$loader = Mustache_Autoloader::register();
$this->assertTrue(spl_autoload_unregister(array($loader, 'autoload')));
}
public function testAutoloader() {
public function testAutoloader()
{
$loader = new Mustache_Autoloader(dirname(__FILE__).'/../../fixtures/autoloader');
$this->assertNull($loader->autoload('NonMustacheClass'));
+10 -5
View File
@@ -12,12 +12,14 @@
/**
* @group unit
*/
class Mustache_Test_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) {
public function testCompile($source, array $tree, $name, $customEscaper, $charset, $expected)
{
$compiler = new Mustache_Compiler;
$compiled = $compiler->compile($source, $tree, $name, $customEscaper, $charset);
@@ -26,7 +28,8 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase {
}
}
public function getCompileValues() {
public function getCompileValues()
{
return array(
array('', array(), 'Banana', false, 'ISO-8859-1', array(
"\nclass Banana extends Mustache_Template",
@@ -84,12 +87,14 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase {
/**
* @expectedException InvalidArgumentException
*/
public function testCompilerThrowsUnknownNodeTypeException() {
public function testCompilerThrowsUnknownNodeTypeException()
{
$compiler = new Mustache_Compiler;
$compiler->compile('', array(array(Mustache_Tokenizer::TYPE => 'invalid')), 'SomeClass');
}
private function createTextToken($value) {
private function createTextToken($value)
{
return array(
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT,
Mustache_Tokenizer::VALUE => $value,
+16 -8
View File
@@ -12,8 +12,10 @@
/**
* @group unit
*/
class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$one = new Mustache_Context;
$this->assertSame('', $one->find('foo'));
$this->assertSame('', $one->find('bar'));
@@ -32,7 +34,8 @@ class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('NAME', $three->find('name'));
}
public function testPushPopAndLast() {
public function testPushPopAndLast()
{
$context = new Mustache_Context;
$this->assertFalse($context->last());
@@ -52,7 +55,8 @@ class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase {
$this->assertFalse($context->last());
}
public function testFind() {
public function testFind()
{
$context = new Mustache_Context;
$dummy = new Mustache_Test_TestDummy;
@@ -94,18 +98,22 @@ class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase {
}
}
class Mustache_Test_TestDummy {
class Mustache_Test_TestDummy
{
public $name = 'dummy';
public function __invoke() {
public function __invoke()
{
// nothing
}
public static function foo() {
public static function foo()
{
return '<foo>';
}
public function bar() {
public function bar()
{
return '<bar>';
}
}
+32 -16
View File
@@ -12,18 +12,21 @@
/**
* @group unit
*/
class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase {
class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
{
private static $tempDir;
public static function setUpBeforeClass() {
public static function setUpBeforeClass()
{
self::$tempDir = sys_get_temp_dir() . '/mustache_test';
if (file_exists(self::$tempDir)) {
self::rmdir(self::$tempDir);
}
}
public function testConstructor() {
public function testConstructor()
{
$loader = new Mustache_Loader_StringLoader;
$partialsLoader = new Mustache_Loader_ArrayLoader;
$mustache = new Mustache_Engine(array(
@@ -53,11 +56,13 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase {
$this->assertFalse($mustache->hasHelper('baz'));
}
public static function getFoo() {
public static function getFoo()
{
return 'foo';
}
public function testRender() {
public function testRender()
{
$source = '{{ foo }}';
$data = array('bar' => 'baz');
$output = 'TEH OUTPUT';
@@ -78,7 +83,8 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($source, $mustache->source);
}
public function testSettingServices() {
public function testSettingServices()
{
$loader = new Mustache_Loader_StringLoader;
$tokenizer = new Mustache_Tokenizer;
$parser = new Mustache_Parser;
@@ -109,7 +115,8 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase {
/**
* @group functional
*/
public function testCache() {
public function testCache()
{
$mustache = new Mustache_Engine(array(
'template_class_prefix' => '__whot__',
'cache' => self::$tempDir,
@@ -128,11 +135,13 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase {
* @expectedException InvalidArgumentException
* @dataProvider getBadEscapers
*/
public function testNonCallableEscapeThrowsException($escape) {
public function testNonCallableEscapeThrowsException($escape)
{
new Mustache_Engine(array('escape' => $escape));
}
public function getBadEscapers() {
public function getBadEscapers()
{
return array(
array('nothing'),
array('foo', 'bar'),
@@ -142,7 +151,8 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase {
/**
* @expectedException RuntimeException
*/
public function testImmutablePartialsLoadersThrowException() {
public function testImmutablePartialsLoadersThrowException()
{
$mustache = new Mustache_Engine(array(
'partials_loader' => new Mustache_Loader_StringLoader,
));
@@ -150,7 +160,8 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase {
$mustache->setPartials(array('foo' => '{{ foo }}'));
}
public function testMissingPartialsTreatedAsEmptyString() {
public function testMissingPartialsTreatedAsEmptyString()
{
$mustache = new Mustache_Engine(array(
'partials_loader' => new Mustache_Loader_ArrayLoader(array(
'foo' => 'FOO',
@@ -161,7 +172,8 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('FOOBAZ', $mustache->render('{{>foo}}{{>bar}}{{>baz}}', array()));
}
public function testHelpers() {
public function testHelpers()
{
$foo = array($this, 'getFoo');
$bar = 'BAR';
$mustache = new Mustache_Engine(array('helpers' => array(
@@ -196,19 +208,22 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('foo - BAR - __qux__', $tpl->render(array('qux' => "won't mess things up")));
}
public static function wrapWithUnderscores($text) {
public static function wrapWithUnderscores($text)
{
return '__'.$text.'__';
}
/**
* @expectedException InvalidArgumentException
*/
public function testSetHelpersThrowsExceptions() {
public function testSetHelpersThrowsExceptions()
{
$mustache = new Mustache_Engine;
$mustache->setHelpers('monkeymonkeymonkey');
}
private static function rmdir($path) {
private static function rmdir($path)
{
$path = rtrim($path, '/').'/';
$handle = opendir($path);
while (($file = readdir($handle)) !== false) {
@@ -232,7 +247,8 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase {
class MustacheStub extends Mustache_Engine {
public $source;
public $template;
public function loadTemplate($source) {
public function loadTemplate($source)
{
$this->source = $source;
return $this->template;
+8 -4
View File
@@ -13,9 +13,11 @@
* @group magic_methods
* @group functional
*/
class Mustache_Test_Functional_CallTest extends PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_CallTest extends PHPUnit_Framework_TestCase
{
public function testCallEatsContext() {
public function testCallEatsContext()
{
$m = new Mustache_Engine;
$tpl = $m->loadTemplate('{{# foo }}{{ label }}: {{ name }}{{/ foo }}');
@@ -28,9 +30,11 @@ class Mustache_Test_Functional_CallTest extends PHPUnit_Framework_TestCase {
}
}
class Mustache_Test_Functional_ClassWithCall {
class Mustache_Test_Functional_ClassWithCall
{
public $name;
public function __call($method, $args) {
public function __call($method, $args)
{
return 'unknown value';
}
}
+10 -5
View File
@@ -13,7 +13,8 @@
* @group examples
* @group functional
*/
class Mustache_Test_Functional_ExamplesTest extends PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_ExamplesTest extends PHPUnit_Framework_TestCase
{
/**
* Test everything in the `examples` directory.
@@ -25,7 +26,8 @@ class Mustache_Test_Functional_ExamplesTest extends PHPUnit_Framework_TestCase {
* @param array $partials
* @param string $expected
*/
public function testExamples($context, $source, $partials, $expected) {
public function testExamples($context, $source, $partials, $expected)
{
$mustache = new Mustache_Engine(array(
'partials' => $partials
));
@@ -43,7 +45,8 @@ class Mustache_Test_Functional_ExamplesTest extends PHPUnit_Framework_TestCase {
*
* @return array
*/
public function getExamples() {
public function getExamples()
{
$path = realpath(dirname(__FILE__).'/../../../fixtures/examples');
$examples = array();
@@ -70,7 +73,8 @@ class Mustache_Test_Functional_ExamplesTest extends PHPUnit_Framework_TestCase {
*
* @return array arguments for testExamples
*/
private function loadExample($path) {
private function loadExample($path)
{
$context = null;
$source = null;
$partials = array();
@@ -114,7 +118,8 @@ class Mustache_Test_Functional_ExamplesTest extends PHPUnit_Framework_TestCase {
*
* @return array $partials
*/
private function loadPartials($path) {
private function loadPartials($path)
{
$partials = array();
$handle = opendir($path);
@@ -13,15 +13,18 @@
* @group lambdas
* @group functional
*/
class Mustache_Test_Functional_HigherOrderSectionsTest extends PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_HigherOrderSectionsTest extends PHPUnit_Framework_TestCase
{
private $mustache;
public function setUp() {
public function setUp()
{
$this->mustache = new Mustache_Engine;
}
public function testRuntimeSectionCallback() {
public function testRuntimeSectionCallback()
{
$tpl = $this->mustache->loadTemplate('{{#doublewrap}}{{name}}{{/doublewrap}}');
$foo = new Mustache_Test_Functional_Foo;
@@ -30,7 +33,8 @@ class Mustache_Test_Functional_HigherOrderSectionsTest extends PHPUnit_Framework
$this->assertEquals(sprintf('<strong><em>%s</em></strong>', $foo->name), $tpl->render($foo));
}
public function testStaticSectionCallback() {
public function testStaticSectionCallback()
{
$tpl = $this->mustache->loadTemplate('{{#trimmer}} {{name}} {{/trimmer}}');
$foo = new Mustache_Test_Functional_Foo;
@@ -39,7 +43,8 @@ class Mustache_Test_Functional_HigherOrderSectionsTest extends PHPUnit_Framework
$this->assertEquals($foo->name, $tpl->render($foo));
}
public function testViewArraySectionCallback() {
public function testViewArraySectionCallback()
{
$tpl = $this->mustache->loadTemplate('{{#trim}} {{name}} {{/trim}}');
$foo = new Mustache_Test_Functional_Foo;
@@ -52,7 +57,8 @@ class Mustache_Test_Functional_HigherOrderSectionsTest extends PHPUnit_Framework
$this->assertEquals($data['name'], $tpl->render($data));
}
public function testMonsters() {
public function testMonsters()
{
$tpl = $this->mustache->loadTemplate('{{#title}}{{title}} {{/title}}{{name}}');
$frank = new Mustache_Test_Functional_Monster();
@@ -67,28 +73,34 @@ class Mustache_Test_Functional_HigherOrderSectionsTest extends PHPUnit_Framework
}
}
class Mustache_Test_Functional_Foo {
class Mustache_Test_Functional_Foo
{
public $name = 'Justin';
public $lorem = 'Lorem ipsum dolor sit amet,';
public function wrapWithEm($text) {
public function wrapWithEm($text)
{
return sprintf('<em>%s</em>', $text);
}
public function wrapWithStrong($text) {
public function wrapWithStrong($text)
{
return sprintf('<strong>%s</strong>', $text);
}
public function wrapWithBoth($text) {
public function wrapWithBoth($text)
{
return self::wrapWithStrong(self::wrapWithEm($text));
}
public static function staticTrim($text) {
public static function staticTrim($text)
{
return trim($text);
}
}
class Mustache_Test_Functional_Monster {
class Mustache_Test_Functional_Monster
{
public $title;
public $name;
}
@@ -13,17 +13,20 @@
* @group mustache_injection
* @group functional
*/
class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_TestCase
{
private $mustache;
public function setUp() {
public function setUp()
{
$this->mustache = new Mustache_Engine;
}
// interpolation
public function testInterpolationInjection() {
public function testInterpolationInjection()
{
$tpl = $this->mustache->loadTemplate('{{ a }}');
$data = array(
@@ -34,7 +37,8 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T
$this->assertEquals('{{ b }}', $tpl->render($data));
}
public function testUnescapedInterpolationInjection() {
public function testUnescapedInterpolationInjection()
{
$tpl = $this->mustache->loadTemplate('{{{ a }}}');
$data = array(
@@ -48,7 +52,8 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T
// sections
public function testSectionInjection() {
public function testSectionInjection()
{
$tpl = $this->mustache->loadTemplate('{{# a }}{{ b }}{{/ a }}');
$data = array(
@@ -60,7 +65,8 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T
$this->assertEquals('{{ c }}', $tpl->render($data));
}
public function testUnescapedSectionInjection() {
public function testUnescapedSectionInjection()
{
$tpl = $this->mustache->loadTemplate('{{# a }}{{{ b }}}{{/ a }}');
$data = array(
@@ -75,7 +81,8 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T
// partials
public function testPartialInjection() {
public function testPartialInjection()
{
$tpl = $this->mustache->loadTemplate('{{> partial }}');
$this->mustache->setPartials(array(
'partial' => '{{ a }}',
@@ -89,7 +96,8 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T
$this->assertEquals('{{ b }}', $tpl->render($data));
}
public function testPartialUnescapedInjection() {
public function testPartialUnescapedInjection()
{
$tpl = $this->mustache->loadTemplate('{{> partial }}');
$this->mustache->setPartials(array(
'partial' => '{{{ a }}}',
@@ -106,7 +114,8 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T
// lambdas
public function testLambdaInterpolationInjection() {
public function testLambdaInterpolationInjection()
{
$tpl = $this->mustache->loadTemplate('{{ a }}');
$data = array(
@@ -118,11 +127,13 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T
$this->assertEquals('{{ c }}', $tpl->render($data));
}
public static function lambdaInterpolationCallback() {
public static function lambdaInterpolationCallback()
{
return '{{ b }}';
}
public function testLambdaSectionInjection() {
public function testLambdaSectionInjection()
{
$tpl = $this->mustache->loadTemplate('{{# a }}b{{/ a }}');
$data = array(
@@ -134,7 +145,8 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T
$this->assertEquals('{{ c }}', $tpl->render($data));
}
public static function lambdaSectionCallback($text) {
public static function lambdaSectionCallback($text)
{
return '{{ ' . $text . ' }}';
}
}
@@ -15,11 +15,13 @@
* @group mustache-spec
* @group functional
*/
class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCase
{
private static $mustache;
public static function setUpBeforeClass() {
public static function setUpBeforeClass()
{
self::$mustache = new Mustache_Engine;
}
@@ -27,7 +29,8 @@ class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCa
* For some reason data providers can't mark tests skipped, so this test exists
* simply to provide a 'skipped' test if the `spec` submodule isn't initialized.
*/
public function testSpecInitialized() {
public function testSpecInitialized()
{
if (!file_exists(dirname(__FILE__).'/../../../../vendor/spec/specs/')) {
$this->markTestSkipped('Mustache spec submodule not initialized: run "git submodule update --init"');
}
@@ -37,12 +40,14 @@ class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCa
* @group comments
* @dataProvider loadCommentSpec
*/
public function testCommentSpec($desc, $source, $partials, $data, $expected) {
public function testCommentSpec($desc, $source, $partials, $data, $expected)
{
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template->render($data), $desc);
}
public function loadCommentSpec() {
public function loadCommentSpec()
{
return $this->loadSpec('comments');
}
@@ -50,12 +55,14 @@ class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCa
* @group delimiters
* @dataProvider loadDelimitersSpec
*/
public function testDelimitersSpec($desc, $source, $partials, $data, $expected) {
public function testDelimitersSpec($desc, $source, $partials, $data, $expected)
{
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template->render($data), $desc);
}
public function loadDelimitersSpec() {
public function loadDelimitersSpec()
{
return $this->loadSpec('delimiters');
}
@@ -63,12 +70,14 @@ class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCa
* @group interpolation
* @dataProvider loadInterpolationSpec
*/
public function testInterpolationSpec($desc, $source, $partials, $data, $expected) {
public function testInterpolationSpec($desc, $source, $partials, $data, $expected)
{
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template->render($data), $desc);
}
public function loadInterpolationSpec() {
public function loadInterpolationSpec()
{
return $this->loadSpec('interpolation');
}
@@ -77,12 +86,14 @@ class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCa
* @group inverted-sections
* @dataProvider loadInvertedSpec
*/
public function testInvertedSpec($desc, $source, $partials, $data, $expected) {
public function testInvertedSpec($desc, $source, $partials, $data, $expected)
{
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template->render($data), $desc);
}
public function loadInvertedSpec() {
public function loadInvertedSpec()
{
return $this->loadSpec('inverted');
}
@@ -90,12 +101,14 @@ class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCa
* @group partials
* @dataProvider loadPartialsSpec
*/
public function testPartialsSpec($desc, $source, $partials, $data, $expected) {
public function testPartialsSpec($desc, $source, $partials, $data, $expected)
{
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template->render($data), $desc);
}
public function loadPartialsSpec() {
public function loadPartialsSpec()
{
return $this->loadSpec('partials');
}
@@ -103,12 +116,14 @@ class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCa
* @group sections
* @dataProvider loadSectionsSpec
*/
public function testSectionsSpec($desc, $source, $partials, $data, $expected) {
public function testSectionsSpec($desc, $source, $partials, $data, $expected)
{
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template->render($data), $desc);
}
public function loadSectionsSpec() {
public function loadSectionsSpec()
{
return $this->loadSpec('sections');
}
@@ -120,7 +135,8 @@ class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCa
* @access public
* @return array
*/
private function loadSpec($name) {
private function loadSpec($name)
{
$filename = dirname(__FILE__) . '/../../../../vendor/spec/specs/' . $name . '.yml';
if (!file_exists($filename)) {
return array();
@@ -150,7 +166,8 @@ class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCa
return $data;
}
private static function loadTemplate($source, $partials) {
private static function loadTemplate($source, $partials)
{
self::$mustache->setPartials($partials);
return self::$mustache->loadTemplate($source);
@@ -13,14 +13,17 @@
* @group sections
* @group functional
*/
class Mustache_Test_Functional_ObjectSectionTest extends PHPUnit_Framework_TestCase {
class Mustache_Test_Functional_ObjectSectionTest extends PHPUnit_Framework_TestCase
{
private $mustache;
public function setUp() {
public function setUp()
{
$this->mustache = new Mustache_Engine;
}
public function testBasicObject() {
public function testBasicObject()
{
$tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
$this->assertEquals('Foo', $tpl->render(new Mustache_Test_Functional_Alpha));
}
@@ -28,7 +31,8 @@ class Mustache_Test_Functional_ObjectSectionTest extends PHPUnit_Framework_TestC
/**
* @group magic_methods
*/
public function testObjectWithGet() {
public function testObjectWithGet()
{
$tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
$this->assertEquals('Foo', $tpl->render(new Mustache_Test_Functional_Beta));
}
@@ -36,12 +40,14 @@ class Mustache_Test_Functional_ObjectSectionTest extends PHPUnit_Framework_TestC
/**
* @group magic_methods
*/
public function testSectionObjectWithGet() {
public function testSectionObjectWithGet()
{
$tpl = $this->mustache->loadTemplate('{{#bar}}{{#foo}}{{name}}{{/foo}}{{/bar}}');
$this->assertEquals('Foo', $tpl->render(new Mustache_Test_Functional_Gamma));
}
public function testSectionObjectWithFunction() {
public function testSectionObjectWithFunction()
{
$tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
$alpha = new Mustache_Test_Functional_Alpha;
$alpha->foo = new Mustache_Test_Functional_Delta;
@@ -49,46 +55,56 @@ class Mustache_Test_Functional_ObjectSectionTest extends PHPUnit_Framework_TestC
}
}
class Mustache_Test_Functional_Alpha {
class Mustache_Test_Functional_Alpha
{
public $foo;
public function __construct() {
public function __construct()
{
$this->foo = new StdClass();
$this->foo->name = 'Foo';
$this->foo->number = 1;
}
}
class Mustache_Test_Functional_Beta {
class Mustache_Test_Functional_Beta
{
protected $_data = array();
public function __construct() {
public function __construct()
{
$this->_data['foo'] = new StdClass();
$this->_data['foo']->name = 'Foo';
$this->_data['foo']->number = 1;
}
public function __isset($name) {
public function __isset($name)
{
return array_key_exists($name, $this->_data);
}
public function __get($name) {
public function __get($name)
{
return $this->_data[$name];
}
}
class Mustache_Test_Functional_Gamma {
class Mustache_Test_Functional_Gamma
{
public $bar;
public function __construct() {
public function __construct()
{
$this->bar = new Mustache_Test_Functional_Beta;
}
}
class Mustache_Test_Functional_Delta {
class Mustache_Test_Functional_Delta
{
protected $_name = 'Foo';
public function name() {
public function name()
{
return $this->_name;
}
}
+14 -7
View File
@@ -9,8 +9,10 @@
* file that was distributed with this source code.
*/
class Mustache_Test_HelperCollectionTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
class Mustache_Test_HelperCollectionTest extends PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$foo = array($this, 'getFoo');
$bar = 'BAR';
@@ -23,11 +25,13 @@ class Mustache_Test_HelperCollectionTest extends PHPUnit_Framework_TestCase {
$this->assertSame($bar, $helpers->get('bar'));
}
public static function getFoo() {
public static function getFoo()
{
echo 'foo';
}
public function testAccessorsAndMutators() {
public function testAccessorsAndMutators()
{
$foo = array($this, 'getFoo');
$bar = 'BAR';
@@ -52,7 +56,8 @@ class Mustache_Test_HelperCollectionTest extends PHPUnit_Framework_TestCase {
$this->assertTrue($helpers->has('bar'));
}
public function testMagicMethods() {
public function testMagicMethods()
{
$foo = array($this, 'getFoo');
$bar = 'BAR';
@@ -88,7 +93,8 @@ class Mustache_Test_HelperCollectionTest extends PHPUnit_Framework_TestCase {
/**
* @dataProvider getInvalidHelperArguments
*/
public function testHelperCollectionIsntAfraidToThrowExceptions($helpers = array(), $actions = array(), $exception = null) {
public function testHelperCollectionIsntAfraidToThrowExceptions($helpers = array(), $actions = array(), $exception = null)
{
if ($exception) {
$this->setExpectedException($exception);
}
@@ -100,7 +106,8 @@ class Mustache_Test_HelperCollectionTest extends PHPUnit_Framework_TestCase {
}
}
public function getInvalidHelperArguments() {
public function getInvalidHelperArguments()
{
return array(
array(
'not helpers',
@@ -12,8 +12,10 @@
/**
* @group unit
*/
class Mustache_Test_Loader_ArrayLoaderTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
class Mustache_Test_Loader_ArrayLoaderTest extends PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$loader = new Mustache_Loader_ArrayLoader(array(
'foo' => 'bar'
));
@@ -21,7 +23,8 @@ class Mustache_Test_Loader_ArrayLoaderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('bar', $loader->load('foo'));
}
public function testSetAndLoadTemplates() {
public function testSetAndLoadTemplates()
{
$loader = new Mustache_Loader_ArrayLoader(array(
'foo' => 'bar'
));
@@ -41,7 +44,8 @@ class Mustache_Test_Loader_ArrayLoaderTest extends PHPUnit_Framework_TestCase {
/**
* @expectedException InvalidArgumentException
*/
public function testMissingTemplatesThrowExceptions() {
public function testMissingTemplatesThrowExceptions()
{
$loader = new Mustache_Loader_ArrayLoader;
$loader->load('not_a_real_template');
}
@@ -12,15 +12,18 @@
/**
* @group unit
*/
class Mustache_Test_Loader_FilesystemLoaderTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
class Mustache_Test_Loader_FilesystemLoaderTest extends PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$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() {
public function testLoadTemplates()
{
$baseDir = realpath(dirname(__FILE__).'/../../../fixtures/templates');
$loader = new Mustache_Loader_FilesystemLoader($baseDir);
$this->assertEquals('one contents', $loader->load('one'));
@@ -30,14 +33,16 @@ class Mustache_Test_Loader_FilesystemLoaderTest extends PHPUnit_Framework_TestCa
/**
* @expectedException RuntimeException
*/
public function testMissingBaseDirThrowsException() {
public function testMissingBaseDirThrowsException()
{
$loader = new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/not_a_directory');
}
/**
* @expectedException InvalidArgumentException
*/
public function testMissingTemplateThrowsException() {
public function testMissingTemplateThrowsException()
{
$baseDir = realpath(dirname(__FILE__).'/../../../fixtures/templates');
$loader = new Mustache_Loader_FilesystemLoader($baseDir);
@@ -12,8 +12,10 @@
/**
* @group unit
*/
class Mustache_Test_Loader_StringLoaderTest extends PHPUnit_Framework_TestCase {
public function testLoadTemplates() {
class Mustache_Test_Loader_StringLoaderTest extends PHPUnit_Framework_TestCase
{
public function testLoadTemplates()
{
$loader = new Mustache_Loader_StringLoader;
$this->assertEquals('foo', $loader->load('foo'));
+6 -3
View File
@@ -12,7 +12,8 @@
/**
* @group unit
*/
class Mustache_Test_ParserTest extends PHPUnit_Framework_TestCase {
class Mustache_Test_ParserTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider getTokenSets
@@ -109,12 +110,14 @@ class Mustache_Test_ParserTest extends PHPUnit_Framework_TestCase {
* @dataProvider getBadParseTrees
* @expectedException LogicException
*/
public function testParserThrowsExceptions($tokens) {
public function testParserThrowsExceptions($tokens)
{
$parser = new Mustache_Parser;
$parser->parse($tokens);
}
public function getBadParseTrees() {
public function getBadParseTrees()
{
return array(
// no close
array(
+12 -6
View File
@@ -12,14 +12,17 @@
/**
* @group unit
*/
class Mustache_Test_TemplateTest extends PHPUnit_Framework_TestCase {
public function testConstructor() {
class Mustache_Test_TemplateTest extends PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$mustache = new Mustache_Engine;
$template = new Mustache_Test_TemplateStub($mustache);
$this->assertSame($mustache, $template->getMustache());
}
public function testRendering() {
public function testRendering()
{
$rendered = '<< wheee >>';
$mustache = new Mustache_Engine;
$template = new Mustache_Test_TemplateStub($mustache);
@@ -36,14 +39,17 @@ class Mustache_Test_TemplateTest extends PHPUnit_Framework_TestCase {
}
}
class Mustache_Test_TemplateStub extends Mustache_Template {
class Mustache_Test_TemplateStub extends Mustache_Template
{
public $rendered;
public function getMustache() {
public function getMustache()
{
return $this->mustache;
}
public function renderInternal(Mustache_Context $context, $indent = '', $escape = false) {
public function renderInternal(Mustache_Context $context, $indent = '', $escape = false)
{
return $this->rendered;
}
}
+6 -3
View File
@@ -12,17 +12,20 @@
/**
* @group unit
*/
class Mustache_Test_TokenizerTest extends PHPUnit_Framework_TestCase {
class Mustache_Test_TokenizerTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider getTokens
*/
public function testScan($text, $delimiters, $expected) {
public function testScan($text, $delimiters, $expected)
{
$tokenizer = new Mustache_Tokenizer;
$this->assertSame($expected, $tokenizer->scan($text, $delimiters));
}
public function getTokens() {
public function getTokens()
{
return array(
array(
'text',
+2 -1
View File
@@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
class Mustache_Bar {
class Mustache_Bar
{
// nada
}
+2 -1
View File
@@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
class Mustache_Foo {
class Mustache_Foo
{
// nada
}
+2 -1
View File
@@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/
class NonMustacheClass {
class NonMustacheClass
{
// noop
}
+2 -1
View File
@@ -1,6 +1,7 @@
<?php
class ChildContext {
class ChildContext
{
public $parent = array(
'child' => 'child works',
);
+4 -2
View File
@@ -1,7 +1,9 @@
<?php
class Comments {
public function title() {
class Comments
{
public function title()
{
return 'A Comedy of Errors';
}
}
+6 -3
View File
@@ -1,6 +1,7 @@
<?php
class Complex {
class Complex
{
public $header = 'Colors';
public $item = array(
@@ -9,11 +10,13 @@ class Complex {
array('name' => 'blue', 'current' => false, 'url' => '#Blue'),
);
public function notEmpty() {
public function notEmpty()
{
return !($this->isEmpty());
}
public function isEmpty() {
public function isEmpty()
{
return count($this->item) === 0;
}
}
+4 -2
View File
@@ -1,9 +1,11 @@
<?php
class Delimiters {
class Delimiters
{
public $start = "It worked the first time.";
public function middle() {
public function middle()
{
return array(
array('item' => "And it worked the second time."),
array('item' => "As well as the third."),
+2 -1
View File
@@ -1,6 +1,7 @@
<?php
class DotNotation {
class DotNotation
{
public $person = array(
'name' => array('first' => 'Chris', 'last' => 'Firescythe'),
'age' => 24,
+4 -2
View File
@@ -1,7 +1,9 @@
<?php
class DoubleSection {
public function t() {
class DoubleSection
{
public function t()
{
return true;
}
+2 -1
View File
@@ -1,5 +1,6 @@
<?php
class Escaped {
class Escaped
{
public $title = '"Bear" > "Shark"';
}
@@ -1,10 +1,12 @@
<?php
class GrandParentContext {
class GrandParentContext
{
public $grand_parent_id = 'grand_parent1';
public $parent_contexts = array();
public function __construct() {
public function __construct()
{
$this->parent_contexts[] = array('parent_id' => 'parent1', 'child_contexts' => array(
array('child_id' => 'parent1-child1'),
array('child_id' => 'parent1-child2')
+4 -2
View File
@@ -1,6 +1,7 @@
<?php
class I18n {
class I18n
{
// Variable to be interpolated
public $name = 'Bob';
@@ -14,7 +15,8 @@ class I18n {
'My name is {{ name }}.' => 'Me llamo {{ name }}.',
);
public static function __trans($text) {
public static function __trans($text)
{
return isset(self::$dictionary[$text]) ? self::$dictionary[$text] : $text;
}
}
@@ -1,5 +1,6 @@
<?php
class ImplicitIterator {
class ImplicitIterator
{
public $data = array('Donkey Kong', 'Luigi', 'Mario', 'Peach', 'Yoshi');
}
@@ -1,6 +1,7 @@
<?php
class InvertedDoubleSection {
class InvertedDoubleSection
{
public $t = false;
public $two = 'second';
}
@@ -1,5 +1,6 @@
<?php
class InvertedSection {
class InvertedSection
{
public $repo = array();
}
@@ -1,6 +1,7 @@
<?php
class RecursivePartials {
class RecursivePartials
{
public $name = 'George';
public $child = array(
'name' => 'Dan',
@@ -1,6 +1,7 @@
<?php
class SectionIteratorObjects {
class SectionIteratorObjects
{
public $start = "It worked the first time.";
protected $_data = array(
@@ -8,7 +9,8 @@ class SectionIteratorObjects {
array('item' => 'As well as the third.'),
);
public function middle() {
public function middle()
{
return new ArrayIterator($this->_data);
}
@@ -1,26 +1,31 @@
<?php
class SectionMagicObjects {
class SectionMagicObjects
{
public $start = "It worked the first time.";
public function middle() {
public function middle()
{
return new MagicObject();
}
public $final = "Then, surprisingly, it worked the final time.";
}
class MagicObject {
class MagicObject
{
protected $_data = array(
'foo' => 'And it worked the second time.',
'bar' => 'As well as the third.'
);
public function __get($key) {
public function __get($key)
{
return isset($this->_data[$key]) ? $this->_data[$key] : NULL;
}
public function __isset($key) {
public function __isset($key)
{
return isset($this->_data[$key]);
}
}
+6 -3
View File
@@ -1,16 +1,19 @@
<?php
class SectionObjects {
class SectionObjects
{
public $start = "It worked the first time.";
public function middle() {
public function middle()
{
return new SectionObject;
}
public $final = "Then, surprisingly, it worked the final time.";
}
class SectionObject {
class SectionObject
{
public $foo = 'And it worked the second time.';
public $bar = 'As well as the third.';
}
+4 -2
View File
@@ -1,9 +1,11 @@
<?php
class Sections {
class Sections
{
public $start = "It worked the first time.";
public function middle() {
public function middle()
{
return array(
array('item' => "And it worked the second time."),
array('item' => "As well as the third."),
+4 -2
View File
@@ -1,9 +1,11 @@
<?php
class SectionsNested {
class SectionsNested
{
public $name = 'Little Mac';
public function enemies() {
public function enemies()
{
return array(
array(
'name' => 'Von Kaiser',
+4 -2
View File
@@ -1,10 +1,12 @@
<?php
class Simple {
class Simple
{
public $name = "Chris";
public $value = 10000;
public function taxed_value() {
public function taxed_value()
{
return $this->value - ($this->value * 0.4);
}
+2 -1
View File
@@ -1,5 +1,6 @@
<?php
class Unescaped {
class Unescaped
{
public $title = "Bear > Shark";
}
+2 -1
View File
@@ -1,5 +1,6 @@
<?php
class UTF8 {
class UTF8
{
public $test = '中文又来啦';
}
+2 -1
View File
@@ -1,5 +1,6 @@
<?php
class UTF8Unescaped {
class UTF8Unescaped
{
public $test = '中文又来啦';
}
+6 -3
View File
@@ -8,16 +8,19 @@
*
* `{{> tag }}` and `{{> tag}}` and `{{>tag}}` should all be equivalent.
*/
class Whitespace {
class Whitespace
{
public $foo = 'alpha';
public $bar = 'beta';
public function baz() {
public function baz()
{
return 'gamma';
}
public function qux() {
public function qux()
{
return array(
array('key with space' => 'A'),
array('key with space' => 'B'),