Caching variables for the PHP templating engine

This commit is contained in:
Florin Patan 2012-07-30 23:58:40 +03:00
parent a172a81296
commit 22cb8173b5

View File

@ -38,7 +38,7 @@ class PhpEngine implements EngineInterface, \ArrayAccess
protected $charset; protected $charset;
protected $cache; protected $cache;
protected $escapers; protected $escapers;
protected $escaperCache; protected static $escaperCache;
protected $globals; protected $globals;
protected $parser; protected $parser;
@ -335,14 +335,18 @@ class PhpEngine implements EngineInterface, \ArrayAccess
*/ */
public function escape($value, $context = 'html') public function escape($value, $context = 'html')
{ {
if (is_numeric($value)) {
return $value;
}
// If we deal with a scalar value, we can cache the result to increase // If we deal with a scalar value, we can cache the result to increase
// the performance when the same value is escaped multiple times (e.g. loops) // the performance when the same value is escaped multiple times (e.g. loops)
if (is_scalar($value)) { if (is_scalar($value)) {
if (!isset($this->escaperCache[$context][$value])) { if (!isset(self::$escaperCache[$context][$value])) {
$this->escaperCache[$context][$value] = call_user_func($this->getEscaper($context), $value); self::$escaperCache[$context][$value] = call_user_func($this->getEscaper($context), $value);
} }
return $this->escaperCache[$context][$value]; return self::$escaperCache[$context][$value];
} }
return call_user_func($this->getEscaper($context), $value); return call_user_func($this->getEscaper($context), $value);
@ -383,7 +387,7 @@ class PhpEngine implements EngineInterface, \ArrayAccess
public function setEscaper($context, $escaper) public function setEscaper($context, $escaper)
{ {
$this->escapers[$context] = $escaper; $this->escapers[$context] = $escaper;
$this->escaperCache[$context] = array(); self::$escaperCache[$context] = array();
} }
/** /**
@ -502,7 +506,7 @@ class PhpEngine implements EngineInterface, \ArrayAccess
}, },
); );
$this->escaperCache = array(); self::$escaperCache = array();
} }
/** /**