public function visit(Twig_Node $node) { // autoescape? if ($node instanceof Twig_Node_AutoEscape) { $this->statusStack[] = $node->getValue(); $node = $this->visitDeep($node); array_pop($this->statusStack); // remove the node return $node; } if (!$node instanceof Twig_Node_Print) { return $this->visitDeep($node); } if (false === $this->needEscaping()) { return $node; } $expression = $node->getExpression(); // don't escape if escape has already been called // or if we want the safe string if ($expression instanceof Twig_Node_Expression_Filter && ($expression->hasFilter('escape') || $expression->hasFilter('safe'))) { return $node; } // escape if ($expression instanceof Twig_Node_Expression_Filter) { $expression->appendFilter(array('escape', array())); return $node; } else { return new Twig_Node_Print(new Twig_Node_Expression_Filter($expression, array(array('escape', array())), $node->getLine()), $node->getLine()); } }
/** * Loads assets from the supplied node. * * @return array An array of asset formulae indexed by name */ private function loadNode(\Twig_Node $node) { $formulae = array(); if ($node instanceof AsseticNode) { $formulae[$node->getAttribute('name')] = array($node->getAttribute('inputs'), $node->getAttribute('filters'), array('output' => $node->getAttribute('asset')->getTargetPath(), 'name' => $node->getAttribute('name'), 'debug' => $node->getAttribute('debug'), 'combine' => $node->getAttribute('combine'))); } elseif ($node instanceof \Twig_Node_Expression_Function) { $name = $node->getNode('name')->getAttribute('name'); if ($this->twig->getFunction($name) instanceof AsseticFilterFunction) { $arguments = array(); foreach ($node->getNode('arguments') as $argument) { $arguments[] = eval('return ' . $this->twig->compile($argument) . ';'); } $invoker = $this->twig->getExtension('assetic')->getFilterInvoker($name); $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'] = $invoker->getFactory()->generateAssetName($inputs, $filters, $options); } $formulae[$options['name']] = array($inputs, $filters, $options); } } foreach ($node as $child) { if ($child instanceof \Twig_Node) { $formulae += $this->loadNode($child); } } return $formulae; }
public function visit(Twig_Node $node) { // filter? if ($node instanceof Twig_Node_Filter) { $this->statusStack[] = $node->getFilters(); $node = $this->visitDeep($node); array_pop($this->statusStack); return $node; } if (!$node instanceof Twig_Node_Print && !$node instanceof Twig_Node_Text) { return $this->visitDeep($node); } if (false === ($filters = $this->getCurrentFilters())) { return $node; } if ($node instanceof Twig_Node_Text) { $expression = new Twig_Node_Expression_Constant($node->getData(), $node->getLine()); } else { $expression = $node->getExpression(); } // filters if ($expression instanceof Twig_Node_Expression_Filter) { $expression->appendFilters($filters); return $node; } else { return new Twig_Node_Print(new Twig_Node_Expression_Filter($expression, $filters, $node->getLine()), $node->getLine()); } }
private function loadNode(Node $node, $resource) { $assets = array(); if ($node instanceof ExpressionFunction) { $name = $node->getAttribute('name'); if ($name === $this->functionName) { $arguments = iterator_to_array($node->getNode('arguments')); if (!is_array($arguments)) { throw new ResourceParsingException('arguments is not an array'); } if (count($arguments) !== 1 && count($arguments) !== 2) { throw new ResourceParsingException(sprintf('Expected exactly one or two arguments passed to function %s in %s at line %s', $this->functionName, $resource, $node->getLine())); } if (!$arguments[0] instanceof ConstantFunction) { throw new ResourceParsingException(sprintf('Argument passed to function %s must be text node to parse without context. File %s, line %s', $this->functionName, $resource, $node->getLine())); } $assets[] = $arguments[0]->getAttribute('value'); return $assets; } } foreach ($node as $child) { if ($child instanceof Node) { $assets = array_merge($assets, $this->loadNode($child, $resource)); } } return $assets; }
/** * Removes node filters. * * This is mostly needed when another visitor adds filters (like the escaper one). * * @param Twig_Node $node A Node */ protected function removeNodeFilter($node) { if ($node instanceof Twig_Node_Expression_Filter) { return $this->removeNodeFilter($node->getNode('node')); } return $node; }
/** * Called after child nodes are visited. * * @param Twig_Node $node The node to visit * @param Twig_Environment $env The Twig environment instance * * @return Twig_Node The modified node */ public function leaveNode(Twig_Node $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; }
/** * Determines at compile time whether the generated URL will be safe and thus * saving the unneeded automatic escaping for performance reasons. * * The URL generation process percent encodes non-alphanumeric characters. So there is no risk * that malicious/invalid characters are part of the URL. The only character within an URL that * must be escaped in html is the ampersand ("&") which separates query params. So we cannot mark * the URL generation as always safe, but only when we are sure there won't be multiple query * params. This is the case when there are none or only one constant parameter given. * E.g. we know beforehand this will be safe: * - path('route') * - path('route', {'param': 'value'}) * But the following may not: * - path('route', var) * - path('route', {'param': ['val1', 'val2'] }) // a sub-array * - path('route', {'param1': 'value1', 'param2': 'value2'}) * If param1 and param2 reference placeholder in the route, it would still be safe. But we don't know. * * @param \Twig_Node $argsNode The arguments of the path/url function * * @return array An array with the contexts the URL is safe */ public function isUrlGenerationSafe(\Twig_Node $argsNode) { // support named arguments $paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : ($argsNode->hasNode(1) ? $argsNode->getNode(1) : null); if (null === $paramsNode || $paramsNode instanceof \Twig_Node_Expression_Array && count($paramsNode) <= 2 && (!$paramsNode->hasNode(1) || $paramsNode->getNode(1) instanceof \Twig_Node_Expression_Constant)) { return array('html'); } return array(); }
public function leaveNode(Twig_Node $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; }
/** * Find placeholder nodes recursively. * * @param Twig_Node $node * @param array $placeholders */ protected function getVariables($node, &$placeholders) { if ($node instanceof Curry_Twig_Node_Placeholder) { $placeholders[$node->getAttribute('name')] = true; } foreach ($node->getIterator() as $n) { if ($n instanceof Twig_Node) { $this->getVariables($n, $placeholders); } } }
protected function compileString(\Twig_Node $body) { if ($body instanceof \Twig_Node_Expression_Constant) { $msg = $body->getAttribute('value'); } elseif ($body instanceof \Twig_Node_Text) { $msg = $body->getAttribute('data'); } else { return $body; } return new \Twig_Node_Expression_Constant(trim($msg), $body->getLine()); }
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Include) { if ($node->hasNode('expr') && $node->getNode('expr')->hasAttribute('value')) { $patternStoreKey = $node->getNode('expr')->getAttribute('value'); $data = Data::getPatternSpecificData($patternStoreKey); $dataNode = new PatternDataIncludeNode($node, $data); return $dataNode; } } return $node; }
public function __construct(Twig_Node $node, Twig_Node_Expression_Constant $filterName, Twig_Node $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_Node $node, $name, Twig_Node $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 instanceof Twig_Node_Expression_GetProperty || $node instanceof Twig_Node_Expression_MethodCall) { $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 visitDeep(Twig_Node $node) { if (!$node instanceof Twig_NodeListInterface) { return $node; } $newNodes = array(); foreach ($nodes = $node->getNodes() as $k => $n) { if (null !== ($n = $this->visit($n))) { $newNodes[$k] = $n; } } $node->setNodes($newNodes); return $node; }
public function __construct(\Twig_Node $left, \Twig_Node $right, $lineno) { parent::__construct(array('left' => $left, 'right' => $right), array(), $lineno); if ($left instanceof \Twig_Node_Expression_Name) { $this->arguments = [$left->getAttribute('name')]; } elseif ($left instanceof Arguments) { $this->arguments = $left->getArguments(); } else { throw new \InvalidArgumentException('Invalid argument\'s list for lambda.'); } if (count($this->arguments) !== count(array_flip($this->arguments))) { throw new \InvalidArgumentException('Each lambda argument must have unique name.'); } }
/** * @param string $lineno * @param string $tag * @param string $key * @param string $lifetime * @param string $data * @author Jonathan Geiger */ public function __construct($lineno, $tag, $key, $lifetime, $data) { parent::__construct($lineno); $this->key = $key; $this->data = $data; $this->lifetime = $lifetime; }
public function __construct($count, Twig_NodeList $body, $plural, $lineno, $tag = null) { parent::__construct($lineno, $tag); $this->count = $count; $this->body = $body; $this->plural = $plural; }
public function __construct($name, $imitates, $settings, $lineNo) { parent::__construct(array(), array(), $lineNo); $this->name = $name; $this->imitates = $imitates; $this->settings = $settings; }
public function __construct($isMultitarget, $names, $values, $lineno, $tag = null) { parent::__construct($lineno, $tag); $this->isMultitarget = $isMultitarget; $this->names = $names; $this->values = $values; }
public function __construct($manifest, $config, $files, $line, $tag = null) { parent::__construct(array(), array(), $line, $tag); $this->manifest = $manifest; $this->config = $config; $this->files = $files; }
public function __construct(Context $context, $type, $resource, $lineno, $tag) { parent::__construct([], [], $lineno, $tag); $this->type = $type; $this->resource = $resource; $this->context = $context; }
public function __construct(array $usedFilters, array $usedTags, array $usedFunctions) { $this->usedFilters = $usedFilters; $this->usedTags = $usedTags; $this->usedFunctions = $usedFunctions; parent::__construct(); }
public function __construct($name, $vars, $lineNo) { parent::__construct(array(), array(), $lineNo); $this->name = $name; $this->vars = $vars; }
/** * @param array $entityType * @param \Twig_NodeInterface $ids * @param string $viewMode * @param null $langcode * @param null $page * @param int $lineno * @param null $tag */ public function __construct($entityType, \Twig_NodeInterface $ids, $viewMode = 'full', $langcode = null, $page = null, $lineno = 0, $tag = null) { $nodes = array(); // Load the entities by type and an array of IDs. // Assemble arguments for entity_load(). $argumentsNodes = array(); $argumentsNodes[] = $entityTypeNode = new \Twig_Node_Expression_Constant($entityType, $lineno); $argumentsNodes[] = $ids; // Construct call to entity_load() and capture the result in temporary // variable named `entities`. $valuesNode = new \Twig_Node(array(new \Twig_Node_Expression_Function('entity_load', new \Twig_Node($argumentsNodes), $lineno))); $entitiesNamesNode = new \Twig_Node(array(new \Twig_Node_Expression_TempName('entities', $lineno))); $nodes[] = new \Twig_Node_Set(false, $entitiesNamesNode, $valuesNode, $lineno); // Assemble arguments for entity_view() $argumentsNodes = array(); $argumentsNodes[] = $entityTypeNode; $argumentsNodes[] = $entitiesNamesNode; $argumentsNodes[] = new \Twig_Node_Expression_Constant($viewMode, $lineno); $argumentsNodes[] = new \Twig_Node_Expression_Constant($langcode, $lineno); $argumentsNodes[] = new \Twig_Node_Expression_Constant($page, $lineno); // Construct call to entity_view() and capture the result in // temporary variable named `output`. $valuesNode = new \Twig_Node(array(new \Twig_Node_Expression_Function('entity_view', new \Twig_Node($argumentsNodes), $lineno))); $outputNamesNode = new \Twig_Node(array(new \Twig_Node_Expression_TempName('output', $lineno))); $nodes[] = new \Twig_Node_Set(false, $outputNamesNode, $valuesNode, $lineno); // Assemble arguments for drupal_render(). $argumentsNodes = array(); $argumentsNodes[] = $outputNamesNode; // Construct call to drupal_render() and echo the result. $expr = new \Twig_Node_Expression_Function('drupal_render', new \Twig_Node($argumentsNodes), $lineno); $nodes[] = new \Twig_Node_Print($expr, $lineno); // Pass the original arguments up. $attributes = array('entity_type' => $entityType, 'view_mode' => $viewMode, 'langcode' => $langcode, 'page' => $page); parent::__construct($nodes, $attributes, $lineno, $tag); }
public function __construct(Twig_Node_Expression $expr, $sandboxed, $variables, $lineno, $tag = null) { parent::__construct($lineno, $tag); $this->expr = $expr; $this->sandboxed = $sandboxed; $this->variables = $variables; }
/** * Constructor * * @param string $lineno * @param string $method * @param string $files * @param string $options * @author Jonathan Geiger */ public function __construct($lineno, $method, $files, $options = NULL) { parent::__construct($lineno); $this->method = $method; $this->files = $files; $this->options = $options; }
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Include) { if ($node->hasNode('expr') && $node->getNode('expr')->hasAttribute('value')) { $patternStoreKey = $node->getNode('expr')->getAttribute('value'); $data = $this->dt->getProcessedPatternSpecificData($patternStoreKey); if ($node instanceof \Twig_Node_Embed) { $dataNode = new PatternDataEmbedNode($node, $data); } else { $dataNode = new PatternDataIncludeNode($node, $data); } return $dataNode; } } return $node; }
public function __construct($name, Twig_NodeList $body, $lineno, $parent = null, $tag = null) { parent::__construct($lineno, $tag); $this->name = $name; $this->body = $body; $this->parent = $parent; }
public function __construct(AssetInterface $asset, \Twig_NodeInterface $body, $attributes, $lineno = 0, $tag = null) { $nodes = array('body' => $body); $attributes['asset'] = $asset; $attributes['var_name'] = 'asset_url'; parent::__construct($nodes, $attributes, $lineno, $tag); }
public function __construct($isMultitarget, $names, Twig_Node_Expression $value, $lineno, $tag = null) { parent::__construct($lineno, $tag); $this->isMultitarget = $isMultitarget; $this->names = $names; $this->value = $value; }