Skip to content

Commit b54be75

Browse files
committed
bug #1772 [TwigComponent] Fix LiveComponent namespace mapping (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] Fix LiveComponent namespace mapping I believe this modification fix #1754 **Original problem** If you render a template `@Acme`/dashboard.html.twig, embedded Components (live or twig) referenced wrong template (dashboard.html.twig here), making LiveComponent impossible to rerender (this file beeing not referenced in the TemplateMap). This behaviour was introduced in #1082 (and worked then). But i'm convinced all the work/changes made by Ryan to fix the embed bug (#1247 and related) made this obselete (and in this particular situation even originates a bug) All tests are green, and i'd like to test it more heavily... but before that : do you think this is a bug fix or is there some kind of BC break here? Commits ------- bb76055 [TwigComponent] Fix LiveComponent namespace mapping
2 parents abd8a44 + bb76055 commit b54be75

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/TwigComponent/src/Twig/TemplateNameParser.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@
1111

1212
namespace Symfony\UX\TwigComponent\Twig;
1313

14+
/**
15+
* @internal
16+
*/
1417
final class TemplateNameParser
1518
{
1619
/**
17-
* Copied from Twig\Loader\FilesystemLoader, and adjusted to needs for this class (no namespace returned).
20+
* Copied from Twig\Loader\FilesystemLoader, and adjusted to needs for this class.
1821
*
1922
* @see \Twig\Loader\FilesystemLoader::parseName
2023
*/
21-
public static function parse(string $name): mixed
24+
public static function parse(string $name): string
2225
{
23-
if (isset($name[0]) && '@' == $name[0]) {
24-
if (false === $pos = strpos($name, '/')) {
26+
if (str_starts_with($name, '@')) {
27+
if (!str_contains($name, '/')) {
2528
throw new \LogicException(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
2629
}
2730

28-
return substr($name, $pos + 1);
31+
return $name;
2932
}
3033

3134
return $name;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\TwigComponent\Tests\Unit\Twig;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\UX\TwigComponent\Twig\TemplateNameParser;
16+
17+
/**
18+
* @author Simon André <smn.andre@gmail.com>
19+
*/
20+
class TemplateNameParserTest extends TestCase
21+
{
22+
/**
23+
* @dataProvider provideParse
24+
*/
25+
public function testParse(string $name, string $parsedName): void
26+
{
27+
$this->assertSame($parsedName, TemplateNameParser::parse($name));
28+
}
29+
30+
public function testParseThrowsExceptionWhenInvalidName(): void
31+
{
32+
$this->expectException(\LogicException::class);
33+
$this->expectExceptionMessage('Malformed namespaced template name "@foo" (expecting "@namespace/template_name").');
34+
TemplateNameParser::parse('@foo');
35+
}
36+
37+
/**
38+
* @return iterable<array{0: string, 1: string}>
39+
*/
40+
public static function provideParse(): iterable
41+
{
42+
yield ['foo', 'foo'];
43+
yield ['foo/bar', 'foo/bar'];
44+
yield ['@Admin/foo', '@Admin/foo'];
45+
yield ['Admin/foo', 'Admin/foo'];
46+
}
47+
}

0 commit comments

Comments
 (0)