Skip to content

Commit c7ccf97

Browse files
committed
[Toolkit] Remove FileType
1 parent b4bb509 commit c7ccf97

File tree

11 files changed

+64
-90
lines changed

11 files changed

+64
-90
lines changed

src/Toolkit/src/File/File.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ final class File implements \Stringable
2727
* @throws \InvalidArgumentException
2828
*/
2929
public function __construct(
30-
public readonly FileType $type,
3130
public readonly string $relativePathNameToKit,
3231
public readonly string $relativePathName,
3332
) {
@@ -46,6 +45,6 @@ public function __construct(
4645

4746
public function __toString(): string
4847
{
49-
return \sprintf('%s (%s)', $this->relativePathNameToKit, $this->type->getLabel());
48+
return $this->relativePathNameToKit;
5049
}
5150
}

src/Toolkit/src/File/FileType.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Toolkit/src/Kit/KitContextRunner.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ public function findAnonymousComponentTemplate(string $name): ?string
9595
}
9696

9797
foreach ($component->files as $file) {
98-
if (FileType::Twig === $file->type) {
99-
return $file->relativePathName;
100-
}
98+
return $file->relativePathName;
10199
}
102100

103101
throw new \LogicException(\sprintf('No Twig files found for component "%s" in kit "%s", it should not happens.', $name, $this->kit->name));

src/Toolkit/src/Kit/KitSynchronizer.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ private function synchronizeComponents(Kit $kit): void
8787
$component = new Component(
8888
name: $componentName,
8989
files: [new File(
90-
type: FileType::Twig,
9190
relativePathNameToKit: $relativePathNameToKit,
9291
relativePathName: $relativePathName,
9392
)],
@@ -124,30 +123,28 @@ private function resolveComponentDependencies(Kit $kit, Component $component): v
124123

125124
$fileContent = file_get_contents($filePath);
126125

127-
if (FileType::Twig === $file->type) {
128-
if (str_contains($fileContent, '<twig:') && preg_match_all(self::RE_TWIG_COMPONENT_REFERENCES, $fileContent, $matches)) {
129-
foreach ($matches[1] as $componentReferenceName) {
130-
if ($componentReferenceName === $component->name) {
131-
continue;
132-
}
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+
}
133131

134-
if (null !== $package = self::UX_COMPONENTS_PACKAGES[strtolower($componentReferenceName)] ?? null) {
135-
if (!$component->hasDependency(new PhpPackageDependency($package))) {
136-
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));
137-
}
138-
} else if (null === $componentReference = $kit->getComponent($componentReferenceName)) {
139-
throw new \RuntimeException(\sprintf('Component "%s" not found in component "%s" (file "%s")', $componentReferenceName, $component->name, $file->relativePathNameToKit));
140-
} else {
141-
$component->addDependency(new ComponentDependency($componentReference->name));
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));
142135
}
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));
143140
}
144141
}
142+
}
145143

146-
if (str_contains($fileContent, 'data-controller=') && preg_match_all(self::RE_STIMULUS_CONTROLLER_REFERENCES, $fileContent, $matches)) {
147-
$controllersName = array_filter(array_map(fn (string $name) => trim($name), explode(' ', $matches['controllersName'][0])));
148-
foreach ($controllersName as $controllerReferenceName) {
149-
$component->addDependency(new StimulusControllerDependency($controllerReferenceName));
150-
}
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));
151148
}
152149
}
153150
}
@@ -171,7 +168,6 @@ private function synchronizeStimulusControllers(Kit $kit): void
171168
$controller = new StimulusController(
172169
name: $controllerName,
173170
files: [new File(
174-
type: FileType::StimulusController,
175171
relativePathNameToKit: $relativePathNameToKit,
176172
relativePathName: $relativePathName,
177173
)],

src/Toolkit/tests/Asset/ComponentTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class ComponentTest extends TestCase
2424
public function testCanBeInstantiated(): void
2525
{
2626
$component = new Component('Button', [
27-
new File(FileType::Twig, 'templates/components/Button/Button.html.twig', 'Button.html.twig'),
27+
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
2828
]);
2929

3030
$this->assertSame('Button', $component->name);
@@ -40,7 +40,7 @@ public function testShouldFailIfComponentNameIsInvalid(): void
4040
$this->expectExceptionMessage('Invalid component name "foobar".');
4141

4242
new Component('foobar', [
43-
new File(FileType::Twig, 'templates/components/Button/Button.html.twig', 'Button.html.twig'),
43+
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
4444
]);
4545
}
4646

@@ -55,7 +55,7 @@ public function testShouldFailIfComponentHasNoFiles(): void
5555
public function testCanAddAndGetDependencies(): void
5656
{
5757
$component = new Component('Button', [
58-
new File(FileType::Twig, 'templates/components/Button/Button.html.twig', 'Button.html.twig'),
58+
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
5959
]);
6060

6161
$component->addDependency($dependency1 = new ComponentDependency('Icon'));
@@ -69,7 +69,7 @@ public function testCanAddAndGetDependencies(): void
6969
public function testShouldNotAddDuplicateComponentDependencies(): void
7070
{
7171
$component = new Component('Button', [
72-
new File(FileType::Twig, 'templates/components/Button/Button.html.twig', 'Button.html.twig'),
72+
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
7373
]);
7474

7575
$component->addDependency($dependency1 = new ComponentDependency('Icon'));
@@ -84,7 +84,7 @@ public function testShouldNotAddDuplicateComponentDependencies(): void
8484
public function testShouldReplacePhpPackageDependencyIfVersionIsHigher(): void
8585
{
8686
$component = new Component('Button', [
87-
new File(FileType::Twig, 'templates/components/Button/Button.html.twig', 'Button.html.twig'),
87+
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
8888
]);
8989

9090
$component->addDependency($dependency1 = new ComponentDependency('Icon'));
@@ -103,7 +103,7 @@ public function testShouldReplacePhpPackageDependencyIfVersionIsHigher(): void
103103
public function testShouldNotReplacePhpPackageDependencyIfVersionIsLower(): void
104104
{
105105
$component = new Component('Button', [
106-
new File(FileType::Twig, 'templates/components/Button/Button.html.twig', 'Button.html.twig'),
106+
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
107107
]);
108108

109109
$component->addDependency($dependency1 = new ComponentDependency('Icon'));

src/Toolkit/tests/Asset/StimulusControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class StimulusControllerTest extends TestCase
2323
public function testCanBeInstantiated(): void
2424
{
2525
$stimulusController = new StimulusController('clipboard', [
26-
new File(FileType::StimulusController, 'assets/controllers/clipboard_controller.js', 'clipboard_controller.js'),
26+
new File('assets/controllers/clipboard_controller.js', 'clipboard_controller.js'),
2727
]);
2828

2929
$this->assertSame('clipboard', $stimulusController->name);
@@ -34,7 +34,7 @@ public function testShouldFailIfStimulusControllerNameIsInvalid(): void
3434
$this->expectException(\InvalidArgumentException::class);
3535
$this->expectExceptionMessage('Invalid Stimulus controller name "invalid_controller".');
3636

37-
new StimulusController('invalid_controller', [new File(FileType::StimulusController, 'assets/controllers/invalid_controller.js', 'invalid_controller.js')]);
37+
new StimulusController('invalid_controller', [new File('assets/controllers/invalid_controller.js', 'invalid_controller.js')]);
3838
}
3939

4040
public function testShouldFailIfStimulusControllerHasNoFiles(): void

src/Toolkit/tests/Command/DebugKitCommandTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,28 @@ public function testShouldBeAbleToDebug(): void
2828
->assertOutputContains('Name Shadcn')
2929
->assertOutputContains('Homepage https://ux.symfony.com/components')
3030
->assertOutputContains('License MIT')
31-
// A component details
31+
// Components details
3232
->assertOutputContains(<<<'EOF'
3333
+--------------+----------------------- Component: "Avatar" --------------------------------------+
34-
| File(s) | templates/components/Avatar.html.twig (Twig) |
34+
| File(s) | templates/components/Avatar.html.twig |
3535
| Dependencies | tales-from-a-dev/twig-tailwind-extra |
3636
| | Avatar:Image |
3737
| | Avatar:Text |
3838
+--------------+----------------------------------------------------------------------------------+
39+
EOF
40+
)
41+
->assertOutputContains(<<<'EOF'
42+
+--------------+----------------------- Component: "Table" ---------------------------------------+
43+
| File(s) | templates/components/Table.html.twig |
44+
| Dependencies | tales-from-a-dev/twig-tailwind-extra |
45+
| | Table:Body |
46+
| | Table:Caption |
47+
| | Table:Cell |
48+
| | Table:Footer |
49+
| | Table:Head |
50+
| | Table:Header |
51+
| | Table:Row |
52+
+--------------+----------------------------------------------------------------------------------+
3953
EOF
4054
);
4155
}

src/Toolkit/tests/File/FileTest.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,40 @@ public function testShouldFailIfPathIsNotRelative(): void
2222
$this->expectException(\InvalidArgumentException::class);
2323
$this->expectExceptionMessage(\sprintf('The path to the kit "%s" must be relative.', __FILE__.'/templates/components/Button.html.twig'));
2424

25-
new File(FileType::Twig, __FILE__.'/templates/components/Button.html.twig', __FILE__.'Button.html.twig');
25+
new File(__FILE__.'/templates/components/Button.html.twig', __FILE__.'Button.html.twig');
2626
}
2727

2828
public function testShouldFailIfPathNameIsNotRelative(): void
2929
{
3030
$this->expectException(\InvalidArgumentException::class);
3131
$this->expectExceptionMessage(\sprintf('The path name "%s" must be relative.', __FILE__.'Button.html.twig'));
3232

33-
new File(FileType::Twig, 'templates/components/Button.html.twig', __FILE__.'Button.html.twig');
33+
new File('templates/components/Button.html.twig', __FILE__.'Button.html.twig');
3434
}
3535

3636
public function testShouldFailIfPathNameIsNotASubpathOfPathToKit(): void
3737
{
3838
$this->expectException(\InvalidArgumentException::class);
3939
$this->expectExceptionMessage(\sprintf('The relative path name "%s" must be a subpath of the relative path to the kit "%s".', 'foo/bar/Button.html.twig', 'templates/components/Button.html.twig'));
4040

41-
new File(FileType::Twig, 'templates/components/Button.html.twig', 'foo/bar/Button.html.twig');
41+
new File('templates/components/Button.html.twig', 'foo/bar/Button.html.twig');
4242
}
4343

4444
public function testCanInstantiateFile(): void
4545
{
46-
$file = new File(FileType::Twig, 'templates/components/Button.html.twig', 'Button.html.twig');
46+
$file = new File('templates/components/Button.html.twig', 'Button.html.twig');
4747

48-
$this->assertSame(FileType::Twig, $file->type);
4948
$this->assertSame('templates/components/Button.html.twig', $file->relativePathNameToKit);
5049
$this->assertSame('Button.html.twig', $file->relativePathName);
51-
$this->assertSame('templates/components/Button.html.twig (Twig)', (string) $file);
50+
$this->assertSame('templates/components/Button.html.twig', (string) $file);
5251
}
5352

5453
public function testCanInstantiateFileWithSubComponent(): void
5554
{
56-
$file = new File(FileType::Twig, 'templates/components/Table/Body.html.twig', 'Table/Body.html.twig');
55+
$file = new File('templates/components/Table/Body.html.twig', 'Table/Body.html.twig');
5756

58-
$this->assertSame(FileType::Twig, $file->type);
5957
$this->assertSame('templates/components/Table/Body.html.twig', $file->relativePathNameToKit);
6058
$this->assertSame('Table/Body.html.twig', $file->relativePathName);
61-
$this->assertSame('templates/components/Table/Body.html.twig (Twig)', (string) $file);
59+
$this->assertSame('templates/components/Table/Body.html.twig', (string) $file);
6260
}
6361
}

src/Toolkit/tests/Installer/PoolTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public function testCanAddFiles(): void
2828

2929
$this->assertCount(0, $pool->getFiles());
3030

31-
$pool->addFile(new File(FileType::Twig, 'path/to/file.html.twig', 'file.html.twig'));
32-
$pool->addFile(new File(FileType::Twig, 'path/to/another-file.html.twig', 'another-file.html.twig'));
31+
$pool->addFile(new File('path/to/file.html.twig', 'file.html.twig'));
32+
$pool->addFile(new File('path/to/another-file.html.twig', 'another-file.html.twig'));
3333

3434
$this->assertCount(2, $pool->getFiles());
3535
}
@@ -38,8 +38,8 @@ public function testCantAddSameFileTwice(): void
3838
{
3939
$pool = new Pool();
4040

41-
$pool->addFile(new File(FileType::Twig, 'path/to/file.html.twig', 'file.html.twig'));
42-
$pool->addFile(new File(FileType::Twig, 'path/to/file.html.twig', 'file.html.twig'));
41+
$pool->addFile(new File('path/to/file.html.twig', 'file.html.twig'));
42+
$pool->addFile(new File('path/to/file.html.twig', 'file.html.twig'));
4343

4444
$this->assertCount(1, $pool->getFiles());
4545
}

src/Toolkit/tests/Kit/KitFactoryTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public function testCanHandleStimulusControllers(): void
8585
// Assert Stimulus Controllers are registered in the Kit
8686
$this->assertNotEmpty($kit->getStimulusControllers());
8787
$this->assertEquals([
88-
$clipboard = new StimulusController('clipboard', [new File(FileType::StimulusController, 'assets/controllers/clipboard_controller.js', 'clipboard_controller.js')]),
89-
$datePicker = new StimulusController('date-picker', [new File(FileType::StimulusController, 'assets/controllers/date_picker_controller.js', 'date_picker_controller.js')]),
90-
$localTime = new StimulusController('local-time', [new File(FileType::StimulusController, 'assets/controllers/local-time-controller.js', 'local-time-controller.js')]),
91-
$usersListItem = new StimulusController('users--list-item', [new File(FileType::StimulusController, 'assets/controllers/users/list_item_controller.js', 'users/list_item_controller.js')]),
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')]),
9292
], $kit->getStimulusControllers());
9393
$this->assertEquals($clipboard, $kit->getStimulusController('clipboard'));
9494
$this->assertEquals($datePicker, $kit->getStimulusController('date-picker'));

0 commit comments

Comments
 (0)