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

View File

@ -23,7 +23,7 @@ namespace Handlebars;
/** /**
* Handlebars helpers * 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 * function ($sender, $name, $arguments) $arguments is unscaped arguments and
* is a string, not array * is a string, not array
* *
@ -35,10 +35,11 @@ namespace Handlebars;
* @version Release: @package_version@ * @version Release: @package_version@
* @link http://xamin.ir * @link http://xamin.ir
*/ */
class Helpers class Helpers
{ {
/** /**
* Raw helper array
*
* @var array array of helpers * @var array array of helpers
*/ */
protected $helpers = array(); protected $helpers = array();
@ -100,7 +101,8 @@ class Helpers
{ {
if (!is_callable($helper) && ! $helper instanceof Helper) { if (!is_callable($helper) && ! $helper instanceof Helper) {
throw new \InvalidArgumentException( 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; $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 * @param string $name helper name
* *
* @return boolean * @return boolean
* @see Handlebras_Helpers::has * @see Handlebras_Helpers::has
*/ */
public function __isset($name) public function __isset($name)
{ {

View File

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

View File

@ -41,6 +41,8 @@ namespace Handlebars;
class Template class Template
{ {
/** /**
* Handlebars instance
*
* @var Handlebars * @var Handlebars
*/ */
protected $handlebars; protected $handlebars;
@ -51,6 +53,8 @@ class Template
protected $source = ''; protected $source = '';
/** /**
* Run stack
*
* @var array Run stack * @var array Run stack
*/ */
private $_stack = array(); 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 * @param string $token token to set as stop token or false to remove
* *
* @return void * @return void
*/ */
public function setStopToken($token) public function setStopToken($token)
{ {
$topStack = array_pop($this->_stack); $topStack = array_pop($this->_stack);
@ -116,11 +119,10 @@ class Template
} }
/** /**
* get current stop token * Get current stop token
* *
* @return string|bool * @return string|bool
*/ */
public function getStopToken() public function getStopToken()
{ {
$topStack = end($this->_stack); $topStack = end($this->_stack);
@ -156,26 +158,35 @@ class Template
) { ) {
break; break;
} }
if (isset($current[Tokenizer::TRIM_LEFT]) && $current[Tokenizer::TRIM_LEFT]) { if (isset($current[Tokenizer::TRIM_LEFT])
&& $current[Tokenizer::TRIM_LEFT]
) {
$buffer = rtrim($buffer); $buffer = rtrim($buffer);
} }
$tmp = $this->_renderInternal($current, $context); $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); $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); $tmp = ltrim($tmp);
} }
$buffer .= $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 //so we need to trim all of them in one shot
$rTrim = (empty($tmp) && $rTrim) || $rTrim = (empty($tmp) && $rTrim) ||
isset($current[Tokenizer::TRIM_RIGHT]) && $current[Tokenizer::TRIM_RIGHT]; isset($current[Tokenizer::TRIM_RIGHT])
&& $current[Tokenizer::TRIM_RIGHT];
} }
if ($stop) { if ($stop) {
//Ok break here, the helper should be aware of this. //Ok break here, the helper should be aware of this.
@ -312,7 +323,9 @@ class Template
} }
// subexpression parsing loop // 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 ); $insideOf = array( 'single' => false, 'double' => false );
$lvl = 0; $lvl = 0;
$cur_start = 0; $cur_start = 0;
@ -334,7 +347,11 @@ class Template
if ($cur == ')' && ! $insideOf['single'] && ! $insideOf['double']) { if ($cur == ')' && ! $insideOf['single'] && ! $insideOf['double']) {
$lvl--; $lvl--;
if ($lvl == 0) { 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::INDEX => $current[Tokenizer::INDEX],
Tokenizer::ARGS => implode(" ", array_slice($cmd, 1)) Tokenizer::ARGS => implode(" ", array_slice($cmd, 1))
); );
// resolve the node recursively // 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 // 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) { if ($return instanceof String) {
return $this->handlebars->loadString($return)->render($context); 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}}. * found, so {{helper arg}} can be used instead of {{#helper arg}}.
* *
* @param Context $context current context * @param Context $context current context