File tree Expand file tree Collapse file tree 2 files changed +55
-5
lines changed Expand file tree Collapse file tree 2 files changed +55
-5
lines changed Original file line number Diff line number Diff line change 1111
1212namespace Symfony \UX \TwigComponent \Twig ;
1313
14+ /**
15+ * @internal
16+ */
1417final 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 ;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments