Merge pull request #223 from bobthecow/feature/faster-type-checking

Faster context lookups
This commit is contained in:
Justin Hileman
2014-08-18 17:08:40 -07:00
3 changed files with 48 additions and 30 deletions
+8 -6
View File
@@ -609,9 +609,9 @@ class Mustache_Compiler
{
if ($this->customEscape) {
return sprintf(self::CUSTOM_ESCAPE, $value);
} else {
return sprintf(self::DEFAULT_ESCAPE, $value, var_export($this->entityFlags, true), var_export($this->charset, true));
}
return sprintf(self::DEFAULT_ESCAPE, $value, var_export($this->entityFlags, true), var_export($this->charset, true));
}
/**
@@ -631,11 +631,13 @@ class Mustache_Compiler
{
if ($id === '.') {
return 'last';
} elseif (strpos($id, '.') === false) {
return 'find';
} else {
return 'findDot';
}
if (strpos($id, '.') === false) {
return 'find';
}
return 'findDot';
}
const IS_CALLABLE = '!is_string(%s) && is_callable(%s)';
+25 -12
View File
@@ -172,19 +172,32 @@ class Mustache_Context
private function findVariableInStack($id, array $stack)
{
for ($i = count($stack) - 1; $i >= 0; $i--) {
if (is_object($stack[$i]) && !($stack[$i] instanceof Closure)) {
$frame = &$stack[$i];
// Note that is_callable() *will not work here*
// See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods
if (method_exists($stack[$i], $id)) {
return $stack[$i]->$id();
} elseif (isset($stack[$i]->$id)) {
return $stack[$i]->$id;
} elseif ($stack[$i] instanceof ArrayAccess && isset($stack[$i][$id])) {
return $stack[$i][$id];
}
} elseif (is_array($stack[$i]) && array_key_exists($id, $stack[$i])) {
return $stack[$i][$id];
switch (gettype($frame)) {
case 'object':
if (!($frame instanceof Closure)) {
// Note that is_callable() *will not work here*
// See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods
if (method_exists($frame, $id)) {
return $frame->$id();
}
if (isset($frame->$id)) {
return $frame->$id;
}
if ($frame instanceof ArrayAccess && isset($frame[$id])) {
return $frame[$id];
}
}
break;
case 'array':
if (array_key_exists($id, $frame)) {
return $frame[$id];
}
break;
}
}
+15 -12
View File
@@ -113,19 +113,22 @@ abstract class Mustache_Template
*/
protected function isIterable($value)
{
if (is_object($value)) {
return $value instanceof Traversable;
} elseif (is_array($value)) {
$i = 0;
foreach ($value as $k => $v) {
if ($k !== $i++) {
return false;
}
}
switch (gettype($value)) {
case 'object':
return $value instanceof Traversable;
return true;
} else {
return false;
case 'array':
$i = 0;
foreach ($value as $k => $v) {
if ($k !== $i++) {
return false;
}
}
return true;
default:
return false;
}
}