Skip to content

[Toolkit] Rename Component and StimulusController property files to file #2801

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 1 addition & 6 deletions src/Toolkit/src/Asset/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,16 @@ final class Component
{
/**
* @param non-empty-string $name
* @param list<File> $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);
}
Expand Down
7 changes: 1 addition & 6 deletions src/Toolkit/src/Asset/StimulusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,11 @@ class StimulusController
{
/**
* @param non-empty-string $name
* @param list<File> $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));
}
}
}
2 changes: 1 addition & 1 deletion src/Toolkit/src/Command/DebugKitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions src/Toolkit/src/Installer/PoolResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/Toolkit/src/Kit/KitContextRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
};
}
Expand Down
53 changes: 25 additions & 28 deletions src/Toolkit/src/Kit/KitSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
);

Expand All @@ -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, '<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, $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));
}
}
}
Expand All @@ -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);
Expand Down
36 changes: 7 additions & 29 deletions src/Toolkit/tests/Asset/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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'));
Expand All @@ -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'));
Expand All @@ -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'));
Expand All @@ -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'));
Expand Down
15 changes: 2 additions & 13 deletions src/Toolkit/tests/Asset/StimulusControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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'));
}
}
1 change: 0 additions & 1 deletion src/Toolkit/tests/File/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 0 additions & 1 deletion src/Toolkit/tests/Installer/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions src/Toolkit/tests/Kit/KitFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'));
Expand Down
13 changes: 6 additions & 7 deletions src/Toolkit/tests/Kit/KitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
Expand All @@ -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);
Expand Down