Skip to content

Commit

Permalink
Generate an example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
smnandre committed Dec 25, 2024
1 parent 86a47ea commit f838b8c
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Maker/MakeStimulusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$io->text([
'Next:',
\sprintf('- Open <info>%s</info> and add the code you need', $filePath),
'- Use the controller in your templates:',
...array_map(
fn (string $line): string => " $line",
explode("\n", $this->generateUsageExample($controllerName, $targetArgs, $valuesArg, $classesArgs)),
),
'Find the documentation at <fg=yellow>https://symfony.com/bundles/StimulusBundle</>',
]);
}
Expand Down Expand Up @@ -287,6 +292,51 @@ private function getValuesTypes(): array
];
}

/**
* @param array<int, string> $targets
* @param array{name: string, type: string} $values
* @param array<int, string> $classes
*/
private function generateUsageExample(string $name, array $targets, array $values, array $classes): string
{
$slugify = fn (string $name) => str_replace('_', '-', Str::asSnakeCase($name));
$controller = $slugify($name);

$htmlTargets = [];
foreach ($targets as $target) {
$htmlTargets[] = \sprintf('<div data-%s-target="%s"></div>', $controller, $target);
}

$htmlValues = [];
foreach ($values as ['name' => $name, 'type' => $type]) {
$value = match ($type) {
'Array' => '[]',
'Boolean' => 'false',
'Number' => '123',
'Object' => '{}',
'String' => 'abc',
default => '',
};
$htmlValues[] = \sprintf('data-%s-%s-value="%s"', $controller, $slugify($name), $value);
}

$htmlClasses = [];
foreach ($classes as $class) {
$value = Str::asLowerCamelCase($class);
$htmlClasses[] = \sprintf('data-%s-%s-class="%s"', $controller, $slugify($class), $value);
}

return \sprintf(
'<div data-controller="%s"%s%s%s>%s%s</div>',
$controller,
$htmlValues ? ("\n ".implode("\n ", $htmlValues)) : '',

Check failure on line 332 in src/Maker/MakeStimulusController.php

View workflow job for this annotation

GitHub Actions / PHPStan

Ternary operator condition is always true.
$htmlClasses ? ("\n ".implode("\n ", $htmlClasses)) : '',
$htmlValues || $htmlClasses ? "\n" : '',

Check failure on line 334 in src/Maker/MakeStimulusController.php

View workflow job for this annotation

GitHub Actions / PHPStan

Left side of || is always true.
$htmlTargets ? ("\n ".implode("\n ", $htmlTargets)) : '',
"\n <!-- ... -->\n",
);
}

public function configureDependencies(DependencyBuilder $dependencies): void
{
// lower than 8.1, allow WebpackEncoreBundle
Expand Down
65 changes: 65 additions & 0 deletions tests/Maker/MakeStimulusControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,70 @@ public function getTestDetails(): \Generator
$this->assertFileDoesNotExist($runner->getPath('assets/controllers/typescript_controller.js'));
}),
];

yield 'it_displays_controller_basic_usage_example' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker(
[
'fooBar',
'js',
],
);

$usageExample = <<<HTML
<div data-controller="foo-bar">
<!-- ... -->
</div>
HTML;

$this->assertStringContainsString('- Use the controller in your templates:', $output);
foreach (explode("\n", $usageExample) as $line) {
$this->assertStringContainsString($line, $output);
}
}),
];

yield 'it_displays_controller_complete_usage_example' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker(
[
'fooBar',
'js',
'yes', // add targets
'firstOne',
'secondOne',
'',
'yes', // add values
'minItems',
'Number',
'email',
'String',
'',
'yes', // add classes
'isVisible',
'hidden',
'',
],
);

$usageExample = <<<HTML
<div data-controller="foo-bar"
data-foo-bar-min-items-value="123"
data-foo-bar-email-value="abc"
data-foo-bar-is-visible-class="isVisible"
data-foo-bar-hidden-class="hidden"
>
<div data-foo-bar-target="firstOne"></div>
<div data-foo-bar-target="secondOne"></div>
<!-- ... -->
</div>
HTML;

$this->assertStringContainsString('- Use the controller in your templates:', $output);
foreach (explode("\n", $usageExample) as $line) {
$this->assertStringContainsString($line, $output);
}
}),
];
}
}

0 comments on commit f838b8c

Please sign in to comment.