mirror of
https://github.com/Mibew/handlebars.php.git
synced 2024-11-15 08:44:12 +03:00
Another cleanup
This commit is contained in:
parent
287a8da70c
commit
8938fdc5c3
@ -46,7 +46,7 @@ class Autoloader
|
||||
protected function __construct($baseDir = null)
|
||||
{
|
||||
if ($baseDir === null) {
|
||||
$this->_baseDir = realpath(__DIR__.'/..');
|
||||
$this->_baseDir = realpath(__DIR__ . '/..');
|
||||
} else {
|
||||
$this->_baseDir = rtrim($baseDir, '/');
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ class APC implements Cache
|
||||
*
|
||||
* @param string $name Cache id
|
||||
*
|
||||
* @return data on hit, boolean false on cache not found
|
||||
* @return mixed data on hit, boolean false on cache not found
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
|
@ -44,16 +44,19 @@ class Disk implements Cache
|
||||
* @param string $path Filesystem path to the disk cache location
|
||||
* @param string $prefix optional file prefix, defaults to empty string
|
||||
* @param string $suffix optional file extension, defaults to empty string
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct($path, $prefix = '', $suffix = '')
|
||||
{
|
||||
if (empty($path)) {
|
||||
throw new InvalidArgumentException('Must specify disk cache path');
|
||||
throw new \InvalidArgumentException('Must specify disk cache path');
|
||||
} elseif (!is_dir($path)) {
|
||||
@mkdir($path, 0777, true);
|
||||
|
||||
if (!is_dir($path)) {
|
||||
throw new RuntimeException('Could not create cache file path');
|
||||
throw new \RuntimeException('Could not create cache file path');
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,7 +85,7 @@ class Disk implements Cache
|
||||
*
|
||||
* @param string $name Cache id
|
||||
*
|
||||
* @return data on hit, boolean false on cache not found
|
||||
* @return mixed data on hit, boolean false on cache not found
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
|
@ -40,7 +40,7 @@ class Dummy implements Cache
|
||||
*
|
||||
* @param string $name Cache id
|
||||
*
|
||||
* @return data on hit, boolean false on cache not found
|
||||
* @return mixed data on hit, boolean false on cache not found
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
|
@ -169,6 +169,7 @@ class Context
|
||||
{
|
||||
$value = $this->get($variableName);
|
||||
$this->push($value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
@ -198,6 +199,7 @@ class Context
|
||||
'can not find variable in context'
|
||||
);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
end($this->stack);
|
||||
@ -212,6 +214,7 @@ class Context
|
||||
'can not find variable in context'
|
||||
);
|
||||
}
|
||||
|
||||
return '';
|
||||
} elseif ($variableName == '.' || $variableName == 'this') {
|
||||
return $current;
|
||||
@ -224,6 +227,7 @@ class Context
|
||||
$current = $this->_findVariableInContext($current, $chunk, $strict);
|
||||
}
|
||||
}
|
||||
|
||||
return $current;
|
||||
}
|
||||
|
||||
@ -240,7 +244,7 @@ class Context
|
||||
private function _findVariableInContext($variable, $inside, $strict = false)
|
||||
{
|
||||
$value = '';
|
||||
if ( empty( $inside ) || ( $inside == 'this' ) ) {
|
||||
if (empty($inside) || ($inside == 'this')) {
|
||||
return $variable;
|
||||
} elseif (is_array($variable)) {
|
||||
if (isset($variable[$inside])) {
|
||||
@ -257,6 +261,7 @@ class Context
|
||||
} elseif ($strict) {
|
||||
throw new \InvalidArgumentException('can not find variable in context');
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
@ -42,13 +42,14 @@ class Handlebars
|
||||
*
|
||||
* @param array $options see __construct's options parameter
|
||||
*
|
||||
* @return void
|
||||
* @return Handlebars
|
||||
*/
|
||||
public static function factory ($options=array())
|
||||
public static function factory($options = array())
|
||||
{
|
||||
if (self::$_instance === false) {
|
||||
self::$_instance = new Handlebars($options);
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
@ -90,7 +91,7 @@ class Handlebars
|
||||
/**
|
||||
* @var array parametes to pass to escape function
|
||||
*/
|
||||
private $_escapeArgs = array (
|
||||
private $_escapeArgs = array(
|
||||
ENT_COMPAT,
|
||||
'UTF-8'
|
||||
);
|
||||
@ -193,6 +194,7 @@ class Handlebars
|
||||
if (!isset($this->_helpers)) {
|
||||
$this->_helpers = new Helpers();
|
||||
}
|
||||
|
||||
return $this->_helpers;
|
||||
}
|
||||
|
||||
@ -218,7 +220,7 @@ class Handlebars
|
||||
*/
|
||||
public function getHelper($name)
|
||||
{
|
||||
return $this->getHelpers()->get($name);
|
||||
return $this->getHelpers()->__get($name);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -267,6 +269,7 @@ class Handlebars
|
||||
if (!isset($this->_loader)) {
|
||||
$this->_loader = new StringLoader();
|
||||
}
|
||||
|
||||
return $this->_loader;
|
||||
}
|
||||
|
||||
@ -292,6 +295,7 @@ class Handlebars
|
||||
if (!isset($this->_partialsLoader)) {
|
||||
$this->_partialsLoader = new StringLoader();
|
||||
}
|
||||
|
||||
return $this->_partialsLoader;
|
||||
}
|
||||
|
||||
@ -317,8 +321,10 @@ class Handlebars
|
||||
if (!isset($this->_cache)) {
|
||||
$this->_cache = new Dummy();
|
||||
}
|
||||
|
||||
return $this->_cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current escape function
|
||||
*
|
||||
@ -330,7 +336,7 @@ class Handlebars
|
||||
}
|
||||
|
||||
/**
|
||||
* Set current escpae function
|
||||
* Set current escape function
|
||||
*
|
||||
* @param callable $escape function
|
||||
*
|
||||
@ -358,7 +364,7 @@ class Handlebars
|
||||
}
|
||||
|
||||
/**
|
||||
* Set current escpae function
|
||||
* Set current escape function
|
||||
*
|
||||
* @param array $escapeArgs arguments to pass as extra arg to function
|
||||
*
|
||||
@ -401,6 +407,7 @@ class Handlebars
|
||||
|
||||
return $this->_tokenizer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Handlebars Parser instance.
|
||||
*
|
||||
@ -429,6 +436,7 @@ class Handlebars
|
||||
|
||||
return $this->_parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a template by name with current template loader
|
||||
*
|
||||
@ -440,6 +448,7 @@ class Handlebars
|
||||
{
|
||||
$source = $this->getLoader()->load($name);
|
||||
$tree = $this->_tokenize($source);
|
||||
|
||||
return new Template($this, $tree, $source);
|
||||
}
|
||||
|
||||
@ -457,6 +466,7 @@ class Handlebars
|
||||
}
|
||||
$source = $this->getPartialsLoader()->load($name);
|
||||
$tree = $this->_tokenize($source);
|
||||
|
||||
return new Template($this, $tree, $source);
|
||||
}
|
||||
|
||||
@ -497,6 +507,7 @@ class Handlebars
|
||||
public function loadString($source)
|
||||
{
|
||||
$tree = $this->_tokenize($source);
|
||||
|
||||
return new Template($this, $tree, $source);
|
||||
}
|
||||
|
||||
@ -516,6 +527,7 @@ class Handlebars
|
||||
$tree = $this->getParser()->parse($tokens);
|
||||
$this->getCache()->set($hash, $tree);
|
||||
}
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,6 @@ use Handlebars\String;
|
||||
* @license MIT <http://opensource.org/licenses/MIT>
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir *
|
||||
* @implements Loader
|
||||
*/
|
||||
|
||||
class FilesystemLoader implements Loader
|
||||
@ -54,7 +53,7 @@ class FilesystemLoader implements Loader
|
||||
* @param string|array $baseDirs A path contain template files or array of paths
|
||||
* @param array $options Array of Loader options (default: array())
|
||||
*
|
||||
* @throws RuntimeException if $baseDir does not exist.
|
||||
* @throws \RuntimeException if $baseDir does not exist.
|
||||
*/
|
||||
public function __construct($baseDirs, array $options = array())
|
||||
{
|
||||
@ -101,6 +100,7 @@ class FilesystemLoader implements Loader
|
||||
if (!isset($this->_templates[$name])) {
|
||||
$this->_templates[$name] = $this->loadFile($name);
|
||||
}
|
||||
|
||||
return new String($this->_templates[$name]);
|
||||
}
|
||||
|
||||
@ -109,8 +109,8 @@ class FilesystemLoader implements Loader
|
||||
*
|
||||
* @param string $name template name
|
||||
*
|
||||
* @throws \InvalidArgumentException if a template file is not found.
|
||||
* @return string Handlebars Template source
|
||||
* @throws InvalidArgumentException if a template file is not found.
|
||||
*/
|
||||
protected function loadFile($name)
|
||||
{
|
||||
@ -149,11 +149,11 @@ class FilesystemLoader implements Loader
|
||||
$fileName .= $this->_extension;
|
||||
}
|
||||
if (file_exists($fileName)) {
|
||||
break;
|
||||
return $fileName;
|
||||
}
|
||||
$fileName = false;
|
||||
}
|
||||
return $fileName;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -30,7 +30,6 @@ use Handlebars\String;
|
||||
* @license MIT <http://opensource.org/licenses/MIT>
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir *
|
||||
* @implements Loader
|
||||
*/
|
||||
|
||||
class StringLoader implements Loader
|
||||
|
@ -60,7 +60,7 @@ class Template
|
||||
$this->handlebars = $engine;
|
||||
$this->tree = $tree;
|
||||
$this->source = $source;
|
||||
array_push($this->_stack, array (0, $this->getTree(), false));
|
||||
array_push($this->_stack, array(0, $this->getTree(), false));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -117,6 +117,7 @@ class Template
|
||||
public function getStopToken()
|
||||
{
|
||||
$topStack = end($this->_stack);
|
||||
|
||||
return $topStack[2];
|
||||
}
|
||||
|
||||
@ -134,7 +135,7 @@ class Template
|
||||
$context = new Context($context);
|
||||
}
|
||||
$topTree = end($this->_stack); // never pop a value from stack
|
||||
list($index ,$tree, $stop) = $topTree;
|
||||
list($index, $tree, $stop) = $topTree;
|
||||
|
||||
$buffer = '';
|
||||
while (array_key_exists($index, $tree)) {
|
||||
@ -192,6 +193,7 @@ class Template
|
||||
$newStack[2] = false; //No stop token from now on
|
||||
array_push($this->_stack, $newStack);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
@ -208,7 +210,7 @@ class Template
|
||||
$context = new Context($context);
|
||||
}
|
||||
$topTree = end($this->_stack); //This method never pop a value from stack
|
||||
list($index ,$tree, $stop) = $topTree;
|
||||
list($index, $tree, $stop) = $topTree;
|
||||
while (array_key_exists($index, $tree)) {
|
||||
$current = $tree[$index];
|
||||
$index++;
|
||||
@ -227,6 +229,7 @@ class Template
|
||||
$newStack[2] = false;
|
||||
array_push($this->_stack, $newStack);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
@ -254,11 +257,11 @@ class Template
|
||||
$source = '';
|
||||
}
|
||||
$params = array(
|
||||
$this, //First argument is this template
|
||||
$this, //First argument is this template
|
||||
$context, //Second is current context
|
||||
$current[Tokenizer::ARGS], //Arguments
|
||||
$current[Tokenizer::ARGS], //Arguments
|
||||
$source
|
||||
);
|
||||
);
|
||||
|
||||
$return = call_user_func_array($helpers->$sectionName, $params);
|
||||
if ($return instanceof String) {
|
||||
@ -293,6 +296,7 @@ class Template
|
||||
} elseif ($sectionVar) {
|
||||
$buffer = $this->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
} else {
|
||||
throw new \RuntimeException(
|
||||
@ -333,7 +337,7 @@ class Template
|
||||
{
|
||||
$partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]);
|
||||
|
||||
if ( $current[Tokenizer::ARGS] ) {
|
||||
if ($current[Tokenizer::ARGS]) {
|
||||
$context = $context->get($current[Tokenizer::ARGS]);
|
||||
}
|
||||
|
||||
@ -353,10 +357,10 @@ class Template
|
||||
{
|
||||
$name = $current[Tokenizer::NAME];
|
||||
$value = $context->get($name);
|
||||
if ( $name == '@index' ) {
|
||||
if ($name == '@index') {
|
||||
return $context->lastIndex();
|
||||
}
|
||||
if ( $name == '@key' ) {
|
||||
if ($name == '@key') {
|
||||
return $context->lastKey();
|
||||
}
|
||||
if ($escaped) {
|
||||
@ -367,6 +371,7 @@ class Template
|
||||
array_values($args)
|
||||
);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
@ -38,56 +38,56 @@ class Tokenizer
|
||||
{
|
||||
|
||||
// Finite state machine states
|
||||
const IN_TEXT = 0;
|
||||
const IN_TEXT = 0;
|
||||
const IN_TAG_TYPE = 1;
|
||||
const IN_TAG = 2;
|
||||
const IN_TAG = 2;
|
||||
|
||||
// Token types
|
||||
const T_SECTION = '#';
|
||||
const T_INVERTED = '^';
|
||||
const T_END_SECTION = '/';
|
||||
const T_COMMENT = '!';
|
||||
const T_SECTION = '#';
|
||||
const T_INVERTED = '^';
|
||||
const T_END_SECTION = '/';
|
||||
const T_COMMENT = '!';
|
||||
// XXX: remove partials support from tokenizer and make it a helper?
|
||||
const T_PARTIAL = '>';
|
||||
const T_PARTIAL_2 = '<';
|
||||
const T_PARTIAL = '>';
|
||||
const T_PARTIAL_2 = '<';
|
||||
const T_DELIM_CHANGE = '=';
|
||||
const T_ESCAPED = '_v';
|
||||
const T_UNESCAPED = '{';
|
||||
const T_UNESCAPED_2 = '&';
|
||||
const T_TEXT = '_t';
|
||||
const T_ESCAPED = '_v';
|
||||
const T_UNESCAPED = '{';
|
||||
const T_UNESCAPED_2 = '&';
|
||||
const T_TEXT = '_t';
|
||||
|
||||
// Valid token types
|
||||
private static $_tagTypes = array(
|
||||
self::T_SECTION => true,
|
||||
self::T_INVERTED => true,
|
||||
self::T_END_SECTION => true,
|
||||
self::T_COMMENT => true,
|
||||
self::T_PARTIAL => true,
|
||||
self::T_PARTIAL_2 => true,
|
||||
self::T_SECTION => true,
|
||||
self::T_INVERTED => true,
|
||||
self::T_END_SECTION => true,
|
||||
self::T_COMMENT => true,
|
||||
self::T_PARTIAL => true,
|
||||
self::T_PARTIAL_2 => true,
|
||||
self::T_DELIM_CHANGE => true,
|
||||
self::T_ESCAPED => true,
|
||||
self::T_UNESCAPED => true,
|
||||
self::T_UNESCAPED_2 => true,
|
||||
self::T_ESCAPED => true,
|
||||
self::T_UNESCAPED => true,
|
||||
self::T_UNESCAPED_2 => true,
|
||||
);
|
||||
|
||||
// Interpolated tags
|
||||
private static $_interpolatedTags = array(
|
||||
self::T_ESCAPED => true,
|
||||
self::T_UNESCAPED => true,
|
||||
self::T_UNESCAPED_2 => true,
|
||||
self::T_ESCAPED => true,
|
||||
self::T_UNESCAPED => true,
|
||||
self::T_UNESCAPED_2 => true,
|
||||
);
|
||||
|
||||
// Token properties
|
||||
const TYPE = 'type';
|
||||
const NAME = 'name';
|
||||
const OTAG = 'otag';
|
||||
const CTAG = 'ctag';
|
||||
const INDEX = 'index';
|
||||
const END = 'end';
|
||||
const TYPE = 'type';
|
||||
const NAME = 'name';
|
||||
const OTAG = 'otag';
|
||||
const CTAG = 'ctag';
|
||||
const INDEX = 'index';
|
||||
const END = 'end';
|
||||
const INDENT = 'indent';
|
||||
const NODES = 'nodes';
|
||||
const VALUE = 'value';
|
||||
const ARGS = 'args';
|
||||
const NODES = 'nodes';
|
||||
const VALUE = 'value';
|
||||
const ARGS = 'args';
|
||||
|
||||
protected $state;
|
||||
protected $tagType;
|
||||
@ -164,7 +164,7 @@ class Tokenizer
|
||||
if ($this->tagChange($this->ctag, $text, $i)) {
|
||||
// Sections (Helpers) can accept parameters
|
||||
// Same thing for Partials (little known fact)
|
||||
if ( ($this->tagType == self::T_SECTION)
|
||||
if (($this->tagType == self::T_SECTION)
|
||||
|| ($this->tagType == self::T_PARTIAL)
|
||||
|| ($this->tagType == self::T_PARTIAL_2)
|
||||
) {
|
||||
@ -176,14 +176,14 @@ class Tokenizer
|
||||
$this->buffer = $newBuffer[0];
|
||||
}
|
||||
$t = array(
|
||||
self::TYPE => $this->tagType,
|
||||
self::NAME => trim($this->buffer),
|
||||
self::OTAG => $this->otag,
|
||||
self::CTAG => $this->ctag,
|
||||
self::TYPE => $this->tagType,
|
||||
self::NAME => trim($this->buffer),
|
||||
self::OTAG => $this->otag,
|
||||
self::CTAG => $this->ctag,
|
||||
self::INDEX => ($this->tagType == self::T_END_SECTION) ?
|
||||
$this->seenTag - strlen($this->otag) :
|
||||
$i + strlen($this->ctag),
|
||||
);
|
||||
$this->seenTag - strlen($this->otag) :
|
||||
$i + strlen($this->ctag),
|
||||
);
|
||||
if (isset($args)) {
|
||||
$t[self::ARGS] = $args;
|
||||
}
|
||||
@ -226,15 +226,15 @@ class Tokenizer
|
||||
*/
|
||||
protected function reset()
|
||||
{
|
||||
$this->state = self::IN_TEXT;
|
||||
$this->tagType = null;
|
||||
$this->tag = null;
|
||||
$this->buffer = '';
|
||||
$this->tokens = array();
|
||||
$this->seenTag = false;
|
||||
$this->state = self::IN_TEXT;
|
||||
$this->tagType = null;
|
||||
$this->tag = null;
|
||||
$this->buffer = '';
|
||||
$this->tokens = array();
|
||||
$this->seenTag = false;
|
||||
$this->lineStart = 0;
|
||||
$this->otag = '{{';
|
||||
$this->ctag = '}}';
|
||||
$this->otag = '{{';
|
||||
$this->ctag = '}}';
|
||||
}
|
||||
|
||||
/**
|
||||
@ -246,10 +246,10 @@ class Tokenizer
|
||||
{
|
||||
if (!empty($this->buffer)) {
|
||||
$this->tokens[] = array(
|
||||
self::TYPE => self::T_TEXT,
|
||||
self::TYPE => self::T_TEXT,
|
||||
self::VALUE => $this->buffer
|
||||
);
|
||||
$this->buffer = '';
|
||||
$this->buffer = '';
|
||||
}
|
||||
}
|
||||
|
||||
@ -305,7 +305,7 @@ class Tokenizer
|
||||
$this->tokens[] = array(self::TYPE => self::T_TEXT, self::VALUE => "\n");
|
||||
}
|
||||
|
||||
$this->seenTag = false;
|
||||
$this->seenTag = false;
|
||||
$this->lineStart = count($this->tokens);
|
||||
}
|
||||
|
||||
@ -320,7 +320,7 @@ class Tokenizer
|
||||
protected function changeDelimiters($text, $index)
|
||||
{
|
||||
$startIndex = strpos($text, '=', $index) + 1;
|
||||
$close = '='.$this->ctag;
|
||||
$close = '=' . $this->ctag;
|
||||
$closeIndex = strpos($text, $close, $index);
|
||||
|
||||
list($otag, $ctag) = explode(
|
||||
|
Loading…
Reference in New Issue
Block a user