Skip to content

[TwigComponent][LiveComponent] Fix Live embedded component within namespaced template #1082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,45 @@ public function testItUseBlocksFromEmbeddedContextUsingMultipleComponents(): voi
;
}

public function testItUseBlocksFromEmbeddedContextUsingMultipleComponentsWithNamespacedTemplate(): void
{
$templateName = 'render_multiple_embedded_with_blocks.html.twig';
$obscuredName = '5c474b02358c46cca3da7340cc79cc2e';

$this->addTemplateMap($obscuredName, $templateName);

$dehydrated = $this->dehydrateComponent(
$this->mountComponent(
'component2',
[
'data-host-template' => $obscuredName,
'data-embedded-template-index' => self::DETERMINISTIC_ID_MULTI_2,
]
)
);

$token = null;

$this->browser()
->visit('/render-namespaced-template/render_multiple_embedded_with_blocks')
->assertSuccessful()
->assertSeeIn('#component1', 'Overridden content from component 1')
->assertSeeIn('#component2', 'Overridden content from component 2 on same line - count: 1')
->assertSeeIn('#component3', 'PreReRenderCalled: No')
->use(function (Crawler $crawler) use (&$token) {
// get a valid token to use for actions
$token = $crawler->filter('div')->eq(1)->attr('data-live-csrf-value');
})
->post('/_components/component2/increase', [
'headers' => ['X-CSRF-TOKEN' => $token],
'body' => ['data' => json_encode(['props' => $dehydrated->getProps()])],
])
->assertSuccessful()
->assertHeaderContains('Content-Type', 'html')
->assertSee('Overridden content from component 2 on same line - count: 2')
;
}

public function testCanRedirectFromComponentAction(): void
{
$dehydrated = $this->dehydrateComponent($this->mountComponent('component2'));
Expand Down
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;
}
}