Spade
Mini Shell
| Directory:~$ /home/lmsyaran/public_html/joomla4/ |
| [Home] [System Details] [Kill Me] |
Component.php000064400000004072151157423710007230 0ustar00<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All
rights reserved.
* @license GNU General Public License version 2 or later; see
LICENSE.txt
*/
namespace Joomla\CMS\Menu\Node;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Menu\Node;
/**
* A Component type of node for MenuTree
*
* @see Node
*
* @since 3.8.0
*/
class Component extends Node
{
/**
* Node Title
*
* @var string
*
* @since 3.8.0
*/
protected $title = null;
/**
* The component name for this node link
*
* @var string
*
* @since 3.8.0
*/
protected $element = null;
/**
* Node Link
*
* @var string
*
* @since 3.8.0
*/
protected $link = null;
/**
* Link Target
*
* @var string
*
* @since 3.8.0
*/
protected $target = null;
/**
* Link title icon
*
* @var string
*
* @since 3.8.0
*/
protected $icon = null;
/**
* Constructor for the class.
*
* @param string $title The title of the node
* @param string $element The component name
* @param string $link The node link
* @param string $target The link target
* @param string $class The CSS class for the node
* @param string $id The node id
* @param string $icon The title icon for the node
*
* @since 3.8.0
*/
public function __construct($title, $element, $link, $target = null,
$class = null, $id = null, $icon = null)
{
$this->title = $title;
$this->element = $element;
$this->link = $link ? \JFilterOutput::ampReplace($link) :
'index.php?option=' . $element;
$this->target = $target;
$this->class = $class;
$this->id = $id;
$this->icon = $icon;
parent::__construct();
}
/**
* Get an attribute value
*
* @param string $name The attribute name
*
* @return mixed
*
* @since 3.8.0
*/
public function get($name)
{
switch ($name)
{
case 'title':
case 'element':
case 'link':
case 'target':
case 'icon':
return $this->$name;
}
return parent::get($name);
}
}
Container.php000064400000000634151157423710007210 0ustar00<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All
rights reserved.
* @license GNU General Public License version 2 or later; see
LICENSE.txt
*/
namespace Joomla\CMS\Menu\Node;
defined('JPATH_PLATFORM') or die;
/**
* A Container type of node for MenuTree
*
* @see Node
*
* @since 3.8.0
*/
class Container extends Heading
{
}
Heading.php000064400000002732151157423710006626 0ustar00<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All
rights reserved.
* @license GNU General Public License version 2 or later; see
LICENSE.txt
*/
namespace Joomla\CMS\Menu\Node;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Menu\Node;
/**
* A Heading type of node for MenuTree
*
* @see Node
*
* @since 3.8.0
*/
class Heading extends Node
{
/**
* Node Title
*
* @var string
*
* @since 3.8.0
*/
protected $title = null;
/**
* Node Link
*
* @var string
*
* @since 3.8.0
*/
protected $link = '#';
/**
* Link title icon
*
* @var string
*
* @since 3.8.0
*/
protected $icon = null;
/**
* Constructor for the class.
*
* @param string $title The title of the node
* @param string $class The CSS class for the node
* @param string $id The node id
* @param string $icon The title icon for the node
*
* @since 3.8.0
*/
public function __construct($title, $class = null, $id = null, $icon =
null)
{
$this->title = $title;
$this->class = $class;
$this->id = $id;
$this->icon = $icon;
parent::__construct();
}
/**
* Get an attribute value
*
* @param string $name The attribute name
*
* @return mixed
*
* @since 3.8.0
*/
public function get($name)
{
switch ($name)
{
case 'title':
case 'link':
case 'icon':
return $this->$name;
}
return parent::get($name);
}
}
Separator.php000064400000002026151157423710007223 0ustar00<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All
rights reserved.
* @license GNU General Public License version 2 or later; see
LICENSE.txt
*/
namespace Joomla\CMS\Menu\Node;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Menu\Node;
/**
* A Separator type of node for MenuTree
*
* @see Node
*
* @since 3.8.0
*/
class Separator extends Node
{
/**
* Node Title
*
* @var string
*
* @since 3.8.0
*/
protected $title = null;
/**
* Constructor for the class.
*
* @param string $title The title of the node
*
* @since 3.8.0
*/
public function __construct($title = null)
{
$this->title = trim($title, '- ') ? $title : null;
parent::__construct();
}
/**
* Get an attribute value
*
* @param string $name The attribute name
*
* @return mixed
*
* @since 3.8.0
*/
public function get($name)
{
switch ($name)
{
case 'title':
return $this->$name;
}
return parent::get($name);
}
}
Url.php000064400000003432151157423710006027 0ustar00<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All
rights reserved.
* @license GNU General Public License version 2 or later; see
LICENSE.txt
*/
namespace Joomla\CMS\Menu\Node;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Menu\Node;
/**
* An external Url type of node for MenuTree
*
* @see Node
*
* @since 3.8.0
*/
class Url extends Node
{
/**
* Node Title
*
* @var string
*
* @since 3.8.0
*/
protected $title = null;
/**
* Node Link
*
* @var string
*
* @since 3.8.0
*/
protected $link = null;
/**
* Link Target
*
* @var string
*
* @since 3.8.0
*/
protected $target = null;
/**
* Link title icon
*
* @var string
*
* @since 3.8.0
*/
protected $icon = null;
/**
* Constructor for the class.
*
* @param string $title The title of the node
* @param string $link The node link
* @param string $target The link target
* @param string $class The CSS class for the node
* @param string $id The node id
* @param string $icon The title icon for the node
*
* @since 3.8.0
*/
public function __construct($title, $link, $target = null, $class = null,
$id = null, $icon = null)
{
$this->title = $title;
$this->link = \JFilterOutput::ampReplace($link);
$this->target = $target;
$this->class = $class;
$this->id = $id;
$this->icon = $icon;
parent::__construct();
}
/**
* Get an attribute value
*
* @param string $name The attribute name
*
* @return mixed
*
* @since 3.8.0
*/
public function get($name)
{
switch ($name)
{
case 'title':
case 'link':
case 'target':
case 'icon':
return $this->$name;
}
return parent::get($name);
}
}
AutoEscapeNode.php000064400000001627151163450570010131 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* Represents an autoescape node.
*
* The value is the escaping strategy (can be html, js, ...)
*
* The true value is equivalent to html.
*
* If autoescaping is disabled, then the value is false.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class AutoEscapeNode extends Node
{
public function __construct($value, \Twig_NodeInterface $body, $lineno,
$tag = 'autoescape')
{
parent::__construct(['body' => $body],
['value' => $value], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler->subcompile($this->getNode('body'));
}
}
class_alias('Twig\Node\AutoEscapeNode',
'Twig_Node_AutoEscape');
BlockNode.php000064400000002005151163450570007121 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* Represents a block node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class BlockNode extends Node
{
public function __construct($name, \Twig_NodeInterface $body, $lineno,
$tag = null)
{
parent::__construct(['body' => $body],
['name' => $name], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write(sprintf("public function block_%s(\$context,
array \$blocks = [])\n", $this->getAttribute('name')),
"{\n")
->indent()
;
$compiler
->subcompile($this->getNode('body'))
->outdent()
->write("}\n\n")
;
}
}
class_alias('Twig\Node\BlockNode', 'Twig_Node_Block');
BlockReferenceNode.php000064400000001561151163450570010746 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* Represents a block call node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class BlockReferenceNode extends Node implements NodeOutputInterface
{
public function __construct($name, $lineno, $tag = null)
{
parent::__construct([], ['name' => $name], $lineno,
$tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write(sprintf("\$this->displayBlock('%s', \$context,
\$blocks);\n", $this->getAttribute('name')))
;
}
}
class_alias('Twig\Node\BlockReferenceNode',
'Twig_Node_BlockReference');
BodyNode.php000064400000000615151163450600006763 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
/**
* Represents a body node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class BodyNode extends Node
{
}
class_alias('Twig\Node\BodyNode', 'Twig_Node_Body');
CheckSecurityNode.php000064400000006025151163450620010636 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class CheckSecurityNode extends Node
{
protected $usedFilters;
protected $usedTags;
protected $usedFunctions;
public function __construct(array $usedFilters, array $usedTags, array
$usedFunctions)
{
$this->usedFilters = $usedFilters;
$this->usedTags = $usedTags;
$this->usedFunctions = $usedFunctions;
parent::__construct();
}
public function compile(Compiler $compiler)
{
$tags = $filters = $functions = [];
foreach (['tags', 'filters',
'functions'] as $type) {
foreach ($this->{'used'.ucfirst($type)} as $name
=> $node) {
if ($node instanceof Node) {
${$type}[$name] = $node->getTemplateLine();
} else {
${$type}[$node] = null;
}
}
}
$compiler
->write("\$this->sandbox =
\$this->env->getExtension('\Twig\Extension\SandboxExtension');\n")
->write('$tags =
')->repr(array_filter($tags))->raw(";\n")
->write('$filters =
')->repr(array_filter($filters))->raw(";\n")
->write('$functions =
')->repr(array_filter($functions))->raw(";\n\n")
->write("try {\n")
->indent()
->write("\$this->sandbox->checkSecurity(\n")
->indent()
->write(!$tags ? "[],\n" :
"['".implode("', '",
array_keys($tags))."'],\n")
->write(!$filters ? "[],\n" :
"['".implode("', '",
array_keys($filters))."'],\n")
->write(!$functions ? "[]\n" :
"['".implode("', '",
array_keys($functions))."']\n")
->outdent()
->write(");\n")
->outdent()
->write("} catch (SecurityError \$e) {\n")
->indent()
->write("\$e->setSourceContext(\$this->getSourceContext());\n\n")
->write("if (\$e instanceof SecurityNotAllowedTagError
&& isset(\$tags[\$e->getTagName()])) {\n")
->indent()
->write("\$e->setTemplateLine(\$tags[\$e->getTagName()]);\n")
->outdent()
->write("} elseif (\$e instanceof
SecurityNotAllowedFilterError &&
isset(\$filters[\$e->getFilterName()])) {\n")
->indent()
->write("\$e->setTemplateLine(\$filters[\$e->getFilterName()]);\n")
->outdent()
->write("} elseif (\$e instanceof
SecurityNotAllowedFunctionError &&
isset(\$functions[\$e->getFunctionName()])) {\n")
->indent()
->write("\$e->setTemplateLine(\$functions[\$e->getFunctionName()]);\n")
->outdent()
->write("}\n\n")
->write("throw \$e;\n")
->outdent()
->write("}\n\n")
;
}
}
class_alias('Twig\Node\CheckSecurityNode',
'Twig_Node_CheckSecurity');
CheckToStringNode.php000064400000002163151163450620010577 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
/**
* Checks if casting an expression to __toString() is allowed by the
sandbox.
*
* For instance, when there is a simple Print statement, like {{ article
}},
* and if the sandbox is enabled, we need to check that the __toString()
* method is allowed if 'article' is an object. The same goes for
{{ article|upper }}
* or {{ random(article) }}
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class CheckToStringNode extends AbstractExpression
{
public function __construct(AbstractExpression $expr)
{
parent::__construct(['expr' => $expr], [],
$expr->getTemplateLine(), $expr->getNodeTag());
}
public function compile(Compiler $compiler)
{
$compiler
->raw('$this->sandbox->ensureToStringAllowed(')
->subcompile($this->getNode('expr'))
->raw(')')
;
}
}
DeprecatedNode.php000064400000002632151163450630010132 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\ConstantExpression;
/**
* Represents a deprecated node.
*
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class DeprecatedNode extends Node
{
public function __construct(AbstractExpression $expr, $lineno, $tag =
null)
{
parent::__construct(['expr' => $expr], [], $lineno,
$tag);
}
public function compile(Compiler $compiler)
{
$compiler->addDebugInfo($this);
$expr = $this->getNode('expr');
if ($expr instanceof ConstantExpression) {
$compiler->write('@trigger_error(')
->subcompile($expr);
} else {
$varName = $compiler->getVarName();
$compiler->write(sprintf('$%s = ', $varName))
->subcompile($expr)
->raw(";\n")
->write(sprintf('@trigger_error($%s',
$varName));
}
$compiler
->raw('.')
->string(sprintf(' ("%s" at line %d).',
$this->getTemplateName(), $this->getTemplateLine()))
->raw(", E_USER_DEPRECATED);\n")
;
}
}
class_alias('Twig\Node\DeprecatedNode',
'Twig_Node_Deprecated');
DoNode.php000064400000001502151163450630006427 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
/**
* Represents a do node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class DoNode extends Node
{
public function __construct(AbstractExpression $expr, $lineno, $tag =
null)
{
parent::__construct(['expr' => $expr], [], $lineno,
$tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write('')
->subcompile($this->getNode('expr'))
->raw(";\n")
;
}
}
class_alias('Twig\Node\DoNode', 'Twig_Node_Do');
EmbedNode.php000064400000003015151163450630007102 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\ConstantExpression;
/**
* Represents an embed node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class EmbedNode extends IncludeNode
{
// we don't inject the module to avoid node visitors to traverse
it twice (as it will be already visited in the main module)
public function __construct($name, $index, AbstractExpression
$variables = null, $only = false, $ignoreMissing = false, $lineno, $tag =
null)
{
parent::__construct(new ConstantExpression('not_used',
$lineno), $variables, $only, $ignoreMissing, $lineno, $tag);
$this->setAttribute('name', $name);
// to be removed in 2.0, used name instead
$this->setAttribute('filename', $name);
$this->setAttribute('index', $index);
}
protected function addGetTemplate(Compiler $compiler)
{
$compiler
->write('$this->loadTemplate(')
->string($this->getAttribute('name'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(', ')
->string($this->getAttribute('index'))
->raw(')')
;
}
}
class_alias('Twig\Node\EmbedNode', 'Twig_Node_Embed');
Expression/AbstractExpression.php000064400000001025151163450630013241
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Node\Node;
/**
* Abstract class for all nodes that represents an expression.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class AbstractExpression extends Node
{
}
class_alias('Twig\Node\Expression\AbstractExpression',
'Twig_Node_Expression');
Expression/ArrayExpression.php000064400000004415151163450630012562
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
class ArrayExpression extends AbstractExpression
{
protected $index;
public function __construct(array $elements, $lineno)
{
parent::__construct($elements, [], $lineno);
$this->index = -1;
foreach ($this->getKeyValuePairs() as $pair) {
if ($pair['key'] instanceof ConstantExpression
&& ctype_digit((string)
$pair['key']->getAttribute('value')) &&
$pair['key']->getAttribute('value') >
$this->index) {
$this->index =
$pair['key']->getAttribute('value');
}
}
}
public function getKeyValuePairs()
{
$pairs = [];
foreach (array_chunk($this->nodes, 2) as $pair) {
$pairs[] = [
'key' => $pair[0],
'value' => $pair[1],
];
}
return $pairs;
}
public function hasElement(AbstractExpression $key)
{
foreach ($this->getKeyValuePairs() as $pair) {
// we compare the string representation of the keys
// to avoid comparing the line numbers which are not relevant
here.
if ((string) $key === (string) $pair['key']) {
return true;
}
}
return false;
}
public function addElement(AbstractExpression $value,
AbstractExpression $key = null)
{
if (null === $key) {
$key = new ConstantExpression(++$this->index,
$value->getTemplateLine());
}
array_push($this->nodes, $key, $value);
}
public function compile(Compiler $compiler)
{
$compiler->raw('[');
$first = true;
foreach ($this->getKeyValuePairs() as $pair) {
if (!$first) {
$compiler->raw(', ');
}
$first = false;
$compiler
->subcompile($pair['key'])
->raw(' => ')
->subcompile($pair['value'])
;
}
$compiler->raw(']');
}
}
class_alias('Twig\Node\Expression\ArrayExpression',
'Twig_Node_Expression_Array');
Expression/ArrowFunctionExpression.php000064400000003034151163450630014300
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\Node\Node;
/**
* Represents an arrow function.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ArrowFunctionExpression extends AbstractExpression
{
public function __construct(AbstractExpression $expr, Node $names,
$lineno, $tag = null)
{
parent::__construct(['expr' => $expr,
'names' => $names], [], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->raw('function (')
;
foreach ($this->getNode('names') as $i => $name) {
if ($i) {
$compiler->raw(', ');
}
$compiler
->raw('$__')
->raw($name->getAttribute('name'))
->raw('__')
;
}
$compiler
->raw(') use ($context) { ')
;
foreach ($this->getNode('names') as $name) {
$compiler
->raw('$context["')
->raw($name->getAttribute('name'))
->raw('"] = $__')
->raw($name->getAttribute('name'))
->raw('__; ')
;
}
$compiler
->raw('return ')
->subcompile($this->getNode('expr'))
->raw('; }')
;
}
}
Expression/AssignNameExpression.php000064400000001151151163450630013523
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
class AssignNameExpression extends NameExpression
{
public function compile(Compiler $compiler)
{
$compiler
->raw('$context[')
->string($this->getAttribute('name'))
->raw(']')
;
}
}
class_alias('Twig\Node\Expression\AssignNameExpression',
'Twig_Node_Expression_AssignName');
Expression/Binary/AbstractBinary.php000064400000002061151163450630013553
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
abstract class AbstractBinary extends AbstractExpression
{
public function __construct(\Twig_NodeInterface $left,
\Twig_NodeInterface $right, $lineno)
{
parent::__construct(['left' => $left,
'right' => $right], [], $lineno);
}
public function compile(Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('left'))
->raw(' ')
;
$this->operator($compiler);
$compiler
->raw(' ')
->subcompile($this->getNode('right'))
->raw(')')
;
}
abstract public function operator(Compiler $compiler);
}
class_alias('Twig\Node\Expression\Binary\AbstractBinary',
'Twig_Node_Expression_Binary');
Expression/Binary/AddBinary.php000064400000001002151163450630012472
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class AddBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('+');
}
}
class_alias('Twig\Node\Expression\Binary\AddBinary',
'Twig_Node_Expression_Binary_Add');
Expression/Binary/AndBinary.php000064400000001003151163450630012505
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class AndBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('&&');
}
}
class_alias('Twig\Node\Expression\Binary\AndBinary',
'Twig_Node_Expression_Binary_And');
Expression/Binary/BitwiseAndBinary.php000064400000001027151163450630014042
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class BitwiseAndBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('&');
}
}
class_alias('Twig\Node\Expression\Binary\BitwiseAndBinary',
'Twig_Node_Expression_Binary_BitwiseAnd');
Expression/Binary/BitwiseOrBinary.php000064400000001024151163450630013715
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class BitwiseOrBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('|');
}
}
class_alias('Twig\Node\Expression\Binary\BitwiseOrBinary',
'Twig_Node_Expression_Binary_BitwiseOr');
Expression/Binary/BitwiseXorBinary.php000064400000001027151163450650014112
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class BitwiseXorBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('^');
}
}
class_alias('Twig\Node\Expression\Binary\BitwiseXorBinary',
'Twig_Node_Expression_Binary_BitwiseXor');
Expression/Binary/ConcatBinary.php000064400000001013151163450670013217
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class ConcatBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('.');
}
}
class_alias('Twig\Node\Expression\Binary\ConcatBinary',
'Twig_Node_Expression_Binary_Concat');
Expression/Binary/DivBinary.php000064400000001002151163450700012522
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class DivBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('/');
}
}
class_alias('Twig\Node\Expression\Binary\DivBinary',
'Twig_Node_Expression_Binary_Div');
Expression/Binary/EndsWithBinary.php000064400000001753151163450700013542
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class EndsWithBinary extends AbstractBinary
{
public function compile(Compiler $compiler)
{
$left = $compiler->getVarName();
$right = $compiler->getVarName();
$compiler
->raw(sprintf('(is_string($%s = ', $left))
->subcompile($this->getNode('left'))
->raw(sprintf(') && is_string($%s = ',
$right))
->subcompile($this->getNode('right'))
->raw(sprintf(') && (\'\' === $%2$s
|| $%2$s === substr($%1$s, -strlen($%2$s))))', $left, $right))
;
}
public function operator(Compiler $compiler)
{
return $compiler->raw('');
}
}
class_alias('Twig\Node\Expression\Binary\EndsWithBinary',
'Twig_Node_Expression_Binary_EndsWith');
Expression/Binary/EqualBinary.php000064400000000763151163450700013064
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class EqualBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('==');
}
}
class_alias('Twig\Node\Expression\Binary\EqualBinary',
'Twig_Node_Expression_Binary_Equal');
Expression/Binary/FloorDivBinary.php000064400000001241151163450700013531
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class FloorDivBinary extends AbstractBinary
{
public function compile(Compiler $compiler)
{
$compiler->raw('(int) floor(');
parent::compile($compiler);
$compiler->raw(')');
}
public function operator(Compiler $compiler)
{
return $compiler->raw('/');
}
}
class_alias('Twig\Node\Expression\Binary\FloorDivBinary',
'Twig_Node_Expression_Binary_FloorDiv');
Expression/Binary/GreaterBinary.php000064400000000770151163450720013406
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class GreaterBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('>');
}
}
class_alias('Twig\Node\Expression\Binary\GreaterBinary',
'Twig_Node_Expression_Binary_Greater');
Expression/Binary/GreaterEqualBinary.php000064400000001010151163450720014362
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class GreaterEqualBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('>=');
}
}
class_alias('Twig\Node\Expression\Binary\GreaterEqualBinary',
'Twig_Node_Expression_Binary_GreaterEqual');
Expression/Binary/InBinary.php000064400000001372151163450720012362
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class InBinary extends AbstractBinary
{
public function compile(Compiler $compiler)
{
$compiler
->raw('twig_in_filter(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
;
}
public function operator(Compiler $compiler)
{
return $compiler->raw('in');
}
}
class_alias('Twig\Node\Expression\Binary\InBinary',
'Twig_Node_Expression_Binary_In');
Expression/Binary/LessBinary.php000064400000000757151163450720012730
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class LessBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('<');
}
}
class_alias('Twig\Node\Expression\Binary\LessBinary',
'Twig_Node_Expression_Binary_Less');
Expression/Binary/LessEqualBinary.php000064400000000777151163450720013722
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class LessEqualBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('<=');
}
}
class_alias('Twig\Node\Expression\Binary\LessEqualBinary',
'Twig_Node_Expression_Binary_LessEqual');
Expression/Binary/MatchesBinary.php000064400000001403151163450720013373
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class MatchesBinary extends AbstractBinary
{
public function compile(Compiler $compiler)
{
$compiler
->raw('preg_match(')
->subcompile($this->getNode('right'))
->raw(', ')
->subcompile($this->getNode('left'))
->raw(')')
;
}
public function operator(Compiler $compiler)
{
return $compiler->raw('');
}
}
class_alias('Twig\Node\Expression\Binary\MatchesBinary',
'Twig_Node_Expression_Binary_Matches');
Expression/Binary/ModBinary.php000064400000001002151163450720012521
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class ModBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('%');
}
}
class_alias('Twig\Node\Expression\Binary\ModBinary',
'Twig_Node_Expression_Binary_Mod');
Expression/Binary/MulBinary.php000064400000001002151163450760012543
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class MulBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('*');
}
}
class_alias('Twig\Node\Expression\Binary\MulBinary',
'Twig_Node_Expression_Binary_Mul');
Expression/Binary/NotEqualBinary.php000064400000000774151163450760013555
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class NotEqualBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('!=');
}
}
class_alias('Twig\Node\Expression\Binary\NotEqualBinary',
'Twig_Node_Expression_Binary_NotEqual');
Expression/Binary/NotInBinary.php000064400000001410151163450760013040
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class NotInBinary extends AbstractBinary
{
public function compile(Compiler $compiler)
{
$compiler
->raw('!twig_in_filter(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
;
}
public function operator(Compiler $compiler)
{
return $compiler->raw('not in');
}
}
class_alias('Twig\Node\Expression\Binary\NotInBinary',
'Twig_Node_Expression_Binary_NotIn');
Expression/Binary/OrBinary.php000064400000001000151163450770012365
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class OrBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('||');
}
}
class_alias('Twig\Node\Expression\Binary\OrBinary',
'Twig_Node_Expression_Binary_Or');
Expression/Binary/PowerBinary.php000064400000001532151163451000013076
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class PowerBinary extends AbstractBinary
{
public function compile(Compiler $compiler)
{
if (\PHP_VERSION_ID >= 50600) {
return parent::compile($compiler);
}
$compiler
->raw('pow(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
;
}
public function operator(Compiler $compiler)
{
return $compiler->raw('**');
}
}
class_alias('Twig\Node\Expression\Binary\PowerBinary',
'Twig_Node_Expression_Binary_Power');
Expression/Binary/RangeBinary.php000064400000001372151163451000013040
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class RangeBinary extends AbstractBinary
{
public function compile(Compiler $compiler)
{
$compiler
->raw('range(')
->subcompile($this->getNode('left'))
->raw(', ')
->subcompile($this->getNode('right'))
->raw(')')
;
}
public function operator(Compiler $compiler)
{
return $compiler->raw('..');
}
}
class_alias('Twig\Node\Expression\Binary\RangeBinary',
'Twig_Node_Expression_Binary_Range');
Expression/Binary/StartsWithBinary.php000064400000001744151163451000014123
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class StartsWithBinary extends AbstractBinary
{
public function compile(Compiler $compiler)
{
$left = $compiler->getVarName();
$right = $compiler->getVarName();
$compiler
->raw(sprintf('(is_string($%s = ', $left))
->subcompile($this->getNode('left'))
->raw(sprintf(') && is_string($%s = ',
$right))
->subcompile($this->getNode('right'))
->raw(sprintf(') && (\'\' === $%2$s
|| 0 === strpos($%1$s, $%2$s)))', $left, $right))
;
}
public function operator(Compiler $compiler)
{
return $compiler->raw('');
}
}
class_alias('Twig\Node\Expression\Binary\StartsWithBinary',
'Twig_Node_Expression_Binary_StartsWith');
Expression/Binary/SubBinary.php000064400000001002151163451000012523
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Binary;
use Twig\Compiler;
class SubBinary extends AbstractBinary
{
public function operator(Compiler $compiler)
{
return $compiler->raw('-');
}
}
class_alias('Twig\Node\Expression\Binary\SubBinary',
'Twig_Node_Expression_Binary_Sub');
Expression/BlockReferenceExpression.php000064400000005202151163451000014340
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\Node\Node;
/**
* Represents a block call node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class BlockReferenceExpression extends AbstractExpression
{
/**
* @param Node|null $template
*/
public function __construct(\Twig_NodeInterface $name, $template =
null, $lineno, $tag = null)
{
if (\is_bool($template)) {
@trigger_error(sprintf('The %s method
"$asString" argument is deprecated since version 1.28 and will be
removed in 2.0.', __METHOD__), E_USER_DEPRECATED);
$template = null;
}
$nodes = ['name' => $name];
if (null !== $template) {
$nodes['template'] = $template;
}
parent::__construct($nodes, ['is_defined_test' =>
false, 'output' => false], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
if ($this->getAttribute('is_defined_test')) {
$this->compileTemplateCall($compiler, 'hasBlock');
} else {
if ($this->getAttribute('output')) {
$compiler->addDebugInfo($this);
$this
->compileTemplateCall($compiler,
'displayBlock')
->raw(";\n");
} else {
$this->compileTemplateCall($compiler,
'renderBlock');
}
}
}
private function compileTemplateCall(Compiler $compiler, $method)
{
if (!$this->hasNode('template')) {
$compiler->write('$this');
} else {
$compiler
->write('$this->loadTemplate(')
->subcompile($this->getNode('template'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')')
;
}
$compiler->raw(sprintf('->%s', $method));
$this->compileBlockArguments($compiler);
return $compiler;
}
private function compileBlockArguments(Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('name'))
->raw(', $context');
if (!$this->hasNode('template')) {
$compiler->raw(', $blocks');
}
return $compiler->raw(')');
}
}
class_alias('Twig\Node\Expression\BlockReferenceExpression',
'Twig_Node_Expression_BlockReference');
Expression/CallExpression.php000064400000026750151163451000012355
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\Error\SyntaxError;
use Twig\Extension\ExtensionInterface;
use Twig\Node\Node;
abstract class CallExpression extends AbstractExpression
{
private $reflector;
protected function compileCallable(Compiler $compiler)
{
$closingParenthesis = false;
$isArray = false;
if ($this->hasAttribute('callable') &&
$callable = $this->getAttribute('callable')) {
if (\is_string($callable) && false ===
strpos($callable, '::')) {
$compiler->raw($callable);
} else {
list($r, $callable) = $this->reflectCallable($callable);
if ($r instanceof \ReflectionMethod &&
\is_string($callable[0])) {
if ($r->isStatic()) {
$compiler->raw(sprintf('%s::%s',
$callable[0], $callable[1]));
} else {
$compiler->raw(sprintf('$this->env->getRuntime(\'%s\')->%s',
$callable[0], $callable[1]));
}
} elseif ($r instanceof \ReflectionMethod &&
$callable[0] instanceof ExtensionInterface) {
$compiler->raw(sprintf('$this->env->getExtension(\'%s\')->%s',
\get_class($callable[0]), $callable[1]));
} else {
$type =
ucfirst($this->getAttribute('type'));
$compiler->raw(sprintf('call_user_func_array($this->env->get%s(\'%s\')->getCallable(),
', $type, $this->getAttribute('name')));
$closingParenthesis = true;
$isArray = true;
}
}
} else {
$compiler->raw($this->getAttribute('thing')->compile());
}
$this->compileArguments($compiler, $isArray);
if ($closingParenthesis) {
$compiler->raw(')');
}
}
protected function compileArguments(Compiler $compiler, $isArray =
false)
{
$compiler->raw($isArray ? '[' : '(');
$first = true;
if ($this->hasAttribute('needs_environment')
&& $this->getAttribute('needs_environment')) {
$compiler->raw('$this->env');
$first = false;
}
if ($this->hasAttribute('needs_context') &&
$this->getAttribute('needs_context')) {
if (!$first) {
$compiler->raw(', ');
}
$compiler->raw('$context');
$first = false;
}
if ($this->hasAttribute('arguments')) {
foreach ($this->getAttribute('arguments') as
$argument) {
if (!$first) {
$compiler->raw(', ');
}
$compiler->string($argument);
$first = false;
}
}
if ($this->hasNode('node')) {
if (!$first) {
$compiler->raw(', ');
}
$compiler->subcompile($this->getNode('node'));
$first = false;
}
if ($this->hasNode('arguments')) {
$callable = $this->hasAttribute('callable') ?
$this->getAttribute('callable') : null;
$arguments = $this->getArguments($callable,
$this->getNode('arguments'));
foreach ($arguments as $node) {
if (!$first) {
$compiler->raw(', ');
}
$compiler->subcompile($node);
$first = false;
}
}
$compiler->raw($isArray ? ']' : ')');
}
protected function getArguments($callable, $arguments)
{
$callType = $this->getAttribute('type');
$callName = $this->getAttribute('name');
$parameters = [];
$named = false;
foreach ($arguments as $name => $node) {
if (!\is_int($name)) {
$named = true;
$name = $this->normalizeName($name);
} elseif ($named) {
throw new SyntaxError(sprintf('Positional arguments
cannot be used after named arguments for %s "%s".',
$callType, $callName), $this->getTemplateLine(),
$this->getSourceContext());
}
$parameters[$name] = $node;
}
$isVariadic = $this->hasAttribute('is_variadic')
&& $this->getAttribute('is_variadic');
if (!$named && !$isVariadic) {
return $parameters;
}
if (!$callable) {
if ($named) {
$message = sprintf('Named arguments are not supported
for %s "%s".', $callType, $callName);
} else {
$message = sprintf('Arbitrary positional arguments are
not supported for %s "%s".', $callType, $callName);
}
throw new \LogicException($message);
}
$callableParameters = $this->getCallableParameters($callable,
$isVariadic);
$arguments = [];
$names = [];
$missingArguments = [];
$optionalArguments = [];
$pos = 0;
foreach ($callableParameters as $callableParameter) {
$names[] = $name =
$this->normalizeName($callableParameter->name);
if (\array_key_exists($name, $parameters)) {
if (\array_key_exists($pos, $parameters)) {
throw new SyntaxError(sprintf('Argument
"%s" is defined twice for %s "%s".', $name,
$callType, $callName), $this->getTemplateLine(),
$this->getSourceContext());
}
if (\count($missingArguments)) {
throw new SyntaxError(sprintf(
'Argument "%s" could not be assigned
for %s "%s(%s)" because it is mapped to an internal PHP function
which cannot determine default value for optional argument%s
"%s".',
$name, $callType, $callName, implode(',
', $names), \count($missingArguments) > 1 ? 's' :
'', implode('", "', $missingArguments)
), $this->getTemplateLine(),
$this->getSourceContext());
}
$arguments = array_merge($arguments, $optionalArguments);
$arguments[] = $parameters[$name];
unset($parameters[$name]);
$optionalArguments = [];
} elseif (\array_key_exists($pos, $parameters)) {
$arguments = array_merge($arguments, $optionalArguments);
$arguments[] = $parameters[$pos];
unset($parameters[$pos]);
$optionalArguments = [];
++$pos;
} elseif ($callableParameter->isDefaultValueAvailable()) {
$optionalArguments[] = new
ConstantExpression($callableParameter->getDefaultValue(), -1);
} elseif ($callableParameter->isOptional()) {
if (empty($parameters)) {
break;
} else {
$missingArguments[] = $name;
}
} else {
throw new SyntaxError(sprintf('Value for argument
"%s" is required for %s "%s".', $name, $callType,
$callName), $this->getTemplateLine(), $this->getSourceContext());
}
}
if ($isVariadic) {
$arbitraryArguments = new ArrayExpression([], -1);
foreach ($parameters as $key => $value) {
if (\is_int($key)) {
$arbitraryArguments->addElement($value);
} else {
$arbitraryArguments->addElement($value, new
ConstantExpression($key, -1));
}
unset($parameters[$key]);
}
if ($arbitraryArguments->count()) {
$arguments = array_merge($arguments, $optionalArguments);
$arguments[] = $arbitraryArguments;
}
}
if (!empty($parameters)) {
$unknownParameter = null;
foreach ($parameters as $parameter) {
if ($parameter instanceof Node) {
$unknownParameter = $parameter;
break;
}
}
throw new SyntaxError(
sprintf(
'Unknown argument%s "%s" for %s
"%s(%s)".',
\count($parameters) > 1 ? 's' :
'', implode('", "', array_keys($parameters)),
$callType, $callName, implode(', ', $names)
),
$unknownParameter ? $unknownParameter->getTemplateLine()
: $this->getTemplateLine(),
$unknownParameter ?
$unknownParameter->getSourceContext() : $this->getSourceContext()
);
}
return $arguments;
}
protected function normalizeName($name)
{
return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/',
'/([a-z\d])([A-Z])/'], ['\\1_\\2',
'\\1_\\2'], $name));
}
private function getCallableParameters($callable, $isVariadic)
{
list($r) = $this->reflectCallable($callable);
if (null === $r) {
return [];
}
$parameters = $r->getParameters();
if ($this->hasNode('node')) {
array_shift($parameters);
}
if ($this->hasAttribute('needs_environment')
&& $this->getAttribute('needs_environment')) {
array_shift($parameters);
}
if ($this->hasAttribute('needs_context') &&
$this->getAttribute('needs_context')) {
array_shift($parameters);
}
if ($this->hasAttribute('arguments') && null
!== $this->getAttribute('arguments')) {
foreach ($this->getAttribute('arguments') as
$argument) {
array_shift($parameters);
}
}
if ($isVariadic) {
$argument = end($parameters);
if ($argument && $argument->isArray() &&
$argument->isDefaultValueAvailable() && [] ===
$argument->getDefaultValue()) {
array_pop($parameters);
} else {
$callableName = $r->name;
if ($r instanceof \ReflectionMethod) {
$callableName =
$r->getDeclaringClass()->name.'::'.$callableName;
}
throw new \LogicException(sprintf('The last parameter
of "%s" for %s "%s" must be an array with default
value, eg. "array $arg = []".', $callableName,
$this->getAttribute('type'),
$this->getAttribute('name')));
}
}
return $parameters;
}
private function reflectCallable($callable)
{
if (null !== $this->reflector) {
return $this->reflector;
}
if (\is_array($callable)) {
if (!method_exists($callable[0], $callable[1])) {
// __call()
return [null, []];
}
$r = new \ReflectionMethod($callable[0], $callable[1]);
} elseif (\is_object($callable) && !$callable instanceof
\Closure) {
$r = new \ReflectionObject($callable);
$r = $r->getMethod('__invoke');
$callable = [$callable, '__invoke'];
} elseif (\is_string($callable) && false !== $pos =
strpos($callable, '::')) {
$class = substr($callable, 0, $pos);
$method = substr($callable, $pos + 2);
if (!method_exists($class, $method)) {
// __staticCall()
return [null, []];
}
$r = new \ReflectionMethod($callable);
$callable = [$class, $method];
} else {
$r = new \ReflectionFunction($callable);
}
return $this->reflector = [$r, $callable];
}
}
class_alias('Twig\Node\Expression\CallExpression',
'Twig_Node_Expression_Call');
Expression/ConditionalExpression.php000064400000001760151163451000013737
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
class ConditionalExpression extends AbstractExpression
{
public function __construct(AbstractExpression $expr1,
AbstractExpression $expr2, AbstractExpression $expr3, $lineno)
{
parent::__construct(['expr1' => $expr1,
'expr2' => $expr2, 'expr3' => $expr3], [],
$lineno);
}
public function compile(Compiler $compiler)
{
$compiler
->raw('((')
->subcompile($this->getNode('expr1'))
->raw(') ? (')
->subcompile($this->getNode('expr2'))
->raw(') : (')
->subcompile($this->getNode('expr3'))
->raw('))')
;
}
}
class_alias('Twig\Node\Expression\ConditionalExpression',
'Twig_Node_Expression_Conditional');
Expression/ConstantExpression.php000064400000001227151163451000013263
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
class ConstantExpression extends AbstractExpression
{
public function __construct($value, $lineno)
{
parent::__construct([], ['value' => $value], $lineno);
}
public function compile(Compiler $compiler)
{
$compiler->repr($this->getAttribute('value'));
}
}
class_alias('Twig\Node\Expression\ConstantExpression',
'Twig_Node_Expression_Constant');
Expression/Filter/DefaultFilter.php000064400000003556151163451030013403
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Filter;
use Twig\Compiler;
use Twig\Node\Expression\ConditionalExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\GetAttrExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Test\DefinedTest;
use Twig\Node\Node;
/**
* Returns the value or the default value when it is undefined or empty.
*
* {{ var.foo|default('foo item on var is not defined') }}
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class DefaultFilter extends FilterExpression
{
public function __construct(\Twig_NodeInterface $node,
ConstantExpression $filterName, \Twig_NodeInterface $arguments, $lineno,
$tag = null)
{
$default = new FilterExpression($node, new
ConstantExpression('default', $node->getTemplateLine()),
$arguments, $node->getTemplateLine());
if ('default' ===
$filterName->getAttribute('value') && ($node
instanceof NameExpression || $node instanceof GetAttrExpression)) {
$test = new DefinedTest(clone $node, 'defined', new
Node(), $node->getTemplateLine());
$false = \count($arguments) ? $arguments->getNode(0) : new
ConstantExpression('', $node->getTemplateLine());
$node = new ConditionalExpression($test, $default, $false,
$node->getTemplateLine());
} else {
$node = $default;
}
parent::__construct($node, $filterName, $arguments, $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler->subcompile($this->getNode('node'));
}
}
class_alias('Twig\Node\Expression\Filter\DefaultFilter',
'Twig_Node_Expression_Filter_Default');
Expression/FilterExpression.php000064400000003103151163451030012715
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\TwigFilter;
class FilterExpression extends CallExpression
{
public function __construct(\Twig_NodeInterface $node,
ConstantExpression $filterName, \Twig_NodeInterface $arguments, $lineno,
$tag = null)
{
parent::__construct(['node' => $node,
'filter' => $filterName, 'arguments' =>
$arguments], [], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$name =
$this->getNode('filter')->getAttribute('value');
$filter = $compiler->getEnvironment()->getFilter($name);
$this->setAttribute('name', $name);
$this->setAttribute('type', 'filter');
$this->setAttribute('thing', $filter);
$this->setAttribute('needs_environment',
$filter->needsEnvironment());
$this->setAttribute('needs_context',
$filter->needsContext());
$this->setAttribute('arguments',
$filter->getArguments());
if ($filter instanceof \Twig_FilterCallableInterface || $filter
instanceof TwigFilter) {
$this->setAttribute('callable',
$filter->getCallable());
}
if ($filter instanceof TwigFilter) {
$this->setAttribute('is_variadic',
$filter->isVariadic());
}
$this->compileCallable($compiler);
}
}
class_alias('Twig\Node\Expression\FilterExpression',
'Twig_Node_Expression_Filter');
Expression/FunctionExpression.php000064400000003265151163451040013267
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\TwigFunction;
class FunctionExpression extends CallExpression
{
public function __construct($name, \Twig_NodeInterface $arguments,
$lineno)
{
parent::__construct(['arguments' => $arguments],
['name' => $name, 'is_defined_test' => false],
$lineno);
}
public function compile(Compiler $compiler)
{
$name = $this->getAttribute('name');
$function = $compiler->getEnvironment()->getFunction($name);
$this->setAttribute('name', $name);
$this->setAttribute('type', 'function');
$this->setAttribute('thing', $function);
$this->setAttribute('needs_environment',
$function->needsEnvironment());
$this->setAttribute('needs_context',
$function->needsContext());
$this->setAttribute('arguments',
$function->getArguments());
if ($function instanceof \Twig_FunctionCallableInterface ||
$function instanceof TwigFunction) {
$callable = $function->getCallable();
if ('constant' === $name &&
$this->getAttribute('is_defined_test')) {
$callable = 'twig_constant_is_defined';
}
$this->setAttribute('callable', $callable);
}
if ($function instanceof TwigFunction) {
$this->setAttribute('is_variadic',
$function->isVariadic());
}
$this->compileCallable($compiler);
}
}
class_alias('Twig\Node\Expression\FunctionExpression',
'Twig_Node_Expression_Function');
Expression/GetAttrExpression.php000064400000005252151163451040013052
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\Template;
class GetAttrExpression extends AbstractExpression
{
public function __construct(AbstractExpression $node,
AbstractExpression $attribute, AbstractExpression $arguments = null, $type,
$lineno)
{
$nodes = ['node' => $node, 'attribute' =>
$attribute];
if (null !== $arguments) {
$nodes['arguments'] = $arguments;
}
parent::__construct($nodes, ['type' => $type,
'is_defined_test' => false, 'ignore_strict_check'
=> false, 'disable_c_ext' => false], $lineno);
}
public function compile(Compiler $compiler)
{
if ($this->getAttribute('disable_c_ext')) {
@trigger_error(sprintf('Using the
"disable_c_ext" attribute on %s is deprecated since version 1.30
and will be removed in 2.0.', __CLASS__), E_USER_DEPRECATED);
}
if (\function_exists('twig_template_get_attributes')
&& !$this->getAttribute('disable_c_ext')) {
$compiler->raw('twig_template_get_attributes($this,
');
} else {
$compiler->raw('$this->getAttribute(');
}
if ($this->getAttribute('ignore_strict_check')) {
$this->getNode('node')->setAttribute('ignore_strict_check',
true);
}
$compiler->subcompile($this->getNode('node'));
$compiler->raw(',
')->subcompile($this->getNode('attribute'));
// only generate optional arguments when needed (to make generated
code more readable)
$needFourth =
$this->getAttribute('ignore_strict_check');
$needThird = $needFourth ||
$this->getAttribute('is_defined_test');
$needSecond = $needThird || Template::ANY_CALL !==
$this->getAttribute('type');
$needFirst = $needSecond ||
$this->hasNode('arguments');
if ($needFirst) {
if ($this->hasNode('arguments')) {
$compiler->raw(',
')->subcompile($this->getNode('arguments'));
} else {
$compiler->raw(', []');
}
}
if ($needSecond) {
$compiler->raw(',
')->repr($this->getAttribute('type'));
}
if ($needThird) {
$compiler->raw(',
')->repr($this->getAttribute('is_defined_test'));
}
if ($needFourth) {
$compiler->raw(',
')->repr($this->getAttribute('ignore_strict_check'));
}
$compiler->raw(')');
}
}
class_alias('Twig\Node\Expression\GetAttrExpression',
'Twig_Node_Expression_GetAttr');
Expression/InlinePrint.php000064400000001233151163451040011646
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\Node\Node;
/**
* @internal
*/
final class InlinePrint extends AbstractExpression
{
public function __construct(Node $node, $lineno)
{
parent::__construct(['node' => $node], [], $lineno);
}
public function compile(Compiler $compiler)
{
$compiler
->raw('print (')
->subcompile($this->getNode('node'))
->raw(')')
;
}
}
Expression/MethodCallExpression.php000064400000002416151163451060013515
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
class MethodCallExpression extends AbstractExpression
{
public function __construct(AbstractExpression $node, $method,
ArrayExpression $arguments, $lineno)
{
parent::__construct(['node' => $node,
'arguments' => $arguments], ['method' => $method,
'safe' => false], $lineno);
if ($node instanceof NameExpression) {
$node->setAttribute('always_defined', true);
}
}
public function compile(Compiler $compiler)
{
$compiler
->subcompile($this->getNode('node'))
->raw('->')
->raw($this->getAttribute('method'))
->raw('(')
;
$first = true;
foreach
($this->getNode('arguments')->getKeyValuePairs() as $pair)
{
if (!$first) {
$compiler->raw(', ');
}
$first = false;
$compiler->subcompile($pair['value']);
}
$compiler->raw(')');
}
}
class_alias('Twig\Node\Expression\MethodCallExpression',
'Twig_Node_Expression_MethodCall');
Expression/NameExpression.php000064400000007173151163451070012367
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
class NameExpression extends AbstractExpression
{
protected $specialVars = [
'_self' => '$this',
'_context' => '$context',
'_charset' =>
'$this->env->getCharset()',
];
public function __construct($name, $lineno)
{
parent::__construct([], ['name' => $name,
'is_defined_test' => false, 'ignore_strict_check'
=> false, 'always_defined' => false], $lineno);
}
public function compile(Compiler $compiler)
{
$name = $this->getAttribute('name');
$compiler->addDebugInfo($this);
if ($this->getAttribute('is_defined_test')) {
if ($this->isSpecial()) {
$compiler->repr(true);
} elseif (\PHP_VERSION_ID >= 700400) {
$compiler
->raw('array_key_exists(')
->string($name)
->raw(', $context)')
;
} else {
$compiler
->raw('(isset($context[')
->string($name)
->raw(']) || array_key_exists(')
->string($name)
->raw(', $context))')
;
}
} elseif ($this->isSpecial()) {
$compiler->raw($this->specialVars[$name]);
} elseif ($this->getAttribute('always_defined')) {
$compiler
->raw('$context[')
->string($name)
->raw(']')
;
} else {
if (\PHP_VERSION_ID >= 70000) {
// use PHP 7 null coalescing operator
$compiler
->raw('($context[')
->string($name)
->raw('] ?? ')
;
if ($this->getAttribute('ignore_strict_check')
|| !$compiler->getEnvironment()->isStrictVariables()) {
$compiler->raw('null)');
} else {
$compiler->raw('$this->getContext($context,
')->string($name)->raw('))');
}
} elseif (\PHP_VERSION_ID >= 50400) {
// PHP 5.4 ternary operator performance was optimized
$compiler
->raw('(isset($context[')
->string($name)
->raw(']) ? $context[')
->string($name)
->raw('] : ')
;
if ($this->getAttribute('ignore_strict_check')
|| !$compiler->getEnvironment()->isStrictVariables()) {
$compiler->raw('null)');
} else {
$compiler->raw('$this->getContext($context,
')->string($name)->raw('))');
}
} else {
$compiler
->raw('$this->getContext($context, ')
->string($name)
;
if
($this->getAttribute('ignore_strict_check')) {
$compiler->raw(', true');
}
$compiler
->raw(')')
;
}
}
}
public function isSpecial()
{
return
isset($this->specialVars[$this->getAttribute('name')]);
}
public function isSimple()
{
return !$this->isSpecial() &&
!$this->getAttribute('is_defined_test');
}
}
class_alias('Twig\Node\Expression\NameExpression',
'Twig_Node_Expression_Name');
Expression/NullCoalesceExpression.php000064400000004310151163451070014046
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\Node\Expression\Binary\AndBinary;
use Twig\Node\Expression\Test\DefinedTest;
use Twig\Node\Expression\Test\NullTest;
use Twig\Node\Expression\Unary\NotUnary;
use Twig\Node\Node;
class NullCoalesceExpression extends ConditionalExpression
{
public function __construct(\Twig_NodeInterface $left,
\Twig_NodeInterface $right, $lineno)
{
$test = new DefinedTest(clone $left, 'defined', new
Node(), $left->getTemplateLine());
// for "block()", we don't need the null test as the
return value is always a string
if (!$left instanceof BlockReferenceExpression) {
$test = new AndBinary(
$test,
new NotUnary(new NullTest($left, 'null', new
Node(), $left->getTemplateLine()), $left->getTemplateLine()),
$left->getTemplateLine()
);
}
parent::__construct($test, $left, $right, $lineno);
}
public function compile(Compiler $compiler)
{
/*
* This optimizes only one case. PHP 7 also supports more complex
expressions
* that can return null. So, for instance, if log is defined,
log("foo") ?? "..." works,
* but log($a["foo"]) ?? "..." does not if
$a["foo"] is not defined. More advanced
* cases might be implemented as an optimizer node visitor, but has
not been done
* as benefits are probably not worth the added complexity.
*/
if (\PHP_VERSION_ID >= 70000 &&
$this->getNode('expr2') instanceof NameExpression) {
$this->getNode('expr2')->setAttribute('always_defined',
true);
$compiler
->raw('((')
->subcompile($this->getNode('expr2'))
->raw(') ?? (')
->subcompile($this->getNode('expr3'))
->raw('))')
;
} else {
parent::compile($compiler);
}
}
}
class_alias('Twig\Node\Expression\NullCoalesceExpression',
'Twig_Node_Expression_NullCoalesce');
Expression/ParentExpression.php000064400000002302151163451100012717
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
/**
* Represents a parent node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ParentExpression extends AbstractExpression
{
public function __construct($name, $lineno, $tag = null)
{
parent::__construct([], ['output' => false,
'name' => $name], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
if ($this->getAttribute('output')) {
$compiler
->addDebugInfo($this)
->write('$this->displayParentBlock(')
->string($this->getAttribute('name'))
->raw(", \$context, \$blocks);\n")
;
} else {
$compiler
->raw('$this->renderParentBlock(')
->string($this->getAttribute('name'))
->raw(', $context, $blocks)')
;
}
}
}
class_alias('Twig\Node\Expression\ParentExpression',
'Twig_Node_Expression_Parent');
Expression/TempNameExpression.php000064400000001301151163451110013173
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
class TempNameExpression extends AbstractExpression
{
public function __construct($name, $lineno)
{
parent::__construct([], ['name' => $name], $lineno);
}
public function compile(Compiler $compiler)
{
$compiler
->raw('$_')
->raw($this->getAttribute('name'))
->raw('_')
;
}
}
class_alias('Twig\Node\Expression\TempNameExpression',
'Twig_Node_Expression_TempName');
Expression/Test/ConstantTest.php000064400000002363151163451120012767
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Test;
use Twig\Compiler;
use Twig\Node\Expression\TestExpression;
/**
* Checks if a variable is the exact same value as a constant.
*
* {% if post.status is constant('Post::PUBLISHED') %}
* the status attribute is exactly the same as Post::PUBLISHED
* {% endif %}
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ConstantTest extends TestExpression
{
public function compile(Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('node'))
->raw(' === constant(')
;
if ($this->getNode('arguments')->hasNode(1)) {
$compiler
->raw('get_class(')
->subcompile($this->getNode('arguments')->getNode(1))
->raw(')."::".')
;
}
$compiler
->subcompile($this->getNode('arguments')->getNode(0))
->raw('))')
;
}
}
class_alias('Twig\Node\Expression\Test\ConstantTest',
'Twig_Node_Expression_Test_Constant');
Expression/Test/DefinedTest.php000064400000004606151163451130012537
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Test;
use Twig\Compiler;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\BlockReferenceExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FunctionExpression;
use Twig\Node\Expression\GetAttrExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\TestExpression;
/**
* Checks if a variable is defined in the current context.
*
* {# defined works with variable names and variable attributes #}
* {% if foo is defined %}
* {# ... #}
* {% endif %}
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class DefinedTest extends TestExpression
{
public function __construct(\Twig_NodeInterface $node, $name,
\Twig_NodeInterface $arguments = null, $lineno)
{
if ($node instanceof NameExpression) {
$node->setAttribute('is_defined_test', true);
} elseif ($node instanceof GetAttrExpression) {
$node->setAttribute('is_defined_test', true);
$this->changeIgnoreStrictCheck($node);
} elseif ($node instanceof BlockReferenceExpression) {
$node->setAttribute('is_defined_test', true);
} elseif ($node instanceof FunctionExpression &&
'constant' === $node->getAttribute('name')) {
$node->setAttribute('is_defined_test', true);
} elseif ($node instanceof ConstantExpression || $node instanceof
ArrayExpression) {
$node = new ConstantExpression(true,
$node->getTemplateLine());
} else {
throw new SyntaxError('The "defined" test only
works with simple variables.', $lineno);
}
parent::__construct($node, $name, $arguments, $lineno);
}
protected function changeIgnoreStrictCheck(GetAttrExpression $node)
{
$node->setAttribute('ignore_strict_check', true);
if ($node->getNode('node') instanceof
GetAttrExpression) {
$this->changeIgnoreStrictCheck($node->getNode('node'));
}
}
public function compile(Compiler $compiler)
{
$compiler->subcompile($this->getNode('node'));
}
}
class_alias('Twig\Node\Expression\Test\DefinedTest',
'Twig_Node_Expression_Test_Defined');
Expression/Test/DivisiblebyTest.php000064400000001565151163451130013447
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Test;
use Twig\Compiler;
use Twig\Node\Expression\TestExpression;
/**
* Checks if a variable is divisible by a number.
*
* {% if loop.index is divisible by(3) %}
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class DivisiblebyTest extends TestExpression
{
public function compile(Compiler $compiler)
{
$compiler
->raw('(0 == ')
->subcompile($this->getNode('node'))
->raw(' % ')
->subcompile($this->getNode('arguments')->getNode(0))
->raw(')')
;
}
}
class_alias('Twig\Node\Expression\Test\DivisiblebyTest',
'Twig_Node_Expression_Test_Divisibleby');
Expression/Test/EvenTest.php000064400000001367151163451130012077
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Test;
use Twig\Compiler;
use Twig\Node\Expression\TestExpression;
/**
* Checks if a number is even.
*
* {{ var is even }}
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class EvenTest extends TestExpression
{
public function compile(Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('node'))
->raw(' % 2 == 0')
->raw(')')
;
}
}
class_alias('Twig\Node\Expression\Test\EvenTest',
'Twig_Node_Expression_Test_Even');
Expression/Test/NullTest.php000064400000001345151163451130012110
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Test;
use Twig\Compiler;
use Twig\Node\Expression\TestExpression;
/**
* Checks that a variable is null.
*
* {{ var is none }}
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class NullTest extends TestExpression
{
public function compile(Compiler $compiler)
{
$compiler
->raw('(null === ')
->subcompile($this->getNode('node'))
->raw(')')
;
}
}
class_alias('Twig\Node\Expression\Test\NullTest',
'Twig_Node_Expression_Test_Null');
Expression/Test/OddTest.php000064400000001362151163451140011704
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Test;
use Twig\Compiler;
use Twig\Node\Expression\TestExpression;
/**
* Checks if a number is odd.
*
* {{ var is odd }}
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class OddTest extends TestExpression
{
public function compile(Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('node'))
->raw(' % 2 == 1')
->raw(')')
;
}
}
class_alias('Twig\Node\Expression\Test\OddTest',
'Twig_Node_Expression_Test_Odd');
Expression/Test/SameasTest.php000064400000001504151163451140012405
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Test;
use Twig\Compiler;
use Twig\Node\Expression\TestExpression;
/**
* Checks if a variable is the same as another one (=== in PHP).
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SameasTest extends TestExpression
{
public function compile(Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('node'))
->raw(' === ')
->subcompile($this->getNode('arguments')->getNode(0))
->raw(')')
;
}
}
class_alias('Twig\Node\Expression\Test\SameasTest',
'Twig_Node_Expression_Test_Sameas');
Expression/TestExpression.php000064400000002703151163451140012416
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\TwigTest;
class TestExpression extends CallExpression
{
public function __construct(\Twig_NodeInterface $node, $name,
\Twig_NodeInterface $arguments = null, $lineno)
{
$nodes = ['node' => $node];
if (null !== $arguments) {
$nodes['arguments'] = $arguments;
}
parent::__construct($nodes, ['name' => $name],
$lineno);
}
public function compile(Compiler $compiler)
{
$name = $this->getAttribute('name');
$test = $compiler->getEnvironment()->getTest($name);
$this->setAttribute('name', $name);
$this->setAttribute('type', 'test');
$this->setAttribute('thing', $test);
if ($test instanceof TwigTest) {
$this->setAttribute('arguments',
$test->getArguments());
}
if ($test instanceof \Twig_TestCallableInterface || $test
instanceof TwigTest) {
$this->setAttribute('callable',
$test->getCallable());
}
if ($test instanceof TwigTest) {
$this->setAttribute('is_variadic',
$test->isVariadic());
}
$this->compileCallable($compiler);
}
}
class_alias('Twig\Node\Expression\TestExpression',
'Twig_Node_Expression_Test');
Expression/Unary/AbstractUnary.php000064400000001532151163451160013300
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Unary;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
abstract class AbstractUnary extends AbstractExpression
{
public function __construct(\Twig_NodeInterface $node, $lineno)
{
parent::__construct(['node' => $node], [], $lineno);
}
public function compile(Compiler $compiler)
{
$compiler->raw(' ');
$this->operator($compiler);
$compiler->subcompile($this->getNode('node'));
}
abstract public function operator(Compiler $compiler);
}
class_alias('Twig\Node\Expression\Unary\AbstractUnary',
'Twig_Node_Expression_Unary');
Expression/Unary/NegUnary.php000064400000000765151163451160012255
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Unary;
use Twig\Compiler;
class NegUnary extends AbstractUnary
{
public function operator(Compiler $compiler)
{
$compiler->raw('-');
}
}
class_alias('Twig\Node\Expression\Unary\NegUnary',
'Twig_Node_Expression_Unary_Neg');
Expression/Unary/NotUnary.php000064400000000765151163451160012304
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Unary;
use Twig\Compiler;
class NotUnary extends AbstractUnary
{
public function operator(Compiler $compiler)
{
$compiler->raw('!');
}
}
class_alias('Twig\Node\Expression\Unary\NotUnary',
'Twig_Node_Expression_Unary_Not');
Expression/Unary/PosUnary.php000064400000000765151163451170012306
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node\Expression\Unary;
use Twig\Compiler;
class PosUnary extends AbstractUnary
{
public function operator(Compiler $compiler)
{
$compiler->raw('+');
}
}
class_alias('Twig\Node\Expression\Unary\PosUnary',
'Twig_Node_Expression_Unary_Pos');
FlushNode.php000064400000001261151163451170007150 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* Represents a flush node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class FlushNode extends Node
{
public function __construct($lineno, $tag)
{
parent::__construct([], [], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("flush();\n")
;
}
}
class_alias('Twig\Node\FlushNode', 'Twig_Node_Flush');
ForLoopNode.php000064400000003061151163451170007447 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* Internal node used by the for node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ForLoopNode extends Node
{
public function __construct($lineno, $tag = null)
{
parent::__construct([], ['with_loop' => false,
'ifexpr' => false, 'else' => false], $lineno,
$tag);
}
public function compile(Compiler $compiler)
{
if ($this->getAttribute('else')) {
$compiler->write("\$context['_iterated'] =
true;\n");
}
if ($this->getAttribute('with_loop')) {
$compiler
->write("++\$context['loop']['index0'];\n")
->write("++\$context['loop']['index'];\n")
->write("\$context['loop']['first'] =
false;\n")
;
if (!$this->getAttribute('ifexpr')) {
$compiler
->write("if
(isset(\$context['loop']['length'])) {\n")
->indent()
->write("--\$context['loop']['revindex0'];\n")
->write("--\$context['loop']['revindex'];\n")
->write("\$context['loop']['last'] = 0 ===
\$context['loop']['revindex0'];\n")
->outdent()
->write("}\n")
;
}
}
}
}
class_alias('Twig\Node\ForLoopNode',
'Twig_Node_ForLoop');
ForNode.php000064400000010365151163451170006622 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\AssignNameExpression;
/**
* Represents a for node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ForNode extends Node
{
protected $loop;
public function __construct(AssignNameExpression $keyTarget,
AssignNameExpression $valueTarget, AbstractExpression $seq,
AbstractExpression $ifexpr = null, \Twig_NodeInterface $body,
\Twig_NodeInterface $else = null, $lineno, $tag = null)
{
$body = new Node([$body, $this->loop = new ForLoopNode($lineno,
$tag)]);
if (null !== $ifexpr) {
$body = new IfNode(new Node([$ifexpr, $body]), null, $lineno,
$tag);
}
$nodes = ['key_target' => $keyTarget,
'value_target' => $valueTarget, 'seq' => $seq,
'body' => $body];
if (null !== $else) {
$nodes['else'] = $else;
}
parent::__construct($nodes, ['with_loop' => true,
'ifexpr' => null !== $ifexpr], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("\$context['_parent'] =
\$context;\n")
->write("\$context['_seq'] =
twig_ensure_traversable(")
->subcompile($this->getNode('seq'))
->raw(");\n")
;
if ($this->hasNode('else')) {
$compiler->write("\$context['_iterated'] =
false;\n");
}
if ($this->getAttribute('with_loop')) {
$compiler
->write("\$context['loop'] = [\n")
->write(" 'parent' =>
\$context['_parent'],\n")
->write(" 'index0' => 0,\n")
->write(" 'index' => 1,\n")
->write(" 'first' => true,\n")
->write("];\n")
;
if (!$this->getAttribute('ifexpr')) {
$compiler
->write("if
(is_array(\$context['_seq']) ||
(is_object(\$context['_seq']) &&
\$context['_seq'] instanceof \Countable)) {\n")
->indent()
->write("\$length =
count(\$context['_seq']);\n")
->write("\$context['loop']['revindex0'] =
\$length - 1;\n")
->write("\$context['loop']['revindex'] =
\$length;\n")
->write("\$context['loop']['length'] =
\$length;\n")
->write("\$context['loop']['last'] = 1 ===
\$length;\n")
->outdent()
->write("}\n")
;
}
}
$this->loop->setAttribute('else',
$this->hasNode('else'));
$this->loop->setAttribute('with_loop',
$this->getAttribute('with_loop'));
$this->loop->setAttribute('ifexpr',
$this->getAttribute('ifexpr'));
$compiler
->write("foreach (\$context['_seq'] as
")
->subcompile($this->getNode('key_target'))
->raw(' => ')
->subcompile($this->getNode('value_target'))
->raw(") {\n")
->indent()
->subcompile($this->getNode('body'))
->outdent()
->write("}\n")
;
if ($this->hasNode('else')) {
$compiler
->write("if (!\$context['_iterated'])
{\n")
->indent()
->subcompile($this->getNode('else'))
->outdent()
->write("}\n")
;
}
$compiler->write("\$_parent =
\$context['_parent'];\n");
// remove some "private" loop variables (needed for
nested loops)
$compiler->write('unset($context[\'_seq\'],
$context[\'_iterated\'],
$context[\''.$this->getNode('key_target')->getAttribute('name').'\'],
$context[\''.$this->getNode('value_target')->getAttribute('name').'\'],
$context[\'_parent\'],
$context[\'loop\']);'."\n");
// keep the values set in the inner context for variables defined
in the outer context
$compiler->write("\$context =
array_intersect_key(\$context, \$_parent) + \$_parent;\n");
}
}
class_alias('Twig\Node\ForNode', 'Twig_Node_For');
IfNode.php000064400000003273151163451170006432 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* Represents an if node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class IfNode extends Node
{
public function __construct(\Twig_NodeInterface $tests,
\Twig_NodeInterface $else = null, $lineno, $tag = null)
{
$nodes = ['tests' => $tests];
if (null !== $else) {
$nodes['else'] = $else;
}
parent::__construct($nodes, [], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler->addDebugInfo($this);
for ($i = 0, $count = \count($this->getNode('tests'));
$i < $count; $i += 2) {
if ($i > 0) {
$compiler
->outdent()
->write('} elseif (')
;
} else {
$compiler
->write('if (')
;
}
$compiler
->subcompile($this->getNode('tests')->getNode($i))
->raw(") {\n")
->indent()
->subcompile($this->getNode('tests')->getNode($i + 1))
;
}
if ($this->hasNode('else')) {
$compiler
->outdent()
->write("} else {\n")
->indent()
->subcompile($this->getNode('else'))
;
}
$compiler
->outdent()
->write("}\n");
}
}
class_alias('Twig\Node\IfNode', 'Twig_Node_If');
ImportNode.php000064400000002706151163451170007346 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\NameExpression;
/**
* Represents an import node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ImportNode extends Node
{
public function __construct(AbstractExpression $expr,
AbstractExpression $var, $lineno, $tag = null)
{
parent::__construct(['expr' => $expr, 'var'
=> $var], [], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write('')
->subcompile($this->getNode('var'))
->raw(' = ')
;
if ($this->getNode('expr') instanceof NameExpression
&& '_self' ===
$this->getNode('expr')->getAttribute('name')) {
$compiler->raw('$this');
} else {
$compiler
->raw('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')->unwrap()')
;
}
$compiler->raw(";\n");
}
}
class_alias('Twig\Node\ImportNode',
'Twig_Node_Import');
IncludeNode.php000064400000006174151163451170007462 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
/**
* Represents an include node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class IncludeNode extends Node implements NodeOutputInterface
{
public function __construct(AbstractExpression $expr,
AbstractExpression $variables = null, $only = false, $ignoreMissing =
false, $lineno, $tag = null)
{
$nodes = ['expr' => $expr];
if (null !== $variables) {
$nodes['variables'] = $variables;
}
parent::__construct($nodes, ['only' => (bool) $only,
'ignore_missing' => (bool) $ignoreMissing], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler->addDebugInfo($this);
if ($this->getAttribute('ignore_missing')) {
$template = $compiler->getVarName();
$compiler
->write(sprintf("$%s = null;\n", $template))
->write("try {\n")
->indent()
->write(sprintf('$%s = ', $template))
;
$this->addGetTemplate($compiler);
$compiler
->raw(";\n")
->outdent()
->write("} catch (LoaderError \$e) {\n")
->indent()
->write("// ignore missing template\n")
->outdent()
->write("}\n")
->write(sprintf("if ($%s) {\n", $template))
->indent()
->write(sprintf('$%s->display(',
$template))
;
$this->addTemplateArguments($compiler);
$compiler
->raw(");\n")
->outdent()
->write("}\n")
;
} else {
$this->addGetTemplate($compiler);
$compiler->raw('->display(');
$this->addTemplateArguments($compiler);
$compiler->raw(");\n");
}
}
protected function addGetTemplate(Compiler $compiler)
{
$compiler
->write('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')')
;
}
protected function addTemplateArguments(Compiler $compiler)
{
if (!$this->hasNode('variables')) {
$compiler->raw(false ===
$this->getAttribute('only') ? '$context' :
'[]');
} elseif (false === $this->getAttribute('only')) {
$compiler
->raw('twig_array_merge($context, ')
->subcompile($this->getNode('variables'))
->raw(')')
;
} else {
$compiler->raw('twig_to_array(');
$compiler->subcompile($this->getNode('variables'));
$compiler->raw(')');
}
}
}
class_alias('Twig\Node\IncludeNode',
'Twig_Node_Include');
MacroNode.php000064400000007425151163451170007140 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Error\SyntaxError;
/**
* Represents a macro node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class MacroNode extends Node
{
const VARARGS_NAME = 'varargs';
public function __construct($name, \Twig_NodeInterface $body,
\Twig_NodeInterface $arguments, $lineno, $tag = null)
{
foreach ($arguments as $argumentName => $argument) {
if (self::VARARGS_NAME === $argumentName) {
throw new SyntaxError(sprintf('The argument
"%s" in macro "%s" cannot be defined because the
variable "%s" is reserved for arbitrary arguments.',
self::VARARGS_NAME, $name, self::VARARGS_NAME),
$argument->getTemplateLine(), $argument->getSourceContext());
}
}
parent::__construct(['body' => $body,
'arguments' => $arguments], ['name' => $name],
$lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write(sprintf('public function get%s(',
$this->getAttribute('name')))
;
$count = \count($this->getNode('arguments'));
$pos = 0;
foreach ($this->getNode('arguments') as $name =>
$default) {
$compiler
->raw('$__'.$name.'__ = ')
->subcompile($default)
;
if (++$pos < $count) {
$compiler->raw(', ');
}
}
if (\PHP_VERSION_ID >= 50600) {
if ($count) {
$compiler->raw(', ');
}
$compiler->raw('...$__varargs__');
}
$compiler
->raw(")\n")
->write("{\n")
->indent()
;
$compiler
->write("\$context =
\$this->env->mergeGlobals([\n")
->indent()
;
foreach ($this->getNode('arguments') as $name =>
$default) {
$compiler
->write('')
->string($name)
->raw(' => $__'.$name.'__')
->raw(",\n")
;
}
$compiler
->write('')
->string(self::VARARGS_NAME)
->raw(' => ')
;
if (\PHP_VERSION_ID >= 50600) {
$compiler->raw("\$__varargs__,\n");
} else {
$compiler
->raw('func_num_args() > ')
->repr($count)
->raw(' ? array_slice(func_get_args(), ')
->repr($count)
->raw(") : [],\n")
;
}
$compiler
->outdent()
->write("]);\n\n")
->write("\$blocks = [];\n\n")
;
if ($compiler->getEnvironment()->isDebug()) {
$compiler->write("ob_start();\n");
} else {
$compiler->write("ob_start(function () { return
''; });\n");
}
$compiler
->write("try {\n")
->indent()
->subcompile($this->getNode('body'))
->outdent()
->write("} catch (\Exception \$e) {\n")
->indent()
->write("ob_end_clean();\n\n")
->write("throw \$e;\n")
->outdent()
->write("} catch (\Throwable \$e) {\n")
->indent()
->write("ob_end_clean();\n\n")
->write("throw \$e;\n")
->outdent()
->write("}\n\n")
->write("return ('' === \$tmp =
ob_get_clean()) ? '' : new Markup(\$tmp,
\$this->env->getCharset());\n")
->outdent()
->write("}\n\n")
;
}
}
class_alias('Twig\Node\MacroNode', 'Twig_Node_Macro');
ModuleNode.php000064400000037414151163451170007325 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Source;
/**
* Represents a module node.
*
* Consider this class as being final. If you need to customize the
behavior of
* the generated class, consider adding nodes to the following nodes:
display_start,
* display_end, constructor_start, constructor_end, and class_end.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ModuleNode extends Node
{
public function __construct(\Twig_NodeInterface $body,
AbstractExpression $parent = null, \Twig_NodeInterface $blocks,
\Twig_NodeInterface $macros, \Twig_NodeInterface $traits,
$embeddedTemplates, $name, $source = '')
{
if (!$name instanceof Source) {
@trigger_error(sprintf('Passing a string as the $name
argument of %s() is deprecated since version 1.27. Pass a \Twig\Source
instance instead.', __METHOD__), E_USER_DEPRECATED);
$source = new Source($source, $name);
} else {
$source = $name;
}
$nodes = [
'body' => $body,
'blocks' => $blocks,
'macros' => $macros,
'traits' => $traits,
'display_start' => new Node(),
'display_end' => new Node(),
'constructor_start' => new Node(),
'constructor_end' => new Node(),
'class_end' => new Node(),
];
if (null !== $parent) {
$nodes['parent'] = $parent;
}
// embedded templates are set as attributes so that they are only
visited once by the visitors
parent::__construct($nodes, [
// source to be remove in 2.0
'source' => $source->getCode(),
// filename to be remove in 2.0 (use getTemplateName() instead)
'filename' => $source->getName(),
'index' => null,
'embedded_templates' => $embeddedTemplates,
], 1);
// populate the template name of all node children
$this->setTemplateName($source->getName());
$this->setSourceContext($source);
}
public function setIndex($index)
{
$this->setAttribute('index', $index);
}
public function compile(Compiler $compiler)
{
$this->compileTemplate($compiler);
foreach ($this->getAttribute('embedded_templates') as
$template) {
$compiler->subcompile($template);
}
}
protected function compileTemplate(Compiler $compiler)
{
if (!$this->getAttribute('index')) {
$compiler->write('<?php');
}
$this->compileClassHeader($compiler);
if (
\count($this->getNode('blocks'))
|| \count($this->getNode('traits'))
|| !$this->hasNode('parent')
|| $this->getNode('parent') instanceof
ConstantExpression
|| \count($this->getNode('constructor_start'))
|| \count($this->getNode('constructor_end'))
) {
$this->compileConstructor($compiler);
}
$this->compileGetParent($compiler);
$this->compileDisplay($compiler);
$compiler->subcompile($this->getNode('blocks'));
$this->compileMacros($compiler);
$this->compileGetTemplateName($compiler);
$this->compileIsTraitable($compiler);
$this->compileDebugInfo($compiler);
$this->compileGetSource($compiler);
$this->compileGetSourceContext($compiler);
$this->compileClassFooter($compiler);
}
protected function compileGetParent(Compiler $compiler)
{
if (!$this->hasNode('parent')) {
return;
}
$parent = $this->getNode('parent');
$compiler
->write("protected function doGetParent(array
\$context)\n", "{\n")
->indent()
->addDebugInfo($parent)
->write('return ')
;
if ($parent instanceof ConstantExpression) {
$compiler->subcompile($parent);
} else {
$compiler
->raw('$this->loadTemplate(')
->subcompile($parent)
->raw(', ')
->repr($this->getSourceContext()->getName())
->raw(', ')
->repr($parent->getTemplateLine())
->raw(')')
;
}
$compiler
->raw(";\n")
->outdent()
->write("}\n\n")
;
}
protected function compileClassHeader(Compiler $compiler)
{
$compiler
->write("\n\n")
;
if (!$this->getAttribute('index')) {
$compiler
->write("use Twig\Environment;\n")
->write("use Twig\Error\LoaderError;\n")
->write("use Twig\Error\RuntimeError;\n")
->write("use Twig\Markup;\n")
->write("use Twig\Sandbox\SecurityError;\n")
->write("use
Twig\Sandbox\SecurityNotAllowedTagError;\n")
->write("use
Twig\Sandbox\SecurityNotAllowedFilterError;\n")
->write("use
Twig\Sandbox\SecurityNotAllowedFunctionError;\n")
->write("use Twig\Source;\n")
->write("use Twig\Template;\n\n")
;
}
$compiler
// if the template name contains */, add a blank to avoid a PHP
parse error
->write('/* '.str_replace('*/', '*
/', $this->getSourceContext()->getName())." */\n")
->write('class
'.$compiler->getEnvironment()->getTemplateClass($this->getSourceContext()->getName(),
$this->getAttribute('index')))
->raw(sprintf(" extends %s\n",
$compiler->getEnvironment()->getBaseTemplateClass()))
->write("{\n")
->indent()
;
}
protected function compileConstructor(Compiler $compiler)
{
$compiler
->write("public function __construct(Environment
\$env)\n", "{\n")
->indent()
->subcompile($this->getNode('constructor_start'))
->write("parent::__construct(\$env);\n\n")
;
// parent
if (!$this->hasNode('parent')) {
$compiler->write("\$this->parent =
false;\n\n");
}
$countTraits = \count($this->getNode('traits'));
if ($countTraits) {
// traits
foreach ($this->getNode('traits') as $i =>
$trait) {
$this->compileLoadTemplate($compiler,
$trait->getNode('template'), sprintf('$_trait_%s',
$i));
$node = $trait->getNode('template');
$compiler
->addDebugInfo($node)
->write(sprintf("if
(!\$_trait_%s->isTraitable()) {\n", $i))
->indent()
->write("throw new RuntimeError('Template
\"'.")
->subcompile($trait->getNode('template'))
->raw(".'\" cannot be used as a
trait.', ")
->repr($node->getTemplateLine())
->raw(",
\$this->getSourceContext());\n")
->outdent()
->write("}\n")
->write(sprintf("\$_trait_%s_blocks =
\$_trait_%s->getBlocks();\n\n", $i, $i))
;
foreach ($trait->getNode('targets') as $key
=> $value) {
$compiler
->write(sprintf('if
(!isset($_trait_%s_blocks[', $i))
->string($key)
->raw("])) {\n")
->indent()
->write("throw new
RuntimeError(sprintf('Block ")
->string($key)
->raw(' is not defined in trait ')
->subcompile($trait->getNode('template'))
->raw(".'), ")
->repr($node->getTemplateLine())
->raw(",
\$this->getSourceContext());\n")
->outdent()
->write("}\n\n")
->write(sprintf('$_trait_%s_blocks[',
$i))
->subcompile($value)
->raw(sprintf('] =
$_trait_%s_blocks[', $i))
->string($key)
->raw(sprintf('];
unset($_trait_%s_blocks[', $i))
->string($key)
->raw("]);\n\n")
;
}
}
if ($countTraits > 1) {
$compiler
->write("\$this->traits =
array_merge(\n")
->indent()
;
for ($i = 0; $i < $countTraits; ++$i) {
$compiler
->write(sprintf('$_trait_%s_blocks'.($i == $countTraits - 1 ?
'' : ',')."\n", $i))
;
}
$compiler
->outdent()
->write(");\n\n")
;
} else {
$compiler
->write("\$this->traits =
\$_trait_0_blocks;\n\n")
;
}
$compiler
->write("\$this->blocks = array_merge(\n")
->indent()
->write("\$this->traits,\n")
->write("[\n")
;
} else {
$compiler
->write("\$this->blocks = [\n")
;
}
// blocks
$compiler
->indent()
;
foreach ($this->getNode('blocks') as $name =>
$node) {
$compiler
->write(sprintf("'%s' => [\$this,
'block_%s'],\n", $name, $name))
;
}
if ($countTraits) {
$compiler
->outdent()
->write("]\n")
->outdent()
->write(");\n")
;
} else {
$compiler
->outdent()
->write("];\n")
;
}
$compiler
->subcompile($this->getNode('constructor_end'))
->outdent()
->write("}\n\n")
;
}
protected function compileDisplay(Compiler $compiler)
{
$compiler
->write("protected function doDisplay(array \$context,
array \$blocks = [])\n", "{\n")
->indent()
->subcompile($this->getNode('display_start'))
->subcompile($this->getNode('body'))
;
if ($this->hasNode('parent')) {
$parent = $this->getNode('parent');
$compiler->addDebugInfo($parent);
if ($parent instanceof ConstantExpression) {
$compiler
->write('$this->parent =
$this->loadTemplate(')
->subcompile($parent)
->raw(', ')
->repr($this->getSourceContext()->getName())
->raw(', ')
->repr($parent->getTemplateLine())
->raw(");\n")
;
$compiler->write('$this->parent');
} else {
$compiler->write('$this->getParent($context)');
}
$compiler->raw("->display(\$context,
array_merge(\$this->blocks, \$blocks));\n");
}
$compiler
->subcompile($this->getNode('display_end'))
->outdent()
->write("}\n\n")
;
}
protected function compileClassFooter(Compiler $compiler)
{
$compiler
->subcompile($this->getNode('class_end'))
->outdent()
->write("}\n")
;
}
protected function compileMacros(Compiler $compiler)
{
$compiler->subcompile($this->getNode('macros'));
}
protected function compileGetTemplateName(Compiler $compiler)
{
$compiler
->write("public function getTemplateName()\n",
"{\n")
->indent()
->write('return ')
->repr($this->getSourceContext()->getName())
->raw(";\n")
->outdent()
->write("}\n\n")
;
}
protected function compileIsTraitable(Compiler $compiler)
{
// A template can be used as a trait if:
// * it has no parent
// * it has no macros
// * it has no body
//
// Put another way, a template can be used as a trait if it
// only contains blocks and use statements.
$traitable = !$this->hasNode('parent') && 0
=== \count($this->getNode('macros'));
if ($traitable) {
if ($this->getNode('body') instanceof BodyNode) {
$nodes =
$this->getNode('body')->getNode(0);
} else {
$nodes = $this->getNode('body');
}
if (!\count($nodes)) {
$nodes = new Node([$nodes]);
}
foreach ($nodes as $node) {
if (!\count($node)) {
continue;
}
if ($node instanceof TextNode &&
ctype_space($node->getAttribute('data'))) {
continue;
}
if ($node instanceof BlockReferenceNode) {
continue;
}
$traitable = false;
break;
}
}
if ($traitable) {
return;
}
$compiler
->write("public function isTraitable()\n",
"{\n")
->indent()
->write(sprintf("return %s;\n", $traitable ?
'true' : 'false'))
->outdent()
->write("}\n\n")
;
}
protected function compileDebugInfo(Compiler $compiler)
{
$compiler
->write("public function getDebugInfo()\n",
"{\n")
->indent()
->write(sprintf("return %s;\n",
str_replace("\n", '',
var_export(array_reverse($compiler->getDebugInfo(), true), true))))
->outdent()
->write("}\n\n")
;
}
protected function compileGetSource(Compiler $compiler)
{
$compiler
->write("/** @deprecated since 1.27 (to be removed in
2.0). Use getSourceContext() instead */\n")
->write("public function getSource()\n",
"{\n")
->indent()
->write("@trigger_error('The
'.__METHOD__.' method is deprecated since version 1.27 and will
be removed in 2.0. Use getSourceContext() instead.',
E_USER_DEPRECATED);\n\n")
->write('return
$this->getSourceContext()->getCode();')
->raw("\n")
->outdent()
->write("}\n\n")
;
}
protected function compileGetSourceContext(Compiler $compiler)
{
$compiler
->write("public function getSourceContext()\n",
"{\n")
->indent()
->write('return new Source(')
->string($compiler->getEnvironment()->isDebug() ?
$this->getSourceContext()->getCode() : '')
->raw(', ')
->string($this->getSourceContext()->getName())
->raw(', ')
->string($this->getSourceContext()->getPath())
->raw(");\n")
->outdent()
->write("}\n")
;
}
protected function compileLoadTemplate(Compiler $compiler, $node, $var)
{
if ($node instanceof ConstantExpression) {
$compiler
->write(sprintf('%s =
$this->loadTemplate(', $var))
->subcompile($node)
->raw(', ')
->repr($node->getTemplateName())
->raw(', ')
->repr($node->getTemplateLine())
->raw(");\n")
;
} else {
throw new \LogicException('Trait templates can only be
constant nodes.');
}
}
}
class_alias('Twig\Node\ModuleNode',
'Twig_Node_Module');
Node.php000064400000016545151163451170006161 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Source;
/**
* Represents a node in the AST.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Node implements \Twig_NodeInterface
{
protected $nodes;
protected $attributes;
protected $lineno;
protected $tag;
private $name;
private $sourceContext;
/**
* @param array $nodes An array of named nodes
* @param array $attributes An array of attributes (should not be
nodes)
* @param int $lineno The line number
* @param string $tag The tag name associated with the Node
*/
public function __construct(array $nodes = [], array $attributes = [],
$lineno = 0, $tag = null)
{
foreach ($nodes as $name => $node) {
if (!$node instanceof \Twig_NodeInterface) {
@trigger_error(sprintf('Using "%s" for the
value of node "%s" of "%s" is deprecated since version
1.25 and will be removed in 2.0.', \is_object($node) ?
\get_class($node) : (null === $node ? 'null' : \gettype($node)),
$name, \get_class($this)), E_USER_DEPRECATED);
}
}
$this->nodes = $nodes;
$this->attributes = $attributes;
$this->lineno = $lineno;
$this->tag = $tag;
}
public function __toString()
{
$attributes = [];
foreach ($this->attributes as $name => $value) {
$attributes[] = sprintf('%s: %s', $name,
str_replace("\n", '', var_export($value, true)));
}
$repr = [\get_class($this).'('.implode(', ',
$attributes)];
if (\count($this->nodes)) {
foreach ($this->nodes as $name => $node) {
$len = \strlen($name) + 4;
$noderepr = [];
foreach (explode("\n", (string) $node) as $line)
{
$noderepr[] = str_repeat(' ', $len).$line;
}
$repr[] = sprintf(' %s: %s', $name,
ltrim(implode("\n", $noderepr)));
}
$repr[] = ')';
} else {
$repr[0] .= ')';
}
return implode("\n", $repr);
}
/**
* @deprecated since 1.16.1 (to be removed in 2.0)
*/
public function toXml($asDom = false)
{
@trigger_error(sprintf('%s is deprecated since version 1.16.1
and will be removed in 2.0.', __METHOD__), E_USER_DEPRECATED);
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom->appendChild($xml =
$dom->createElement('twig'));
$xml->appendChild($node =
$dom->createElement('node'));
$node->setAttribute('class', \get_class($this));
foreach ($this->attributes as $name => $value) {
$node->appendChild($attribute =
$dom->createElement('attribute'));
$attribute->setAttribute('name', $name);
$attribute->appendChild($dom->createTextNode($value));
}
foreach ($this->nodes as $name => $n) {
if (null === $n) {
continue;
}
$child =
$n->toXml(true)->getElementsByTagName('node')->item(0);
$child = $dom->importNode($child, true);
$child->setAttribute('name', $name);
$node->appendChild($child);
}
return $asDom ? $dom : $dom->saveXML();
}
public function compile(Compiler $compiler)
{
foreach ($this->nodes as $node) {
$node->compile($compiler);
}
}
public function getTemplateLine()
{
return $this->lineno;
}
/**
* @deprecated since 1.27 (to be removed in 2.0)
*/
public function getLine()
{
@trigger_error('The '.__METHOD__.' method is
deprecated since version 1.27 and will be removed in 2.0. Use
getTemplateLine() instead.', E_USER_DEPRECATED);
return $this->lineno;
}
public function getNodeTag()
{
return $this->tag;
}
/**
* @return bool
*/
public function hasAttribute($name)
{
return \array_key_exists($name, $this->attributes);
}
/**
* @return mixed
*/
public function getAttribute($name)
{
if (!\array_key_exists($name, $this->attributes)) {
throw new \LogicException(sprintf('Attribute
"%s" does not exist for Node "%s".', $name,
\get_class($this)));
}
return $this->attributes[$name];
}
/**
* @param string $name
* @param mixed $value
*/
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
}
public function removeAttribute($name)
{
unset($this->attributes[$name]);
}
/**
* @return bool
*/
public function hasNode($name)
{
return \array_key_exists($name, $this->nodes);
}
/**
* @return Node
*/
public function getNode($name)
{
if (!\array_key_exists($name, $this->nodes)) {
throw new \LogicException(sprintf('Node "%s"
does not exist for Node "%s".', $name, \get_class($this)));
}
return $this->nodes[$name];
}
public function setNode($name, $node = null)
{
if (!$node instanceof \Twig_NodeInterface) {
@trigger_error(sprintf('Using "%s" for the value
of node "%s" of "%s" is deprecated since version 1.25
and will be removed in 2.0.', \is_object($node) ? \get_class($node) :
(null === $node ? 'null' : \gettype($node)), $name,
\get_class($this)), E_USER_DEPRECATED);
}
$this->nodes[$name] = $node;
}
public function removeNode($name)
{
unset($this->nodes[$name]);
}
public function count()
{
return \count($this->nodes);
}
public function getIterator()
{
return new \ArrayIterator($this->nodes);
}
public function setTemplateName($name)
{
$this->name = $name;
foreach ($this->nodes as $node) {
if (null !== $node) {
$node->setTemplateName($name);
}
}
}
public function getTemplateName()
{
return $this->name;
}
public function setSourceContext(Source $source)
{
$this->sourceContext = $source;
foreach ($this->nodes as $node) {
if ($node instanceof Node) {
$node->setSourceContext($source);
}
}
}
public function getSourceContext()
{
return $this->sourceContext;
}
/**
* @deprecated since 1.27 (to be removed in 2.0)
*/
public function setFilename($name)
{
@trigger_error('The '.__METHOD__.' method is
deprecated since version 1.27 and will be removed in 2.0. Use
setTemplateName() instead.', E_USER_DEPRECATED);
$this->setTemplateName($name);
}
/**
* @deprecated since 1.27 (to be removed in 2.0)
*/
public function getFilename()
{
@trigger_error('The '.__METHOD__.' method is
deprecated since version 1.27 and will be removed in 2.0. Use
getTemplateName() instead.', E_USER_DEPRECATED);
return $this->name;
}
}
class_alias('Twig\Node\Node', 'Twig_Node');
// Ensure that the aliased name is loaded to keep BC for classes
implementing the typehint with the old aliased name.
class_exists('Twig\Compiler');
NodeCaptureInterface.php000064400000000715151163451170011316
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
/**
* Represents a node that captures any nested displayable nodes.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface NodeCaptureInterface
{
}
class_alias('Twig\Node\NodeCaptureInterface',
'Twig_NodeCaptureInterface');
NodeOutputInterface.php000064400000000666151163451170011220
0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
/**
* Represents a displayable node in the AST.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface NodeOutputInterface
{
}
class_alias('Twig\Node\NodeOutputInterface',
'Twig_NodeOutputInterface');
PrintNode.php000064400000001635151163451170007170 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
/**
* Represents a node that outputs an expression.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class PrintNode extends Node implements NodeOutputInterface
{
public function __construct(AbstractExpression $expr, $lineno, $tag =
null)
{
parent::__construct(['expr' => $expr], [], $lineno,
$tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write('echo ')
->subcompile($this->getNode('expr'))
->raw(";\n")
;
}
}
class_alias('Twig\Node\PrintNode', 'Twig_Node_Print');
SandboxedPrintNode.php000064400000003457151163451170011024 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
/**
* Adds a check for the __toString() method when the variable is an object
and the sandbox is activated.
*
* When there is a simple Print statement, like {{ article }},
* and if the sandbox is enabled, we need to check that the __toString()
* method is allowed if 'article' is an object.
*
* Not used anymore, to be deprecated in 2.x and removed in 3.0
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SandboxedPrintNode extends PrintNode
{
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write('echo ')
;
$expr = $this->getNode('expr');
if ($expr instanceof ConstantExpression) {
$compiler
->subcompile($expr)
->raw(";\n")
;
} else {
$compiler
->write('$this->env->getExtension(\'\Twig\Extension\SandboxExtension\')->ensureToStringAllowed(')
->subcompile($expr)
->raw(");\n")
;
}
}
/**
* Removes node filters.
*
* This is mostly needed when another visitor adds filters (like the
escaper one).
*
* @return Node
*/
protected function removeNodeFilter(Node $node)
{
if ($node instanceof FilterExpression) {
return
$this->removeNodeFilter($node->getNode('node'));
}
return $node;
}
}
class_alias('Twig\Node\SandboxedPrintNode',
'Twig_Node_SandboxedPrint');
SandboxNode.php000064400000002215151163451170007465 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* Represents a sandbox node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SandboxNode extends Node
{
public function __construct(\Twig_NodeInterface $body, $lineno, $tag =
null)
{
parent::__construct(['body' => $body], [], $lineno,
$tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("if (!\$alreadySandboxed =
\$this->sandbox->isSandboxed()) {\n")
->indent()
->write("\$this->sandbox->enableSandbox();\n")
->outdent()
->write("}\n")
->subcompile($this->getNode('body'))
->write("if (!\$alreadySandboxed) {\n")
->indent()
->write("\$this->sandbox->disableSandbox();\n")
->outdent()
->write("}\n")
;
}
}
class_alias('Twig\Node\SandboxNode',
'Twig_Node_Sandbox');
SetNode.php000064400000006547151163451170006636 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
use Twig\Node\Expression\ConstantExpression;
/**
* Represents a set node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SetNode extends Node implements NodeCaptureInterface
{
public function __construct($capture, \Twig_NodeInterface $names,
\Twig_NodeInterface $values, $lineno, $tag = null)
{
parent::__construct(['names' => $names,
'values' => $values], ['capture' => $capture,
'safe' => false], $lineno, $tag);
/*
* Optimizes the node when capture is used for a large block of
text.
*
* {% set foo %}foo{% endset %} is compiled to
$context['foo'] = new Twig\Markup("foo");
*/
if ($this->getAttribute('capture')) {
$this->setAttribute('safe', true);
$values = $this->getNode('values');
if ($values instanceof TextNode) {
$this->setNode('values', new
ConstantExpression($values->getAttribute('data'),
$values->getTemplateLine()));
$this->setAttribute('capture', false);
}
}
}
public function compile(Compiler $compiler)
{
$compiler->addDebugInfo($this);
if (\count($this->getNode('names')) > 1) {
$compiler->write('list(');
foreach ($this->getNode('names') as $idx =>
$node) {
if ($idx) {
$compiler->raw(', ');
}
$compiler->subcompile($node);
}
$compiler->raw(')');
} else {
if ($this->getAttribute('capture')) {
if ($compiler->getEnvironment()->isDebug()) {
$compiler->write("ob_start();\n");
} else {
$compiler->write("ob_start(function () { return
''; });\n");
}
$compiler
->subcompile($this->getNode('values'))
;
}
$compiler->subcompile($this->getNode('names'),
false);
if ($this->getAttribute('capture')) {
$compiler->raw(" = ('' === \$tmp =
ob_get_clean()) ? '' : new Markup(\$tmp,
\$this->env->getCharset())");
}
}
if (!$this->getAttribute('capture')) {
$compiler->raw(' = ');
if (\count($this->getNode('names')) > 1) {
$compiler->write('[');
foreach ($this->getNode('values') as $idx
=> $value) {
if ($idx) {
$compiler->raw(', ');
}
$compiler->subcompile($value);
}
$compiler->raw(']');
} else {
if ($this->getAttribute('safe')) {
$compiler
->raw("('' === \$tmp = ")
->subcompile($this->getNode('values'))
->raw(") ? '' : new Markup(\$tmp,
\$this->env->getCharset())")
;
} else {
$compiler->subcompile($this->getNode('values'));
}
}
}
$compiler->raw(";\n");
}
}
class_alias('Twig\Node\SetNode', 'Twig_Node_Set');
SetTempNode.php000064400000001644151163451170007455 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* @internal
*/
class SetTempNode extends Node
{
public function __construct($name, $lineno)
{
parent::__construct([], ['name' => $name], $lineno);
}
public function compile(Compiler $compiler)
{
$name = $this->getAttribute('name');
$compiler
->addDebugInfo($this)
->write('if (isset($context[')
->string($name)
->raw('])) { $_')
->raw($name)
->raw('_ = $context[')
->repr($name)
->raw(']; } else { $_')
->raw($name)
->raw("_ = null; }\n")
;
}
}
class_alias('Twig\Node\SetTempNode',
'Twig_Node_SetTemp');
SpacelessNode.php000064400000002153151163451170010012 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* Represents a spaceless node.
*
* It removes spaces between HTML tags.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SpacelessNode extends Node
{
public function __construct(\Twig_NodeInterface $body, $lineno, $tag =
'spaceless')
{
parent::__construct(['body' => $body], [], $lineno,
$tag);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
;
if ($compiler->getEnvironment()->isDebug()) {
$compiler->write("ob_start();\n");
} else {
$compiler->write("ob_start(function () { return
''; });\n");
}
$compiler
->subcompile($this->getNode('body'))
->write("echo
trim(preg_replace('/>\s+</', '><',
ob_get_clean()));\n")
;
}
}
class_alias('Twig\Node\SpacelessNode',
'Twig_Node_Spaceless');
TextNode.php000064400000001462151163451170007016 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* Represents a text node.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TextNode extends Node implements NodeOutputInterface
{
public function __construct($data, $lineno)
{
parent::__construct([], ['data' => $data], $lineno);
}
public function compile(Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write('echo ')
->string($this->getAttribute('data'))
->raw(";\n")
;
}
}
class_alias('Twig\Node\TextNode', 'Twig_Node_Text');
WithNode.php000064400000004241151163451200006775 0ustar00<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Node;
use Twig\Compiler;
/**
* Represents a nested "with" scope.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class WithNode extends Node
{
public function __construct(Node $body, Node $variables = null, $only =
false, $lineno, $tag = null)
{
$nodes = ['body' => $body];
if (null !== $variables) {
$nodes['variables'] = $variables;
}
parent::__construct($nodes, ['only' => (bool) $only],
$lineno, $tag);
}
public function compile(Compiler $compiler)
{
$compiler->addDebugInfo($this);
if ($this->hasNode('variables')) {
$node = $this->getNode('variables');
$varsName = $compiler->getVarName();
$compiler
->write(sprintf('$%s = ', $varsName))
->subcompile($node)
->raw(";\n")
->write(sprintf("if (!twig_test_iterable(\$%s))
{\n", $varsName))
->indent()
->write("throw new RuntimeError('Variables
passed to the \"with\" tag must be a hash.', ")
->repr($node->getTemplateLine())
->raw(", \$this->getSourceContext());\n")
->outdent()
->write("}\n")
->write(sprintf("\$%s =
twig_to_array(\$%s);\n", $varsName, $varsName))
;
if ($this->getAttribute('only')) {
$compiler->write("\$context = ['_parent'
=> \$context];\n");
} else {
$compiler->write("\$context['_parent'] =
\$context;\n");
}
$compiler->write(sprintf("\$context =
\$this->env->mergeGlobals(array_merge(\$context, \$%s));\n",
$varsName));
} else {
$compiler->write("\$context['_parent'] =
\$context;\n");
}
$compiler
->subcompile($this->getNode('body'))
->write("\$context =
\$context['_parent'];\n")
;
}
}
class_alias('Twig\Node\WithNode', 'Twig_Node_With');