Skip to content

Commit 722ccdd

Browse files
committed
PR requested changes
1 parent 806dad3 commit 722ccdd

File tree

8 files changed

+94
-54
lines changed

8 files changed

+94
-54
lines changed

src/TwigComponent/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 2.13.0
44

5+
- Deprecate calling `ComponentTemplateFinder` constructor without `directory` argument.
56
- Add profiler integration: `TwigComponentDataCollector` and debug toolbar templates
67
- Add search feature in `debug:twig-component` command.
78

src/TwigComponent/src/Command/TwigComponentDebugCommand.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\Console\Helper\TableSeparator;
1919
use Symfony\Component\Console\Input\InputArgument;
2020
use Symfony\Component\Console\Input\InputInterface;
21-
use Symfony\Component\Console\Input\InputOption;
2221
use Symfony\Component\Console\Output\OutputInterface;
2322
use Symfony\Component\Console\Style\SymfonyStyle;
2423
use Symfony\Component\Finder\Finder;
@@ -50,7 +49,6 @@ protected function configure(): void
5049
$this
5150
->setDefinition([
5251
new InputArgument('name', InputArgument::OPTIONAL, 'A component name or part of the component name'),
53-
new InputOption('dir', null, InputOption::VALUE_REQUIRED, 'Show all components with template in a specific directory'),
5452
])
5553
->setHelp(<<<'EOF'
5654
The <info>%command.name%</info> display all the Twig components in your application.
@@ -59,13 +57,9 @@ protected function configure(): void
5957
6058
<info>php %command.full_name%</info>
6159
62-
To get specific information about a component, specify its name:
60+
To get specific information about a component, specify its name (or a part of it):
6361
6462
<info>php %command.full_name% Alert</info>
65-
66-
To find all component templates within a directory, specifying the <info>--dir</info> option:
67-
68-
<info>php %command.full_name% --dir=bar/foo</info>
6963
EOF
7064
);
7165
}
@@ -74,7 +68,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7468
{
7569
$io = new SymfonyStyle($input, $output);
7670
$name = $input->getArgument('name');
77-
$directory = $input->getOption('dir');
7871

7972
if (\is_string($name)) {
8073
$component = $this->findComponentName($io, $name, $input->isInteractive());
@@ -89,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8982
return Command::SUCCESS;
9083
}
9184

92-
$components = $this->findComponents($directory);
85+
$components = $this->findComponents();
9386
$this->displayComponentsTable($io, $components);
9487

9588
return Command::SUCCESS;
@@ -113,7 +106,6 @@ private function findComponentName(SymfonyStyle $io, string $name, bool $interac
113106
$components[$componentName] = $componentName;
114107
}
115108
}
116-
117109
foreach ($this->findAnonymousComponents() as $componentName) {
118110
if (isset($components[$componentName])) {
119111
continue;
@@ -122,10 +114,9 @@ private function findComponentName(SymfonyStyle $io, string $name, bool $interac
122114
return $name;
123115
}
124116
if (str_contains($componentName, $name)) {
125-
$components[] = $componentName;
117+
$components[$componentName] = $componentName;
126118
}
127119
}
128-
129120
if ($interactive && \count($components)) {
130121
return $io->choice('Select one of the following component to display its information', array_values($components), 0);
131122
}
@@ -136,17 +127,13 @@ private function findComponentName(SymfonyStyle $io, string $name, bool $interac
136127
/**
137128
* @return array<string, ComponentMetadata>
138129
*/
139-
private function findComponents(string $directory = null): array
130+
private function findComponents(): array
140131
{
141132
$components = [];
142133
foreach ($this->componentClassMap as $name => $class) {
143-
$metadata = $this->componentFactory->metadataFor($name);
144-
if (\is_string($directory) && !str_contains($metadata->getTemplate(), $directory)) {
145-
continue;
146-
}
147-
$components[$name] ??= $metadata;
134+
$components[$name] ??= $this->componentFactory->metadataFor($name);
148135
}
149-
foreach ($this->findAnonymousComponents($directory) as $name => $template) {
136+
foreach ($this->findAnonymousComponents() as $name => $template) {
150137
$components[$name] ??= $this->componentFactory->metadataFor($name);
151138
}
152139

@@ -158,7 +145,7 @@ private function findComponents(string $directory = null): array
158145
*
159146
* @return array<string, string>
160147
*/
161-
private function findAnonymousComponents(string $directory = null): array
148+
private function findAnonymousComponents(): array
162149
{
163150
$components = [];
164151
$anonymousPath = $this->twigTemplatesPath.'/'.$this->anonymousDirectory;

src/TwigComponent/src/ComponentFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class ComponentFactory
3030
* @param array<class-string, string> $classMap
3131
*/
3232
public function __construct(
33-
private ComponentTemplateFinder $componentTemplateFinder,
33+
private ComponentTemplateFinderInterface $componentTemplateFinder,
3434
private ServiceLocator $components,
3535
private PropertyAccessorInterface $propertyAccessor,
3636
private EventDispatcherInterface $eventDispatcher,

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\UX\TwigComponent;
1313

14-
use Composer\InstalledVersions;
1514
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1615
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1716
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
@@ -66,9 +65,7 @@ public function render(MountedComponent $mounted): string
6665
$event = $this->preRender($mounted);
6766

6867
try {
69-
if (InstalledVersions::getVersion('twig/twig') < 3) {
70-
trigger_deprecation('twig/twig', $version, $message, $args);
71-
68+
if ($this->twig::MAJOR_VERSION < 3) {
7269
return $this->twig->loadTemplate($event->getTemplate(), $event->getTemplateIndex())->render($event->getVariables());
7370
}
7471

src/TwigComponent/src/ComponentTemplateFinder.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,24 @@
1515

1616
/**
1717
* @author Matheo Daninos <[email protected]>
18-
*
19-
* @internal
2018
*/
2119
final class ComponentTemplateFinder implements ComponentTemplateFinderInterface
2220
{
23-
private readonly ?string $directory;
24-
2521
public function __construct(
2622
private Environment $environment,
27-
string $directory = null,
23+
private readonly ?string $directory = null,
2824
) {
29-
$this->directory = \is_string($directory) ? (rtrim($directory, '/').'/') : null;
25+
if (null === $this->directory) {
26+
trigger_deprecation('symfony/ux-twig-component', '2.13', 'The "%s()" method will require "string $directory" argument in 3.0. Not defining it or passing null is deprecated.', __METHOD__);
27+
}
3028
}
3129

3230
public function findAnonymousComponentTemplate(string $name): ?string
3331
{
3432
$loader = $this->environment->getLoader();
3533
$componentPath = rtrim(str_replace(':', '/', $name));
3634

35+
// Legacy auto-naming rules < 2.13
3736
if (null === $this->directory) {
3837
if ($loader->exists('components/'.$componentPath.'.html.twig')) {
3938
return 'components/'.$componentPath.'.html.twig';
@@ -54,12 +53,7 @@ public function findAnonymousComponentTemplate(string $name): ?string
5453
return null;
5554
}
5655

57-
$template = $this->directory.$componentPath.'.html.twig';
58-
if ($loader->exists($template)) {
59-
return $template;
60-
}
61-
62-
$template = $this->directory.$componentPath;
56+
$template = rtrim($this->directory, '/').'/'.$componentPath.'.html.twig';
6357
if ($loader->exists($template)) {
6458
return $template;
6559
}

src/TwigComponent/src/ComponentTemplateFinderInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
/**
1515
* @author Matheo Daninos <[email protected]>
16-
*
17-
* @internal
1816
*/
1917
interface ComponentTemplateFinderInterface
2018
{

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

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

44+
public function testWithOnePartialMatchComponent(): void
45+
{
46+
$commandTester = $this->createCommandTester();
47+
$commandTester->setInputs([]);
48+
$result = $commandTester->execute(['name' => 'DivComponentNoPas']);
49+
50+
$this->assertEquals(0, $result);
51+
// Choices
52+
$this->assertStringNotContainsString('] DivComponent\n', $commandTester->getDisplay());
53+
$this->assertStringContainsString('] DivComponentNoPass', $commandTester->getDisplay());
54+
// Component table
55+
$this->assertStringContainsString('Component\\DivComponentNoPass', $commandTester->getDisplay());
56+
}
57+
58+
public function testWithMultiplePartialMatchComponent(): void
59+
{
60+
$commandTester = $this->createCommandTester();
61+
$commandTester->setInputs(['DivComponent5']);
62+
$result = $commandTester->execute(['name' => 'DivCompon']);
63+
64+
$this->assertEquals(0, $result);
65+
// Choices
66+
$this->assertStringContainsString('Select one of the following component to display its information', $commandTester->getDisplay());
67+
$this->assertStringContainsString('] DivComponent4', $commandTester->getDisplay());
68+
$this->assertStringContainsString('] DivComponent5', $commandTester->getDisplay());
69+
$this->assertStringContainsString('] DivComponent6', $commandTester->getDisplay());
70+
// Component table
71+
$this->assertStringNotContainsString('Component\\DivComponent4', $commandTester->getDisplay());
72+
$this->assertStringContainsString('Component\\DivComponent5', $commandTester->getDisplay());
73+
$this->assertStringNotContainsString('Component\\DivComponent6', $commandTester->getDisplay());
74+
}
75+
4476
public function testComponentWithClass(): void
4577
{
4678
$commandTester = $this->createCommandTester();
@@ -141,21 +173,6 @@ public function testWithExposedVariables(): void
141173
$this->assertStringNotContainsString('prop3', $display);
142174
}
143175

144-
public function testWithDirectoryOption()
145-
{
146-
$commandTester = $this->createCommandTester();
147-
$commandTester->execute(['--dir' => 'bar']);
148-
149-
$commandTester->assertCommandIsSuccessful();
150-
151-
$display = $commandTester->getDisplay();
152-
153-
$this->assertStringContainsString('foo:bar:baz', $display);
154-
$this->assertStringContainsString('OtherDirectory', $display);
155-
$this->assertStringContainsString('components/foo/bar/baz.html.twig', $display);
156-
$this->assertStringContainsString('bar/OtherDirectory.html.twig', $display);
157-
}
158-
159176
private function createCommandTester(): CommandTester
160177
{
161178
$kernel = self::bootKernel();

src/TwigComponent/tests/Unit/ComponentTemplateFinderTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\TwigComponent\Tests\Unit;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\UX\TwigComponent\ComponentTemplateFinder;
1617
use Twig\Environment;
1718
use Twig\Loader\ArrayLoader;
@@ -21,7 +22,36 @@
2122
*/
2223
final class ComponentTemplateFinderTest extends TestCase
2324
{
25+
use ExpectDeprecationTrait;
26+
2427
public function testFindTemplate(): void
28+
{
29+
$templates = [
30+
'components/aa.html.twig',
31+
'components/aa:bb.html.twig',
32+
'components/bb.html.twig',
33+
'components/aa/bb.html.twig',
34+
'b',
35+
'components/b',
36+
'components/b.html.twig',
37+
'components/c',
38+
];
39+
$environment = $this->createEnvironment($templates);
40+
$finder = new ComponentTemplateFinder($environment, 'components');
41+
42+
$this->assertEquals('components/aa.html.twig', $finder->findAnonymousComponentTemplate('aa'));
43+
$this->assertEquals('components/aa/bb.html.twig', $finder->findAnonymousComponentTemplate('aa:bb'));
44+
$this->assertEquals('components/b.html.twig', $finder->findAnonymousComponentTemplate('b'));
45+
46+
$this->assertNull($finder->findAnonymousComponentTemplate('b.html.twig'));
47+
$this->assertNull($finder->findAnonymousComponentTemplate('components:b'));
48+
$this->assertNull($finder->findAnonymousComponentTemplate('c'));
49+
}
50+
51+
/**
52+
* @group legacy
53+
*/
54+
public function testFindTemplateWithLegacyAutonaming(): void
2555
{
2656
$templates = [
2757
'components/aa.html.twig',
@@ -39,6 +69,8 @@ public function testFindTemplate(): void
3969
'components/d',
4070
];
4171
$environment = $this->createEnvironment($templates);
72+
73+
$this->expectDeprecation('Since symfony/ux-twig-component 2.13: The "Symfony\UX\TwigComponent\ComponentTemplateFinder::__construct()" method will require "string $directory" argument in 3.0. Not defining it or passing null is deprecated.');
4274
$finder = new ComponentTemplateFinder($environment);
4375

4476
$this->assertEquals('components/aa.html.twig', $finder->findAnonymousComponentTemplate('aa'));
@@ -52,6 +84,20 @@ public function testFindTemplate(): void
5284
$this->assertNull($finder->findAnonymousComponentTemplate('nope:bar'));
5385
}
5486

87+
/**
88+
* @group legacy
89+
*/
90+
public function testTriggerDeprecationWhenDirectoryArgumentIsNullOrNotProvided(): void
91+
{
92+
$environment = $this->createEnvironment([]);
93+
94+
$this->expectDeprecation('Since symfony/ux-twig-component 2.13: The "Symfony\UX\TwigComponent\ComponentTemplateFinder::__construct()" method will require "string $directory" argument in 3.0. Not defining it or passing null is deprecated.');
95+
$finder = new ComponentTemplateFinder($environment);
96+
97+
$this->expectDeprecation('Since symfony/ux-twig-component 2.13: The "Symfony\UX\TwigComponent\ComponentTemplateFinder::__construct()" method will require "string $directory" argument in 3.0. Not defining it or passing null is deprecated.');
98+
$finder = new ComponentTemplateFinder($environment, null);
99+
}
100+
55101
public function testFindTemplateWithinDirectory(): void
56102
{
57103
$templates = [

0 commit comments

Comments
 (0)