Fix and add tests

Also use var_export in exceptions in case array/object/etc get passed
This commit is contained in:
Pascal Thormeier 2015-06-17 15:50:10 +02:00
parent a4b6f9627a
commit 0111689f47
7 changed files with 58 additions and 24 deletions

View File

@ -144,7 +144,7 @@ class Arguments
} else { } else {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
'Malformed arguments string: "%s"', 'Malformed arguments string: "%s"',
$args_string var_export($args_String, true)
)); ));
} }
} }

View File

@ -198,7 +198,7 @@ class Context
if ($strict) { if ($strict) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
'Can not find variable in context: "%s"', 'Can not find variable in context: "%s"',
$variableName var_export($variableName, true)
)); ));
} }
@ -218,7 +218,7 @@ class Context
if ($strict) { if ($strict) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
'Can not find variable in context: "%s"', 'Can not find variable in context: "%s"',
$variableName var_export($variableName, true)
)); ));
} }
@ -232,7 +232,7 @@ class Context
} elseif ($strict) { } elseif ($strict) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
'Can not find variable in context: "%s"', 'Can not find variable in context: "%s"',
$variableName var_export($variableName, true)
)); ));
} else { } else {
return ''; return '';
@ -281,7 +281,7 @@ class Context
if ($strict) { if ($strict) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
'Can not find variable in context: "%s"', 'Can not find variable in context: "%s"',
$variable var_export($variable, true)
)); ));
} }

View File

@ -459,10 +459,10 @@ 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( throw new \InvalidArgumentException(sprintf(
'Custom template class "%s" must extend Template', 'Custom template class "%s" must extend Template',
$class var_export($class, true)
); ));
} }
$this->_templateClass = $class; $this->_templateClass = $class;

View File

@ -101,7 +101,7 @@ class Helpers
if (!is_callable($helper) && ! $helper instanceof Helper) { if (!is_callable($helper) && ! $helper instanceof Helper) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
"%s Helper is not a callable or doesn't implement the Helper interface.", "%s Helper is not a callable or doesn't implement the Helper interface.",
$name var_export($name, true)
)); ));
} }
$this->helpers[$name] = $helper; $this->helpers[$name] = $helper;
@ -139,7 +139,7 @@ class Helpers
if (!$this->has($name)) { if (!$this->has($name)) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
'Unknown helper: "%s"', 'Unknown helper: "%s"',
$name var_export($name, true)
)); ));
} }
@ -177,7 +177,7 @@ class Helpers
if (!$this->has($name)) { if (!$this->has($name)) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
'Unknown helper: "%s"', 'Unknown helper: "%s"',
$name var_export($name, true)
)); ));
} }
@ -235,7 +235,7 @@ class Helpers
if (!$this->has($name)) { if (!$this->has($name)) {
throw new \InvalidArgumentException(sprintf( throw new \InvalidArgumentException(sprintf(
'Unknown helper: "%s"', 'Unknown helper: "%s"',
$name var_export($name, true)
)); ));
} }

View File

@ -80,7 +80,7 @@ class Parser
if ($result === null) { if ($result === null) {
throw new \LogicException(sprintf( throw new \LogicException(sprintf(
'Unexpected closing tag: /%s', 'Unexpected closing tag: /%s',
$token[Tokenizer::NAME] var_export($token[Tokenizer::NAME], true)
)); ));
} }

View File

@ -406,7 +406,7 @@ class Template
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
throw new \RuntimeException(sprintf( throw new \RuntimeException(sprintf(
'"%s" is not registered as a helper', '"%s" is not registered as a helper',
$sectionName var_export($sectionName, true)
)); ));
} }
$buffer = ''; $buffer = '';
@ -464,7 +464,7 @@ class Template
} else { } else {
throw new \RuntimeException(sprintf( throw new \RuntimeException(sprintf(
'"%s"" is not registered as a helper', '"%s"" is not registered as a helper',
$sectionName var_export($sectionName, true)
)); ));
} }
} }
@ -503,23 +503,50 @@ class Template
if ($current[Tokenizer::ARGS]) { if ($current[Tokenizer::ARGS]) {
preg_match_all( preg_match_all(
'/(\w+\=["|\'][^"\']+["|\']|\w+\=[a-zA-Z0-9\.\=]+)+/m', '/(\w+\=["|\'][^"\']+["|\']|\w+\=[a-zA-Z0-9\.\=]+|[a-zA-Z0-9\.]+)+/m',
$current[Tokenizer::ARGS], $current[Tokenizer::ARGS],
$args $args
); );
$partialArgs = array(); $context = new Context($this->_getPartialArguments($context, $args[0]));
foreach ($args[0] as $arg) {
list($key, $value) = explode('=', $arg, 2);
$partialArgs[$key] = strpos($value, '"') === false ? $context->get($value) : trim('"', $value);
}
$context = new Context($partialArgs);
} }
return $partial->render($context); return $partial->render($context);
} }
/**
* Prepare the arguments of a partial to actual array values to be used in a new context
*
* @param Context $context
* @param array $args
*
* @return array
*/
private function _getPartialArguments(Context $context, array $args)
{
$partialArgs = array();
foreach ($args as $arg) {
$parts = explode('=', $arg, 2);
$key = $parts[0];
if (false === isset($parts[1])) {
$value = $context->get($parts[0]);
if (is_array($value)) {
foreach ($value as $varKey => $varValue) {
$partialArgs[$varKey] = $varValue;
}
} else {
$partialArgs[$key] = $value;
}
} else {
$partialArgs[$key] = strpos($parts[1], '"') === false ? $context->get($parts[1]) : trim('"', $parts[1]);
}
}
return $partialArgs;
}
/** /**
* Check if there is a helper with this variable name available or not. * Check if there is a helper with this variable name available or not.

View File

@ -715,7 +715,11 @@ EOM;
public function testPartial() public function testPartial()
{ {
$loader = new \Handlebars\Loader\StringLoader(); $loader = new \Handlebars\Loader\StringLoader();
$partialLoader = new \Handlebars\Loader\ArrayLoader(array('test' => '{{key}}', 'bar' => 'its foo')); $partialLoader = new \Handlebars\Loader\ArrayLoader(array(
'test' => '{{key}}',
'bar' => 'its foo',
'presetVariables' => '{{myVar}}',
));
$partialAliasses = array('foo' => 'bar'); $partialAliasses = array('foo' => 'bar');
$engine = new \Handlebars\Handlebars( $engine = new \Handlebars\Handlebars(
array( array(
@ -733,6 +737,9 @@ EOM;
$this->setExpectedException('RuntimeException'); $this->setExpectedException('RuntimeException');
$engine->render('{{>foo-again}}', array()); $engine->render('{{>foo-again}}', array());
$this->assertEquals('foobar', $engine->render("{{>presetVariables myVar='foobar'}}", array()));
$this->assertEquals('foobar=barbaz', $engine->render("{{>presetVariables myVar='foobar=barbaz'}}", array()));
} }
/** /**