Repository: symfony/ux
Pull request #2506 (2025-01-16 13:24:33)

[TwigComponent] Remove obsolete TemplateNameParser
It no longer parses (and should not have been stripping namespaces as it did before)

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no 
| Issues        | 
| License       | MIT

As discussed in https://github.com/symfony/ux/pull/1772#issuecomment-2595130307, the code added in #1070 (& expanded in #1082) was not needed (even incorrect, based upon a decorator TemplateIterator of Shopware).


Patch:
diff --git a/src/TwigComponent/src/Twig/ComponentNode.php b/src/TwigComponent/src/Twig/ComponentNode.php
index 439175c5..df735079 100644
--- a/src/TwigComponent/src/Twig/ComponentNode.php
+++ b/src/TwigComponent/src/Twig/ComponentNode.php
@@ -112,7 +112,7 @@ final class ComponentNode extends Node implements NodeOutputInterface
             ->raw('), ')
             ->raw($this->getAttribute('only') ? '[]' : '$context')
             ->raw(', ')
-            ->string(TemplateNameParser::parse($this->getAttribute('embedded_template')))
+            ->string($this->getAttribute('embedded_template'))
             ->raw(', ')
             ->raw($this->getAttribute('embedded_index'))
             ->raw(");\n");
diff --git a/src/TwigComponent/src/Twig/ComponentTokenParser.php b/src/TwigComponent/src/Twig/ComponentTokenParser.php
index 3f94c09d..31756468 100644
--- a/src/TwigComponent/src/Twig/ComponentTokenParser.php
+++ b/src/TwigComponent/src/Twig/ComponentTokenParser.php
@@ -63,7 +63,7 @@ final class ComponentTokenParser extends AbstractTokenParser
         $this->parser->embedTemplate($module);
 
         // override the embedded index with a deterministic value, so it can be loaded in a controlled manner
-        $module->setAttribute('index', $this->generateEmbeddedTemplateIndex(TemplateNameParser::parse($stream->getSourceContext()->getName()), $token->getLine()));
+        $module->setAttribute('index', $this->generateEmbeddedTemplateIndex($stream->getSourceContext()->getName(), $token->getLine()));
 
         $stream->expect(Token::BLOCK_END_TYPE);
 
diff --git a/src/TwigComponent/src/Twig/TemplateNameParser.php b/src/TwigComponent/src/Twig/TemplateNameParser.php
deleted file mode 100644
index 2c9d5918..00000000
--- a/src/TwigComponent/src/Twig/TemplateNameParser.php
+++ /dev/null
@@ -1,36 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\UX\TwigComponent\Twig;
-
-/**
- * @internal
- */
-final class TemplateNameParser
-{
-    /**
-     * Copied from Twig\Loader\FilesystemLoader, and adjusted to needs for this class.
-     *
-     * @see \Twig\Loader\FilesystemLoader::parseName
-     */
-    public static function parse(string $name): string
-    {
-        if (str_starts_with($name, '@')) {
-            if (!str_contains($name, '/')) {
-                throw new \LogicException(\sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
-            }
-
-            return $name;
-        }
-
-        return $name;
-    }
-}


Test patch:
diff --git a/src/TwigComponent/tests/Unit/Twig/TemplateNameParserTest.php b/src/TwigComponent/tests/Unit/Twig/TemplateNameParserTest.php
deleted file mode 100644
index 890a8caa..00000000
--- a/src/TwigComponent/tests/Unit/Twig/TemplateNameParserTest.php
+++ /dev/null
@@ -1,47 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\UX\TwigComponent\Tests\Unit\Twig;
-
-use PHPUnit\Framework\TestCase;
-use Symfony\UX\TwigComponent\Twig\TemplateNameParser;
-
-/**
- * @author Simon André <smn.andre@gmail.com>
- */
-class TemplateNameParserTest extends TestCase
-{
-    /**
-     * @dataProvider provideParse
-     */
-    public function testParse(string $name, string $parsedName): void
-    {
-        $this->assertSame($parsedName, TemplateNameParser::parse($name));
-    }
-
-    public function testParseThrowsExceptionWhenInvalidName(): void
-    {
-        $this->expectException(\LogicException::class);
-        $this->expectExceptionMessage('Malformed namespaced template name "@foo" (expecting "@namespace/template_name").');
-        TemplateNameParser::parse('@foo');
-    }
-
-    /**
-     * @return iterable<array{0: string, 1: string}>
-     */
-    public static function provideParse(): iterable
-    {
-        yield ['foo', 'foo'];
-        yield ['foo/bar', 'foo/bar'];
-        yield ['@Admin/foo', '@Admin/foo'];
-        yield ['Admin/foo', 'Admin/foo'];
-    }
-}