Skip to content

[Toolkit] Remove FileType #2800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Toolkit/src/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ final class File implements \Stringable
* @throws \InvalidArgumentException
*/
public function __construct(
public readonly FileType $type,
public readonly string $relativePathNameToKit,
public readonly string $relativePathName,
) {
Expand All @@ -46,6 +45,6 @@ public function __construct(

public function __toString(): string
{
return \sprintf('%s (%s)', $this->relativePathNameToKit, $this->type->getLabel());
return $this->relativePathNameToKit;
}
}
31 changes: 0 additions & 31 deletions src/Toolkit/src/File/FileType.php

This file was deleted.

4 changes: 1 addition & 3 deletions src/Toolkit/src/Kit/KitContextRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ public function findAnonymousComponentTemplate(string $name): ?string
}

foreach ($component->files as $file) {
if (FileType::Twig === $file->type) {
return $file->relativePathName;
}
return $file->relativePathName;
}

throw new \LogicException(\sprintf('No Twig files found for component "%s" in kit "%s", it should not happens.', $name, $this->kit->name));
Expand Down
38 changes: 17 additions & 21 deletions src/Toolkit/src/Kit/KitSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ private function synchronizeComponents(Kit $kit): void
$component = new Component(
name: $componentName,
files: [new File(
type: FileType::Twig,
relativePathNameToKit: $relativePathNameToKit,
relativePathName: $relativePathName,
)],
Expand Down Expand Up @@ -124,30 +123,28 @@ private function resolveComponentDependencies(Kit $kit, Component $component): v

$fileContent = file_get_contents($filePath);

if (FileType::Twig === $file->type) {
if (str_contains($fileContent, '<twig:') && preg_match_all(self::RE_TWIG_COMPONENT_REFERENCES, $fileContent, $matches)) {
foreach ($matches[1] as $componentReferenceName) {
if ($componentReferenceName === $component->name) {
continue;
}
if (str_contains($fileContent, '<twig:') && preg_match_all(self::RE_TWIG_COMPONENT_REFERENCES, $fileContent, $matches)) {
foreach ($matches[1] as $componentReferenceName) {
if ($componentReferenceName === $component->name) {
continue;
}

if (null !== $package = self::UX_COMPONENTS_PACKAGES[strtolower($componentReferenceName)] ?? null) {
if (!$component->hasDependency(new PhpPackageDependency($package))) {
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));
}
} else if (null === $componentReference = $kit->getComponent($componentReferenceName)) {
throw new \RuntimeException(\sprintf('Component "%s" not found in component "%s" (file "%s")', $componentReferenceName, $component->name, $file->relativePathNameToKit));
} else {
$component->addDependency(new ComponentDependency($componentReference->name));
if (null !== $package = self::UX_COMPONENTS_PACKAGES[strtolower($componentReferenceName)] ?? null) {
if (!$component->hasDependency(new PhpPackageDependency($package))) {
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));
}
} else if (null === $componentReference = $kit->getComponent($componentReferenceName)) {
throw new \RuntimeException(\sprintf('Component "%s" not found in component "%s" (file "%s")', $componentReferenceName, $component->name, $file->relativePathNameToKit));
} else {
$component->addDependency(new ComponentDependency($componentReference->name));
}
}
}

if (str_contains($fileContent, 'data-controller=') && preg_match_all(self::RE_STIMULUS_CONTROLLER_REFERENCES, $fileContent, $matches)) {
$controllersName = array_filter(array_map(fn (string $name) => trim($name), explode(' ', $matches['controllersName'][0])));
foreach ($controllersName as $controllerReferenceName) {
$component->addDependency(new StimulusControllerDependency($controllerReferenceName));
}
if (str_contains($fileContent, 'data-controller=') && preg_match_all(self::RE_STIMULUS_CONTROLLER_REFERENCES, $fileContent, $matches)) {
$controllersName = array_filter(array_map(fn (string $name) => trim($name), explode(' ', $matches['controllersName'][0])));
foreach ($controllersName as $controllerReferenceName) {
$component->addDependency(new StimulusControllerDependency($controllerReferenceName));
}
}
}
Expand All @@ -171,7 +168,6 @@ private function synchronizeStimulusControllers(Kit $kit): void
$controller = new StimulusController(
name: $controllerName,
files: [new File(
type: FileType::StimulusController,
relativePathNameToKit: $relativePathNameToKit,
relativePathName: $relativePathName,
)],
Expand Down
12 changes: 6 additions & 6 deletions src/Toolkit/tests/Asset/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class ComponentTest extends TestCase
public function testCanBeInstantiated(): void
{
$component = new Component('Button', [
new File(FileType::Twig, 'templates/components/Button/Button.html.twig', 'Button.html.twig'),
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
]);

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

new Component('foobar', [
new File(FileType::Twig, 'templates/components/Button/Button.html.twig', 'Button.html.twig'),
new File('templates/components/Button/Button.html.twig', 'Button.html.twig'),
]);
}

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

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

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

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

$component->addDependency($dependency1 = new ComponentDependency('Icon'));
Expand Down
4 changes: 2 additions & 2 deletions src/Toolkit/tests/Asset/StimulusControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class StimulusControllerTest extends TestCase
public function testCanBeInstantiated(): void
{
$stimulusController = new StimulusController('clipboard', [
new File(FileType::StimulusController, 'assets/controllers/clipboard_controller.js', 'clipboard_controller.js'),
new File('assets/controllers/clipboard_controller.js', 'clipboard_controller.js'),
]);

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

new StimulusController('invalid_controller', [new File(FileType::StimulusController, 'assets/controllers/invalid_controller.js', 'invalid_controller.js')]);
new StimulusController('invalid_controller', [new File('assets/controllers/invalid_controller.js', 'invalid_controller.js')]);
}

public function testShouldFailIfStimulusControllerHasNoFiles(): void
Expand Down
18 changes: 16 additions & 2 deletions src/Toolkit/tests/Command/DebugKitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,28 @@ public function testShouldBeAbleToDebug(): void
->assertOutputContains('Name Shadcn')
->assertOutputContains('Homepage https://ux.symfony.com/components')
->assertOutputContains('License MIT')
// A component details
// Components details
->assertOutputContains(<<<'EOF'
+--------------+----------------------- Component: "Avatar" --------------------------------------+
| File(s) | templates/components/Avatar.html.twig (Twig) |
| File(s) | templates/components/Avatar.html.twig |
| Dependencies | tales-from-a-dev/twig-tailwind-extra |
| | Avatar:Image |
| | Avatar:Text |
+--------------+----------------------------------------------------------------------------------+
EOF
)
->assertOutputContains(<<<'EOF'
+--------------+----------------------- Component: "Table" ---------------------------------------+
| File(s) | templates/components/Table.html.twig |
| Dependencies | tales-from-a-dev/twig-tailwind-extra |
| | Table:Body |
| | Table:Caption |
| | Table:Cell |
| | Table:Footer |
| | Table:Head |
| | Table:Header |
| | Table:Row |
+--------------+----------------------------------------------------------------------------------+
EOF
);
}
Expand Down
16 changes: 7 additions & 9 deletions src/Toolkit/tests/File/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,40 @@ public function testShouldFailIfPathIsNotRelative(): void
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf('The path to the kit "%s" must be relative.', __FILE__.'/templates/components/Button.html.twig'));

new File(FileType::Twig, __FILE__.'/templates/components/Button.html.twig', __FILE__.'Button.html.twig');
new File(__FILE__.'/templates/components/Button.html.twig', __FILE__.'Button.html.twig');
}

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

new File(FileType::Twig, 'templates/components/Button.html.twig', __FILE__.'Button.html.twig');
new File('templates/components/Button.html.twig', __FILE__.'Button.html.twig');
}

public function testShouldFailIfPathNameIsNotASubpathOfPathToKit(): void
{
$this->expectException(\InvalidArgumentException::class);
$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'));

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

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

$this->assertSame(FileType::Twig, $file->type);
$this->assertSame('templates/components/Button.html.twig', $file->relativePathNameToKit);
$this->assertSame('Button.html.twig', $file->relativePathName);
$this->assertSame('templates/components/Button.html.twig (Twig)', (string) $file);
$this->assertSame('templates/components/Button.html.twig', (string) $file);
}

public function testCanInstantiateFileWithSubComponent(): void
{
$file = new File(FileType::Twig, 'templates/components/Table/Body.html.twig', 'Table/Body.html.twig');
$file = new File('templates/components/Table/Body.html.twig', 'Table/Body.html.twig');

$this->assertSame(FileType::Twig, $file->type);
$this->assertSame('templates/components/Table/Body.html.twig', $file->relativePathNameToKit);
$this->assertSame('Table/Body.html.twig', $file->relativePathName);
$this->assertSame('templates/components/Table/Body.html.twig (Twig)', (string) $file);
$this->assertSame('templates/components/Table/Body.html.twig', (string) $file);
}
}
8 changes: 4 additions & 4 deletions src/Toolkit/tests/Installer/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function testCanAddFiles(): void

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

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

$this->assertCount(2, $pool->getFiles());
}
Expand All @@ -38,8 +38,8 @@ public function testCantAddSameFileTwice(): void
{
$pool = new Pool();

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

$this->assertCount(1, $pool->getFiles());
}
Expand Down
8 changes: 4 additions & 4 deletions src/Toolkit/tests/Kit/KitFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public function testCanHandleStimulusControllers(): void
// Assert Stimulus Controllers are registered in the Kit
$this->assertNotEmpty($kit->getStimulusControllers());
$this->assertEquals([
$clipboard = new StimulusController('clipboard', [new File(FileType::StimulusController, 'assets/controllers/clipboard_controller.js', 'clipboard_controller.js')]),
$datePicker = new StimulusController('date-picker', [new File(FileType::StimulusController, 'assets/controllers/date_picker_controller.js', 'date_picker_controller.js')]),
$localTime = new StimulusController('local-time', [new File(FileType::StimulusController, 'assets/controllers/local-time-controller.js', 'local-time-controller.js')]),
$usersListItem = new StimulusController('users--list-item', [new File(FileType::StimulusController, 'assets/controllers/users/list_item_controller.js', 'users/list_item_controller.js')]),
$clipboard = new StimulusController('clipboard', [new File('assets/controllers/clipboard_controller.js', 'clipboard_controller.js')]),
$datePicker = new StimulusController('date-picker', [new File('assets/controllers/date_picker_controller.js', 'date_picker_controller.js')]),
$localTime = new StimulusController('local-time', [new File('assets/controllers/local-time-controller.js', 'local-time-controller.js')]),
$usersListItem = new StimulusController('users--list-item', [new File('assets/controllers/users/list_item_controller.js', 'users/list_item_controller.js')]),
], $kit->getStimulusControllers());
$this->assertEquals($clipboard, $kit->getStimulusController('clipboard'));
$this->assertEquals($datePicker, $kit->getStimulusController('date-picker'));
Expand Down
Loading