Skip to content

Commit 77a8655

Browse files
committed
[TwigComponent] Fix Live embedded component with namespace
1 parent 569173a commit 77a8655

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/Twig/ComponentNode.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function compile(Compiler $compiler): void
6868
->raw('), ')
6969
->raw($this->getAttribute('only') ? '[]' : '$context')
7070
->raw(', ')
71-
->string($this->parseTemplateName($this->getAttribute('name')))
71+
->string(TemplateNameParser::parse($this->getAttribute('name')))
7272
->raw(', ')
7373
->raw($this->getAttribute('index'))
7474
->raw(");\n")
@@ -91,20 +91,4 @@ public function compile(Compiler $compiler): void
9191
->raw("\n")
9292
;
9393
}
94-
95-
/**
96-
* Copied from Twig\Loader\FilesystemLoader, and adjusted to needs for this class.
97-
*/
98-
private function parseTemplateName(string $name): mixed
99-
{
100-
if (isset($name[0]) && '@' == $name[0]) {
101-
if (false === $pos = strpos($name, '/')) {
102-
throw new \LogicException(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
103-
}
104-
105-
return substr($name, $pos + 1);
106-
}
107-
108-
return $name;
109-
}
11094
}

src/Twig/ComponentTokenParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function parse(Token $token): Node
8787
$this->parser->embedTemplate($module);
8888

8989
// use deterministic index for the embedded template, so it can be loaded in a controlled manner
90-
$module->setAttribute('index', $this->generateEmbeddedTemplateIndex($stream->getSourceContext()->getName(), $token->getLine()));
90+
$module->setAttribute('index', $this->generateEmbeddedTemplateIndex(TemplateNameParser::parse($stream->getSourceContext()->getName()), $token->getLine()));
9191

9292
$stream->expect(Token::BLOCK_END_TYPE);
9393

src/Twig/TemplateNameParser.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\UX\TwigComponent\Twig;
6+
7+
final class TemplateNameParser
8+
{
9+
/**
10+
* Copied from Twig\Loader\FilesystemLoader, and adjusted to needs for this class (no namespace returned).
11+
*
12+
* @see \Twig\Loader\FilesystemLoader::parseName
13+
*/
14+
public static function parse(string $name): mixed
15+
{
16+
if (isset($name[0]) && '@' == $name[0]) {
17+
if (false === $pos = strpos($name, '/')) {
18+
throw new \LogicException(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
19+
}
20+
21+
return substr($name, $pos + 1);
22+
}
23+
24+
return $name;
25+
}
26+
}

0 commit comments

Comments
 (0)