Initial commit with Symfony 2.1+Vendors

Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely
2012-07-01 09:52:20 +02:00
commit 082a0130c2
5381 changed files with 416709 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Autoloads Twig Extensions classes.
*
* @package twig
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Twig_Extensions_Autoloader
{
/**
* Registers Twig_Extensions_Autoloader as an SPL autoloader.
*/
static public function register()
{
ini_set('unserialize_callback_func', 'spl_autoload_call');
spl_autoload_register(array(new self, 'autoload'));
}
/**
* Handles autoloading of classes.
*
* @param string $class A class name.
*
* @return boolean Returns true if the class has been loaded
*/
static public function autoload($class)
{
if (0 !== strpos($class, 'Twig_Extensions')) {
return;
}
if (file_exists($file = dirname(__FILE__).'/../../'.str_replace('_', '/', $class).'.php')) {
require $file;
}
}
}

View File

@@ -0,0 +1,34 @@
<?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_Extensions_Extension_Debug extends Twig_Extension
{
/**
* Returns the token parser instance to add to the existing list.
*
* @return array An array of Twig_TokenParser instances
*/
public function getTokenParsers()
{
return array(
new Twig_Extensions_TokenParser_Debug(),
);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'debug';
}
}

View File

@@ -0,0 +1,44 @@
<?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_Extensions_Extension_I18n extends Twig_Extension
{
/**
* Returns the token parser instances to add to the existing list.
*
* @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
*/
public function getTokenParsers()
{
return array(new Twig_Extensions_TokenParser_Trans());
}
/**
* Returns a list of filters to add to the existing list.
*
* @return array An array of filters
*/
public function getFilters()
{
return array(
'trans' => new Twig_Filter_Function('gettext'),
);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'i18n';
}
}

View File

@@ -0,0 +1,70 @@
<?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_Extensions_Extension_Intl extends Twig_Extension
{
public function __construct()
{
if (!class_exists('IntlDateFormatter')) {
throw new RuntimeException('The intl extension is needed to use intl-based filters.');
}
}
/**
* Returns a list of filters to add to the existing list.
*
* @return array An array of filters
*/
public function getFilters()
{
return array(
'localizeddate' => new Twig_Filter_Function('twig_localized_date_filter'),
);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'intl';
}
}
function twig_localized_date_filter($date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null)
{
$formatValues = array(
'none' => IntlDateFormatter::NONE,
'short' => IntlDateFormatter::SHORT,
'medium' => IntlDateFormatter::MEDIUM,
'long' => IntlDateFormatter::LONG,
'full' => IntlDateFormatter::FULL,
);
$formatter = IntlDateFormatter::create(
$locale !== null ? $locale : Locale::getDefault(),
$formatValues[$dateFormat],
$formatValues[$timeFormat]
);
if (!$date instanceof DateTime) {
if (ctype_digit((string) $date)) {
$date = new DateTime('@'.$date);
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
} else {
$date = new DateTime($date);
}
}
return $formatter->format($date->getTimestamp());
}

View File

@@ -0,0 +1,109 @@
<?php
/**
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Henrik Bjornskov <hb@peytz.dk>
* @package Twig
* @subpackage Twig-extensions
*/
class Twig_Extensions_Extension_Text extends Twig_Extension
{
/**
* Returns a list of filters.
*
* @return array
*/
public function getFilters()
{
$filters = array(
'truncate' => new Twig_Filter_Function('twig_truncate_filter', array('needs_environment' => true)),
'wordwrap' => new Twig_Filter_Function('twig_wordwrap_filter', array('needs_environment' => true)),
);
if (version_compare(Twig_Environment::VERSION, '1.5.0-DEV', '<')) {
$filters['nl2br'] = new Twig_Filter_Function('twig_nl2br_filter', array('pre_escape' => 'html', 'is_safe' => array('html')));
}
return $filters;
}
/**
* Name of this extension
*
* @return string
*/
public function getName()
{
return 'Text';
}
}
function twig_nl2br_filter($value, $sep = '<br />')
{
return str_replace("\n", $sep."\n", $value);
}
if (function_exists('mb_get_info')) {
function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
{
if (mb_strlen($value, $env->getCharset()) > $length) {
if ($preserve) {
if (false !== ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) {
$length = $breakpoint;
}
}
return mb_substr($value, 0, $length, $env->getCharset()) . $separator;
}
return $value;
}
function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
{
$sentences = array();
$previous = mb_regex_encoding();
mb_regex_encoding($env->getCharset());
$pieces = mb_split($separator, $value);
mb_regex_encoding($previous);
foreach ($pieces as $piece) {
while(!$preserve && mb_strlen($piece, $env->getCharset()) > $length) {
$sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
$piece = mb_substr($piece, $length, 2048, $env->getCharset());
}
$sentences[] = $piece;
}
return implode($separator, $sentences);
}
} else {
function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
{
if (strlen($value) > $length) {
if ($preserve) {
if (false !== ($breakpoint = strpos($value, ' ', $length))) {
$length = $breakpoint;
}
}
return substr($value, 0, $length) . $separator;
}
return $value;
}
function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
{
return wordwrap($value, $length, $separator, !$preserve);
}
}

View File

@@ -0,0 +1,30 @@
<?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.
*/
abstract class Twig_Extensions_Grammar implements Twig_Extensions_GrammarInterface
{
protected $name;
protected $parser;
public function __construct($name)
{
$this->name = $name;
}
public function setParser(Twig_ParserInterface $parser)
{
$this->parser = $parser;
}
public function getName()
{
return $this->name;
}
}

View File

@@ -0,0 +1,22 @@
<?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_Extensions_Grammar_Arguments extends Twig_Extensions_Grammar
{
public function __toString()
{
return sprintf('<%s:arguments>', $this->name);
}
public function parse(Twig_Token $token)
{
return $this->parser->getExpressionParser()->parseArguments();
}
}

View File

@@ -0,0 +1,22 @@
<?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_Extensions_Grammar_Array extends Twig_Extensions_Grammar
{
public function __toString()
{
return sprintf('<%s:array>', $this->name);
}
public function parse(Twig_Token $token)
{
return $this->parser->getExpressionParser()->parseArrayExpression();
}
}

View File

@@ -0,0 +1,39 @@
<?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_Extensions_Grammar_Body extends Twig_Extensions_Grammar
{
protected $end;
public function __construct($name, $end = null)
{
parent::__construct($name);
$this->end = null === $end ? 'end'.$name : $end;
}
public function __toString()
{
return sprintf('<%s:body>', $this->name);
}
public function parse(Twig_Token $token)
{
$stream = $this->parser->getStream();
$stream->expect(Twig_Token::BLOCK_END_TYPE);
return $this->parser->subparse(array($this, 'decideBlockEnd'), true);
}
public function decideBlockEnd($token)
{
return $token->test($this->end);
}
}

View File

@@ -0,0 +1,24 @@
<?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_Extensions_Grammar_Boolean extends Twig_Extensions_Grammar
{
public function __toString()
{
return sprintf('<%s:boolean>', $this->name);
}
public function parse(Twig_Token $token)
{
$this->parser->getStream()->expect(Twig_Token::NAME_TYPE, array('true', 'false'));
return new Twig_Node_Expression_Constant('true' === $token->getValue() ? true : false, $token->getLine());
}
}

View File

@@ -0,0 +1,37 @@
<?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_Extensions_Grammar_Constant extends Twig_Extensions_Grammar
{
protected $type;
public function __construct($name, $type = null)
{
$this->name = $name;
$this->type = null === $type ? Twig_Token::NAME_TYPE : $type;
}
public function __toString()
{
return $this->name;
}
public function parse(Twig_Token $token)
{
$this->parser->getStream()->expect($this->type, $this->name);
return $this->name;
}
public function getType()
{
return $this->type;
}
}

View File

@@ -0,0 +1,22 @@
<?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_Extensions_Grammar_Expression extends Twig_Extensions_Grammar
{
public function __toString()
{
return sprintf('<%s>', $this->name);
}
public function parse(Twig_Token $token)
{
return $this->parser->getExpressionParser()->parseExpression();
}
}

View File

@@ -0,0 +1,22 @@
<?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_Extensions_Grammar_Hash extends Twig_Extensions_Grammar
{
public function __toString()
{
return sprintf('<%s:hash>', $this->name);
}
public function parse(Twig_Token $token)
{
return $this->parser->getExpressionParser()->parseHashExpression();
}
}

View File

@@ -0,0 +1,24 @@
<?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_Extensions_Grammar_Number extends Twig_Extensions_Grammar
{
public function __toString()
{
return sprintf('<%s:number>', $this->name);
}
public function parse(Twig_Token $token)
{
$this->parser->getStream()->expect(Twig_Token::NUMBER_TYPE);
return new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
}
}

View File

@@ -0,0 +1,69 @@
<?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_Extensions_Grammar_Optional extends Twig_Extensions_Grammar
{
protected $grammar;
public function __construct()
{
$this->grammar = array();
foreach (func_get_args() as $grammar) {
$this->addGrammar($grammar);
}
}
public function __toString()
{
$repr = array();
foreach ($this->grammar as $grammar) {
$repr[] = (string) $grammar;
}
return sprintf('[%s]', implode(' ', $repr));
}
public function addGrammar(Twig_Extensions_GrammarInterface $grammar)
{
$this->grammar[] = $grammar;
}
public function parse(Twig_Token $token)
{
// test if we have the optional element before consuming it
if ($this->grammar[0] instanceof Twig_Extensions_Grammar_Constant) {
if (!$this->parser->getStream()->test($this->grammar[0]->getType(), $this->grammar[0]->getName())) {
return array();
}
} elseif ($this->grammar[0] instanceof Twig_Extensions_Grammar_Name) {
if (!$this->parser->getStream()->test(Twig_Token::NAME_TYPE)) {
return array();
}
} elseif ($this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE)) {
// if this is not a Constant or a Name, it must be the last element of the tag
return array();
}
$elements = array();
foreach ($this->grammar as $grammar) {
$grammar->setParser($this->parser);
$element = $grammar->parse($token);
if (is_array($element)) {
$elements = array_merge($elements, $element);
} else {
$elements[$grammar->getName()] = $element;
}
}
return $elements;
}
}

View File

@@ -0,0 +1,24 @@
<?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_Extensions_Grammar_Switch extends Twig_Extensions_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());
}
}

View File

@@ -0,0 +1,56 @@
<?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_Extensions_Grammar_Tag extends Twig_Extensions_Grammar
{
protected $grammar;
public function __construct()
{
$this->grammar = array();
foreach (func_get_args() as $grammar) {
$this->addGrammar($grammar);
}
}
public function __toString()
{
$repr = array();
foreach ($this->grammar as $grammar) {
$repr[] = (string) $grammar;
}
return implode(' ', $repr);
}
public function addGrammar(Twig_Extensions_GrammarInterface $grammar)
{
$this->grammar[] = $grammar;
}
public function parse(Twig_Token $token)
{
$elements = array();
foreach ($this->grammar as $grammar) {
$grammar->setParser($this->parser);
$element = $grammar->parse($token);
if (is_array($element)) {
$elements = array_merge($elements, $element);
} else {
$elements[$grammar->getName()] = $element;
}
}
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
return $elements;
}
}

View File

@@ -0,0 +1,18 @@
<?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.
*/
interface Twig_Extensions_GrammarInterface
{
function setParser(Twig_ParserInterface $parser);
function parse(Twig_Token $token);
function getName();
}

View File

@@ -0,0 +1,69 @@
<?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.
*/
/**
* Represents a debug node.
*
* @package twig
* @subpackage Twig-extensions
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id$
*/
class Twig_Extensions_Node_Debug extends Twig_Node
{
public function __construct(Twig_Node_Expression $expr = null, $lineno, $tag = null)
{
parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
}
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
public function compile(Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);
$compiler
->write("if (\$this->env->isDebug()) {\n")
->indent()
;
if (null === $this->getNode('expr')) {
// remove embedded templates (macros) from the context
$compiler
->write("\$vars = array();\n")
->write("foreach (\$context as \$key => \$value) {\n")
->indent()
->write("if (!\$value instanceof Twig_Template) {\n")
->indent()
->write("\$vars[\$key] = \$value;\n")
->outdent()
->write("}\n")
->outdent()
->write("}\n")
->write("var_dump(\$vars);\n")
;
} else {
$compiler
->write("var_dump(")
->subcompile($this->getNode('expr'))
->raw(");\n")
;
}
$compiler
->outdent()
->write("}\n")
;
}
}

View File

@@ -0,0 +1,133 @@
<?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.
*/
/**
* Represents a trans node.
*
* @package twig
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Twig_Extensions_Node_Trans extends Twig_Node
{
public function __construct(Twig_NodeInterface $body, Twig_NodeInterface $plural = null, Twig_Node_Expression $count = null, $lineno, $tag = null)
{
parent::__construct(array('count' => $count, 'body' => $body, 'plural' => $plural), array(), $lineno, $tag);
}
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
public function compile(Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);
list($msg, $vars) = $this->compileString($this->getNode('body'));
if (null !== $this->getNode('plural')) {
list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
$vars = array_merge($vars, $vars1);
}
$function = null === $this->getNode('plural') ? 'gettext' : 'ngettext';
if ($vars) {
$compiler
->write('echo strtr('.$function.'(')
->subcompile($msg)
;
if (null !== $this->getNode('plural')) {
$compiler
->raw(', ')
->subcompile($msg1)
->raw(', abs(')
->subcompile($this->getNode('count'))
->raw(')')
;
}
$compiler->raw('), array(');
foreach ($vars as $var) {
if ('count' === $var->getAttribute('name')) {
$compiler
->string('%count%')
->raw(' => abs(')
->subcompile($this->getNode('count'))
->raw('), ')
;
} else {
$compiler
->string('%'.$var->getAttribute('name').'%')
->raw(' => ')
->subcompile($var)
->raw(', ')
;
}
}
$compiler->raw("));\n");
} else {
$compiler
->write('echo '.$function.'(')
->subcompile($msg)
;
if (null !== $this->getNode('plural')) {
$compiler
->raw(', ')
->subcompile($msg1)
->raw(', abs(')
->subcompile($this->getNode('count'))
->raw(')')
;
}
$compiler->raw(");\n");
}
}
protected function compileString(Twig_NodeInterface $body)
{
if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant || $body instanceof Twig_Node_Expression_TempName) {
return array($body, array());
}
$vars = array();
if (count($body)) {
$msg = '';
foreach ($body as $node) {
if (get_class($node) === 'Twig_Node' && $node->getNode(0) instanceof Twig_Node_SetTemp) {
$node = $node->getNode(1);
}
if ($node instanceof Twig_Node_Print) {
$n = $node->getNode('expr');
while ($n instanceof Twig_Node_Expression_Filter) {
$n = $n->getNode('node');
}
$msg .= sprintf('%%%s%%', $n->getAttribute('name'));
$vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
} else {
$msg .= $node->getAttribute('data');
}
}
} else {
$msg = $body->getAttribute('data');
}
return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars);
}
}

View File

@@ -0,0 +1,132 @@
<?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.
*/
abstract class Twig_Extensions_SimpleTokenParser extends Twig_TokenParser
{
/**
* Parses a token and returns a node.
*
* @param Twig_Token $token A Twig_Token instance
*
* @return Twig_NodeInterface A Twig_NodeInterface instance
*/
public function parse(Twig_Token $token)
{
$grammar = $this->getGrammar();
if (!is_object($grammar)) {
$grammar = self::parseGrammar($grammar);
}
$grammar->setParser($this->parser);
$values = $grammar->parse($token);
return $this->getNode($values, $token->getLine());
}
/**
* Gets the grammar as an object or as a string.
*
* @return string|Twig_Extensions_Grammar A Twig_Extensions_Grammar instance or a string
*/
abstract protected function getGrammar();
/**
* Gets the nodes based on the parsed values.
*
* @param array $values An array of values
* @param integer $line The parser line
*/
abstract protected function getNode(array $values, $line);
protected function getAttribute($node, $attribute, $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY, $line = -1)
{
return new Twig_Node_Expression_GetAttr(
$node instanceof Twig_NodeInterface ? $node : new Twig_Node_Expression_Name($node, $line),
$attribute instanceof Twig_NodeInterface ? $attribute : new Twig_Node_Expression_Constant($attribute, $line),
$arguments instanceof Twig_NodeInterface ? $arguments : new Twig_Node($arguments),
$type,
$line
);
}
protected function call($node, $attribute, $arguments = array(), $line = -1)
{
return $this->getAttribute($node, $attribute, $arguments, Twig_Node_Expression_GetAttr::TYPE_METHOD, $line);
}
protected function markAsSafe(Twig_NodeInterface $node, $line = -1)
{
return new Twig_Node_Expression_Filter(
$node,
new Twig_Node_Expression_Constant('raw', $line),
new Twig_Node(),
$line
);
}
protected function output(Twig_NodeInterface $node, $line = -1)
{
return new Twig_Node_Print($node, $line);
}
protected function getNodeValues(array $values)
{
$nodes = array();
foreach ($values as $value) {
if ($value instanceof Twig_NodeInterface) {
$nodes[] = $value;
}
}
return $nodes;
}
static public function parseGrammar($str, $main = true)
{
static $cursor;
if (true === $main) {
$cursor = 0;
$grammar = new Twig_Extensions_Grammar_Tag();
} else {
$grammar = new Twig_Extensions_Grammar_Optional();
}
while ($cursor < strlen($str)) {
if (preg_match('/\s+/A', $str, $match, null, $cursor)) {
$cursor += strlen($match[0]);
} elseif (preg_match('/<(\w+)(?:\:(\w+))?>/A', $str, $match, null, $cursor)) {
$class = sprintf('Twig_Extensions_Grammar_%s', ucfirst(isset($match[2]) ? $match[2] : 'Expression'));
if (!class_exists($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]);
} elseif (preg_match('/\w+/A', $str, $match, null, $cursor)) {
$grammar->addGrammar(new Twig_Extensions_Grammar_Constant($match[0]));
$cursor += strlen($match[0]);
} elseif (preg_match('/,/A', $str, $match, null, $cursor)) {
$grammar->addGrammar(new Twig_Extensions_Grammar_Constant($match[0], Twig_Token::PUNCTUATION_TYPE));
$cursor += strlen($match[0]);
} elseif (preg_match('/\[/A', $str, $match, null, $cursor)) {
$cursor += strlen($match[0]);
$grammar->addGrammar(self::parseGrammar($str, false));
} elseif (true !== $main && preg_match('/\]/A', $str, $match, null, $cursor)) {
$cursor += strlen($match[0]);
return $grammar;
} else {
throw new Twig_Error_Runtime(sprintf('Unable to parse grammar "%s" near "...%s..."', $str, substr($str, $cursor, 10)));
}
}
return $grammar;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009-2010 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Extensions_TokenParser_Debug extends Twig_TokenParser
{
/**
* Parses a token and returns a node.
*
* @param Twig_Token $token A Twig_Token instance
*
* @return Twig_NodeInterface A Twig_NodeInterface instance
*/
public function parse(Twig_Token $token)
{
$lineno = $token->getLine();
$expr = null;
if (!$this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE)) {
$expr = $this->parser->getExpressionParser()->parseExpression();
}
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
return new Twig_Extensions_Node_Debug($expr, $lineno, $this->getTag());
}
/**
* Gets the tag name associated with this token parser.
*
* @param string The tag name
*/
public function getTag()
{
return 'debug';
}
}

View File

@@ -0,0 +1,80 @@
<?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_Extensions_TokenParser_Trans extends Twig_TokenParser
{
/**
* Parses a token and returns a node.
*
* @param Twig_Token $token A Twig_Token instance
*
* @return Twig_NodeInterface A Twig_NodeInterface instance
*/
public function parse(Twig_Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$count = null;
$plural = null;
if (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
$body = $this->parser->getExpressionParser()->parseExpression();
} else {
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideForFork'));
if ('plural' === $stream->next()->getValue()) {
$count = $this->parser->getExpressionParser()->parseExpression();
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$plural = $this->parser->subparse(array($this, 'decideForEnd'), true);
}
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$this->checkTransString($body, $lineno);
return new Twig_Extensions_Node_Trans($body, $plural, $count, $lineno, $this->getTag());
}
public function decideForFork($token)
{
return $token->test(array('plural', 'endtrans'));
}
public function decideForEnd($token)
{
return $token->test('endtrans');
}
/**
* Gets the tag name associated with this token parser.
*
* @param string The tag name
*/
public function getTag()
{
return 'trans';
}
protected function checkTransString(Twig_NodeInterface $body, $lineno)
{
foreach ($body as $i => $node) {
if (
$node instanceof Twig_Node_Text
||
($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_Name)
) {
continue;
}
throw new Twig_Error_Syntax(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
}
}
}