This commit is contained in:
Pascal Thormeier 2015-06-17 16:11:50 +02:00
parent 0111689f47
commit e1956be453
7 changed files with 102 additions and 69 deletions

View File

@ -142,10 +142,12 @@ class Arguments
// Remove found argument from arguments string. // Remove found argument from arguments string.
$current_str = ltrim(substr($current_str, strlen($matches[0]))); $current_str = ltrim(substr($current_str, strlen($matches[0])));
} else { } else {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'Malformed arguments string: "%s"', sprintf(
var_export($args_String, true) 'Malformed arguments string: "%s"',
)); var_export($args_string, true)
)
);
} }
} }
} }

View File

@ -196,10 +196,12 @@ class Context
} }
if (count($this->stack) < $level) { if (count($this->stack) < $level) {
if ($strict) { if ($strict) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'Can not find variable in context: "%s"', sprintf(
var_export($variableName, true) 'Can not find variable in context: "%s"',
)); var_export($variableName, true)
)
);
} }
return ''; return '';
@ -216,10 +218,12 @@ class Context
$current = current($this->stack); $current = current($this->stack);
if (!$variableName) { if (!$variableName) {
if ($strict) { if ($strict) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'Can not find variable in context: "%s"', sprintf(
var_export($variableName, true) 'Can not find variable in context: "%s"',
)); var_export($variableName, true)
)
);
} }
return ''; return '';
@ -230,10 +234,12 @@ class Context
if (isset($specialVariables[$variableName])) { if (isset($specialVariables[$variableName])) {
return $specialVariables[$variableName]; return $specialVariables[$variableName];
} elseif ($strict) { } elseif ($strict) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'Can not find variable in context: "%s"', sprintf(
var_export($variableName, true) 'Can not find variable in context: "%s"',
)); var_export($variableName, true)
)
);
} else { } else {
return ''; return '';
} }
@ -279,10 +285,12 @@ class Context
} }
if ($strict) { if ($strict) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'Can not find variable in context: "%s"', sprintf(
var_export($variable, true) 'Can not find variable in context: "%s"',
)); var_export($variable, true)
)
);
} }
return $value; return $value;
@ -306,10 +314,12 @@ class Context
$get_pattern = "/(?:" . $name_pattern . ")/"; $get_pattern = "/(?:" . $name_pattern . ")/";
if (!preg_match($check_pattern, $variableName)) { if (!preg_match($check_pattern, $variableName)) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'Variable name is invalid: "%s"', sprintf(
$variableName 'Variable name is invalid: "%s"',
)); $variableName
)
);
} }
preg_match_all($get_pattern, $variableName, $matches); preg_match_all($get_pattern, $variableName, $matches);

View File

@ -459,10 +459,12 @@ class Handlebars
public function setTemplateClass($class) public function setTemplateClass($class)
{ {
if (!is_a($class, 'Handlebars\\Template', true)) { if (!is_a($class, 'Handlebars\\Template', true)) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'Custom template class "%s" must extend Template', sprintf(
var_export($class, true) 'Custom template class "%s" must extend Template',
)); var_export($class, true)
)
);
} }
$this->_templateClass = $class; $this->_templateClass = $class;

View File

@ -99,10 +99,12 @@ class Helpers
public function add($name, $helper) public function add($name, $helper)
{ {
if (!is_callable($helper) && ! $helper instanceof Helper) { if (!is_callable($helper) && ! $helper instanceof Helper) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
"%s Helper is not a callable or doesn't implement the Helper interface.", sprintf(
var_export($name, true) "%s Helper is not a callable or doesn't implement the Helper interface.",
)); var_export($name, true)
)
);
} }
$this->helpers[$name] = $helper; $this->helpers[$name] = $helper;
} }
@ -137,10 +139,12 @@ class Helpers
public function call($name, Template $template, Context $context, $args, $source) public function call($name, Template $template, Context $context, $args, $source)
{ {
if (!$this->has($name)) { if (!$this->has($name)) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'Unknown helper: "%s"', sprintf(
var_export($name, true) 'Unknown helper: "%s"',
)); var_export($name, true)
)
);
} }
if ($this->helpers[$name] instanceof Helper) { if ($this->helpers[$name] instanceof Helper) {
@ -175,10 +179,12 @@ class Helpers
public function __get($name) public function __get($name)
{ {
if (!$this->has($name)) { if (!$this->has($name)) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'Unknown helper: "%s"', sprintf(
var_export($name, true) 'Unknown helper: "%s"',
)); var_export($name, true)
)
);
} }
return $this->helpers[$name]; return $this->helpers[$name];
@ -233,10 +239,12 @@ class Helpers
public function remove($name) public function remove($name)
{ {
if (!$this->has($name)) { if (!$this->has($name)) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'Unknown helper: "%s"', sprintf(
var_export($name, true) 'Unknown helper: "%s"',
)); var_export($name, true)
)
);
} }
unset($this->helpers[$name]); unset($this->helpers[$name]);

View File

@ -75,17 +75,21 @@ class InlineLoader implements Loader
public function __construct($fileName, $offset) public function __construct($fileName, $offset)
{ {
if (!is_file($fileName)) { if (!is_file($fileName)) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'InlineLoader expects a valid filename, "%s" given.', sprintf(
$fileName 'InlineLoader expects a valid filename, "%s" given.',
)); $fileName
)
);
} }
if (!is_int($offset) || $offset < 0) { if (!is_int($offset) || $offset < 0) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(
'InlineLoader expects a valid file offset, "%s" given.', sprintf(
$offset 'InlineLoader expects a valid file offset, "%s" given.',
)); $offset
)
);
} }
$this->fileName = $fileName; $this->fileName = $fileName;

View File

@ -78,10 +78,12 @@ class Parser
do { do {
$result = array_pop($stack); $result = array_pop($stack);
if ($result === null) { if ($result === null) {
throw new \LogicException(sprintf( throw new \LogicException(
'Unexpected closing tag: /%s', sprintf(
var_export($token[Tokenizer::NAME], true) 'Unexpected closing tag: /%s',
)); var_export($token[Tokenizer::NAME], true)
)
);
} }
if (!array_key_exists(Tokenizer::NODES, $result) if (!array_key_exists(Tokenizer::NODES, $result)

View File

@ -30,7 +30,8 @@ namespace Handlebars;
* *
* @category Xamin * @category Xamin
* @package Handlebars * @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>, Pascal Thormeier <pascal.thormeier@gmail.com> * @author fzerorubigd <fzerorubigd@gmail.com>
* @author Pascal Thormeier <pascal.thormeier@gmail.com>
* @copyright 2010-2012 (c) Justin Hileman * @copyright 2010-2012 (c) Justin Hileman
* @copyright 2012 (c) ParsPooyesh Co * @copyright 2012 (c) ParsPooyesh Co
* @license MIT <http://opensource.org/licenses/MIT> * @license MIT <http://opensource.org/licenses/MIT>
@ -404,10 +405,12 @@ class Template
try { try {
$sectionVar = $context->get($sectionName, true); $sectionVar = $context->get($sectionName, true);
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
throw new \RuntimeException(sprintf( throw new \RuntimeException(
'"%s" is not registered as a helper', sprintf(
var_export($sectionName, true) '"%s" is not registered as a helper',
)); var_export($sectionName, true)
)
);
} }
$buffer = ''; $buffer = '';
if (is_array($sectionVar) || $sectionVar instanceof \Traversable) { if (is_array($sectionVar) || $sectionVar instanceof \Traversable) {
@ -462,10 +465,12 @@ class Template
} elseif (trim($current[Tokenizer::ARGS]) == '') { } elseif (trim($current[Tokenizer::ARGS]) == '') {
return $this->_mustacheStyleSection($context, $current); return $this->_mustacheStyleSection($context, $current);
} else { } else {
throw new \RuntimeException(sprintf( throw new \RuntimeException(
'"%s"" is not registered as a helper', sprintf(
var_export($sectionName, true) '"%s"" is not registered as a helper',
)); var_export($sectionName, true)
)
);
} }
} }
@ -508,7 +513,7 @@ class Template
$args $args
); );
$context = new Context($this->_getPartialArguments($context, $args[0])); $context = new Context($this->_getPartialArgumentsValues($context, $args[0]));
} }
return $partial->render($context); return $partial->render($context);
@ -517,12 +522,12 @@ class Template
/** /**
* Prepare the arguments of a partial to actual array values to be used in a new context * Prepare the arguments of a partial to actual array values to be used in a new context
* *
* @param Context $context * @param Context $context Current context
* @param array $args * @param array $args Arguments given by tokenizer, splitted to key=>value pairs
* *
* @return array * @return array
*/ */
private function _getPartialArguments(Context $context, array $args) private function _getPartialArgumentsValues(Context $context, array $args)
{ {
$partialArgs = array(); $partialArgs = array();