/** * {@inheritdoc} */ public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Expression_Name) { print $node->getAttribute('name') . "\n"; } return $node; }
function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Print) { // make sure that every {{ }} printed object is handled as a TFD_Node_Render node (aka autorender) if (!$node->getNode('expr') instanceof Twig_Node_Expression_Function) { if ($env->isAutoRender()) { $targetNode = $node->getNode('expr'); if ($targetNode instanceof Twig_Node_Expression_Name) { $targetNode->setAttribute('always_defined', TRUE); } if (!$targetNode instanceof Twig_Node_Expression_MethodCall) { $node = new TFD_Node_Render($targetNode, $node->getLine(), $node->getNodeTag()); } } } elseif ($node->getNode('expr') instanceof Twig_Node_Expression_Function) { $targetNode = $node->getNode('expr'); if ($targetNode->getAttribute('name') == 'hide') { $targetNode = $this->castObject('TFD_Node_Expression_Nocall', $targetNode); $targetNode->setAttribute('always_defined', TRUE); $node = new TFD_Node_Hide($targetNode, $node->getLine(), $node->getNodeTag()); } } } return $node; }
/** * Called before child nodes are visited. * * @param Twig_NodeInterface $node The node to visit * @param Twig_Environment $env The Twig environment instance * * @param Twig_NodeInterface The modified node */ public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $this->inAModule = true; $this->tags = array(); $this->filters = array(); $this->functions = array(); return $node; } elseif ($this->inAModule) { // look for tags if ($node->getNodeTag()) { $this->tags[] = $node->getNodeTag(); } // look for filters if ($node instanceof Twig_Node_Expression_Filter) { $this->filters[] = $node->getNode('filter')->getAttribute('value'); } // look for functions if ($node instanceof Twig_Node_Expression_Function) { $this->functions[] = $node->getAttribute('name'); } // wrap print to check __toString() calls if ($node instanceof Twig_Node_Print) { return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag()); } } return $node; }
/** * Called before child nodes are visited. * * @param Twig_NodeInterface $node The node to visit * @param Twig_Environment $env The Twig environment instance * * @param Twig_NodeInterface The modified node */ public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $this->inAModule = true; $this->tags = array(); $this->filters = array(); return $node; } elseif ($this->inAModule) { // look for tags if ($node->getNodeTag()) { $this->tags[] = $node->getNodeTag(); } // look for filters if ($node instanceof Twig_Node_Expression_Filter) { for ($i = 0; $i < count($node->filters); $i += 2) { $this->filters[] = $node->filters->{$i}['value']; } } // look for simple print statements ({{ article }}) if ($node instanceof Twig_Node_Print && $node->expr instanceof Twig_Node_Expression_Name) { return new Twig_Node_SandboxedPrint($node); } } return $node; }
public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Expression_Binary_Concat && ($left = $node->getNode('left')) instanceof \Twig_Node_Expression_Constant && ($right = $node->getNode('right')) instanceof \Twig_Node_Expression_Constant) { return new \Twig_Node_Expression_Constant($left->getAttribute('value') . $right->getAttribute('value'), $left->getLine()); } return $node; }
/** * {@inheritdoc} */ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Expression_Function && 'constant' === $node->getAttribute('name') && 1 === $node->count() && null !== ($resolved = $this->resolve($node))) { return $this->resolve($node); } return $node; }
/** * @inheritdoc */ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Print) { $expression = $node->getNode('expr'); if ($expression instanceof \Twig_Node_Expression_Function) { $name = $expression->getAttribute('name'); if (preg_match('/^(?:register_.+_asset|use|.+_begin|.+_end)$/', $name)) { return new \Twig_Node_Do($expression, $expression->getLine()); } elseif (in_array($name, ['begin_page', 'end_page', 'begin_body', 'end_body', 'head'])) { $arguments = [new \Twig_Node_Expression_Constant($name, $expression->getLine())]; if ($expression->hasNode('arguments') && $expression->getNode('arguments') !== null) { foreach ($expression->getNode('arguments') as $key => $value) { if (is_int($key)) { $arguments[] = $value; } else { $arguments[$key] = $value; } } } $expression->setNode('arguments', new \Twig_Node($arguments)); return new \Twig_Node_Do($expression, $expression->getLine()); } } } return $node; }
/** * Extracts formulae from filter function nodes. * * @return array|null The formula */ private function checkNode(\Twig_NodeInterface $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Expression_Function) { $name = $node->getNode('name')->getAttribute('name'); if ($env->getFunction($name) instanceof AsseticFilterFunction) { $arguments = array(); foreach ($node->getNode('arguments') as $argument) { $arguments[] = eval('return '.$env->compile($argument).';'); } $invoker = $env->getExtension('assetic')->getFilterInvoker($name); $factory = $invoker->getFactory(); $inputs = isset($arguments[0]) ? (array) $arguments[0] : array(); $filters = $invoker->getFilters(); $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array()); if (!isset($options['name'])) { $options['name'] = $factory->generateAssetName($inputs, $filters); } return array($inputs, $filters, $options); } } }
public function __construct(Twig_Node_Expression_AssignName $keyTarget, Twig_Node_Expression_AssignName $valueTarget, Twig_Node_Expression $seq, Twig_Node_Expression $ifexpr = null, Twig_NodeInterface $body, Twig_NodeInterface $else = null, $lineno, $tag = null) { $body->setNode('_for_loop', $this->loop = new Twig_Node_ForLoop($lineno, $tag)); if (null !== $ifexpr) { $body = new Twig_Node_If(new Twig_Node(array($ifexpr, $body)), null, $lineno, $tag); } parent::__construct(array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body, 'else' => $else), array('with_loop' => true, 'ifexpr' => null !== $ifexpr), $lineno, $tag); }
/** * Called after child nodes are visited. * * @param Twig_NodeInterface $node The node to visit * @param Twig_Environment $env The Twig environment instance * * @return Twig_NodeInterface The modified node */ public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $this->inAModule = false; $node->setNode('display_start', new Twig_Node(array(new Twig_Node_CheckSecurity($this->filters, $this->tags, $this->functions), $node->getNode('display_start')))); } return $node; }
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $node->setUsedFilters(array_keys($this->filters)); $node->setUsedTags(array_keys($this->tags)); $this->inAModule = false; } return $node; }
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) { if ($this->enabled && $node instanceof \Twig_Node_Expression_Filter) { $name = $node->getNode('filter')->getAttribute('value'); if ('desc' === $name || 'meaning' === $name) { return $this->enterNode($node->getNode('node'), $env); } } return $node; }
public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) { if ($node instanceof TwigJsNode) { if ($node->hasAttribute('name')) { $this->moduleNode->setAttribute('twig_js_name', $node->getAttribute('name')); } return false; } return $node; }
/** * Renders the asset URL using Symfony's asset() function. */ protected function getAssetUrlNode(\Twig_NodeInterface $body) { return new \Twig_Node_Expression_Function( new \Twig_Node_Expression_Name('asset', $body->getLine()), new \Twig_Node(array( new \Twig_Node_Expression_Constant($this->getAttribute('target_url'), $body->getLine()), )), $body->getLine() ); }
/** * @param \Twig_NodeInterface $node * @param \Twig_Environment $env * @return \Twig_NodeInterface */ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) { $this->stack[] = $node; if ($node instanceof TransNode) { $id = $node->getNode('body')->getAttribute('data'); $domain = 'messages'; if (null !== ($domainNode = $node->getNode('domain'))) { $domain = $domainNode->getAttribute('value'); } $message = new Message($id, $domain); $message->addSource(new FileSource((string) $this->file, $node->getLine())); $this->catalogue->add($message); } elseif ($node instanceof \Twig_Node_Expression_Filter) { $name = $node->getNode('filter')->getAttribute('value'); if ('trans' === $name || 'transchoice' === $name) { $idNode = $node->getNode('node'); if (!$idNode instanceof \Twig_Node_Expression_Constant) { return $node; // FIXME: see below // throw new \RuntimeException(sprintf('Cannot infer translation id from node "%s". Please refactor to only translate constants.', get_class($idNode))); } $id = $idNode->getAttribute('value'); $index = 'trans' === $name ? 1 : 2; $domain = 'messages'; $arguments = $node->getNode('arguments'); if ($arguments->hasNode($index)) { $argument = $arguments->getNode($index); if (!$argument instanceof \Twig_Node_Expression_Constant) { return $node; // FIXME: Throw exception if there is some way for the user to turn this off // on a case-by-case basis, similar to @Ignore in PHP } $domain = $argument->getAttribute('value'); } $message = new Message($id, $domain); $message->addSource(new FileSource((string) $this->file, $node->getLine())); for ($i = count($this->stack) - 2; $i >= 0; $i -= 1) { if (!$this->stack[$i] instanceof \Twig_Node_Expression_Filter) { break; } $name = $this->stack[$i]->getNode('filter')->getAttribute('value'); if ('desc' === $name || 'meaning' === $name) { $arguments = $this->stack[$i]->getNode('arguments'); if (!$arguments->hasNode(0)) { throw new RuntimeException(sprintf('The "%s" filter requires exactly one argument, the description text.', $name)); } $text = $arguments->getNode(0); if (!$text instanceof \Twig_Node_Expression_Constant) { throw new RuntimeException(sprintf('The first argument of the "%s" filter must be a constant expression, such as a string.', $name)); } $message->{'set' . $name}($text->getAttribute('value')); } elseif ('trans' === $name) { break; } } $this->catalogue->add($message); } } return $node; }
/** * {@inheritdoc} */ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Module) { $node->setAttribute(self::CONTEXT_REGIONS, $this->regions); $node->setAttribute(self::CONTEXT_INCLUDES, $this->includes); $module = new ContentRegionModule($node); $this->includes = []; $this->regions = []; return $module; } return $node; }
public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Module) { $this->currentModule = null; } elseif ($node instanceof \Twig_Node_Expression_Function) { $name = $node->getAttribute('name'); if ('render_documents' == $name) { $this->currentRenderDocuments = null; } } return $node; }
public function __construct(Twig_NodeInterface $node, Twig_Node_Expression_Constant $filterName, Twig_NodeInterface $arguments, $lineno, $tag = null) { $default = new Twig_Node_Expression_Filter($node, new Twig_Node_Expression_Constant('_default', $node->getLine()), $arguments, $node->getLine()); if ('default' === $filterName->getAttribute('value') && ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr)) { $test = new Twig_Node_Expression_Test_Defined(clone $node, 'defined', new Twig_Node(), $node->getLine()); $false = count($arguments) ? $arguments->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine()); $node = new Twig_Node_Expression_Conditional($test, $default, $false, $node->getLine()); } else { $node = $default; } parent::__construct($node, $filterName, $arguments, $lineno, $tag); }
public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno) { parent::__construct(array('node' => $node, 'arguments' => $arguments), array('name' => $name), $lineno); // defined is a special case if ('defined' === $name) { if ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr) { $node->setAttribute('is_defined_test', true); } else { throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine()); } } }
/** * {@inheritdoc} */ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) { if (!$this->enabled) { return $node; } if ($node instanceof \Twig_Node_Expression_Filter && 'trans' === $node->getNode('filter')->getAttribute('value') && $node->getNode('node') instanceof \Twig_Node_Expression_Constant) { // extract constant nodes with a trans filter $this->messages[] = array($node->getNode('node')->getAttribute('value'), $node->getNode('arguments')->hasNode(1) ? $node->getNode('arguments')->getNode(1)->getAttribute('value') : null); } elseif ($node instanceof TransNode) { // extract trans nodes $this->messages[] = array($node->getNode('body')->getAttribute('data'), $node->getNode('domain')->getAttribute('value')); } return $node; }
public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno) { if ($node instanceof Twig_Node_Expression_Name) { $node->setAttribute('is_defined_test', true); } elseif ($node instanceof Twig_Node_Expression_GetAttr) { $node->setAttribute('is_defined_test', true); $this->changeIgnoreStrictCheck($node); } elseif ($node instanceof Twig_Node_Expression_Constant || $node instanceof Twig_Node_Expression_Array) { $node = new Twig_Node_Expression_Constant(true, $node->getLine()); } else { throw new Twig_Error_Syntax('The "defined" test only works with simple variables.', $this->getLine()); } parent::__construct($node, $name, $arguments, $lineno); }
protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type) { if (false === $type) { return $node; } $expression = $node instanceof Twig_Node_Print ? $node->expr : $node; if ($expression instanceof Twig_Node_Expression_Filter) { // don't escape if the primary node of the filter is not a variable if (!$expression->node instanceof Twig_Node_Expression_GetAttr && !$expression->node instanceof Twig_Node_Expression_Name) { return $node; } // don't escape if there is already an "escaper" in the filter chain $filterMap = $env->getFilters(); for ($i = 0; $i < count($expression->filters); $i += 2) { $name = $expression->filters->{$i}['value']; if (isset($filterMap[$name]) && $filterMap[$name]->isEscaper()) { return $node; } } } elseif (!$expression instanceof Twig_Node_Expression_GetAttr && !$expression instanceof Twig_Node_Expression_Name) { // don't escape if the node is not a variable return $node; } // escape if ($expression instanceof Twig_Node_Expression_Filter) { // escape all variables in filters arguments for ($i = 0; $i < count($expression->filters); $i += 2) { foreach ($expression->filters->{$i + 1} as $j => $n) { $expression->filters->{$i + 1}->{$j} = $this->escapeNode($n, $env, $type); } } $filter = $this->getEscaperFilter($type, $expression->getLine()); $expression->prependFilter($filter[0], $filter[1]); return $node; } elseif ($node instanceof Twig_Node_Print) { return new Twig_Node_Print( new Twig_Node_Expression_Filter($expression, new Twig_Node($this->getEscaperFilter($type, $node->getLine())), $node->getLine()) , $node->getLine() ); } else { return new Twig_Node_Expression_Filter($node, new Twig_Node($this->getEscaperFilter($type, $node->getLine())), $node->getLine()); } }
/** * Optimizes "for" tag by removing the "loop" variable creation whenever possible. * * @param Twig_NodeInterface $node A Node * @param Twig_Environment $env The current Twig environment */ protected function enterOptimizeFor($node, $env) { if ($node instanceof Twig_Node_For) { // disable the loop variable by default $node->setAttribute('with_loop', false); array_unshift($this->loops, $node); } elseif (!$this->loops) { // we are outside a loop return; } elseif ($node instanceof Twig_Node_Expression_Name && 'loop' === $node->getAttribute('name')) { $this->addLoopToCurrent(); } elseif ($node instanceof Twig_Node_Include && !$node->getAttribute('only')) { $this->addLoopToAll(); } elseif ($node instanceof Twig_Node_Expression_GetAttr && (!$node->getNode('attribute') instanceof Twig_Node_Expression_Constant || 'parent' === $node->getNode('attribute')->getAttribute('value')) && (true === $this->loops[0]->getAttribute('with_loop') || $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' === $node->getNode('node')->getAttribute('name'))) { $this->addLoopToAll(); } }
/** * Called before child nodes are visited. * * @param \Twig_NodeInterface $node The node to visit * @param \Twig_Environment $env The Twig environment instance * * @return Twig_NodeInterface The modified node */ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Expression_Filter) { $name = $node->getNode('filter')->getAttribute('value'); if ('trans' === $name) { $node->setAttribute('file', $this->file->getRelativePathname()); $this->stack = $this->dataExtractor->collectDataFromTransFilterNode($node); } } if ($node instanceof \Twig_Node_Set) { $this->dataExtractor->addSetCollection($node); } if ($node instanceof \Twig_Node_For) { $this->dataExtractor->addForCollection($node); } return $node; }
protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars) { if ($body instanceof \Twig_Node_Expression_Constant) { $msg = $body->getAttribute('value'); } elseif ($body instanceof \Twig_Node_Text) { $msg = $body->getAttribute('data'); } else { return array($body, $vars); } preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches); if (version_compare(\Twig_Environment::VERSION, '1.5', '>=')) { foreach ($matches[1] as $var) { $key = new \Twig_Node_Expression_Constant('%' . $var . '%', $body->getLine()); if (!$vars->hasElement($key)) { $vars->addElement(new \Twig_Node_Expression_Name($var, $body->getLine()), $key); } } } else { $current = array(); foreach ($vars as $name => $var) { $current[$name] = true; } foreach ($matches[1] as $var) { if (!isset($current['%' . $var . '%'])) { $vars->setNode('%' . $var . '%', new \Twig_Node_Expression_Name($var, $body->getLine())); } } } return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars); }
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) { $this->stack[] = $node; if ($node instanceof \Twig_Node_Expression_Function) { $name = $node->getAttribute('name'); if (in_array($name, $this->methodNames)) { $args = $node->getNode('arguments'); switch ($name) { case '_n': case '_fn': $id = $args->getNode(0)->getAttribute('value') . '|' . $args->getNode(1)->getAttribute('value'); break; default: case '__f': case '__': $id = $args->getNode(0)->getAttribute('value'); break; } // obtain translation domain from composer file $composerPath = str_replace($this->file->getRelativePathname(), '', $this->file->getPathname()); if (isset(self::$domainCache[$composerPath])) { $domain = self::$domainCache[$composerPath]; } else { $scanner = new Scanner(); $scanner->scan(array($composerPath), 1); $metaData = $scanner->getModulesMetaData(true); $domains = array_keys($metaData); if (isset($domains[0])) { $domain = strtolower($domains[0]); // cache result of file lookup self::$domainCache[$composerPath] = $domain; } else { $domain = 'messages'; } } $domainNode = array_search($name, $this->methodNames); $domain = $args->hasNode($domainNode) ? $args->getNode($domainNode)->getAttribute('value') : $domain; $message = new Message($id, $domain); $message->addSource(new FileSource((string) $this->file, $node->getLine())); $this->catalogue->add($message); } } return $node; }
protected function checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node) { if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) { $attribute = $node->getNode('attribute'); if ($attribute instanceof Twig_Node_Expression_Constant && in_array($attribute->getAttribute('value'), array('length', 'revindex0', 'revindex', 'last'))) { throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition', $attribute->getAttribute('value')), $node->getLine(), $stream->getFilename()); } } // should check for parent.loop.XXX usage if ($node instanceof Twig_Node_For) { return; } foreach ($node as $n) { if (!$n) { continue; } $this->checkLoopUsageBody($stream, $n); } }
protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type) { if (false === $type) { return $node; } $expression = $node instanceof Twig_Node_Print ? $node->getExpression() : $node; if ($expression instanceof Twig_Node_Expression_Filter) { // don't escape if the primary node of the filter is not a variable $nodes = $expression->getNodes(); if (!$nodes[0] instanceof Twig_Node_Expression_Name) { return $node; } // don't escape if there is already an "escaper" in the filter chain $filterMap = $env->getFilters(); foreach ($expression->getFilters() as $filter) { if (isset($filterMap[$filter[0]]) && $filterMap[$filter[0]]->isEscaper()) { return $node; } } } elseif (!$expression instanceof Twig_Node_Expression_GetAttr && !$expression instanceof Twig_Node_Expression_Name) { // don't escape if the node is not a variable return $node; } // escape if ($expression instanceof Twig_Node_Expression_Filter) { // escape all variables in filters arguments $filters = $expression->getFilters(); foreach ($filters as $i => $filter) { foreach ($filter[1] as $j => $argument) { $filters[$i][1][$j] = $this->escapeNode($argument, $env, $type); } } $expression->setFilters($filters); $expression->prependFilter($this->getEscaperFilter($type)); return $node; } elseif ($node instanceof Twig_Node_Print) { return new Twig_Node_Print(new Twig_Node_Expression_Filter($expression, array($this->getEscaperFilter($type)), $node->getLine()), $node->getLine()); } else { return new Twig_Node_Expression_Filter($node, array($this->getEscaperFilter($type)), $node->getLine()); } }
protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type) { if (false === $type) { return $node; } $expression = $node instanceof Twig_Node_Print ? $node->expr : $node; if (!$expression instanceof Zwig_Node_Expression_ViewHelper) { return $node; } if ($this->isViewHelperEscaper($env, $expression->getHelper())) { return $node; } $escaped = $node instanceof Twig_Node_Print ? $expression : $node; $escaped = $this->getFilterNode($escaped, $this->getCastFilter($escaped->getLine())); $escaped = $this->getFilterNode($escaped, $this->getEscaperFilter($type, $escaped->getLine())); if ($node instanceof Twig_Node_Print) { return new Twig_Node_Print($escaped, $node->getLine()); } else { return $escaped; } }
protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars) { if ($body instanceof \Twig_Node_Expression_Constant) { $msg = $body->getAttribute('value'); } elseif ($body instanceof \Twig_Node_Text) { $msg = $body->getAttribute('data'); } else { return array($body, $vars); } $current = array(); foreach ($vars as $name => $var) { $current[$name] = true; } preg_match_all('/\\%([^\\%]+)\\%/', $msg, $matches); foreach ($matches[1] as $var) { if (!isset($current['%' . $var . '%'])) { $vars->setNode('%' . $var . '%', new \Twig_Node_Expression_Name($var, $body->getLine())); } } return array(new \Twig_Node_Expression_Constant(trim($msg), $body->getLine()), $vars); }