Merge pull request #124 from thormeier/partial-support-and-error-reporting

Partial support and error reporting
This commit is contained in:
fzerorubigd 2015-06-20 10:28:40 +04:30
commit 071793118a
8 changed files with 121 additions and 20 deletions

View File

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

View File

@ -197,7 +197,10 @@ class Context
if (count($this->stack) < $level) {
if ($strict) {
throw new \InvalidArgumentException(
'can not find variable in context'
sprintf(
'Can not find variable in context: "%s"',
$variableName
)
);
}
@ -216,9 +219,13 @@ class Context
if (!$variableName) {
if ($strict) {
throw new \InvalidArgumentException(
'can not find variable in context'
sprintf(
'Can not find variable in context: "%s"',
$variableName
)
);
}
return '';
} elseif ($variableName == '.' || $variableName == 'this') {
return $current;
@ -228,7 +235,10 @@ class Context
return $specialVariables[$variableName];
} elseif ($strict) {
throw new \InvalidArgumentException(
'can not find variable in context'
sprintf(
'Can not find variable in context: "%s"',
$variableName
)
);
} else {
return '';
@ -275,7 +285,12 @@ class Context
}
if ($strict) {
throw new \InvalidArgumentException('can not find variable in context');
throw new \InvalidArgumentException(
sprintf(
'Can not find variable in context: "%s"',
var_export($variable, true)
)
);
}
return $value;
@ -299,7 +314,12 @@ class Context
$get_pattern = "/(?:" . $name_pattern . ")/";
if (!preg_match($check_pattern, $variableName)) {
throw new \InvalidArgumentException('variable name is invalid');
throw new \InvalidArgumentException(
sprintf(
'Variable name is invalid: "%s"',
$variableName
)
);
}
preg_match_all($get_pattern, $variableName, $matches);

View File

@ -460,7 +460,10 @@ class Handlebars
{
if (!is_a($class, 'Handlebars\\Template', true)) {
throw new \InvalidArgumentException(
'Custom template class must extend Template'
sprintf(
'Custom template class "%s" must extend Template',
$class
)
);
}

View File

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

View File

@ -75,11 +75,21 @@ class InlineLoader implements Loader
public function __construct($fileName, $offset)
{
if (!is_file($fileName)) {
throw new \InvalidArgumentException('InlineLoader expects a valid filename.');
throw new \InvalidArgumentException(
sprintf(
'InlineLoader expects a valid filename, "%s" given.',
$fileName
)
);
}
if (!is_int($offset) || $offset < 0) {
throw new \InvalidArgumentException('InlineLoader expects a valid file offset.');
throw new \InvalidArgumentException(
sprintf(
'InlineLoader expects a valid file offset, "%s" given.',
$offset
)
);
}
$this->fileName = $fileName;
@ -98,7 +108,7 @@ class InlineLoader implements Loader
$this->loadTemplates();
if (!array_key_exists($name, $this->templates)) {
throw new \InvalidArgumentException("Template {$name} not found.");
throw new \InvalidArgumentException("Template $name not found.");
}
return $this->templates[$name];

View File

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

View File

@ -23,6 +23,7 @@
*/
namespace Handlebars;
use Handlebars\Arguments;
/**
* Handlebars base template
@ -31,6 +32,7 @@ namespace Handlebars;
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Pascal Thormeier <pascal.thormeier@gmail.com>
* @copyright 2010-2012 (c) Justin Hileman
* @copyright 2012 (c) ParsPooyesh Co
* @license MIT <http://opensource.org/licenses/MIT>
@ -405,7 +407,10 @@ class Template
$sectionVar = $context->get($sectionName, true);
} catch (\InvalidArgumentException $e) {
throw new \RuntimeException(
$sectionName . ' is not registered as a helper'
sprintf(
'"%s" is not registered as a helper',
$sectionName
)
);
}
$buffer = '';
@ -462,7 +467,10 @@ class Template
return $this->_mustacheStyleSection($context, $current);
} else {
throw new \RuntimeException(
$sectionName . ' is not registered as a helper'
sprintf(
'"%s"" is not registered as a helper',
$sectionName
)
);
}
}
@ -500,12 +508,39 @@ class Template
$partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]);
if ($current[Tokenizer::ARGS]) {
$context = $context->get($current[Tokenizer::ARGS]);
$arguments = new Arguments($current[Tokenizer::ARGS]);
$context = new Context($this->_preparePartialArguments($context, $arguments));
}
return $partial->render($context);
}
/**
* Prepare the arguments of a partial to actual array values to be used in a new context
*
* @param Context $context Current context
* @param Arguments $arguments Arguments for partial
*
* @return array
*/
private function _preparePartialArguments(Context $context, Arguments $arguments)
{
$positionalArgs = array();
foreach ($arguments->getPositionalArguments() as $positionalArg) {
$contextArg = $context->get($positionalArg);
if (is_array($contextArg)) {
foreach ($contextArg as $key => $value) {
$positionalArgs[$key] = $value;
}
} else {
$positionalArgs[$positionalArg] = $contextArg;
}
}
return array_merge($positionalArgs, $arguments->getNamedArguments());
}
/**
* Check if there is a helper with this variable name available or not.

View File

@ -176,7 +176,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
'{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}',
array('first' => false, 'second' => true),
'The second'
)
),
);
}
@ -715,7 +715,11 @@ EOM;
public function testPartial()
{
$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');
$engine = new \Handlebars\Handlebars(
array(
@ -733,6 +737,9 @@ EOM;
$this->setExpectedException('RuntimeException');
$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()));
}
/**