files i made no changes to, but got cs errors on.. not sure why. Just
let me know what the protocol is for this one.
This commit is contained in:
Christian Blanquera 2015-09-21 20:23:20 +08:00
parent 0070ad79c0
commit 08f4328f9d
4 changed files with 94 additions and 31 deletions

View File

@ -40,7 +40,6 @@ namespace Handlebars;
class Context
{
/**
* List of charcters that cannot be used in identifiers.
*/
@ -53,22 +52,30 @@ class Context
const NOT_VALID_SEGMENT_NAME_CHARS = "]";
/**
* Context stack
*
* @var array stack for context only top stack is available
*/
protected $stack = array();
/**
* Section stack index
*
* @var array index stack for sections
*/
protected $index = array();
/**
* Object stack keys
*
* @var array key stack for objects
*/
protected $key = array();
/**
* @var array Special variables stack for sections. Each stack element can
* Special variables stack for sections.
*
* @var array Each stack element can
* contain elements with "@index", "@key", "@first" and "@last" keys.
*/
protected $specialVariables = array();
@ -290,8 +297,18 @@ class Context
$bad_chars = preg_quote(self::NOT_VALID_NAME_CHARS, '/');
$bad_seg_chars = preg_quote(self::NOT_VALID_SEGMENT_NAME_CHARS, '/');
$name_pattern = "(?:[^" . $bad_chars . "\s]+)|(?:\[[^" . $bad_seg_chars . "]+\])";
$check_pattern = "/^((" . $name_pattern . ")\.)*(" . $name_pattern . ")\.?$/";
$name_pattern = "(?:[^"
. $bad_chars
. "\s]+)|(?:\[[^"
. $bad_seg_chars
. "]+\])";
$check_pattern = "/^(("
. $name_pattern
. ")\.)*("
. $name_pattern
. ")\.?$/";
$get_pattern = "/(?:" . $name_pattern . ")/";
if (!preg_match($check_pattern, $variableName)) {

View File

@ -23,7 +23,7 @@ namespace Handlebars;
/**
* Handlebars helpers
*
* a collection of helper function. normally a function like
* A collection of helper function. normally a function like
* function ($sender, $name, $arguments) $arguments is unscaped arguments and
* is a string, not array
*
@ -35,10 +35,11 @@ namespace Handlebars;
* @version Release: @package_version@
* @link http://xamin.ir
*/
class Helpers
{
/**
* Raw helper array
*
* @var array array of helpers
*/
protected $helpers = array();
@ -100,7 +101,8 @@ class Helpers
{
if (!is_callable($helper) && ! $helper instanceof Helper) {
throw new \InvalidArgumentException(
"$name Helper is not a callable or doesn't implement the Helper interface."
"$name Helper is not a callable or ".
"doesn't implement the Helper interface."
);
}
$this->helpers[$name] = $helper;
@ -131,7 +133,13 @@ class Helpers
);
}
return call_user_func($this->helpers[$name], $template, $context, $parsedArgs, $source);
return call_user_func(
$this->helpers[$name],
$template,
$context,
$parsedArgs,
$source
);
}
/**
@ -169,7 +177,7 @@ class Helpers
* @param string $name helper name
*
* @return boolean
* @see Handlebras_Helpers::has
* @see Handlebras_Helpers::has
*/
public function __isset($name)
{

View File

@ -61,7 +61,6 @@ class Parser
* @throws \LogicException when nesting errors or mismatched section tags
* are encountered.
* @return array Token parse tree
*
*/
private function _buildTree(\ArrayIterator $tokens)
{
@ -87,14 +86,20 @@ class Parser
&& isset($result[Tokenizer::NAME])
&& $result[Tokenizer::NAME] == $token[Tokenizer::NAME]
) {
if (isset($result[Tokenizer::TRIM_RIGHT]) && $result[Tokenizer::TRIM_RIGHT]) {
// If the start node has trim right, then its equal with the first item in the loop with
if (isset($result[Tokenizer::TRIM_RIGHT])
&& $result[Tokenizer::TRIM_RIGHT]
) {
// If the start node has trim right, then its equal
//with the first item in the loop with
// Trim left
$newNodes[0][Tokenizer::TRIM_LEFT] = true;
}
if (isset($token[Tokenizer::TRIM_RIGHT]) && $token[Tokenizer::TRIM_RIGHT]) {
//OK, if we have trim right here, we should pass it to the upper level.
if (isset($token[Tokenizer::TRIM_RIGHT])
&& $token[Tokenizer::TRIM_RIGHT]
) {
//OK, if we have trim right here, we should
//pass it to the upper level.
$result[Tokenizer::TRIM_RIGHT] = true;
}
@ -117,4 +122,4 @@ class Parser
return $stack;
}
}
}

View File

@ -41,6 +41,8 @@ namespace Handlebars;
class Template
{
/**
* Handlebars instance
*
* @var Handlebars
*/
protected $handlebars;
@ -51,6 +53,8 @@ class Template
protected $source = '';
/**
* Run stack
*
* @var array Run stack
*/
private $_stack = array();
@ -101,13 +105,12 @@ class Template
}
/**
* set stop token for render and discard method
* Set stop token for render and discard method
*
* @param string $token token to set as stop token or false to remove
*
* @return void
*/
public function setStopToken($token)
{
$topStack = array_pop($this->_stack);
@ -116,11 +119,10 @@ class Template
}
/**
* get current stop token
* Get current stop token
*
* @return string|bool
*/
public function getStopToken()
{
$topStack = end($this->_stack);
@ -156,26 +158,35 @@ class Template
) {
break;
}
if (isset($current[Tokenizer::TRIM_LEFT]) && $current[Tokenizer::TRIM_LEFT]) {
if (isset($current[Tokenizer::TRIM_LEFT])
&& $current[Tokenizer::TRIM_LEFT]
) {
$buffer = rtrim($buffer);
}
$tmp = $this->_renderInternal($current, $context);
if (isset($current[Tokenizer::TRIM_LEFT]) && $current[Tokenizer::TRIM_LEFT]) {
if (isset($current[Tokenizer::TRIM_LEFT])
&& $current[Tokenizer::TRIM_LEFT]
) {
$tmp = rtrim($tmp);
}
if ($rTrim || (isset($current[Tokenizer::TRIM_RIGHT]) && $current[Tokenizer::TRIM_RIGHT])) {
if ($rTrim
|| (isset($current[Tokenizer::TRIM_RIGHT])
&& $current[Tokenizer::TRIM_RIGHT])
) {
$tmp = ltrim($tmp);
}
$buffer .= $tmp;
// Some time, there is more than one string token (first is empty),
// Some time, there is more than
//one string token (first is empty),
//so we need to trim all of them in one shot
$rTrim = (empty($tmp) && $rTrim) ||
isset($current[Tokenizer::TRIM_RIGHT]) && $current[Tokenizer::TRIM_RIGHT];
isset($current[Tokenizer::TRIM_RIGHT])
&& $current[Tokenizer::TRIM_RIGHT];
}
if ($stop) {
//Ok break here, the helper should be aware of this.
@ -312,7 +323,9 @@ class Template
}
// subexpression parsing loop
$subexprs = array(); // will contain all subexpressions inside outermost brackets
// will contain all subexpressions
// inside outermost brackets
$subexprs = array();
$insideOf = array( 'single' => false, 'double' => false );
$lvl = 0;
$cur_start = 0;
@ -334,7 +347,11 @@ class Template
if ($cur == ')' && ! $insideOf['single'] && ! $insideOf['double']) {
$lvl--;
if ($lvl == 0) {
$subexprs[] = substr($current[Tokenizer::ARGS], $cur_start, $i - $cur_start);
$subexprs[] = substr(
$current[Tokenizer::ARGS],
$cur_start,
$i - $cur_start
);
}
}
@ -353,14 +370,30 @@ class Template
Tokenizer::INDEX => $current[Tokenizer::INDEX],
Tokenizer::ARGS => implode(" ", array_slice($cmd, 1))
);
// resolve the node recursively
$resolved = addcslashes($this->_handlebarsStyleSection($context, $section_node), '"');
$resolved = $this->_handlebarsStyleSection(
$context,
$section_node
);
$resolved = addcslashes($resolved, '"');
// replace original subexpression with result
$current[Tokenizer::ARGS] = str_replace('('.$expr.')', '"' . $resolved . '"', $current[Tokenizer::ARGS]);
$current[Tokenizer::ARGS] = str_replace(
'('.$expr.')',
'"' . $resolved . '"',
$current[Tokenizer::ARGS]
);
}
}
$return = $helpers->call($sectionName, $this, $context, $current[Tokenizer::ARGS], $source);
$return = $helpers->call(
$sectionName,
$this,
$context,
$current[Tokenizer::ARGS],
$source
);
if ($return instanceof String) {
return $this->handlebars->loadString($return)->render($context);
@ -507,9 +540,9 @@ class Template
}
/**
* get replacing value of a tag
* Get replacing value of a tag
*
* will process the tag as section, if a helper with the same name could be
* Will process the tag as section, if a helper with the same name could be
* found, so {{helper arg}} can be used instead of {{#helper arg}}.
*
* @param Context $context current context