From 8069e235fd2971675ee5fc05026ffa9bce5cbbb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 2 Aug 2013 22:43:56 +0200 Subject: move Twig in 3rdparty --- inc/3rdparty/Twig/Extensions/Node/Debug.php | 69 +++++++++++++++ inc/3rdparty/Twig/Extensions/Node/Trans.php | 133 ++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 inc/3rdparty/Twig/Extensions/Node/Debug.php create mode 100644 inc/3rdparty/Twig/Extensions/Node/Trans.php (limited to 'inc/3rdparty/Twig/Extensions/Node') diff --git a/inc/3rdparty/Twig/Extensions/Node/Debug.php b/inc/3rdparty/Twig/Extensions/Node/Debug.php new file mode 100644 index 00000000..7d01bbe5 --- /dev/null +++ b/inc/3rdparty/Twig/Extensions/Node/Debug.php @@ -0,0 +1,69 @@ + + * @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") + ; + } +} diff --git a/inc/3rdparty/Twig/Extensions/Node/Trans.php b/inc/3rdparty/Twig/Extensions/Node/Trans.php new file mode 100644 index 00000000..d12564a7 --- /dev/null +++ b/inc/3rdparty/Twig/Extensions/Node/Trans.php @@ -0,0 +1,133 @@ + + */ +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); + } +} -- cgit v1.2.3