Merge pull request #223 from bobthecow/feature/faster-type-checking
Faster context lookups
This commit is contained in:
@@ -609,9 +609,9 @@ class Mustache_Compiler
|
|||||||
{
|
{
|
||||||
if ($this->customEscape) {
|
if ($this->customEscape) {
|
||||||
return sprintf(self::CUSTOM_ESCAPE, $value);
|
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 === '.') {
|
if ($id === '.') {
|
||||||
return 'last';
|
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)';
|
const IS_CALLABLE = '!is_string(%s) && is_callable(%s)';
|
||||||
|
|||||||
@@ -172,19 +172,32 @@ class Mustache_Context
|
|||||||
private function findVariableInStack($id, array $stack)
|
private function findVariableInStack($id, array $stack)
|
||||||
{
|
{
|
||||||
for ($i = count($stack) - 1; $i >= 0; $i--) {
|
for ($i = count($stack) - 1; $i >= 0; $i--) {
|
||||||
if (is_object($stack[$i]) && !($stack[$i] instanceof Closure)) {
|
$frame = &$stack[$i];
|
||||||
|
|
||||||
|
switch (gettype($frame)) {
|
||||||
|
case 'object':
|
||||||
|
if (!($frame instanceof Closure)) {
|
||||||
// Note that is_callable() *will not work here*
|
// Note that is_callable() *will not work here*
|
||||||
// See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods
|
// See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods
|
||||||
if (method_exists($stack[$i], $id)) {
|
if (method_exists($frame, $id)) {
|
||||||
return $stack[$i]->$id();
|
return $frame->$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];
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -113,9 +113,11 @@ abstract class Mustache_Template
|
|||||||
*/
|
*/
|
||||||
protected function isIterable($value)
|
protected function isIterable($value)
|
||||||
{
|
{
|
||||||
if (is_object($value)) {
|
switch (gettype($value)) {
|
||||||
|
case 'object':
|
||||||
return $value instanceof Traversable;
|
return $value instanceof Traversable;
|
||||||
} elseif (is_array($value)) {
|
|
||||||
|
case 'array':
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($value as $k => $v) {
|
foreach ($value as $k => $v) {
|
||||||
if ($k !== $i++) {
|
if ($k !== $i++) {
|
||||||
@@ -124,7 +126,8 @@ abstract class Mustache_Template
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user