Skip to content

Commit 673279f

Browse files
committed
bug #1882 [TwigComponent] Restrict anonymous component lookup to Twig files in debug command (squrious)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] Restrict anonymous component lookup to Twig files in debug command | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | | License | MIT Hi! The `debug:twig-component` command looks for files in the anonymous template directory, but fails if some files are actually not Twig components (like .md). For example, with a `README.md` file in `templates/components`: ```shell $ php bin/console debug:twig-component [critical] Error thrown while running command "debug:twig-component". Message: "Unknown component "README.md". And no matching anonymous component template was found." In ComponentFactory.php line 254: Unknown component "README.md". And no matching anonymous component template was found. debug:twig-component [<name>] ``` This PR restricts the finder to `*.html.twig` files, so others are ignored. Commits ------- df82b4a [TwigComponent] Restrict anonymous component lookup to Twig files in debug command
2 parents b797732 + df82b4a commit 673279f

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/TwigComponent/src/Command/TwigComponentDebugCommand.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,10 @@ private function findAnonymousComponents(): array
151151
$components = [];
152152
$anonymousPath = $this->twigTemplatesPath.'/'.$this->anonymousDirectory;
153153
$finderTemplates = new Finder();
154-
$finderTemplates->files()->in($anonymousPath)->notPath('/_');
154+
$finderTemplates->files()->in($anonymousPath)->notPath('/_')->name('*.html.twig');
155155
foreach ($finderTemplates as $template) {
156156
$component = str_replace('/', ':', $template->getRelativePathname());
157-
if (str_ends_with($component, '.html.twig')) {
158-
$component = substr($component, 0, -10);
159-
}
157+
$component = substr($component, 0, -10);
160158
$components[$component] = $component;
161159
}
162160

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file should not cause issues with the debug:twig:component command.

src/TwigComponent/tests/Integration/Command/TwigComponentDebugCommandTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public function testWithNoMatchComponent(): void
4141
$this->assertStringContainsString('Unknown component "NoMatchComponent".', $commandTester->getDisplay());
4242
}
4343

44+
public function testNotComponentsIsNotListed(): void
45+
{
46+
$commandTester = $this->createCommandTester();
47+
$result = $commandTester->execute(['name' => 'NotAComponent']);
48+
49+
$this->assertEquals(1, $result);
50+
$this->assertStringContainsString('Unknown component "NotAComponent".', $commandTester->getDisplay());
51+
}
52+
4453
public function testWithOnePartialMatchComponent(): void
4554
{
4655
$commandTester = $this->createCommandTester();

0 commit comments

Comments
 (0)