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

View File

@ -40,7 +40,7 @@ interface 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);

View File

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

View File

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

View File

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

View File

@ -51,12 +51,12 @@ class Parser
/**
* 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
*
* @throws LogicException when nesting errors or mismatched section tags
* are encountered.
*/
private function _buildTree(\ArrayIterator $tokens)
{
@ -72,7 +72,6 @@ class Parser
switch ($token[Tokenizer::TYPE]) {
case Tokenizer::T_END_SECTION:
$newNodes = array();
$continue = true;
do {
$result = array_pop($stack);
if ($result === null) {

View File

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