Skip to content

Commit

Permalink
[TwigComponent] Fix Live embedded component with namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakyvv committed Aug 30, 2023
1 parent 14115f1 commit 5ae1982
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
18 changes: 1 addition & 17 deletions src/TwigComponent/src/Twig/ComponentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function compile(Compiler $compiler): void
->raw('), ')
->raw($this->getAttribute('only') ? '[]' : '$context')
->raw(', ')
->string($this->parseTemplateName($this->getAttribute('name')))
->string(TemplateNameParser::parse($this->getAttribute('name')))
->raw(', ')
->raw($this->getAttribute('index'))
->raw(");\n")
Expand All @@ -91,20 +91,4 @@ public function compile(Compiler $compiler): void
->raw("\n")
;
}

/**
* Copied from Twig\Loader\FilesystemLoader, and adjusted to needs for this class.
*/
private function parseTemplateName(string $name): mixed
{
if (isset($name[0]) && '@' == $name[0]) {
if (false === $pos = strpos($name, '/')) {
throw new \LogicException(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
}

return substr($name, $pos + 1);
}

return $name;
}
}
2 changes: 1 addition & 1 deletion src/TwigComponent/src/Twig/ComponentTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function parse(Token $token): Node
$this->parser->embedTemplate($module);

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

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

Expand Down
26 changes: 26 additions & 0 deletions src/TwigComponent/src/Twig/TemplateNameParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Symfony\UX\TwigComponent\Twig;

final class TemplateNameParser
{
/**
* Copied from Twig\Loader\FilesystemLoader, and adjusted to needs for this class (no namespace returned).
*
* @see \Twig\Loader\FilesystemLoader::parseName
*/
public static function parse(string $name): mixed
{
if (isset($name[0]) && '@' == $name[0]) {
if (false === $pos = strpos($name, '/')) {
throw new \LogicException(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
}

return substr($name, $pos + 1);
}

return $name;
}
}

0 comments on commit 5ae1982

Please sign in to comment.