本文整理汇总了PHP中Twig_NodeInterface::getNode方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_NodeInterface::getNode方法的具体用法?PHP Twig_NodeInterface::getNode怎么用?PHP Twig_NodeInterface::getNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_NodeInterface
的用法示例。
在下文中一共展示了Twig_NodeInterface::getNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: leaveNode
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;
}
示例2: enterNode
/**
* @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($this->fileSourceFactory->create($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($this->fileSourceFactory->create($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;
}
示例3: checkNode
/**
* 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);
}
}
}
示例4: leaveNode
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;
}
示例5: enterNode
/**
* Defined by Twig_NodeVisitorInterface
*
* Extracts messages from calls to the translate function.
*/
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
if ($node instanceof \Skeleton\I18n\Template\Twig\Extension\Node\Trans\Tigron) {
if ($node->getNodeTag() == 'trans') {
$this->extracted[] = $node->getNode('body')->getAttribute('value');
}
} elseif ($node instanceof \Twig_Node_Print) {
$n = $node->getNode('expr');
while ($n instanceof \Twig_Node_Expression_Filter) {
$filter = null;
if ($n->hasNode('filter')) {
$filter = $n->getNode('filter')->getAttribute('value');
}
$n = $n->getNode('node');
if ($n instanceof \Twig_Node_Expression_Constant and $filter == 'trans') {
$this->extracted[] = $n->getAttribute('value');
}
}
} elseif ($node instanceof \Twig_Node_Expression_Array) {
$data = $node->getIterator();
foreach ($data as $row) {
if ($row instanceof \Twig_Node_Expression_Filter) {
if ($row->hasNode('filter') and $row->getNode('filter')->getAttribute('value') == 'trans') {
$this->extracted[] = $row->getNode('node')->getAttribute('value');
}
}
}
}
return $node;
}
示例6: enterNode
/**
* 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->getNode('filters')); $i += 2) {
$this->filters[] = $node->getNode('filters')->getNode($i)->getAttribute('value');
}
}
// look for simple print statements ({{ article }})
if ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_Name) {
return new Twig_Node_SandboxedPrint($node);
}
}
return $node;
}
示例7: enterNode
/**
* {@inheritdoc}
*/
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
if ($node instanceof \Twig_Node_Module) {
$this->domain = null;
}
if ($node instanceof TransDefaultDomainNode) {
$var = $env->getParser()->getVarName();
$name = new \Twig_Node_Expression_AssignName($var, $node->getLine());
$this->domain = new \Twig_Node_Expression_Name($var, $node->getLine());
return new \Twig_Node_Set(false, new \Twig_Node(array($name)), new \Twig_Node(array($node->getNode('expr'))), $node->getLine());
}
if (null === $this->domain) {
return $node;
}
if ($node instanceof \Twig_Node_Expression_Filter && in_array($node->getNode('filter')->getAttribute('value'), array('trans', 'transchoice'))) {
$ind = 'trans' === $node->getNode('filter')->getAttribute('value') ? 1 : 2;
$arguments = $node->getNode('arguments');
if (!$arguments->hasNode($ind)) {
if (!$arguments->hasNode($ind - 1)) {
$arguments->setNode($ind - 1, new \Twig_Node_Expression_Array(array(), $node->getLine()));
}
$arguments->setNode($ind, $this->domain);
}
} elseif ($node instanceof TransNode) {
if (null === $node->getNode('domain')) {
$node->setNode('domain', $this->domain);
}
}
return $node;
}
示例8: optimizeRawFilter
/**
* Removes "raw" filters.
*
* @param Twig_NodeInterface $node A Node
* @param Twig_Environment $env The current Twig environment
*/
protected function optimizeRawFilter($node, $env)
{
if ($node instanceof Twig_Node_Expression_Filter && 'raw' == $node->getNode('filter')->getAttribute('value')) {
return $node->getNode('node');
}
return $node;
}
示例9: enterNode
/**
* 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;
}
示例10: enterNode
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;
}
示例11: leaveNode
/**
* {@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;
}
示例12: leaveNode
/**
* @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;
}
示例13: leaveNode
/**
* 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;
}
示例14: enterNode
/**
* {@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'), $this->getReadDomainFromArguments($node->getNode('arguments'), 1));
} elseif ($node instanceof \Twig_Node_Expression_Filter && 'transchoice' === $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'), $this->getReadDomainFromArguments($node->getNode('arguments'), 2));
} elseif ($node instanceof TransNode) {
// extract trans nodes
$this->messages[] = array($node->getNode('body')->getAttribute('data'), $this->getReadDomainFromNode($node->getNode('domain')));
}
return $node;
}
示例15: enterNode
/**
* @param \Twig_NodeInterface $node
* @param \Twig_Environment $env
* @return \Twig_NodeInterface
*/
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
if (!$this->enabled) {
return $node;
}
if ($node instanceof \Twig_Node_Expression_Filter && 'desc' === $node->getNode('filter')->getAttribute('value')) {
$transNode = $node->getNode('node');
while ($transNode instanceof \Twig_Node_Expression_Filter && 'trans' !== $transNode->getNode('filter')->getAttribute('value') && 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')) {
$transNode = $transNode->getNode('node');
}
if (!$transNode instanceof \Twig_Node_Expression_Filter) {
throw new RuntimeException(sprintf('The "desc" filter must be applied after a "trans", or "transchoice" filter.'));
}
$wrappingNode = $node->getNode('node');
$testNode = clone $wrappingNode;
$defaultNode = $node->getNode('arguments')->getNode(0);
// if the |transchoice filter is used, delegate the call to the TranslationExtension
// so that we can catch a possible exception when the default translation has not yet
// been extracted
if ('transchoice' === $transNode->getNode('filter')->getAttribute('value')) {
$transchoiceArguments = new \Twig_Node_Expression_Array(array(), $transNode->getLine());
$transchoiceArguments->addElement($wrappingNode->getNode('node'));
$transchoiceArguments->addElement($defaultNode);
foreach ($wrappingNode->getNode('arguments') as $arg) {
$transchoiceArguments->addElement($arg);
}
$transchoiceNode = new \Twig_Node_Expression_MethodCall(new \Twig_Node_Expression_ExtensionReference('jms_translation', $transNode->getLine()), 'transchoiceWithDefault', $transchoiceArguments, $transNode->getLine());
$node->setNode('node', $transchoiceNode);
return $node;
}
// if the |trans filter has replacements parameters
// (e.g. |trans({'%foo%': 'bar'}))
if ($wrappingNode->getNode('arguments')->hasNode(0)) {
$lineno = $wrappingNode->getLine();
// remove the replacements from the test node
$testNode->setNode('arguments', clone $testNode->getNode('arguments'));
$testNode->getNode('arguments')->setNode(0, new \Twig_Node_Expression_Array(array(), $lineno));
// wrap the default node in a |replace filter
$defaultNode = new \Twig_Node_Expression_Filter(clone $node->getNode('arguments')->getNode(0), new \Twig_Node_Expression_Constant('replace', $lineno), new \Twig_Node(array(clone $wrappingNode->getNode('arguments')->getNode(0))), $lineno);
}
$condition = new \Twig_Node_Expression_Conditional(new \Twig_Node_Expression_Binary_Equal($testNode, $transNode->getNode('node'), $wrappingNode->getLine()), $defaultNode, clone $wrappingNode, $wrappingNode->getLine());
$node->setNode('node', $condition);
}
return $node;
}