Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/sitepres.../lib/twig/src
File: Parser.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/*
[2] Fix | Delete
* This file is part of Twig.
[3] Fix | Delete
*
[4] Fix | Delete
* (c) Fabien Potencier
[5] Fix | Delete
* (c) Armin Ronacher
[6] Fix | Delete
*
[7] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[8] Fix | Delete
* file that was distributed with this source code.
[9] Fix | Delete
*/
[10] Fix | Delete
namespace WPML\Core\Twig;
[11] Fix | Delete
[12] Fix | Delete
use WPML\Core\Twig\Error\SyntaxError;
[13] Fix | Delete
use WPML\Core\Twig\Node\BlockNode;
[14] Fix | Delete
use WPML\Core\Twig\Node\BlockReferenceNode;
[15] Fix | Delete
use WPML\Core\Twig\Node\BodyNode;
[16] Fix | Delete
use WPML\Core\Twig\Node\Expression\AbstractExpression;
[17] Fix | Delete
use WPML\Core\Twig\Node\MacroNode;
[18] Fix | Delete
use WPML\Core\Twig\Node\ModuleNode;
[19] Fix | Delete
use WPML\Core\Twig\Node\Node;
[20] Fix | Delete
use WPML\Core\Twig\Node\NodeCaptureInterface;
[21] Fix | Delete
use WPML\Core\Twig\Node\NodeOutputInterface;
[22] Fix | Delete
use WPML\Core\Twig\Node\PrintNode;
[23] Fix | Delete
use WPML\Core\Twig\Node\TextNode;
[24] Fix | Delete
use WPML\Core\Twig\NodeVisitor\NodeVisitorInterface;
[25] Fix | Delete
use WPML\Core\Twig\TokenParser\TokenParserInterface;
[26] Fix | Delete
/**
[27] Fix | Delete
* Default parser implementation.
[28] Fix | Delete
*
[29] Fix | Delete
* @author Fabien Potencier <fabien@symfony.com>
[30] Fix | Delete
*/
[31] Fix | Delete
class Parser implements \WPML\Core\Twig_ParserInterface
[32] Fix | Delete
{
[33] Fix | Delete
protected $stack = [];
[34] Fix | Delete
protected $stream;
[35] Fix | Delete
protected $parent;
[36] Fix | Delete
protected $handlers;
[37] Fix | Delete
protected $visitors;
[38] Fix | Delete
protected $expressionParser;
[39] Fix | Delete
protected $blocks;
[40] Fix | Delete
protected $blockStack;
[41] Fix | Delete
protected $macros;
[42] Fix | Delete
protected $env;
[43] Fix | Delete
protected $reservedMacroNames;
[44] Fix | Delete
protected $importedSymbols;
[45] Fix | Delete
protected $traits;
[46] Fix | Delete
protected $embeddedTemplates = [];
[47] Fix | Delete
private $varNameSalt = 0;
[48] Fix | Delete
public function __construct(\WPML\Core\Twig\Environment $env)
[49] Fix | Delete
{
[50] Fix | Delete
$this->env = $env;
[51] Fix | Delete
}
[52] Fix | Delete
/**
[53] Fix | Delete
* @deprecated since 1.27 (to be removed in 2.0)
[54] Fix | Delete
*/
[55] Fix | Delete
public function getEnvironment()
[56] Fix | Delete
{
[57] Fix | Delete
@\trigger_error('The ' . __METHOD__ . ' method is deprecated since version 1.27 and will be removed in 2.0.', \E_USER_DEPRECATED);
[58] Fix | Delete
return $this->env;
[59] Fix | Delete
}
[60] Fix | Delete
public function getVarName()
[61] Fix | Delete
{
[62] Fix | Delete
return \sprintf('__internal_%s', \hash('sha256', __METHOD__ . $this->stream->getSourceContext()->getCode() . $this->varNameSalt++));
[63] Fix | Delete
}
[64] Fix | Delete
/**
[65] Fix | Delete
* @deprecated since 1.27 (to be removed in 2.0). Use $parser->getStream()->getSourceContext()->getPath() instead.
[66] Fix | Delete
*/
[67] Fix | Delete
public function getFilename()
[68] Fix | Delete
{
[69] Fix | Delete
@\trigger_error(\sprintf('The "%s" method is deprecated since version 1.27 and will be removed in 2.0. Use $parser->getStream()->getSourceContext()->getPath() instead.', __METHOD__), \E_USER_DEPRECATED);
[70] Fix | Delete
return $this->stream->getSourceContext()->getName();
[71] Fix | Delete
}
[72] Fix | Delete
public function parse(\WPML\Core\Twig\TokenStream $stream, $test = null, $dropNeedle = \false)
[73] Fix | Delete
{
[74] Fix | Delete
// push all variables into the stack to keep the current state of the parser
[75] Fix | Delete
// using get_object_vars() instead of foreach would lead to https://bugs.php.net/71336
[76] Fix | Delete
// This hack can be removed when min version if PHP 7.0
[77] Fix | Delete
$vars = [];
[78] Fix | Delete
foreach ($this as $k => $v) {
[79] Fix | Delete
$vars[$k] = $v;
[80] Fix | Delete
}
[81] Fix | Delete
unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames']);
[82] Fix | Delete
$this->stack[] = $vars;
[83] Fix | Delete
// tag handlers
[84] Fix | Delete
if (null === $this->handlers) {
[85] Fix | Delete
$this->handlers = $this->env->getTokenParsers();
[86] Fix | Delete
$this->handlers->setParser($this);
[87] Fix | Delete
}
[88] Fix | Delete
// node visitors
[89] Fix | Delete
if (null === $this->visitors) {
[90] Fix | Delete
$this->visitors = $this->env->getNodeVisitors();
[91] Fix | Delete
}
[92] Fix | Delete
if (null === $this->expressionParser) {
[93] Fix | Delete
$this->expressionParser = new \WPML\Core\Twig\ExpressionParser($this, $this->env);
[94] Fix | Delete
}
[95] Fix | Delete
$this->stream = $stream;
[96] Fix | Delete
$this->parent = null;
[97] Fix | Delete
$this->blocks = [];
[98] Fix | Delete
$this->macros = [];
[99] Fix | Delete
$this->traits = [];
[100] Fix | Delete
$this->blockStack = [];
[101] Fix | Delete
$this->importedSymbols = [[]];
[102] Fix | Delete
$this->embeddedTemplates = [];
[103] Fix | Delete
$this->varNameSalt = 0;
[104] Fix | Delete
try {
[105] Fix | Delete
$body = $this->subparse($test, $dropNeedle);
[106] Fix | Delete
if (null !== $this->parent && null === ($body = $this->filterBodyNodes($body))) {
[107] Fix | Delete
$body = new \WPML\Core\Twig\Node\Node();
[108] Fix | Delete
}
[109] Fix | Delete
} catch (\WPML\Core\Twig\Error\SyntaxError $e) {
[110] Fix | Delete
if (!$e->getSourceContext()) {
[111] Fix | Delete
$e->setSourceContext($this->stream->getSourceContext());
[112] Fix | Delete
}
[113] Fix | Delete
if (!$e->getTemplateLine()) {
[114] Fix | Delete
$e->setTemplateLine($this->stream->getCurrent()->getLine());
[115] Fix | Delete
}
[116] Fix | Delete
throw $e;
[117] Fix | Delete
}
[118] Fix | Delete
$node = new \WPML\Core\Twig\Node\ModuleNode(new \WPML\Core\Twig\Node\BodyNode([$body]), $this->parent, new \WPML\Core\Twig\Node\Node($this->blocks), new \WPML\Core\Twig\Node\Node($this->macros), new \WPML\Core\Twig\Node\Node($this->traits), $this->embeddedTemplates, $stream->getSourceContext());
[119] Fix | Delete
$traverser = new \WPML\Core\Twig\NodeTraverser($this->env, $this->visitors);
[120] Fix | Delete
$node = $traverser->traverse($node);
[121] Fix | Delete
// restore previous stack so previous parse() call can resume working
[122] Fix | Delete
foreach (\array_pop($this->stack) as $key => $val) {
[123] Fix | Delete
$this->{$key} = $val;
[124] Fix | Delete
}
[125] Fix | Delete
return $node;
[126] Fix | Delete
}
[127] Fix | Delete
public function subparse($test, $dropNeedle = \false)
[128] Fix | Delete
{
[129] Fix | Delete
$lineno = $this->getCurrentToken()->getLine();
[130] Fix | Delete
$rv = [];
[131] Fix | Delete
while (!$this->stream->isEOF()) {
[132] Fix | Delete
switch ($this->getCurrentToken()->getType()) {
[133] Fix | Delete
case \WPML\Core\Twig\Token::TEXT_TYPE:
[134] Fix | Delete
$token = $this->stream->next();
[135] Fix | Delete
$rv[] = new \WPML\Core\Twig\Node\TextNode($token->getValue(), $token->getLine());
[136] Fix | Delete
break;
[137] Fix | Delete
case \WPML\Core\Twig\Token::VAR_START_TYPE:
[138] Fix | Delete
$token = $this->stream->next();
[139] Fix | Delete
$expr = $this->expressionParser->parseExpression();
[140] Fix | Delete
$this->stream->expect(\WPML\Core\Twig\Token::VAR_END_TYPE);
[141] Fix | Delete
$rv[] = new \WPML\Core\Twig\Node\PrintNode($expr, $token->getLine());
[142] Fix | Delete
break;
[143] Fix | Delete
case \WPML\Core\Twig\Token::BLOCK_START_TYPE:
[144] Fix | Delete
$this->stream->next();
[145] Fix | Delete
$token = $this->getCurrentToken();
[146] Fix | Delete
if (\WPML\Core\Twig\Token::NAME_TYPE !== $token->getType()) {
[147] Fix | Delete
throw new \WPML\Core\Twig\Error\SyntaxError('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext());
[148] Fix | Delete
}
[149] Fix | Delete
if (null !== $test && \call_user_func($test, $token)) {
[150] Fix | Delete
if ($dropNeedle) {
[151] Fix | Delete
$this->stream->next();
[152] Fix | Delete
}
[153] Fix | Delete
if (1 === \count($rv)) {
[154] Fix | Delete
return $rv[0];
[155] Fix | Delete
}
[156] Fix | Delete
return new \WPML\Core\Twig\Node\Node($rv, [], $lineno);
[157] Fix | Delete
}
[158] Fix | Delete
$subparser = $this->handlers->getTokenParser($token->getValue());
[159] Fix | Delete
if (null === $subparser) {
[160] Fix | Delete
if (null !== $test) {
[161] Fix | Delete
$e = new \WPML\Core\Twig\Error\SyntaxError(\sprintf('Unexpected "%s" tag', $token->getValue()), $token->getLine(), $this->stream->getSourceContext());
[162] Fix | Delete
if (\is_array($test) && isset($test[0]) && $test[0] instanceof \WPML\Core\Twig\TokenParser\TokenParserInterface) {
[163] Fix | Delete
$e->appendMessage(\sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $test[0]->getTag(), $lineno));
[164] Fix | Delete
}
[165] Fix | Delete
} else {
[166] Fix | Delete
$e = new \WPML\Core\Twig\Error\SyntaxError(\sprintf('Unknown "%s" tag.', $token->getValue()), $token->getLine(), $this->stream->getSourceContext());
[167] Fix | Delete
$e->addSuggestions($token->getValue(), \array_keys($this->env->getTags()));
[168] Fix | Delete
}
[169] Fix | Delete
throw $e;
[170] Fix | Delete
}
[171] Fix | Delete
$this->stream->next();
[172] Fix | Delete
$node = $subparser->parse($token);
[173] Fix | Delete
if (null !== $node) {
[174] Fix | Delete
$rv[] = $node;
[175] Fix | Delete
}
[176] Fix | Delete
break;
[177] Fix | Delete
default:
[178] Fix | Delete
throw new \WPML\Core\Twig\Error\SyntaxError('Lexer or parser ended up in unsupported state.', $this->getCurrentToken()->getLine(), $this->stream->getSourceContext());
[179] Fix | Delete
}
[180] Fix | Delete
}
[181] Fix | Delete
if (1 === \count($rv)) {
[182] Fix | Delete
return $rv[0];
[183] Fix | Delete
}
[184] Fix | Delete
return new \WPML\Core\Twig\Node\Node($rv, [], $lineno);
[185] Fix | Delete
}
[186] Fix | Delete
/**
[187] Fix | Delete
* @deprecated since 1.27 (to be removed in 2.0)
[188] Fix | Delete
*/
[189] Fix | Delete
public function addHandler($name, $class)
[190] Fix | Delete
{
[191] Fix | Delete
@\trigger_error('The ' . __METHOD__ . ' method is deprecated since version 1.27 and will be removed in 2.0.', \E_USER_DEPRECATED);
[192] Fix | Delete
$this->handlers[$name] = $class;
[193] Fix | Delete
}
[194] Fix | Delete
/**
[195] Fix | Delete
* @deprecated since 1.27 (to be removed in 2.0)
[196] Fix | Delete
*/
[197] Fix | Delete
public function addNodeVisitor(\WPML\Core\Twig\NodeVisitor\NodeVisitorInterface $visitor)
[198] Fix | Delete
{
[199] Fix | Delete
@\trigger_error('The ' . __METHOD__ . ' method is deprecated since version 1.27 and will be removed in 2.0.', \E_USER_DEPRECATED);
[200] Fix | Delete
$this->visitors[] = $visitor;
[201] Fix | Delete
}
[202] Fix | Delete
public function getBlockStack()
[203] Fix | Delete
{
[204] Fix | Delete
return $this->blockStack;
[205] Fix | Delete
}
[206] Fix | Delete
public function peekBlockStack()
[207] Fix | Delete
{
[208] Fix | Delete
return isset($this->blockStack[\count($this->blockStack) - 1]) ? $this->blockStack[\count($this->blockStack) - 1] : null;
[209] Fix | Delete
}
[210] Fix | Delete
public function popBlockStack()
[211] Fix | Delete
{
[212] Fix | Delete
\array_pop($this->blockStack);
[213] Fix | Delete
}
[214] Fix | Delete
public function pushBlockStack($name)
[215] Fix | Delete
{
[216] Fix | Delete
$this->blockStack[] = $name;
[217] Fix | Delete
}
[218] Fix | Delete
public function hasBlock($name)
[219] Fix | Delete
{
[220] Fix | Delete
return isset($this->blocks[$name]);
[221] Fix | Delete
}
[222] Fix | Delete
public function getBlock($name)
[223] Fix | Delete
{
[224] Fix | Delete
return $this->blocks[$name];
[225] Fix | Delete
}
[226] Fix | Delete
public function setBlock($name, \WPML\Core\Twig\Node\BlockNode $value)
[227] Fix | Delete
{
[228] Fix | Delete
$this->blocks[$name] = new \WPML\Core\Twig\Node\BodyNode([$value], [], $value->getTemplateLine());
[229] Fix | Delete
}
[230] Fix | Delete
public function hasMacro($name)
[231] Fix | Delete
{
[232] Fix | Delete
return isset($this->macros[$name]);
[233] Fix | Delete
}
[234] Fix | Delete
public function setMacro($name, \WPML\Core\Twig\Node\MacroNode $node)
[235] Fix | Delete
{
[236] Fix | Delete
if ($this->isReservedMacroName($name)) {
[237] Fix | Delete
throw new \WPML\Core\Twig\Error\SyntaxError(\sprintf('"%s" cannot be used as a macro name as it is a reserved keyword.', $name), $node->getTemplateLine(), $this->stream->getSourceContext());
[238] Fix | Delete
}
[239] Fix | Delete
$this->macros[$name] = $node;
[240] Fix | Delete
}
[241] Fix | Delete
public function isReservedMacroName($name)
[242] Fix | Delete
{
[243] Fix | Delete
if (null === $this->reservedMacroNames) {
[244] Fix | Delete
$this->reservedMacroNames = [];
[245] Fix | Delete
$r = new \ReflectionClass($this->env->getBaseTemplateClass());
[246] Fix | Delete
foreach ($r->getMethods() as $method) {
[247] Fix | Delete
$methodName = \strtolower($method->getName());
[248] Fix | Delete
if ('get' === \substr($methodName, 0, 3) && isset($methodName[3])) {
[249] Fix | Delete
$this->reservedMacroNames[] = \substr($methodName, 3);
[250] Fix | Delete
}
[251] Fix | Delete
}
[252] Fix | Delete
}
[253] Fix | Delete
return \in_array(\strtolower($name), $this->reservedMacroNames);
[254] Fix | Delete
}
[255] Fix | Delete
public function addTrait($trait)
[256] Fix | Delete
{
[257] Fix | Delete
$this->traits[] = $trait;
[258] Fix | Delete
}
[259] Fix | Delete
public function hasTraits()
[260] Fix | Delete
{
[261] Fix | Delete
return \count($this->traits) > 0;
[262] Fix | Delete
}
[263] Fix | Delete
public function embedTemplate(\WPML\Core\Twig\Node\ModuleNode $template)
[264] Fix | Delete
{
[265] Fix | Delete
$template->setIndex(\mt_rand());
[266] Fix | Delete
$this->embeddedTemplates[] = $template;
[267] Fix | Delete
}
[268] Fix | Delete
public function addImportedSymbol($type, $alias, $name = null, \WPML\Core\Twig\Node\Expression\AbstractExpression $node = null)
[269] Fix | Delete
{
[270] Fix | Delete
$this->importedSymbols[0][$type][$alias] = ['name' => $name, 'node' => $node];
[271] Fix | Delete
}
[272] Fix | Delete
public function getImportedSymbol($type, $alias)
[273] Fix | Delete
{
[274] Fix | Delete
if (null !== $this->peekBlockStack()) {
[275] Fix | Delete
foreach ($this->importedSymbols as $functions) {
[276] Fix | Delete
if (isset($functions[$type][$alias])) {
[277] Fix | Delete
if (\count($this->blockStack) > 1) {
[278] Fix | Delete
return null;
[279] Fix | Delete
}
[280] Fix | Delete
return $functions[$type][$alias];
[281] Fix | Delete
}
[282] Fix | Delete
}
[283] Fix | Delete
} else {
[284] Fix | Delete
return isset($this->importedSymbols[0][$type][$alias]) ? $this->importedSymbols[0][$type][$alias] : null;
[285] Fix | Delete
}
[286] Fix | Delete
}
[287] Fix | Delete
public function isMainScope()
[288] Fix | Delete
{
[289] Fix | Delete
return 1 === \count($this->importedSymbols);
[290] Fix | Delete
}
[291] Fix | Delete
public function pushLocalScope()
[292] Fix | Delete
{
[293] Fix | Delete
\array_unshift($this->importedSymbols, []);
[294] Fix | Delete
}
[295] Fix | Delete
public function popLocalScope()
[296] Fix | Delete
{
[297] Fix | Delete
\array_shift($this->importedSymbols);
[298] Fix | Delete
}
[299] Fix | Delete
/**
[300] Fix | Delete
* @return ExpressionParser
[301] Fix | Delete
*/
[302] Fix | Delete
public function getExpressionParser()
[303] Fix | Delete
{
[304] Fix | Delete
return $this->expressionParser;
[305] Fix | Delete
}
[306] Fix | Delete
public function getParent()
[307] Fix | Delete
{
[308] Fix | Delete
return $this->parent;
[309] Fix | Delete
}
[310] Fix | Delete
public function setParent($parent)
[311] Fix | Delete
{
[312] Fix | Delete
$this->parent = $parent;
[313] Fix | Delete
}
[314] Fix | Delete
/**
[315] Fix | Delete
* @return TokenStream
[316] Fix | Delete
*/
[317] Fix | Delete
public function getStream()
[318] Fix | Delete
{
[319] Fix | Delete
return $this->stream;
[320] Fix | Delete
}
[321] Fix | Delete
/**
[322] Fix | Delete
* @return Token
[323] Fix | Delete
*/
[324] Fix | Delete
public function getCurrentToken()
[325] Fix | Delete
{
[326] Fix | Delete
return $this->stream->getCurrent();
[327] Fix | Delete
}
[328] Fix | Delete
protected function filterBodyNodes(\WPML\Core\Twig_NodeInterface $node)
[329] Fix | Delete
{
[330] Fix | Delete
// check that the body does not contain non-empty output nodes
[331] Fix | Delete
if ($node instanceof \WPML\Core\Twig\Node\TextNode && !\ctype_space($node->getAttribute('data')) || !$node instanceof \WPML\Core\Twig\Node\TextNode && !$node instanceof \WPML\Core\Twig\Node\BlockReferenceNode && $node instanceof \WPML\Core\Twig\Node\NodeOutputInterface) {
[332] Fix | Delete
if (\false !== \strpos((string) $node, \chr(0xef) . \chr(0xbb) . \chr(0xbf))) {
[333] Fix | Delete
$t = \substr($node->getAttribute('data'), 3);
[334] Fix | Delete
if ('' === $t || \ctype_space($t)) {
[335] Fix | Delete
// bypass empty nodes starting with a BOM
[336] Fix | Delete
return;
[337] Fix | Delete
}
[338] Fix | Delete
}
[339] Fix | Delete
throw new \WPML\Core\Twig\Error\SyntaxError('A template that extends another one cannot include content outside Twig blocks. Did you forget to put the content inside a {% block %} tag?', $node->getTemplateLine(), $this->stream->getSourceContext());
[340] Fix | Delete
}
[341] Fix | Delete
// bypass nodes that will "capture" the output
[342] Fix | Delete
if ($node instanceof \WPML\Core\Twig\Node\NodeCaptureInterface) {
[343] Fix | Delete
return $node;
[344] Fix | Delete
}
[345] Fix | Delete
if ($node instanceof \WPML\Core\Twig\Node\NodeOutputInterface) {
[346] Fix | Delete
return;
[347] Fix | Delete
}
[348] Fix | Delete
foreach ($node as $k => $n) {
[349] Fix | Delete
if (null !== $n && null === $this->filterBodyNodes($n)) {
[350] Fix | Delete
$node->removeNode($k);
[351] Fix | Delete
}
[352] Fix | Delete
}
[353] Fix | Delete
return $node;
[354] Fix | Delete
}
[355] Fix | Delete
}
[356] Fix | Delete
\class_alias('WPML\\Core\\Twig\\Parser', 'WPML\\Core\\Twig_Parser');
[357] Fix | Delete
[358] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function