diff --git a/src/Toolkit/src/Asset/Component.php b/src/Toolkit/src/Asset/Component.php index 8e6ee0868af..3e87cab9419 100644 --- a/src/Toolkit/src/Asset/Component.php +++ b/src/Toolkit/src/Asset/Component.php @@ -29,21 +29,16 @@ final class Component { /** * @param non-empty-string $name - * @param list $files */ public function __construct( public readonly string $name, - public readonly array $files, + public readonly File $file, public ?Doc $doc = null, public ?ComponentMeta $meta = null, private array $dependencies = [], ) { Assert::componentName($name); - if ([] === $files) { - throw new \InvalidArgumentException(\sprintf('The component "%s" must have at least one file.', $name)); - } - foreach ($this->meta?->dependencies ?? [] as $dependency) { $this->addDependency($dependency); } diff --git a/src/Toolkit/src/Asset/StimulusController.php b/src/Toolkit/src/Asset/StimulusController.php index 94db7e19a89..323a082f002 100644 --- a/src/Toolkit/src/Asset/StimulusController.php +++ b/src/Toolkit/src/Asset/StimulusController.php @@ -23,16 +23,11 @@ class StimulusController { /** * @param non-empty-string $name - * @param list $files */ public function __construct( public readonly string $name, - public readonly array $files, + public readonly File $file, ) { Assert::stimulusControllerName($this->name); - - if ([] === $files) { - throw new \InvalidArgumentException(\sprintf('Stimulus controller "%s" has no files.', $name)); - } } } diff --git a/src/Toolkit/src/Command/DebugKitCommand.php b/src/Toolkit/src/Command/DebugKitCommand.php index 6a79cb84d71..bed630aeb30 100644 --- a/src/Toolkit/src/Command/DebugKitCommand.php +++ b/src/Toolkit/src/Command/DebugKitCommand.php @@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'Dependencies', ]) ->addRow([ - implode("\n", $component->files), + $component->file, implode("\n", $component->getDependencies()), ]) ->setColumnWidth(1, 80) diff --git a/src/Toolkit/src/Installer/PoolResolver.php b/src/Toolkit/src/Installer/PoolResolver.php index 00edbf661f2..6e0db9105fd 100644 --- a/src/Toolkit/src/Installer/PoolResolver.php +++ b/src/Toolkit/src/Installer/PoolResolver.php @@ -39,9 +39,7 @@ public function resolveForComponent(Kit $kit, Component $component): Pool $visitedComponents->attach($currentComponent); - foreach ($currentComponent->files as $file) { - $pool->addFile($file); - } + $pool->addFile($currentComponent->file); foreach ($currentComponent->getDependencies() as $dependency) { if ($dependency instanceof ComponentDependency) { @@ -53,11 +51,9 @@ public function resolveForComponent(Kit $kit, Component $component): Pool throw new \RuntimeException(\sprintf('Stimulus controller "%s" not found.', $dependency->name)); } - foreach ($stimulusController->files as $file) { - $pool->addFile($file); - } + $pool->addFile($stimulusController->file); } else { - throw new \RuntimeException(\sprintf('Unknown dependency type: %s', $dependency::class)); + throw new \RuntimeException(\sprintf('Unknown dependency type: "%s"', $dependency::class)); } } } diff --git a/src/Toolkit/src/Kit/KitContextRunner.php b/src/Toolkit/src/Kit/KitContextRunner.php index 43171b01330..24576ac18de 100644 --- a/src/Toolkit/src/Kit/KitContextRunner.php +++ b/src/Toolkit/src/Kit/KitContextRunner.php @@ -12,7 +12,6 @@ namespace Symfony\UX\Toolkit\Kit; use Symfony\Component\Filesystem\Path; -use Symfony\UX\Toolkit\File\FileType; use Symfony\UX\TwigComponent\ComponentFactory; use Symfony\UX\TwigComponent\ComponentTemplateFinderInterface; use Twig\Loader\ChainLoader; @@ -94,11 +93,7 @@ public function findAnonymousComponentTemplate(string $name): ?string throw new \RuntimeException(\sprintf('Component "%s" does not exist in kit "%s".', $name, $this->kit->name)); } - foreach ($component->files as $file) { - 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)); + return $component->file->relativePathName; } }; } diff --git a/src/Toolkit/src/Kit/KitSynchronizer.php b/src/Toolkit/src/Kit/KitSynchronizer.php index 80fbf393c35..449a8359e59 100644 --- a/src/Toolkit/src/Kit/KitSynchronizer.php +++ b/src/Toolkit/src/Kit/KitSynchronizer.php @@ -22,7 +22,6 @@ use Symfony\UX\Toolkit\File\ComponentMeta; use Symfony\UX\Toolkit\File\Doc; use Symfony\UX\Toolkit\File\File; -use Symfony\UX\Toolkit\File\FileType; /** * @internal @@ -86,10 +85,10 @@ private function synchronizeComponents(Kit $kit): void $component = new Component( name: $componentName, - files: [new File( + file: new File( relativePathNameToKit: $relativePathNameToKit, relativePathName: $relativePathName, - )], + ), meta: $meta, ); @@ -116,36 +115,34 @@ private function resolveComponentDependencies(Kit $kit, Component $component): v } // Find dependencies based on file content - foreach ($component->files as $file) { - if (!$this->filesystem->exists($filePath = Path::join($kit->path, $file->relativePathNameToKit))) { - throw new \RuntimeException(\sprintf('File "%s" not found', $filePath)); - } + if (!$this->filesystem->exists($filePath = Path::join($kit->path, $component->file->relativePathNameToKit))) { + throw new \RuntimeException(\sprintf('File "%s" not found', $filePath)); + } - $fileContent = file_get_contents($filePath); + $fileContent = file_get_contents($filePath); - if (str_contains($fileContent, 'name) { - continue; - } + if (str_contains($fileContent, '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, $component->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)); } } } @@ -167,10 +164,10 @@ private function synchronizeStimulusControllers(Kit $kit): void $controllerName = $this->extractStimulusControllerName($relativePathName); $controller = new StimulusController( name: $controllerName, - files: [new File( + file: new File( relativePathNameToKit: $relativePathNameToKit, relativePathName: $relativePathName, - )], + ), ); $kit->addStimulusController($controller); diff --git a/src/Toolkit/tests/Asset/ComponentTest.php b/src/Toolkit/tests/Asset/ComponentTest.php index 8a8886c6b11..de4027d7270 100644 --- a/src/Toolkit/tests/Asset/ComponentTest.php +++ b/src/Toolkit/tests/Asset/ComponentTest.php @@ -17,19 +17,15 @@ use Symfony\UX\Toolkit\Dependency\PhpPackageDependency; use Symfony\UX\Toolkit\Dependency\Version; use Symfony\UX\Toolkit\File\File; -use Symfony\UX\Toolkit\File\FileType; final class ComponentTest extends TestCase { public function testCanBeInstantiated(): void { - $component = new Component('Button', [ - new File('templates/components/Button/Button.html.twig', 'Button.html.twig'), - ]); + $component = new Component('Button', new File('templates/components/Button/Button.html.twig', 'Button.html.twig')); $this->assertSame('Button', $component->name); - $this->assertCount(1, $component->files); - $this->assertInstanceOf(File::class, $component->files[0]); + $this->assertInstanceOf(File::class, $component->file); $this->assertNull($component->doc); $this->assertCount(0, $component->getDependencies()); } @@ -39,24 +35,12 @@ public function testShouldFailIfComponentNameIsInvalid(): void $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Invalid component name "foobar".'); - new Component('foobar', [ - new File('templates/components/Button/Button.html.twig', 'Button.html.twig'), - ]); - } - - public function testShouldFailIfComponentHasNoFiles(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('The component "Button" must have at least one file.'); - - new Component('Button', []); + new Component('foobar', new File('templates/components/Button/Button.html.twig', 'Button.html.twig')); } public function testCanAddAndGetDependencies(): void { - $component = new Component('Button', [ - new File('templates/components/Button/Button.html.twig', 'Button.html.twig'), - ]); + $component = new Component('Button', new File('templates/components/Button/Button.html.twig', 'Button.html.twig')); $component->addDependency($dependency1 = new ComponentDependency('Icon')); $component->addDependency($dependency2 = new ComponentDependency('Label')); @@ -68,9 +52,7 @@ public function testCanAddAndGetDependencies(): void public function testShouldNotAddDuplicateComponentDependencies(): void { - $component = new Component('Button', [ - new File('templates/components/Button/Button.html.twig', 'Button.html.twig'), - ]); + $component = new Component('Button', new File('templates/components/Button/Button.html.twig', 'Button.html.twig')); $component->addDependency($dependency1 = new ComponentDependency('Icon')); $component->addDependency($dependency2 = new ComponentDependency('Label')); @@ -83,9 +65,7 @@ public function testShouldNotAddDuplicateComponentDependencies(): void public function testShouldReplacePhpPackageDependencyIfVersionIsHigher(): void { - $component = new Component('Button', [ - new File('templates/components/Button/Button.html.twig', 'Button.html.twig'), - ]); + $component = new Component('Button', new File('templates/components/Button/Button.html.twig', 'Button.html.twig')); $component->addDependency($dependency1 = new ComponentDependency('Icon')); $component->addDependency($dependency2 = new ComponentDependency('Label')); @@ -102,9 +82,7 @@ public function testShouldReplacePhpPackageDependencyIfVersionIsHigher(): void public function testShouldNotReplacePhpPackageDependencyIfVersionIsLower(): void { - $component = new Component('Button', [ - new File('templates/components/Button/Button.html.twig', 'Button.html.twig'), - ]); + $component = new Component('Button', new File('templates/components/Button/Button.html.twig', 'Button.html.twig')); $component->addDependency($dependency1 = new ComponentDependency('Icon')); $component->addDependency($dependency2 = new ComponentDependency('Label')); diff --git a/src/Toolkit/tests/Asset/StimulusControllerTest.php b/src/Toolkit/tests/Asset/StimulusControllerTest.php index 092755e0515..cccce38d550 100644 --- a/src/Toolkit/tests/Asset/StimulusControllerTest.php +++ b/src/Toolkit/tests/Asset/StimulusControllerTest.php @@ -16,15 +16,12 @@ use PHPUnit\Framework\TestCase; use Symfony\UX\Toolkit\Asset\StimulusController; use Symfony\UX\Toolkit\File\File; -use Symfony\UX\Toolkit\File\FileType; final class StimulusControllerTest extends TestCase { public function testCanBeInstantiated(): void { - $stimulusController = new StimulusController('clipboard', [ - new File('assets/controllers/clipboard_controller.js', 'clipboard_controller.js'), - ]); + $stimulusController = new StimulusController('clipboard', new File('assets/controllers/clipboard_controller.js', 'clipboard_controller.js')); $this->assertSame('clipboard', $stimulusController->name); } @@ -34,14 +31,6 @@ public function testShouldFailIfStimulusControllerNameIsInvalid(): void $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Invalid Stimulus controller name "invalid_controller".'); - new StimulusController('invalid_controller', [new File('assets/controllers/invalid_controller.js', 'invalid_controller.js')]); - } - - public function testShouldFailIfStimulusControllerHasNoFiles(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Stimulus controller "clipboard" has no files.'); - - new StimulusController('clipboard', []); + new StimulusController('invalid_controller', new File('assets/controllers/invalid_controller.js', 'invalid_controller.js')); } } diff --git a/src/Toolkit/tests/File/FileTest.php b/src/Toolkit/tests/File/FileTest.php index b0568475040..d92958ed2f5 100644 --- a/src/Toolkit/tests/File/FileTest.php +++ b/src/Toolkit/tests/File/FileTest.php @@ -13,7 +13,6 @@ use PHPUnit\Framework\TestCase; use Symfony\UX\Toolkit\File\File; -use Symfony\UX\Toolkit\File\FileType; final class FileTest extends TestCase { diff --git a/src/Toolkit/tests/Installer/PoolTest.php b/src/Toolkit/tests/Installer/PoolTest.php index 61e76763612..a5dc227689e 100644 --- a/src/Toolkit/tests/Installer/PoolTest.php +++ b/src/Toolkit/tests/Installer/PoolTest.php @@ -17,7 +17,6 @@ use Symfony\UX\Toolkit\Dependency\PhpPackageDependency; use Symfony\UX\Toolkit\Dependency\Version; use Symfony\UX\Toolkit\File\File; -use Symfony\UX\Toolkit\File\FileType; use Symfony\UX\Toolkit\Installer\Pool; final class PoolTest extends TestCase diff --git a/src/Toolkit/tests/Kit/KitFactoryTest.php b/src/Toolkit/tests/Kit/KitFactoryTest.php index 5fb29c1f1b8..fd160179593 100644 --- a/src/Toolkit/tests/Kit/KitFactoryTest.php +++ b/src/Toolkit/tests/Kit/KitFactoryTest.php @@ -17,7 +17,6 @@ use Symfony\UX\Toolkit\Dependency\PhpPackageDependency; use Symfony\UX\Toolkit\Dependency\StimulusControllerDependency; use Symfony\UX\Toolkit\File\File; -use Symfony\UX\Toolkit\File\FileType; use Symfony\UX\Toolkit\Kit\KitFactory; final class KitFactoryTest extends KernelTestCase @@ -54,7 +53,6 @@ public function testCanCreateShadKit(): void $table = $kit->getComponent('Table'); $this->assertNotNull($table); - $this->assertNotEmpty($table->files); $this->assertEquals([ new PhpPackageDependency('tales-from-a-dev/twig-tailwind-extra'), new ComponentDependency('Table:Body'), @@ -85,10 +83,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('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')]), + $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')); diff --git a/src/Toolkit/tests/Kit/KitTest.php b/src/Toolkit/tests/Kit/KitTest.php index 8dda2fcf2ba..c0327a6dbc4 100644 --- a/src/Toolkit/tests/Kit/KitTest.php +++ b/src/Toolkit/tests/Kit/KitTest.php @@ -14,7 +14,6 @@ use PHPUnit\Framework\TestCase; use Symfony\UX\Toolkit\Asset\Component; use Symfony\UX\Toolkit\File\File; -use Symfony\UX\Toolkit\File\FileType; use Symfony\UX\Toolkit\Kit\Kit; final class KitTest extends TestCase @@ -38,8 +37,8 @@ public function testShouldFailIfKitPathIsNotAbsolute(): void public function testCanAddComponentsToTheKit(): void { $kit = new Kit(__DIR__, 'foo', 'https://example.com', 'MIT'); - $kit->addComponent(new Component('Table', [new File('Table.html.twig', 'Table.html.twig')], null)); - $kit->addComponent(new Component('Table:Row', [new File('Table/Row.html.twig', 'Table/Row.html.twig')], null)); + $kit->addComponent(new Component('Table', new File('Table.html.twig', 'Table.html.twig'), null)); + $kit->addComponent(new Component('Table:Row', new File('Table/Row.html.twig', 'Table/Row.html.twig'), null)); $this->assertCount(2, $kit->getComponents()); } @@ -50,15 +49,15 @@ public function testShouldFailIfComponentIsAlreadyRegisteredInTheKit(): void $this->expectExceptionMessage('Component "Table" is already registered in the kit.'); $kit = new Kit(__DIR__, 'foo', 'https://example.com', 'MIT'); - $kit->addComponent(new Component('Table', [new File('Table.html.twig', 'Table.html.twig')], null)); - $kit->addComponent(new Component('Table', [new File('Table.html.twig', 'Table.html.twig')], null)); + $kit->addComponent(new Component('Table', new File('Table.html.twig', 'Table.html.twig'), null)); + $kit->addComponent(new Component('Table', new File('Table.html.twig', 'Table.html.twig'), null)); } public function testCanGetComponentByName(): void { $kit = new Kit(__DIR__, 'foo', 'https://example.com', 'MIT'); - $kit->addComponent(new Component('Table', [new File('Table.html.twig', 'Table.html.twig')], null)); - $kit->addComponent(new Component('Table:Row', [new File('Table/Row.html.twig', 'Table/Row.html.twig')], null)); + $kit->addComponent(new Component('Table', new File('Table.html.twig', 'Table.html.twig'), null)); + $kit->addComponent(new Component('Table:Row', new File('Table/Row.html.twig', 'Table/Row.html.twig'), null)); $this->assertSame('Table', $kit->getComponent('Table')->name); $this->assertSame('Table:Row', $kit->getComponent('Table:Row')->name);