public function getExtension($name)
{
if (!isset($this->extensions[$name])) {
- throw new LogicException(sprintf('The "%s" extension is not enabled.', $name));
+ throw new Twig_Error_Runtime(sprintf('The "%s" extension is not enabled.', $name));
}
return $this->extensions[$name];
} else if ($parser instanceof Twig_TokenParserBrokerInterface) {
$this->parsers->addTokenParserBroker($parser);
} else {
- throw new InvalidArgumentException('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances');
+ throw new Twig_Error_Runtime('getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances');
}
}
}
}
}
- throw new RuntimeException(sprintf('Failed to write cache file "%s".', $file));
+ throw new Twig_Error_Runtime(sprintf('Failed to write cache file "%s".', $file));
}
}
/*
* This file is part of Twig.
*
- * (c) 2009 Fabien Potencier
- * (c) 2009 Armin Ronacher
+ * (c) 2010 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
- * Exception thrown when an error occurs at runtime.
+ * Exception thrown when an error occurs during template loading.
*
* @package twig
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
-class Twig_RuntimeError extends Twig_Error
+class Twig_Error_Loader extends Twig_Error
{
}
* @package twig
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
-class Twig_RuntimeError extends Twig_Error
+class Twig_Error_Runtime extends Twig_Error
{
}
* @package twig
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
-class Twig_SyntaxError extends Twig_Error
+class Twig_Error_Syntax extends Twig_Error
{
protected $lineno;
protected $filename;
$node = $this->parseExpression();
$this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, ')');
} else {
- throw new Twig_SyntaxError(sprintf('Unexpected token "%s" of value "%s"', Twig_Token::getTypeAsString($token->getType()), $token->getValue()), $token->getLine());
+ throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', Twig_Token::getTypeAsString($token->getType()), $token->getValue()), $token->getLine());
}
}
$arguments = new Twig_Node();
}
} else {
- throw new Twig_SyntaxError('Expected name or number', $lineno);
+ throw new Twig_Error_Syntax('Expected name or number', $lineno);
}
} else {
$type = Twig_Node_Expression_GetAttr::TYPE_ARRAY;
}
if (null === $string = preg_replace_callback('#[^\p{L}\p{N} ]#u', '_twig_escape_js_callback', $string)) {
- throw new InvalidArgumentException('The string to escape is not a valid UTF-8 string.');
+ throw new Twig_Error_Runtime('The string to escape is not a valid UTF-8 string.');
}
if ('UTF-8' != $charset) {
return htmlspecialchars($string, ENT_QUOTES, $env->getCharset());
default:
- throw new Exception(sprintf('Invalid escape type "%s".', $type));
+ throw new Twig_Error_Runtime(sprintf('Invalid escape type "%s".', $type));
}
}
} else {
function _twig_convert_encoding($string, $to, $from)
{
- throw new RuntimeException('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).');
+ throw new Twig_Error_Runtime('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).');
}
}
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2010 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+class Twig_Grammar_Switch extends Twig_Grammar
+{
+ public function __toString()
+ {
+ return sprintf('<%s:switch>', $this->name);
+ }
+
+ public function parse(Twig_Token $token)
+ {
+ $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, $this->name);
+
+ return new Twig_Node_Expression_Constant(true, $token->getLine());
+ }
+}
switch ($token) {
case $this->options['tag_comment'][0]:
if (!preg_match('/(.*?)'.preg_quote($this->options['tag_comment'][1], '/').'/As', $this->code, $match, null, $this->cursor)) {
- throw new Twig_SyntaxError('unclosed comment', $this->lineno, $this->filename);
+ throw new Twig_Error_Syntax('unclosed comment', $this->lineno, $this->filename);
}
$this->moveCursor($match[0]);
$this->moveLineNo($match[0]);
// sanity check
if ($this->cursor >= $this->end) {
- throw new Twig_SyntaxError('Unexpected end of stream', $this->lineno, $this->filename);
+ throw new Twig_Error_Syntax('Unexpected end of stream', $this->lineno, $this->filename);
}
// first parse operators
}
// unlexable
- throw new Twig_SyntaxError(sprintf("Unexpected character '%s'", $this->code[$this->cursor]), $this->lineno, $this->filename);
+ throw new Twig_Error_Syntax(sprintf("Unexpected character '%s'", $this->code[$this->cursor]), $this->lineno, $this->filename);
}
protected function moveLineNo($text)
public function getSource($name)
{
if (!isset($this->templates[$name])) {
- throw new LogicException(sprintf('Template "%s" is not defined.', $name));
+ throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
}
return $this->templates[$name];
public function getCacheKey($name)
{
if (!isset($this->templates[$name])) {
- throw new LogicException(sprintf('Template "%s" is not defined.', $name));
+ throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
}
return $this->templates[$name];
$this->paths = array();
foreach ($paths as $path) {
if (!is_dir($path)) {
- throw new InvalidArgumentException(sprintf('The "%s" directory does not exist.', $path));
+ throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
}
$this->paths[] = realpath($path);
// simple security check
if (0 !== strpos($file, $path)) {
- throw new RuntimeException('Looks like you try to load a template outside configured directories.');
+ throw new Twig_Error_Loader('Looks like you try to load a template outside configured directories.');
}
return $this->cache[$name] = $file;
}
- throw new RuntimeException(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths)));
+ throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths)));
}
}
public function getAttribute($name)
{
if (!array_key_exists($name, $this->attributes)) {
- throw new InvalidArgumentException(sprintf('Attribute "%s" does not exist for Node "%s".', $name, get_class($this)));
+ throw new Twig_Error_Runtime(sprintf('Attribute "%s" does not exist for Node "%s".', $name, get_class($this)));
}
return $this->attributes[$name];
public function getNode($name)
{
if (!array_key_exists($name, $this->nodes)) {
- throw new InvalidArgumentException(sprintf('Node "%s" does not exist for Node "%s".', $name, get_class($this)));
+ throw new Twig_Error_Runtime(sprintf('Node "%s" does not exist for Node "%s".', $name, get_class($this)));
}
return $this->nodes[$name];
$name = $this->getNode('filters')->getNode($i - 1)->getAttribute('value');
$attrs = $this->getNode('filters')->getNode($i);
if (!isset($filterMap[$name])) {
- throw new Twig_SyntaxError(sprintf('The filter "%s" does not exist', $name), $this->getLine());
+ throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine());
} else {
$compiler->raw($filterMap[$name]->compile().($filterMap[$name]->needsEnvironment() ? '($this->env, ' : '('));
}
{
$testMap = $compiler->getEnvironment()->getTests();
if (!isset($testMap[$this->getAttribute('name')])) {
- throw new Twig_SyntaxError(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
+ throw new Twig_Error_Syntax(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
}
$compiler
try {
$body = $this->subparse(null);
- } catch (Twig_SyntaxError $e) {
+ } catch (Twig_Error_Syntax $e) {
if (is_null($e->getFilename())) {
$e->setFilename($this->stream->getFilename());
}
$token = $this->getCurrentToken();
if ($token->getType() !== Twig_Token::NAME_TYPE) {
- throw new Twig_SyntaxError('A block must start with a tag name', $token->getLine());
+ throw new Twig_Error_Syntax('A block must start with a tag name', $token->getLine());
}
if (!is_null($test) && call_user_func($test, $token)) {
$subparser = $this->handlers->getTokenParser($token->getValue());
if (null === $subparser) {
- throw new Twig_SyntaxError(sprintf('Unknown tag name "%s"', $token->getValue()), $token->getLine());
+ throw new Twig_Error_Syntax(sprintf('Unknown tag name "%s"', $token->getValue()), $token->getLine());
}
$this->stream->next();
break;
default:
- throw new LogicException('Lexer or parser ended up in unsupported state.');
+ throw new Twig_Error_Syntax('Lexer or parser ended up in unsupported state.');
}
}
||
(!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && !$node instanceof Twig_Node_Import)
) {
- throw new Twig_SyntaxError('A template that extends another one cannot have a body', $node->getLine(), $this->stream->getFilename());
+ throw new Twig_Error_Syntax('A template that extends another one cannot have a body', $node->getLine(), $this->stream->getFilename());
}
}
}
} elseif (preg_match('/<(\w+)(?:\:(\w+))?>/A', $str, $match, null, $cursor)) {
$class = sprintf('Twig_Grammar_%s', ucfirst(isset($match[2]) ? $match[2] : 'Expression'));
if (!class_exists($class)) {
- throw new InvalidArgumentException(sprintf('Unable to understand "%s" in grammar (%s class does not exist)', $match[0], $class));
+ throw new Twig_Error_Runtime(sprintf('Unable to understand "%s" in grammar (%s class does not exist)', $match[0], $class));
}
$grammar->addGrammar(new $class($match[1]));
$cursor += strlen($match[0]);
return $grammar;
} else {
- throw new InvalidArgumentException(sprintf('Unable to parse grammar "%s" near "...%s..."', $str, substr($str, $cursor, 10)));
+ throw new Twig_Error_Runtime(sprintf('Unable to parse grammar "%s" near "...%s..."', $str, substr($str, $cursor, 10)));
}
}
if (false !== $parent = $this->getParent($context)) {
return $parent->getBlock($name, $context, $blocks);
} else {
- throw new LogicException('This template has no parent.');
+ throw new Twig_Error_Runtime('This template has no parent.');
}
}
protected function getContext($context, $item)
{
if (!array_key_exists($item, $context)) {
- throw new InvalidArgumentException(sprintf('Variable "%s" does not exist.', $item));
+ throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist.', $item));
}
return $context[$item];
return null;
}
- throw new InvalidArgumentException(sprintf('Key "%s" for array "%s" does not exist.', $item, $object));
+ throw new Twig_Error_Runtime(sprintf('Key "%s" for array "%s" does not exist.', $item, $object));
}
}
return null;
}
- throw new InvalidArgumentException(sprintf('Item "%s" for "%s" does not exist.', $item, $object));
+ throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist.', $item, $object));
}
// get some information about the object
return null;
}
- throw new InvalidArgumentException(sprintf('Method "%s" for object "%s" does not exist.', $item, get_class($object)));
+ throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist.', $item, get_class($object)));
}
if ($this->env->hasExtension('sandbox')) {
$name = 'OPERATOR_TYPE';
break;
default:
- throw new InvalidArgumentException(sprintf('Token of type %s does not exist.', $type));
+ throw new Twig_Error_Syntax(sprintf('Token of type %s does not exist.', $type));
}
return $short ? $name : 'Twig_Token::'.$name;
$lineno = $token->getLine();
$value = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue();
if (!in_array($value, array('on', 'off'))) {
- throw new Twig_SyntaxError("Autoescape value must be 'on' or 'off'", $lineno);
+ throw new Twig_Error_Syntax("Autoescape value must be 'on' or 'off'", $lineno);
}
$value = 'on' === $value ? 'html' : false;
if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE)) {
if (false === $value) {
- throw new Twig_SyntaxError(sprintf('Unexpected escaping strategy as you set autoescaping to off.', $lineno), -1);
+ throw new Twig_Error_Syntax(sprintf('Unexpected escaping strategy as you set autoescaping to off.', $lineno), -1);
}
$value = $this->parser->getStream()->next()->getValue();
$stream = $this->parser->getStream();
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
if ($this->parser->hasBlock($name)) {
- throw new Twig_SyntaxError("The block '$name' has already been defined", $lineno);
+ throw new Twig_Error_Syntax("The block '$name' has already been defined", $lineno);
}
$this->parser->pushBlockStack($name);
$value = $stream->next()->getValue();
if ($value != $name) {
- throw new Twig_SyntaxError(sprintf("Expected endblock for block '$name' (but %s given)", $value), $lineno);
+ throw new Twig_Error_Syntax(sprintf("Expected endblock for block '$name' (but %s given)", $value), $lineno);
}
}
} else {
public function parse(Twig_Token $token)
{
if (null !== $this->parser->getParent()) {
- throw new Twig_SyntaxError('Multiple extends tags are forbidden', $token->getLine());
+ throw new Twig_Error_Syntax('Multiple extends tags are forbidden', $token->getLine());
}
$this->parser->setParent($this->parser->getExpressionParser()->parseExpression());
break;
default:
- throw new Twig_SyntaxError(sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d)', $lineno), -1);
+ throw new Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d)', $lineno), -1);
}
}
public function parse(Twig_Token $token)
{
if (!count($this->parser->getBlockStack())) {
- throw new Twig_SyntaxError('Calling "parent" outside a block is forbidden', $token->getLine());
+ throw new Twig_Error_Syntax('Calling "parent" outside a block is forbidden', $token->getLine());
}
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
if (count($names) !== count($values)) {
- throw new Twig_SyntaxError("When using set, you must have the same number of variables and assignements.", $lineno);
+ throw new Twig_Error_Syntax("When using set, you must have the same number of variables and assignements.", $lineno);
}
} else {
$capture = true;
if (count($names) > 1) {
- throw new Twig_SyntaxError("When using set with a block, you cannot have a multi-target.", $lineno);
+ throw new Twig_Error_Syntax("When using set with a block, you cannot have a multi-target.", $lineno);
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
continue;
}
- throw new Twig_SyntaxError(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
+ throw new Twig_Error_Syntax(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
}
}
}
{
foreach($parsers as $parser) {
if (!$parser instanceof Twig_TokenParserInterface) {
- throw new InvalidArgumentException(
- '$parsers must a an array of Twig_TokenParserInterface');
+ throw new Twig_Error('$parsers must a an array of Twig_TokenParserInterface');
}
$this->parsers[$parser->getTag()] = $parser;
}
foreach($brokers as $broker) {
if (!$broker instanceof Twig_TokenParserBrokerInterface) {
- throw new InvalidArgumentException(
- '$brokers must a an array of Twig_TokenParserBrokerInterface');
+ throw new Twig_Error('$brokers must a an array of Twig_TokenParserBrokerInterface');
}
$this->brokers[] = $broker;
}
}
if (null === $token) {
- throw new Twig_SyntaxError('Unexpected end of template', -1);
+ throw new Twig_Error_Syntax('Unexpected end of template', -1);
}
// trim blocks
{
$token = $this->current;
if (!$token->test($primary, $secondary)) {
- throw new Twig_SyntaxError(sprintf('Unexpected token "%s" of value "%s" ("%s" expected%s)',
+ throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s" ("%s" expected%s)',
Twig_Token::getTypeAsString($token->getType()), $token->getValue(),
Twig_Token::getTypeAsString($primary), $secondary ? sprintf(' with value "%s"', $secondary) : ''),
$this->current->getLine()
$node->compile($this->getCompiler());
$this->fail();
} catch (Exception $e) {
- $this->assertEquals('Twig_SyntaxError', get_class($e));
+ $this->assertEquals('Twig_Error_Syntax', get_class($e));
}
}
Twig_SimpleTokenParser::parseGrammar('<foo:foo>');
$this->fail();
} catch (Exception $e) {
- $this->assertEquals('InvalidArgumentException', get_class($e));
+ $this->assertEquals('Twig_Error_Runtime', get_class($e));
}
try {
Twig_SimpleTokenParser::parseGrammar('<foo:foo');
$this->fail();
} catch (Exception $e) {
- $this->assertEquals('InvalidArgumentException', get_class($e));
+ $this->assertEquals('Twig_Error_Runtime', get_class($e));
}
try {
Twig_SimpleTokenParser::parseGrammar('<foo:foo> (with');
$this->fail();
} catch (Exception $e) {
- $this->assertEquals('InvalidArgumentException', get_class($e));
+ $this->assertEquals('Twig_Error_Runtime', get_class($e));
}
}
{
return array(
array('foo1', '', '{% foo1 %}', '|', false),
- array('foo2', '', '{% foo2 "bar" %}', '|', 'Twig_SyntaxError'),
+ array('foo2', '', '{% foo2 "bar" %}', '|', 'Twig_Error_Syntax'),
array('foo3', '<foo>', '{% foo3 "bar" %}', '|bar|', false),
array('foo4', '<foo>', '{% foo4 1 + 2 %}', '|3|', false),
array('foo5', '<foo:expression>', '{% foo5 1 + 2 %}', '|3|', false),
- array('foo6', '<foo:array>', '{% foo6 1 + 2 %}', '|3|', 'Twig_SyntaxError'),
- array('foo7', '<foo>', '{% foo7 %}', '|3|', 'Twig_SyntaxError'),
+ array('foo6', '<foo:array>', '{% foo6 1 + 2 %}', '|3|', 'Twig_Error_Syntax'),
+ array('foo7', '<foo>', '{% foo7 %}', '|3|', 'Twig_Error_Syntax'),
array('foo8', '<foo:array>', '{% foo8 [1, 2] %}', '|Array|', false),
array('foo9', '<foo> with <bar>', '{% foo9 "bar" with "foobar" %}', '|bar|with|foobar|', false),
array('foo10', '<foo> [with <bar>]', '{% foo10 "bar" with "foobar" %}', '|bar|with|foobar|', false),