From 5ae1982e0ad382084202126474a244c9168b4924 Mon Sep 17 00:00:00 2001 From: sneakyvv Date: Wed, 30 Aug 2023 12:09:10 +0200 Subject: [PATCH] [TwigComponent] Fix Live embedded component with namespace --- src/TwigComponent/src/Twig/ComponentNode.php | 18 +------------ .../src/Twig/ComponentTokenParser.php | 2 +- .../src/Twig/TemplateNameParser.php | 26 +++++++++++++++++++ 3 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 src/TwigComponent/src/Twig/TemplateNameParser.php diff --git a/src/TwigComponent/src/Twig/ComponentNode.php b/src/TwigComponent/src/Twig/ComponentNode.php index d5f87e373c0..98d5aeff796 100644 --- a/src/TwigComponent/src/Twig/ComponentNode.php +++ b/src/TwigComponent/src/Twig/ComponentNode.php @@ -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") @@ -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; - } } diff --git a/src/TwigComponent/src/Twig/ComponentTokenParser.php b/src/TwigComponent/src/Twig/ComponentTokenParser.php index 3e4ee3f6e5f..1f98e90b0cd 100644 --- a/src/TwigComponent/src/Twig/ComponentTokenParser.php +++ b/src/TwigComponent/src/Twig/ComponentTokenParser.php @@ -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); diff --git a/src/TwigComponent/src/Twig/TemplateNameParser.php b/src/TwigComponent/src/Twig/TemplateNameParser.php new file mode 100644 index 00000000000..e87ed296026 --- /dev/null +++ b/src/TwigComponent/src/Twig/TemplateNameParser.php @@ -0,0 +1,26 @@ +