Skip to content

[TwigComponent] Fix LiveComponent namespace mapping #1772

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 1 commit into from
Apr 25, 2024
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
13 changes: 8 additions & 5 deletions src/TwigComponent/src/Twig/TemplateNameParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@

namespace Symfony\UX\TwigComponent\Twig;

/**
* @internal
*/
final class TemplateNameParser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this class still necessary?

It's not parsing a template name anymore, but just validating the template name, which already happens in Twig\Loader\FilesystemLoader::parseName (in all scenarios in this class, it's just returning the name as-is, so it's just duplicating the validation logic from FilesystemLoader)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we cannot remove it because BC i guess.. @kbond ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consider this class internal so let's remove if not required anymore.

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

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

return $name;
Expand Down
47 changes: 47 additions & 0 deletions src/TwigComponent/tests/Unit/Twig/TemplateNameParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\TwigComponent\Tests\Unit\Twig;

use PHPUnit\Framework\TestCase;
use Symfony\UX\TwigComponent\Twig\TemplateNameParser;

/**
* @author Simon André <[email protected]>
*/
class TemplateNameParserTest extends TestCase
{
/**
* @dataProvider provideParse
*/
public function testParse(string $name, string $parsedName): void
{
$this->assertSame($parsedName, TemplateNameParser::parse($name));
}

public function testParseThrowsExceptionWhenInvalidName(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Malformed namespaced template name "@foo" (expecting "@namespace/template_name").');
TemplateNameParser::parse('@foo');
}

/**
* @return iterable<array{0: string, 1: string}>
*/
public static function provideParse(): iterable
{
yield ['foo', 'foo'];
yield ['foo/bar', 'foo/bar'];
yield ['@Admin/foo', '@Admin/foo'];
yield ['Admin/foo', 'Admin/foo'];
}
}