This commit is contained in:
fzerorubigd 2013-11-08 15:00:49 +03:30
parent 131f15133d
commit d1856a4ed3
No known key found for this signature in database
GPG Key ID: D6EE858AF9D2999A
7 changed files with 94 additions and 87 deletions

View File

@ -43,7 +43,7 @@ class Autoloader
* @param string $baseDir Handlebars library base directory default is * @param string $baseDir Handlebars library base directory default is
* __DIR__.'/..' * __DIR__.'/..'
*/ */
public function __construct($baseDir = null) protected function __construct($baseDir = null)
{ {
if ($baseDir === null) { if ($baseDir === null) {
$this->_baseDir = realpath(__DIR__.'/..'); $this->_baseDir = realpath(__DIR__.'/..');
@ -58,7 +58,7 @@ class Autoloader
* @param string $baseDir Handlebars library base directory, default is * @param string $baseDir Handlebars library base directory, default is
* __DIR__.'/..' * __DIR__.'/..'
* *
* @return Handlebars\Autoloader Registered Autoloader instance * @return \Handlebars\Autoloader Registered Autoloader instance
*/ */
public static function register($baseDir = null) public static function register($baseDir = null)
{ {

View File

@ -40,7 +40,7 @@ interface Cache
* *
* @param string $name Cache id * @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); public function get($name);

View File

@ -41,12 +41,12 @@ class Context
protected $stack = array(); protected $stack = array();
/** /**
* @var index stack for sections * @var array index stack for sections
*/ */
protected $index = array(); protected $index = array();
/** /**
* @var key stack for objects * @var array key stack for objects
*/ */
protected $key = array(); protected $key = array();
@ -180,8 +180,8 @@ class Context
* @param string $variableName variavle name to get from current context * @param string $variableName variavle name to get from current context
* @param boolean $strict strict search? if not found then throw exception * @param boolean $strict strict search? if not found then throw exception
* *
* @throws \InvalidArgumentException in strict mode and variable not found
* @return mixed * @return mixed
* @throws InvalidArgumentException in strict mode and variable not found
*/ */
public function get($variableName, $strict = false) public function get($variableName, $strict = false)
{ {
@ -234,8 +234,8 @@ class Context
* @param string $inside property/method to check * @param string $inside property/method to check
* @param boolean $strict strict search? if not found then throw exception * @param boolean $strict strict search? if not found then throw exception
* *
* @throws \InvalidArgumentException in strict mode and variable not found
* @return boolean true if exist * @return boolean true if exist
* @throws InvalidArgumentException in strict mode and variable not found
*/ */
private function _findVariableInContext($variable, $inside, $strict = false) private function _findVariableInContext($variable, $inside, $strict = false)
{ {

View File

@ -108,6 +108,8 @@ class Handlebars
* cache => Cache object * cache => Cache object
* *
* @param array $options array of options to set * @param array $options array of options to set
*
* @throws \InvalidArgumentException
*/ */
public function __construct(array $options = array()) public function __construct(array $options = array())
{ {
@ -332,6 +334,7 @@ class Handlebars
* *
* @param callable $escape function * @param callable $escape function
* *
* @throws \InvalidArgumentException
* @return void * @return void
*/ */
public function setEscape($escape) public function setEscape($escape)
@ -347,7 +350,7 @@ class Handlebars
/** /**
* Get current escape function * Get current escape function
* *
* @return callable * @return array
*/ */
public function getEscapeArgs() public function getEscapeArgs()
{ {

View File

@ -46,10 +46,10 @@ class Helpers
/** /**
* Create new helper container class * Create new helper container class
* *
* @param array $helpers array of name=>$value helpers * @param array $helpers array of name=>$value helpers
* @param array $defaults add defaults helper (if, unless, each,with) * @param array|bool $defaults add defaults helper (if, unless, each,with)
* *
* @throws InvalidArgumentException when $helpers is not an array * @throws \InvalidArgumentException when $helpers is not an array
* (or traversable) or helper is not a callable * (or traversable) or helper is not a callable
*/ */
public function __construct($helpers = null, $defaults = true) public function __construct($helpers = null, $defaults = true)
@ -58,7 +58,7 @@ class Helpers
$this->addDefaultHelpers(); $this->addDefaultHelpers();
} }
if ($helpers != null) { if ($helpers != null) {
if (!is_array($helpers) && !$helpers instanceof Traversable) { if (!is_array($helpers) && !$helpers instanceof \Traversable) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'HelperCollection constructor expects an array of helpers' 'HelperCollection constructor expects an array of helpers'
); );
@ -75,18 +75,17 @@ class Helpers
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous * Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions. * functions.
* *
* @param Handlebars\Template $template template that is being rendered * @param \Handlebars\Template $template template that is being rendered
* @param Handlebars\Context $context context object * @param \Handlebars\Context $context context object
* @param array $args passed arguments to helper * @param array $args passed arguments to helper
* @param string $source part of template that is wrapped * @param string $source part of template that is wrapped
* within helper * within helper
* *
* @return mixed * @return mixed
*/ */
public static function helperIf($template, $context, $args, $source) public static function helperIf($template, $context, $args, $source)
{ {
$tmp = $context->get($args); $tmp = $context->get($args);
$buffer = '';
if ($tmp) { if ($tmp) {
$template->setStopToken('else'); $template->setStopToken('else');
@ -99,6 +98,7 @@ class Helpers
$template->setStopToken(false); $template->setStopToken(false);
$buffer = $template->render($context); $buffer = $template->render($context);
} }
return $buffer; return $buffer;
} }
@ -108,11 +108,11 @@ class Helpers
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous * Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions. * functions.
* *
* @param Handlebars\Template $template template that is being rendered * @param \Handlebars\Template $template template that is being rendered
* @param Handlebars\Context $context context object * @param \Handlebars\Context $context context object
* @param array $args passed arguments to helper * @param array $args passed arguments to helper
* @param string $source part of template that is wrapped * @param string $source part of template that is wrapped
* within helper * within helper
* *
* @return mixed * @return mixed
*/ */
@ -120,11 +120,11 @@ class Helpers
{ {
$tmp = $context->get($args); $tmp = $context->get($args);
$buffer = ''; $buffer = '';
if (is_array($tmp) || $tmp instanceof Traversable) { if (is_array($tmp) || $tmp instanceof \Traversable) {
$islist = ( array_keys($tmp) == range(0, count($tmp) - 1) ); $islist = (array_keys($tmp) == range(0, count($tmp) - 1));
foreach ($tmp as $key => $var) { foreach ($tmp as $key => $var) {
if ( $islist ) { if ($islist) {
$context->pushIndex($key); $context->pushIndex($key);
} else { } else {
$context->pushKey($key); $context->pushKey($key);
@ -132,13 +132,14 @@ class Helpers
$context->push($var); $context->push($var);
$buffer .= $template->render($context); $buffer .= $template->render($context);
$context->pop(); $context->pop();
if ( $islist ) { if ($islist) {
$context->popIndex(); $context->popIndex();
} else { } else {
$context->popKey(); $context->popKey();
} }
} }
} }
return $buffer; return $buffer;
} }
@ -147,11 +148,11 @@ class Helpers
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous * Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions. * functions.
* *
* @param Handlebars\Template $template template that is being rendered * @param \Handlebars\Template $template template that is being rendered
* @param Handlebars\Context $context context object * @param \Handlebars\Context $context context object
* @param array $args passed arguments to helper * @param array $args passed arguments to helper
* @param string $source part of template that is wrapped * @param string $source part of template that is wrapped
* within helper * within helper
* *
* @return mixed * @return mixed
*/ */
@ -162,6 +163,7 @@ class Helpers
if (!$tmp) { if (!$tmp) {
$buffer = $template->render($context); $buffer = $template->render($context);
} }
return $buffer; return $buffer;
} }
@ -170,11 +172,11 @@ class Helpers
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous * Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions. * functions.
* *
* @param Handlebars\Template $template template that is being rendered * @param \Handlebars\Template $template template that is being rendered
* @param Handlebars\Context $context context object * @param \Handlebars\Context $context context object
* @param array $args passed arguments to helper * @param array $args passed arguments to helper
* @param string $source part of template that is wrapped * @param string $source part of template that is wrapped
* within helper * within helper
* *
* @return mixed * @return mixed
*/ */
@ -184,6 +186,7 @@ class Helpers
$context->push($tmp); $context->push($tmp);
$buffer = $template->render($context); $buffer = $template->render($context);
$context->pop(); $context->pop();
return $buffer; return $buffer;
} }
@ -192,11 +195,11 @@ class Helpers
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous * Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions. * functions.
* *
* @param Handlebars\Template $template template that is being rendered * @param \Handlebars\Template $template template that is being rendered
* @param Handlebars\Context $context context object * @param \Handlebars\Context $context context object
* @param array $args passed arguments to helper * @param array $args passed arguments to helper
* @param string $source part of template that is wrapped * @param string $source part of template that is wrapped
* within helper * within helper
* *
* @return mixed * @return mixed
*/ */
@ -245,10 +248,10 @@ class Helpers
* @param string $name helper name * @param string $name helper name
* @param callable $helper a function as a helper * @param callable $helper a function as a helper
* *
* @throws \InvalidArgumentException if $helper is not a callable
* @return void * @return void
* @throws InvalidArgumentException if $helper is not a callable
*/ */
public function add($name ,$helper) public function add($name, $helper)
{ {
if (!is_callable($helper)) { if (!is_callable($helper)) {
throw new \InvalidArgumentException("$name Helper is not a callable."); throw new \InvalidArgumentException("$name Helper is not a callable.");
@ -273,14 +276,15 @@ class Helpers
* *
* @param string $name helper name * @param string $name helper name
* *
* @throws \InvalidArgumentException if $name is not available
* @return callable helper function * @return callable helper function
* @throws InvalidArgumentException if $name is not available
*/ */
public function __get($name) public function __get($name)
{ {
if (!$this->has($name)) { if (!$this->has($name)) {
throw new \InvalidArgumentException('Unknow helper :' . $name); throw new \InvalidArgumentException('Unknown helper :' . $name);
} }
return $this->helpers[$name]; return $this->helpers[$name];
} }
@ -304,18 +308,17 @@ class Helpers
* @param callable $helper a function as a helper * @param callable $helper a function as a helper
* *
* @return void * @return void
* @throws InvalidArgumentException if $helper is not a callable
*/ */
public function __set($name ,$helper) public function __set($name, $helper)
{ {
$this->add($name, $helpers); $this->add($name, $helper);
} }
/** /**
* Unset a helper * Unset a helper
* *
* @param string $name helpername to remove * @param string $name helper name to remove
* *
* @return void * @return void
*/ */
@ -329,8 +332,8 @@ class Helpers
* *
* @param string $name helper name * @param string $name helper name
* *
* @throws \InvalidArgumentException if the requested helper is not present.
* @return void * @return void
* @throws InvalidArgumentException if the requested helper is not present.
*/ */
public function remove($name) public function remove($name)
{ {

View File

@ -51,12 +51,12 @@ class Parser
/** /**
* Helper method for recursively building a parse tree. * Helper method for recursively building a parse tree.
* *
* @param ArrayIterator $tokens Stream of tokens * @param \ArrayIterator $tokens Stream of tokens
* *
* @throws \LogicException when nesting errors or mismatched section tags
* are encountered.
* @return array Token parse tree * @return array Token parse tree
* *
* @throws LogicException when nesting errors or mismatched section tags
* are encountered.
*/ */
private function _buildTree(\ArrayIterator $tokens) private function _buildTree(\ArrayIterator $tokens)
{ {
@ -70,32 +70,31 @@ class Parser
continue; continue;
} else { } else {
switch ($token[Tokenizer::TYPE]) { switch ($token[Tokenizer::TYPE]) {
case Tokenizer::T_END_SECTION: case Tokenizer::T_END_SECTION:
$newNodes = array (); $newNodes = array();
$continue = true; do {
do { $result = array_pop($stack);
$result = array_pop($stack); if ($result === null) {
if ($result === null) { throw new \LogicException(
throw new \LogicException( 'Unexpected closing tag: /' . $token[Tokenizer::NAME]
'Unexpected closing tag: /'. $token[Tokenizer::NAME] );
); }
}
if (!array_key_exists(Tokenizer::NODES, $result) if (!array_key_exists(Tokenizer::NODES, $result)
&& isset($result[Tokenizer::NAME]) && isset($result[Tokenizer::NAME])
&& $result[Tokenizer::NAME] == $token[Tokenizer::NAME] && $result[Tokenizer::NAME] == $token[Tokenizer::NAME]
) { ) {
$result[Tokenizer::NODES] = $newNodes; $result[Tokenizer::NODES] = $newNodes;
$result[Tokenizer::END] = $token[Tokenizer::INDEX]; $result[Tokenizer::END] = $token[Tokenizer::INDEX];
array_push($stack, $result); array_push($stack, $result);
break 2; break 2;
} else { } else {
array_unshift($newNodes, $result); array_unshift($newNodes, $result);
} }
} while (true); } while (true);
break; break;
default: default:
array_push($stack, $token); array_push($stack, $token);
} }
} }

View File

@ -111,7 +111,7 @@ class Template
/** /**
* get current stop token * get current stop token
* *
* @return string|false * @return string|bool
*/ */
public function getStopToken() public function getStopToken()
@ -119,11 +119,13 @@ class Template
$topStack = end($this->_stack); $topStack = end($this->_stack);
return $topStack[2]; return $topStack[2];
} }
/** /**
* Render top tree * Render top tree
* *
* @param mixed $context current context * @param mixed $context current context
* *
* @throws \RuntimeException
* @return string * @return string
*/ */
public function render($context) public function render($context)
@ -234,6 +236,7 @@ class Template
* @param Context $context current context * @param Context $context current context
* @param array $current section node data * @param array $current section node data
* *
* @throws \RuntimeException
* @return string the result * @return string the result
*/ */
private function _section(Context $context, $current) private function _section(Context $context, $current)
@ -252,7 +255,7 @@ class Template
} }
$params = array( $params = array(
$this, //First argument is this template $this, //First argument is this template
$context, //Secound is current context $context, //Second is current context
$current[Tokenizer::ARGS], //Arguments $current[Tokenizer::ARGS], //Arguments
$source $source
); );
@ -268,13 +271,13 @@ class Template
// no argument at all. // no argument at all.
try { try {
$sectionVar = $context->get($sectionName, true); $sectionVar = $context->get($sectionName, true);
} catch (InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
throw new \RuntimeException( throw new \RuntimeException(
$sectionName . ' is not registered as a helper' $sectionName . ' is not registered as a helper'
); );
} }
$buffer = ''; $buffer = '';
if (is_array($sectionVar) || $sectionVar instanceof Traversable) { if (is_array($sectionVar) || $sectionVar instanceof \Traversable) {
foreach ($sectionVar as $index => $d) { foreach ($sectionVar as $index => $d) {
$context->pushIndex($index); $context->pushIndex($index);
$context->push($d); $context->push($d);
@ -313,7 +316,7 @@ class Template
if (!$data) { if (!$data) {
return $this->render($context); return $this->render($context);
} else { } else {
//No need to disacard here, since itshas no else //No need to discard here, since it has no else
return ''; return '';
} }
} }
@ -326,7 +329,7 @@ class Template
* *
* @return string the result * @return string the result
*/ */
private function _partial($context, $current) private function _partial(Context $context, $current)
{ {
$partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]); $partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]);
@ -346,7 +349,7 @@ class Template
* *
* @return string the result * @return string the result
*/ */
private function _variables($context, $current, $escaped) private function _variables(Context $context, $current, $escaped)
{ {
$name = $current[Tokenizer::NAME]; $name = $current[Tokenizer::NAME];
$value = $context->get($name); $value = $context->get($name);
@ -366,5 +369,4 @@ class Template
} }
return $value; return $value;
} }
} }