Skip to content

Commit 114aeba

Browse files
committed
more robust deferred logic + deprecated fixes
Signed-off-by: Andy Miller <rhuk@mac.com>
1 parent 370dfd6 commit 114aeba

File tree

11 files changed

+114
-72
lines changed

11 files changed

+114
-72
lines changed

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

system/src/Grav/Common/Twig/Node/TwigNodeSwitch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(Node $value, Node $cases, ?Node $default = null, $li
3131
$nodes = ['value' => $value, 'cases' => $cases, 'default' => $default];
3232
$nodes = array_filter($nodes);
3333

34-
parent::__construct($nodes, [], $lineno, $tag);
34+
parent::__construct($nodes, [], $lineno);
3535
}
3636

3737
/**

system/src/Grav/Common/Twig/TokenParser/TwigTokenParserCache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function parse(Token $token)
4444
$lifetime = null;
4545
while (!$stream->test(Token::BLOCK_END_TYPE)) {
4646
if ($stream->test(Token::STRING_TYPE)) {
47-
$key = $this->parser->getExpressionParser()->parseExpression();
47+
$key = $this->parser->parseExpression();
4848
} elseif ($stream->test(Token::NUMBER_TYPE)) {
49-
$lifetime = $this->parser->getExpressionParser()->parseExpression();
49+
$lifetime = $this->parser->parseExpression();
5050
} else {
5151
throw new \Twig\Error\SyntaxError("Unexpected token type in cache tag.", $token->getLine(), $stream->getSourceContext());
5252
}

system/src/Grav/Common/Twig/TokenParser/TwigTokenParserLink.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@ protected function parseArguments(Token $token): array
7373

7474
$file = null;
7575
if (!$stream->test(Token::NAME_TYPE) && !$stream->test(Token::BLOCK_END_TYPE)) {
76-
$file = $this->parser->getExpressionParser()->parseExpression();
76+
$file = $this->parser->parseExpression();
7777
}
7878

7979
$group = null;
8080
if ($stream->nextIf(Token::NAME_TYPE, 'at')) {
81-
$group = $this->parser->getExpressionParser()->parseExpression();
81+
$group = $this->parser->parseExpression();
8282
}
8383

8484
$priority = null;
8585
if ($stream->nextIf(Token::NAME_TYPE, 'priority')) {
8686
$stream->expect(Token::PUNCTUATION_TYPE, ':');
87-
$priority = $this->parser->getExpressionParser()->parseExpression();
87+
$priority = $this->parser->parseExpression();
8888
}
8989

9090
$attributes = null;
9191
if ($stream->nextIf(Token::NAME_TYPE, 'with')) {
92-
$attributes = $this->parser->getExpressionParser()->parseExpression();
92+
$attributes = $this->parser->parseExpression();
9393
}
9494

9595
$stream->expect(Token::BLOCK_END_TYPE);

system/src/Grav/Common/Twig/TokenParser/TwigTokenParserRender.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ protected function parseArguments(Token $token): array
4444
{
4545
$stream = $this->parser->getStream();
4646

47-
$object = $this->parser->getExpressionParser()->parseExpression();
47+
$object = $this->parser->parseExpression();
4848

4949
$layout = null;
5050
if ($stream->nextIf(Token::NAME_TYPE, 'layout')) {
5151
$stream->expect(Token::PUNCTUATION_TYPE, ':');
52-
$layout = $this->parser->getExpressionParser()->parseExpression();
52+
$layout = $this->parser->parseExpression();
5353
}
5454

5555
$context = null;
5656
if ($stream->nextIf(Token::NAME_TYPE, 'with')) {
57-
$context = $this->parser->getExpressionParser()->parseExpression();
57+
$context = $this->parser->parseExpression();
5858
}
5959

6060
$stream->expect(Token::BLOCK_END_TYPE);

system/src/Grav/Common/Twig/TokenParser/TwigTokenParserScript.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,23 @@ protected function parseArguments(Token $token): array
8787

8888
$file = null;
8989
if (!$stream->test(Token::NAME_TYPE) && !$stream->test(Token::OPERATOR_TYPE, 'in') && !$stream->test(Token::BLOCK_END_TYPE)) {
90-
$file = $this->parser->getExpressionParser()->parseExpression();
90+
$file = $this->parser->parseExpression();
9191
}
9292

9393
$group = null;
9494
if ($stream->nextIf(Token::NAME_TYPE, 'at') || $stream->nextIf(Token::OPERATOR_TYPE, 'in')) {
95-
$group = $this->parser->getExpressionParser()->parseExpression();
95+
$group = $this->parser->parseExpression();
9696
}
9797

9898
$priority = null;
9999
if ($stream->nextIf(Token::NAME_TYPE, 'priority')) {
100100
$stream->expect(Token::PUNCTUATION_TYPE, ':');
101-
$priority = $this->parser->getExpressionParser()->parseExpression();
101+
$priority = $this->parser->parseExpression();
102102
}
103103

104104
$attributes = null;
105105
if ($stream->nextIf(Token::NAME_TYPE, 'with')) {
106-
$attributes = $this->parser->getExpressionParser()->parseExpression();
106+
$attributes = $this->parser->parseExpression();
107107
}
108108

109109
$stream->expect(Token::BLOCK_END_TYPE);

system/src/Grav/Common/Twig/TokenParser/TwigTokenParserStyle.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,23 @@ protected function parseArguments(Token $token): array
7474

7575
$file = null;
7676
if (!$stream->test(Token::NAME_TYPE) && !$stream->test(Token::OPERATOR_TYPE, 'in') && !$stream->test(Token::BLOCK_END_TYPE)) {
77-
$file = $this->parser->getExpressionParser()->parseExpression();
77+
$file = $this->parser->parseExpression();
7878
}
7979

8080
$group = null;
8181
if ($stream->nextIf(Token::NAME_TYPE, 'at') || $stream->nextIf(Token::OPERATOR_TYPE, 'in')) {
82-
$group = $this->parser->getExpressionParser()->parseExpression();
82+
$group = $this->parser->parseExpression();
8383
}
8484

8585
$priority = null;
8686
if ($stream->nextIf(Token::NAME_TYPE, 'priority')) {
8787
$stream->expect(Token::PUNCTUATION_TYPE, ':');
88-
$priority = $this->parser->getExpressionParser()->parseExpression();
88+
$priority = $this->parser->parseExpression();
8989
}
9090

9191
$attributes = null;
9292
if ($stream->nextIf(Token::NAME_TYPE, 'with')) {
93-
$attributes = $this->parser->getExpressionParser()->parseExpression();
93+
$attributes = $this->parser->parseExpression();
9494
}
9595

9696
$stream->expect(Token::BLOCK_END_TYPE);

system/src/Grav/Common/Twig/TokenParser/TwigTokenParserSwitch.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Grav\Common\Twig\Node\TwigNodeSwitch;
1414
use Twig\Error\SyntaxError;
1515
use Twig\Node\Node;
16+
use Twig\Node\Nodes;
1617
use Twig\Token;
1718
use Twig\TokenParser\AbstractTokenParser;
1819

@@ -40,21 +41,22 @@ public function parse(Token $token)
4041
$lineno = $token->getLine();
4142
$stream = $this->parser->getStream();
4243

43-
$name = $this->parser->getExpressionParser()->parseExpression();
44+
$name = $this->parser->parseExpression();
4445
$stream->expect(Token::BLOCK_END_TYPE);
4546

4647
// There can be some whitespace between the {% switch %} and first {% case %} tag.
47-
while ($stream->getCurrent()->getType() === Token::TEXT_TYPE && trim((string) $stream->getCurrent()->getValue()) === '') {
48+
while ($stream->getCurrent()->test(Token::TEXT_TYPE) && trim((string) $stream->getCurrent()->getValue()) === '') {
4849
$stream->next();
4950
}
5051

5152
$stream->expect(Token::BLOCK_START_TYPE);
5253

53-
$expressionParser = $this->parser->getExpressionParser();
54-
5554
$default = null;
5655
$cases = [];
5756
$end = false;
57+
58+
// 'or' operator precedence is 10. We want to stop parsing if we encounter it.
59+
$orPrecedence = 10;
5860

5961
while (!$end) {
6062
$next = $stream->next();
@@ -64,7 +66,7 @@ public function parse(Token $token)
6466
$values = [];
6567

6668
while (true) {
67-
$values[] = $expressionParser->parsePrimaryExpression();
69+
$values[] = $this->parser->parseExpression($orPrecedence + 1);
6870
// Multiple allowed values?
6971
if ($stream->test(Token::OPERATOR_TYPE, 'or')) {
7072
$stream->next();
@@ -75,10 +77,10 @@ public function parse(Token $token)
7577

7678
$stream->expect(Token::BLOCK_END_TYPE);
7779
$body = $this->parser->subparse($this->decideIfFork(...));
78-
$cases[] = new Node([
79-
'values' => new Node($values),
80+
$cases[] = new class([
81+
'values' => new Nodes($values),
8082
'body' => $body
81-
]);
83+
]) extends Node {};
8284
break;
8385

8486
case 'default':
@@ -97,7 +99,7 @@ public function parse(Token $token)
9799

98100
$stream->expect(Token::BLOCK_END_TYPE);
99101

100-
return new TwigNodeSwitch($name, new Node($cases), $default, $lineno, $this->getTag());
102+
return new TwigNodeSwitch($name, new Nodes($cases), $default, $lineno, $this->getTag());
101103
}
102104

103105
/**

system/src/Grav/Common/Twig/TokenParser/TwigTokenParserThrow.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function parse(Token $token)
3737
$stream = $this->parser->getStream();
3838

3939
$code = $stream->expect(Token::NUMBER_TYPE)->getValue();
40-
$message = $this->parser->getExpressionParser()->parseExpression();
40+
$message = $this->parser->parseExpression();
4141
$stream->expect(Token::BLOCK_END_TYPE);
4242

4343
return new TwigNodeThrow((int)$code, $message, $lineno, $this->getTag());

system/src/Grav/Common/Twig/TwigEnvironment.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
use Twig\Environment;
1313
use Twig\Error\LoaderError;
14+
use Twig\Extension\EscaperExtension;
15+
use Twig\Extension\ExtensionInterface;
1416
use Twig\Loader\ExistsLoaderInterface;
1517
use Twig\Loader\LoaderInterface;
18+
use Twig\Runtime\EscaperRuntime;
1619
use Twig\Template;
1720
use Twig\TemplateWrapper;
1821

@@ -22,6 +25,39 @@
2225
*/
2326
class TwigEnvironment extends Environment
2427
{
28+
/**
29+
* @inheritDoc
30+
*/
31+
public function getExtension(string $name): ExtensionInterface
32+
{
33+
$extension = parent::getExtension($name);
34+
35+
if ($name === EscaperExtension::class && class_exists(EscaperRuntime::class)) {
36+
return new class($extension, $this) extends EscaperExtension {
37+
private $original;
38+
private $env;
39+
40+
public function __construct($original, $env)
41+
{
42+
$this->original = $original;
43+
$this->env = $env;
44+
}
45+
46+
public function setEscaper($strategy, $callable)
47+
{
48+
$this->env->getRuntime(EscaperRuntime::class)->setEscaper($strategy, $callable);
49+
}
50+
51+
public function getDefaultStrategy($filename)
52+
{
53+
return $this->original->getDefaultStrategy($filename);
54+
}
55+
};
56+
}
57+
58+
return $extension;
59+
}
60+
2561
/**
2662
* @inheritDoc
2763
*

0 commit comments

Comments
 (0)