Skip to content

Commit 592ddbc

Browse files
fix!: compatibilty fix with twig 3.21 (#34)
1 parent 1a18875 commit 592ddbc

12 files changed

+60
-64
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: true
1010
matrix:
1111
os: [ubuntu-latest]
12-
php: [8.0, 8.1, 8.2]
12+
php: [8.2, 8.3, 8.4]
1313
dependency-version: [prefer-lowest, prefer-stable]
1414

1515
name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
2+
cache/*
23
.php_cs
34
.php_cs.cache
45
.phpunit.result.cache

composer.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^7.4|^8.0",
20-
"twig/twig": "^3.0"
19+
"php": "^8.2",
20+
"twig/twig": "^3.21"
2121
},
2222
"require-dev": {
23-
"friendsofphp/php-cs-fixer": "^3.0",
24-
"pestphp/pest": "^1.0",
25-
"phpunit/phpunit": "^9.3",
26-
"symfony/var-dumper": "^5.2",
27-
"vimeo/psalm": "^3.11"
23+
"friendsofphp/php-cs-fixer": "^3.75",
24+
"pestphp/pest": "^3.8",
25+
"symfony/var-dumper": "^7.2",
26+
"vimeo/psalm": "^6.10"
2827
},
2928
"autoload": {
3029
"psr-4": {

phpunit.xml.dist

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
44
bootstrap="vendor/autoload.php"
55
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
verbose="true"
126
>
137
<testsuites>
14-
<testsuite name="Digital Test Suite">
8+
<testsuite name="Arch">
159
<directory>tests</directory>
1610
</testsuite>
1711
</testsuites>
18-
<coverage>
12+
<source>
1913
<include>
20-
<directory suffix=".php">./src</directory>
14+
<directory>src</directory>
2115
</include>
22-
<report>
23-
<html outputDirectory="build/coverage"/>
24-
<text outputFile="build/coverage.txt"/>
25-
<clover outputFile="build/logs/clover.xml"/>
26-
</report>
27-
</coverage>
28-
<logging>
29-
<junit outputFile="build/report.junit.xml"/>
30-
</logging>
16+
<exclude>
17+
<directory>vendor</directory>
18+
</exclude>
19+
</source>
3120
</phpunit>

src/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ public function setup()
157157
$this->twig->setLexer(new ComponentLexer($this->twig));
158158
}
159159

160-
/** @var \Twig\Extension\EscaperExtension */
161-
$escaper = $this->twig->getExtension(\Twig\Extension\EscaperExtension::class);
160+
/** @var \Twig\Runtime\EscaperRuntime */
161+
$escaper = $this->twig->getRuntime(\Twig\Runtime\EscaperRuntime::class);
162162
$escaper->addSafeClass(ComponentAttributeBag::class, ['all']);
163163
$escaper->addSafeClass(ComponentSlot::class, ['all']);
164164
}

src/Node/ComponentNode.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,12 @@ protected function addGetTemplate(Compiler $compiler)
6161
$repr = $this->isDynamicComponent() ? 'raw' : 'repr';
6262

6363
$compiler
64-
->raw('$this->loadTemplate(' . PHP_EOL)
64+
->raw('$this->load(' . PHP_EOL)
6565
->indent(1)
6666
->write('')
6767
->$repr($this->getTemplateName())
6868
->raw(', ' . PHP_EOL)
6969
->write('')
70-
->$repr($this->getTemplateName())
71-
->raw(', ' . PHP_EOL)
72-
->write('')
7370
->repr($this->getTemplateLine())
7471
->indent(-1)
7572
->raw(PHP_EOL . ');' . PHP_EOL . PHP_EOL);
@@ -97,17 +94,19 @@ public function getDynamicComponent()
9794
if ($value->hasAttribute('value')) {
9895
// Returns the component string value
9996
$component = '\'' . $value->getAttribute('value') . '\'';
97+
10098
break;
10199
}
102100

103101
if ($value->hasAttribute('name')) {
104102
// Uses the context to get the component value
105103
$component = '($context[\'' . $value->getAttribute('name') . '\'] ?? null)';
104+
106105
break;
107106
}
108107
}
109108

110-
if (!$component) {
109+
if (! $component) {
111110
throw new Exception('Dynamic component must have a component attribute');
112111
}
113112

@@ -130,6 +129,7 @@ public static function parseDynamicComponent($path, $component)
130129
// Strip anything from the path before the dynamic component name, so it begins with the namespace
131130
$dynamicComponentEndPosition = strpos($path, self::DYNAMIC_COMPONENT_NAME) + strlen(self::DYNAMIC_COMPONENT_NAME);
132131
$pathEnd = substr($path, $dynamicComponentEndPosition);
132+
133133
return $component . $pathEnd;
134134
}
135135

@@ -185,7 +185,7 @@ protected function addTemplateArguments(Compiler $compiler)
185185

186186
public function filterVariables()
187187
{
188-
if (!$this->isDynamicComponent()) {
188+
if (! $this->isDynamicComponent()) {
189189
return;
190190
}
191191

src/Node/SlotNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class SlotNode extends Node implements NodeOutputInterface
1313
{
1414
public function __construct($name, $body, ?AbstractExpression $variables, int $lineno = 0)
1515
{
16-
parent::__construct(['body' => $body], ['name' => $name], $lineno, null);
16+
parent::__construct(['body' => $body], ['name' => $name], $lineno);
1717

1818
if ($variables) {
1919
$this->setNode('variables', $variables);

src/TokenParser/ComponentTokenParser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function parseArguments()
6666
}
6767

6868
if ($stream->nextIf(/* Token::NAME_TYPE */5, 'with')) {
69-
$variables = $this->parser->getExpressionParser()->parseExpression();
69+
$variables = $this->parser->parseExpression();
7070
}
7171

7272
$stream->expect(/* Token::BLOCK_END_TYPE */3);
@@ -80,9 +80,9 @@ public function parseComponentName(): string
8080

8181
$path = [];
8282

83-
if ($this->parser->getCurrentToken()->getType() != /** Token::NAME_TYPE */ 5) {
84-
throw new Exception('First token must be a name type');
85-
}
83+
// if ($this->parser->getCurrentToken()->getType() !== Token::NAME_TYPE) {
84+
// throw new Exception('First token must be a name type');
85+
// }
8686

8787
$name = $this->getNameSection();
8888

@@ -93,7 +93,7 @@ public function parseComponentName(): string
9393

9494
$path[] = $name;
9595

96-
while ($stream->nextIf(9 /** Token::PUNCTUATION_TYPE */, '.')) {
96+
while ($stream->nextIf(8 /** Token::PUNCTUATION_TYPE */, '.')) {
9797
$path[] = $this->getNameSection();
9898
}
9999

src/TokenParser/SlotTokenParser.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function parseArguments(): array
3333
}
3434

3535
if ($stream->nextIf(/* Token::NAME_TYPE */5, 'with')) {
36-
$variables = $this->parser->getExpressionParser()->parseExpression();
36+
$variables = $this->parser->parseExpression();
3737
}
3838

3939
$stream->expect(/* Token::BLOCK_END_TYPE */3);
@@ -45,9 +45,10 @@ public function parseSlotName(): string
4545
{
4646
$stream = $this->parser->getStream();
4747

48-
if ($this->parser->getCurrentToken()->getType() != /** Token::NAME_TYPE */ 5) {
49-
throw new Exception('First token must be a name type');
50-
}
48+
//$stream->expect(5);
49+
// if ($this->parser->getCurrentToken()->getType() != /** Token::NAME_TYPE */ 5) {
50+
// throw new Exception('First token must be a name type');
51+
// }
5152

5253
return $stream->next()->getValue();
5354
}

tests/ComponentTokenParserTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Performing\TwigComponents\Configuration;
66
use Performing\TwigComponents\TokenParser\ComponentTokenParser;
7+
use PHPUnit\Framework\Attributes\Test;
78
use PHPUnit\Framework\TestCase;
89

910
class ComponentTokenParserTest extends TestCase
1011
{
12+
#[Test]
1113
public function testGetComponentPathWithHintPath()
1214
{
1315
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');
@@ -27,6 +29,7 @@ public function testGetComponentPathWithHintPath()
2729
$this->assertEquals('mynamespace.myplugin::components.test.component', $componentPath);
2830
}
2931

32+
#[Test]
3033
public function testGetComponentPathWithoutHintPath()
3134
{
3235
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');

0 commit comments

Comments
 (0)