diff --git a/src/TwigComponent/doc/index.rst b/src/TwigComponent/doc/index.rst index d1fa5ee12fc..eba5bdf3d66 100644 --- a/src/TwigComponent/doc/index.rst +++ b/src/TwigComponent/doc/index.rst @@ -1634,7 +1634,7 @@ Anonymous Components .. versionadded:: 2.20 - The bundle convention for Anonymous components was added in TwigComponents 2.18. + The bundle convention for Anonymous components was added in TwigComponents 2.20. Using a component from a third-party bundle is just as straightforward as using one from your own application. Once the bundle is installed and configured, you diff --git a/src/TwigComponent/src/Command/TwigComponentDebugCommand.php b/src/TwigComponent/src/Command/TwigComponentDebugCommand.php index 2ceae41719b..4b43216acdf 100644 --- a/src/TwigComponent/src/Command/TwigComponentDebugCommand.php +++ b/src/TwigComponent/src/Command/TwigComponentDebugCommand.php @@ -184,9 +184,14 @@ private function findAnonymousComponents(): array foreach ($finderTemplates as $template) { $component = str_replace('/', ':', $template->getRelativePathname()); $component = substr($component, 0, -10); // remove file extension ".html.twig" + $path = $template->getPath(); - if (isset($dirs[$template->getPath()]) && FilesystemLoader::MAIN_NAMESPACE !== $dirs[$template->getPath()]) { - $component = $dirs[$template->getPath()].':'.$component; + if ($template->getRelativePath()) { + $path = \rtrim(\substr($template->getPath(), 0, -1 * \strlen($template->getRelativePath())), '/'); + } + + if (isset($dirs[$path]) && FilesystemLoader::MAIN_NAMESPACE !== $dirs[$path]) { + $component = $dirs[$path].':'.$component; } $components[$component] = $component; diff --git a/src/TwigComponent/tests/Fixtures/Bundle/AcmeBundle/templates/components/Table/Header.html.twig b/src/TwigComponent/tests/Fixtures/Bundle/AcmeBundle/templates/components/Table/Header.html.twig new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/TwigComponent/tests/Integration/Command/TwigComponentDebugCommandTest.php b/src/TwigComponent/tests/Integration/Command/TwigComponentDebugCommandTest.php index f94b25d182b..0e6ffa8d895 100644 --- a/src/TwigComponent/tests/Integration/Command/TwigComponentDebugCommandTest.php +++ b/src/TwigComponent/tests/Integration/Command/TwigComponentDebugCommandTest.php @@ -165,6 +165,21 @@ public function testWithBundleAnonymousComponent(): void $this->assertStringContainsString('Anonymous', $display); } + public function testWithBundleAnonymousComponentSubDir(): void + { + $commandTester = $this->createCommandTester(); + $commandTester->execute(['name' => 'Acme:Table:Header']); + + $commandTester->assertCommandIsSuccessful(); + + $display = $commandTester->getDisplay(); + + $this->tableDisplayCheck($display); + $this->assertStringContainsString('Acme:Table:Header', $display); + $this->assertStringContainsString('@Acme/components/Table/Header.html.twig', $display); + $this->assertStringContainsString('Anonymous', $display); + } + public function testWithoutPublicProps(): void { $commandTester = $this->createCommandTester();