Skip to content

Commit ab087e0

Browse files
committed
[Toolkit] Rename Component and StimulusController property files to file
1 parent 3fce961 commit ab087e0

File tree

12 files changed

+51
-111
lines changed

12 files changed

+51
-111
lines changed

src/Toolkit/src/Asset/Component.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,16 @@ final class Component
2929
{
3030
/**
3131
* @param non-empty-string $name
32-
* @param list<File> $files
3332
*/
3433
public function __construct(
3534
public readonly string $name,
36-
public readonly array $files,
35+
public readonly File $file,
3736
public ?Doc $doc = null,
3837
public ?ComponentMeta $meta = null,
3938
private array $dependencies = [],
4039
) {
4140
Assert::componentName($name);
4241

43-
if ([] === $files) {
44-
throw new \InvalidArgumentException(\sprintf('The component "%s" must have at least one file.', $name));
45-
}
46-
4742
foreach ($this->meta?->dependencies ?? [] as $dependency) {
4843
$this->addDependency($dependency);
4944
}

src/Toolkit/src/Asset/StimulusController.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,11 @@ class StimulusController
2323
{
2424
/**
2525
* @param non-empty-string $name
26-
* @param list<File> $files
2726
*/
2827
public function __construct(
2928
public readonly string $name,
30-
public readonly array $files,
29+
public readonly File $file,
3130
) {
3231
Assert::stimulusControllerName($this->name);
33-
34-
if ([] === $files) {
35-
throw new \InvalidArgumentException(\sprintf('Stimulus controller "%s" has no files.', $name));
36-
}
3732
}
3833
}

src/Toolkit/src/Command/DebugKitCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8484
'Dependencies',
8585
])
8686
->addRow([
87-
implode("\n", $component->files),
87+
$component->file,
8888
implode("\n", $component->getDependencies()),
8989
])
9090
->setColumnWidth(1, 80)

src/Toolkit/src/Installer/PoolResolver.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ public function resolveForComponent(Kit $kit, Component $component): Pool
3939

4040
$visitedComponents->attach($currentComponent);
4141

42-
foreach ($currentComponent->files as $file) {
43-
$pool->addFile($file);
44-
}
42+
$pool->addFile($currentComponent->file);
4543

4644
foreach ($currentComponent->getDependencies() as $dependency) {
4745
if ($dependency instanceof ComponentDependency) {
@@ -53,11 +51,9 @@ public function resolveForComponent(Kit $kit, Component $component): Pool
5351
throw new \RuntimeException(\sprintf('Stimulus controller "%s" not found.', $dependency->name));
5452
}
5553

56-
foreach ($stimulusController->files as $file) {
57-
$pool->addFile($file);
58-
}
54+
$pool->addFile($stimulusController->file);
5955
} else {
60-
throw new \RuntimeException(\sprintf('Unknown dependency type: %s', $dependency::class));
56+
throw new \RuntimeException(\sprintf('Unknown dependency type: "%s"', $dependency::class));
6157
}
6258
}
6359
}

src/Toolkit/src/Kit/KitContextRunner.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\UX\Toolkit\Kit;
1313

1414
use Symfony\Component\Filesystem\Path;
15-
use Symfony\UX\Toolkit\File\FileType;
1615
use Symfony\UX\TwigComponent\ComponentFactory;
1716
use Symfony\UX\TwigComponent\ComponentTemplateFinderInterface;
1817
use Twig\Loader\ChainLoader;
@@ -94,11 +93,7 @@ public function findAnonymousComponentTemplate(string $name): ?string
9493
throw new \RuntimeException(\sprintf('Component "%s" does not exist in kit "%s".', $name, $this->kit->name));
9594
}
9695

97-
foreach ($component->files as $file) {
98-
return $file->relativePathName;
99-
}
100-
101-
throw new \LogicException(\sprintf('No Twig files found for component "%s" in kit "%s", it should not happens.', $name, $this->kit->name));
96+
return $component->file->relativePathName;
10297
}
10398
};
10499
}

src/Toolkit/src/Kit/KitSynchronizer.php

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Symfony\UX\Toolkit\File\ComponentMeta;
2323
use Symfony\UX\Toolkit\File\Doc;
2424
use Symfony\UX\Toolkit\File\File;
25-
use Symfony\UX\Toolkit\File\FileType;
2625

2726
/**
2827
* @internal
@@ -86,10 +85,10 @@ private function synchronizeComponents(Kit $kit): void
8685

8786
$component = new Component(
8887
name: $componentName,
89-
files: [new File(
88+
file: new File(
9089
relativePathNameToKit: $relativePathNameToKit,
9190
relativePathName: $relativePathName,
92-
)],
91+
),
9392
meta: $meta,
9493
);
9594

@@ -116,36 +115,34 @@ private function resolveComponentDependencies(Kit $kit, Component $component): v
116115
}
117116

118117
// Find dependencies based on file content
119-
foreach ($component->files as $file) {
120-
if (!$this->filesystem->exists($filePath = Path::join($kit->path, $file->relativePathNameToKit))) {
121-
throw new \RuntimeException(\sprintf('File "%s" not found', $filePath));
122-
}
118+
if (!$this->filesystem->exists($filePath = Path::join($kit->path, $component->file->relativePathNameToKit))) {
119+
throw new \RuntimeException(\sprintf('File "%s" not found', $filePath));
120+
}
123121

124-
$fileContent = file_get_contents($filePath);
122+
$fileContent = file_get_contents($filePath);
125123

126-
if (str_contains($fileContent, '<twig:') && preg_match_all(self::RE_TWIG_COMPONENT_REFERENCES, $fileContent, $matches)) {
127-
foreach ($matches[1] as $componentReferenceName) {
128-
if ($componentReferenceName === $component->name) {
129-
continue;
130-
}
124+
if (str_contains($fileContent, '<twig:') && preg_match_all(self::RE_TWIG_COMPONENT_REFERENCES, $fileContent, $matches)) {
125+
foreach ($matches[1] as $componentReferenceName) {
126+
if ($componentReferenceName === $component->name) {
127+
continue;
128+
}
131129

132-
if (null !== $package = self::UX_COMPONENTS_PACKAGES[strtolower($componentReferenceName)] ?? null) {
133-
if (!$component->hasDependency(new PhpPackageDependency($package))) {
134-
throw new \RuntimeException(\sprintf('Component "%s" uses "%s" UX Twig component, but the composer package "%s" is not listed as a dependency in meta file.', $component->name, $componentReferenceName, $package));
135-
}
136-
} else if (null === $componentReference = $kit->getComponent($componentReferenceName)) {
137-
throw new \RuntimeException(\sprintf('Component "%s" not found in component "%s" (file "%s")', $componentReferenceName, $component->name, $file->relativePathNameToKit));
138-
} else {
139-
$component->addDependency(new ComponentDependency($componentReference->name));
130+
if (null !== $package = self::UX_COMPONENTS_PACKAGES[strtolower($componentReferenceName)] ?? null) {
131+
if (!$component->hasDependency(new PhpPackageDependency($package))) {
132+
throw new \RuntimeException(\sprintf('Component "%s" uses "%s" UX Twig component, but the composer package "%s" is not listed as a dependency in meta file.', $component->name, $componentReferenceName, $package));
140133
}
134+
} else if (null === $componentReference = $kit->getComponent($componentReferenceName)) {
135+
throw new \RuntimeException(\sprintf('Component "%s" not found in component "%s" (file "%s")', $componentReferenceName, $component->name, $component->file->relativePathNameToKit));
136+
} else {
137+
$component->addDependency(new ComponentDependency($componentReference->name));
141138
}
142139
}
140+
}
143141

144-
if (str_contains($fileContent, 'data-controller=') && preg_match_all(self::RE_STIMULUS_CONTROLLER_REFERENCES, $fileContent, $matches)) {
145-
$controllersName = array_filter(array_map(fn (string $name) => trim($name), explode(' ', $matches['controllersName'][0])));
146-
foreach ($controllersName as $controllerReferenceName) {
147-
$component->addDependency(new StimulusControllerDependency($controllerReferenceName));
148-
}
142+
if (str_contains($fileContent, 'data-controller=') && preg_match_all(self::RE_STIMULUS_CONTROLLER_REFERENCES, $fileContent, $matches)) {
143+
$controllersName = array_filter(array_map(fn (string $name) => trim($name), explode(' ', $matches['controllersName'][0])));
144+
foreach ($controllersName as $controllerReferenceName) {
145+
$component->addDependency(new StimulusControllerDependency($controllerReferenceName));
149146
}
150147
}
151148
}
@@ -167,10 +164,10 @@ private function synchronizeStimulusControllers(Kit $kit): void
167164
$controllerName = $this->extractStimulusControllerName($relativePathName);
168165
$controller = new StimulusController(
169166
name: $controllerName,
170-
files: [new File(
167+
file: new File(
171168
relativePathNameToKit: $relativePathNameToKit,
172169
relativePathName: $relativePathName,
173-
)],
170+
),
174171
);
175172

176173
$kit->addStimulusController($controller);

src/Toolkit/tests/Asset/ComponentTest.php

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@
1717
use Symfony\UX\Toolkit\Dependency\PhpPackageDependency;
1818
use Symfony\UX\Toolkit\Dependency\Version;
1919
use Symfony\UX\Toolkit\File\File;
20-
use Symfony\UX\Toolkit\File\FileType;
2120

2221
final class ComponentTest extends TestCase
2322
{
2423
public function testCanBeInstantiated(): void
2524
{
26-
$component = new Component('Button', [
27-
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
28-
]);
25+
$component = new Component('Button', new File('templates/components/Button/Button.html.twig', 'Button.html.twig'));
2926

3027
$this->assertSame('Button', $component->name);
31-
$this->assertCount(1, $component->files);
32-
$this->assertInstanceOf(File::class, $component->files[0]);
28+
$this->assertInstanceOf(File::class, $component->file);
3329
$this->assertNull($component->doc);
3430
$this->assertCount(0, $component->getDependencies());
3531
}
@@ -39,24 +35,12 @@ public function testShouldFailIfComponentNameIsInvalid(): void
3935
$this->expectException(\InvalidArgumentException::class);
4036
$this->expectExceptionMessage('Invalid component name "foobar".');
4137

42-
new Component('foobar', [
43-
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
44-
]);
45-
}
46-
47-
public function testShouldFailIfComponentHasNoFiles(): void
48-
{
49-
$this->expectException(\InvalidArgumentException::class);
50-
$this->expectExceptionMessage('The component "Button" must have at least one file.');
51-
52-
new Component('Button', []);
38+
new Component('foobar', new File('templates/components/Button/Button.html.twig', 'Button.html.twig'));
5339
}
5440

5541
public function testCanAddAndGetDependencies(): void
5642
{
57-
$component = new Component('Button', [
58-
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
59-
]);
43+
$component = new Component('Button', new File('templates/components/Button/Button.html.twig', 'Button.html.twig'));
6044

6145
$component->addDependency($dependency1 = new ComponentDependency('Icon'));
6246
$component->addDependency($dependency2 = new ComponentDependency('Label'));
@@ -68,9 +52,7 @@ public function testCanAddAndGetDependencies(): void
6852

6953
public function testShouldNotAddDuplicateComponentDependencies(): void
7054
{
71-
$component = new Component('Button', [
72-
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
73-
]);
55+
$component = new Component('Button', new File('templates/components/Button/Button.html.twig', 'Button.html.twig'));
7456

7557
$component->addDependency($dependency1 = new ComponentDependency('Icon'));
7658
$component->addDependency($dependency2 = new ComponentDependency('Label'));
@@ -83,9 +65,7 @@ public function testShouldNotAddDuplicateComponentDependencies(): void
8365

8466
public function testShouldReplacePhpPackageDependencyIfVersionIsHigher(): void
8567
{
86-
$component = new Component('Button', [
87-
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
88-
]);
68+
$component = new Component('Button', new File('templates/components/Button/Button.html.twig', 'Button.html.twig'));
8969

9070
$component->addDependency($dependency1 = new ComponentDependency('Icon'));
9171
$component->addDependency($dependency2 = new ComponentDependency('Label'));
@@ -102,9 +82,7 @@ public function testShouldReplacePhpPackageDependencyIfVersionIsHigher(): void
10282

10383
public function testShouldNotReplacePhpPackageDependencyIfVersionIsLower(): void
10484
{
105-
$component = new Component('Button', [
106-
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
107-
]);
85+
$component = new Component('Button', new File('templates/components/Button/Button.html.twig', 'Button.html.twig'));
10886

10987
$component->addDependency($dependency1 = new ComponentDependency('Icon'));
11088
$component->addDependency($dependency2 = new ComponentDependency('Label'));

src/Toolkit/tests/Asset/StimulusControllerTest.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616
use PHPUnit\Framework\TestCase;
1717
use Symfony\UX\Toolkit\Asset\StimulusController;
1818
use Symfony\UX\Toolkit\File\File;
19-
use Symfony\UX\Toolkit\File\FileType;
2019

2120
final class StimulusControllerTest extends TestCase
2221
{
2322
public function testCanBeInstantiated(): void
2423
{
25-
$stimulusController = new StimulusController('clipboard', [
26-
new File('assets/controllers/clipboard_controller.js', 'clipboard_controller.js'),
27-
]);
24+
$stimulusController = new StimulusController('clipboard', new File('assets/controllers/clipboard_controller.js', 'clipboard_controller.js'));
2825

2926
$this->assertSame('clipboard', $stimulusController->name);
3027
}
@@ -34,14 +31,6 @@ public function testShouldFailIfStimulusControllerNameIsInvalid(): void
3431
$this->expectException(\InvalidArgumentException::class);
3532
$this->expectExceptionMessage('Invalid Stimulus controller name "invalid_controller".');
3633

37-
new StimulusController('invalid_controller', [new File('assets/controllers/invalid_controller.js', 'invalid_controller.js')]);
38-
}
39-
40-
public function testShouldFailIfStimulusControllerHasNoFiles(): void
41-
{
42-
$this->expectException(\InvalidArgumentException::class);
43-
$this->expectExceptionMessage('Stimulus controller "clipboard" has no files.');
44-
45-
new StimulusController('clipboard', []);
34+
new StimulusController('invalid_controller', new File('assets/controllers/invalid_controller.js', 'invalid_controller.js'));
4635
}
4736
}

src/Toolkit/tests/File/FileTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\UX\Toolkit\File\File;
16-
use Symfony\UX\Toolkit\File\FileType;
1716

1817
final class FileTest extends TestCase
1918
{

src/Toolkit/tests/Installer/PoolTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\UX\Toolkit\Dependency\PhpPackageDependency;
1818
use Symfony\UX\Toolkit\Dependency\Version;
1919
use Symfony\UX\Toolkit\File\File;
20-
use Symfony\UX\Toolkit\File\FileType;
2120
use Symfony\UX\Toolkit\Installer\Pool;
2221

2322
final class PoolTest extends TestCase

src/Toolkit/tests/Kit/KitFactoryTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\UX\Toolkit\Dependency\PhpPackageDependency;
1818
use Symfony\UX\Toolkit\Dependency\StimulusControllerDependency;
1919
use Symfony\UX\Toolkit\File\File;
20-
use Symfony\UX\Toolkit\File\FileType;
2120
use Symfony\UX\Toolkit\Kit\KitFactory;
2221

2322
final class KitFactoryTest extends KernelTestCase
@@ -54,7 +53,6 @@ public function testCanCreateShadKit(): void
5453
$table = $kit->getComponent('Table');
5554

5655
$this->assertNotNull($table);
57-
$this->assertNotEmpty($table->files);
5856
$this->assertEquals([
5957
new PhpPackageDependency('tales-from-a-dev/twig-tailwind-extra'),
6058
new ComponentDependency('Table:Body'),
@@ -85,10 +83,10 @@ public function testCanHandleStimulusControllers(): void
8583
// Assert Stimulus Controllers are registered in the Kit
8684
$this->assertNotEmpty($kit->getStimulusControllers());
8785
$this->assertEquals([
88-
$clipboard = new StimulusController('clipboard', [new File('assets/controllers/clipboard_controller.js', 'clipboard_controller.js')]),
89-
$datePicker = new StimulusController('date-picker', [new File('assets/controllers/date_picker_controller.js', 'date_picker_controller.js')]),
90-
$localTime = new StimulusController('local-time', [new File('assets/controllers/local-time-controller.js', 'local-time-controller.js')]),
91-
$usersListItem = new StimulusController('users--list-item', [new File('assets/controllers/users/list_item_controller.js', 'users/list_item_controller.js')]),
86+
$clipboard = new StimulusController('clipboard', new File('assets/controllers/clipboard_controller.js', 'clipboard_controller.js')),
87+
$datePicker = new StimulusController('date-picker', new File('assets/controllers/date_picker_controller.js', 'date_picker_controller.js')),
88+
$localTime = new StimulusController('local-time', new File('assets/controllers/local-time-controller.js', 'local-time-controller.js')),
89+
$usersListItem = new StimulusController('users--list-item', new File('assets/controllers/users/list_item_controller.js', 'users/list_item_controller.js')),
9290
], $kit->getStimulusControllers());
9391
$this->assertEquals($clipboard, $kit->getStimulusController('clipboard'));
9492
$this->assertEquals($datePicker, $kit->getStimulusController('date-picker'));

src/Toolkit/tests/Kit/KitTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\UX\Toolkit\Asset\Component;
1616
use Symfony\UX\Toolkit\File\File;
17-
use Symfony\UX\Toolkit\File\FileType;
1817
use Symfony\UX\Toolkit\Kit\Kit;
1918

2019
final class KitTest extends TestCase
@@ -38,8 +37,8 @@ public function testShouldFailIfKitPathIsNotAbsolute(): void
3837
public function testCanAddComponentsToTheKit(): void
3938
{
4039
$kit = new Kit(__DIR__, 'foo', 'https://example.com', 'MIT');
41-
$kit->addComponent(new Component('Table', [new File('Table.html.twig', 'Table.html.twig')], null));
42-
$kit->addComponent(new Component('Table:Row', [new File('Table/Row.html.twig', 'Table/Row.html.twig')], null));
40+
$kit->addComponent(new Component('Table', new File('Table.html.twig', 'Table.html.twig'), null));
41+
$kit->addComponent(new Component('Table:Row', new File('Table/Row.html.twig', 'Table/Row.html.twig'), null));
4342

4443
$this->assertCount(2, $kit->getComponents());
4544
}
@@ -50,15 +49,15 @@ public function testShouldFailIfComponentIsAlreadyRegisteredInTheKit(): void
5049
$this->expectExceptionMessage('Component "Table" is already registered in the kit.');
5150

5251
$kit = new Kit(__DIR__, 'foo', 'https://example.com', 'MIT');
53-
$kit->addComponent(new Component('Table', [new File('Table.html.twig', 'Table.html.twig')], null));
54-
$kit->addComponent(new Component('Table', [new File('Table.html.twig', 'Table.html.twig')], null));
52+
$kit->addComponent(new Component('Table', new File('Table.html.twig', 'Table.html.twig'), null));
53+
$kit->addComponent(new Component('Table', new File('Table.html.twig', 'Table.html.twig'), null));
5554
}
5655

5756
public function testCanGetComponentByName(): void
5857
{
5958
$kit = new Kit(__DIR__, 'foo', 'https://example.com', 'MIT');
60-
$kit->addComponent(new Component('Table', [new File('Table.html.twig', 'Table.html.twig')], null));
61-
$kit->addComponent(new Component('Table:Row', [new File('Table/Row.html.twig', 'Table/Row.html.twig')], null));
59+
$kit->addComponent(new Component('Table', new File('Table.html.twig', 'Table.html.twig'), null));
60+
$kit->addComponent(new Component('Table:Row', new File('Table/Row.html.twig', 'Table/Row.html.twig'), null));
6261

6362
$this->assertSame('Table', $kit->getComponent('Table')->name);
6463
$this->assertSame('Table:Row', $kit->getComponent('Table:Row')->name);

0 commit comments

Comments
 (0)