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); }
/** * 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_Module) { $this->inAModule = true; $this->tags = array(); $this->filters = array(); $this->functions = array(); return $node; } elseif ($this->inAModule) { // look for tags if ($node->getNodeTag() && !isset($this->tags[$node->getNodeTag()])) { $this->tags[$node->getNodeTag()] = $node; } // look for filters if ($node instanceof Twig_Node_Expression_Filter && !isset($this->filters[$node->getNode('filter')->getAttribute('value')])) { $this->filters[$node->getNode('filter')->getAttribute('value')] = $node; } // look for functions if ($node instanceof Twig_Node_Expression_Function && !isset($this->functions[$node->getAttribute('name')])) { $this->functions[$node->getAttribute('name')] = $node; } // 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_AutoEscape) { $this->statusStack[] = $node->getAttribute('value'); } elseif ($node instanceof Twig_Node_Block) { $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env); } return $node; }
/** * 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_Module) { if ($env->hasExtension('escaper') && ($defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename')))) { $this->defaultStrategy = $defaultStrategy; } } elseif ($node instanceof Twig_Node_AutoEscape) { $this->statusStack[] = $node->getAttribute('value'); } elseif ($node instanceof Twig_Node_Block) { $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env); } return $node; }
/** * {@inheritdoc} */ public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { $varName = $this->getVarName(); $node->setNode('display_start', new Twig_Node(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::TEMPLATE, $node->getAttribute('filename'), $varName), $node->getNode('display_start')))); $node->setNode('display_end', new Twig_Node(array(new Twig_Profiler_Node_LeaveProfile($varName), $node->getNode('display_end')))); } elseif ($node instanceof Twig_Node_Block) { $varName = $this->getVarName(); $node->setNode('body', new Twig_Node_Body(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::BLOCK, $node->getAttribute('name'), $varName), $node->getNode('body'), new Twig_Profiler_Node_LeaveProfile($varName)))); } elseif ($node instanceof Twig_Node_Macro) { $varName = $this->getVarName(); $node->setNode('body', new Twig_Node_Body(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::MACRO, $node->getAttribute('name'), $varName), $node->getNode('body'), new Twig_Profiler_Node_LeaveProfile($varName)))); } 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 enterNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Expression_Name) { print $node->getAttribute('name') . "\n"; } return $node; }
/** * Compiles a node. * * @param Twig_NodeInterface $node The node to compile * @param int $indentation The current indentation * * @return Twig_Compiler The current compiler instance */ public function compile(Twig_NodeInterface $node, $indentation = 0) { $this->lastLine = null; $this->source = ''; $this->debugInfo = array(); $this->sourceOffset = 0; // source code starts at 1 (as we then increment it when we encounter new lines) $this->sourceLine = 1; $this->indentation = $indentation; if ($node instanceof Twig_Node_Module) { $node->setFilename($node->getAttribute('filename')); // to be removed in 2.0 $this->filename = $node->getAttribute('filename'); } $node->compile($this); return $this; }
/** * Called after 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 leaveNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_AutoEscape || $node instanceof Twig_Node_Block) { array_pop($this->statusStack); } elseif ($node instanceof Twig_Node_BlockReference) { $this->blocks[$node->getAttribute('name')] = $this->needEscaping($env); } 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); 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); } } return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars); }
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; }
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); }
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; }
protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars, $ignoreStrictCheck = false) { 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); foreach ($matches[1] as $var) { $key = new \Twig_Node_Expression_Constant('%' . $var . '%', $body->getLine()); if (!$vars->hasElement($key)) { if ('count' === $var && null !== $this->getNode('count')) { $vars->addElement($this->getNode('count'), $key); } else { $varExpr = new \Twig_Node_Expression_Name($var, $body->getLine()); $varExpr->setAttribute('ignore_strict_check', $ignoreStrictCheck); $vars->addElement($varExpr, $key); } } } 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; }
/** * Extracts formulae from filter function nodes. * * @return array|null The formula */ private function checkNode(\Twig_NodeInterface $node, \Twig_Environment $env, &$name = null) { if ($node instanceof \Twig_Node_Expression_Function) { $name = $this->useNodeName ? $node->getNode('name')->getAttribute('name') : $node->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); } } }
/** * 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) { // in a sandbox tag, only include tags are allowed if ($node instanceof Twig_Node_Sandbox && !$node->getNode('body') instanceof Twig_Node_Include) { foreach ($node->getNode('body') as $n) { if ($n instanceof Twig_Node_Text && ctype_space($n->getAttribute('data'))) { continue; } if (!$n instanceof Twig_Node_Include) { throw new Twig_Error_Syntax('Only "include" tags are allowed within a "sandbox" section', $n->getLine()); } } } 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; }
protected function filterBodyNodes(Twig_NodeInterface $node) { // check that the body does not contain non-empty output nodes if ($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data')) || !$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface) { if (false !== strpos((string) $node, chr(0xef) . chr(0xbb) . chr(0xbf))) { throw new Twig_Error_Syntax('A template that extends another one cannot start with a byte order mark (BOM); it must be removed.', $node->getTemplateLine(), $this->stream->getSourceContext()->getName()); } throw new Twig_Error_Syntax('A template that extends another one cannot include contents outside Twig blocks. Did you forget to put the contents inside a {% block %} tag?', $node->getTemplateLine(), $this->stream->getSourceContext()->getName()); } // bypass "set" nodes as they "capture" the output if ($node instanceof Twig_Node_Set) { return $node; } if ($node instanceof Twig_NodeOutputInterface) { return; } foreach ($node as $k => $n) { if (null !== $n && null === $this->filterBodyNodes($n)) { $node->removeNode($k); } } return $node; }
protected function filterBodyNodes(Twig_NodeInterface $node) { // check that the body does not contain non-empty output nodes if ($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data')) || !$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface) { if (false !== strpos((string) $node, chr(0xef) . chr(0xbb) . chr(0xbf))) { throw new Twig_Error_Syntax('A template that extends another one cannot have a body but a byte order mark (BOM) has been detected; it must be removed.', $node->getLine(), $this->stream->getFilename()); } else { throw new Twig_Error_Syntax('A template that extends another one cannot have a body.', $node->getLine(), $this->stream->getFilename()); } } // bypass "set" nodes as they "capture" the output if ($node instanceof Twig_Node_Set) { return $node; } if ($node instanceof Twig_NodeOutputInterface) { return; } foreach ($node as $k => $n) { if (null !== $n && null === ($n = $this->filterBodyNodes($n))) { $node->removeNode($k); } } return $node; }
protected function filterBodyNodes(Twig_NodeInterface $node) { // check that the body does not contain non-empty output nodes if ($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data')) || !$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface) { throw new Twig_Error_Syntax(sprintf('A template that extends another one cannot have a body (%s).', $node), $node->getLine(), $this->stream->getFilename()); } if ($node instanceof Twig_NodeOutputInterface) { return null; } foreach ($node as $k => $n) { if (null !== $n && null === ($n = $this->filterBodyNodes($n))) { $node->removeNode($k); } } return $node; }