Skip to content

Commit f838b8c

Browse files
committed
Generate an example usage
1 parent 86a47ea commit f838b8c

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

src/Maker/MakeStimulusController.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
158158
$io->text([
159159
'Next:',
160160
\sprintf('- Open <info>%s</info> and add the code you need', $filePath),
161+
'- Use the controller in your templates:',
162+
...array_map(
163+
fn (string $line): string => " $line",
164+
explode("\n", $this->generateUsageExample($controllerName, $targetArgs, $valuesArg, $classesArgs)),
165+
),
161166
'Find the documentation at <fg=yellow>https://symfony.com/bundles/StimulusBundle</>',
162167
]);
163168
}
@@ -287,6 +292,51 @@ private function getValuesTypes(): array
287292
];
288293
}
289294

295+
/**
296+
* @param array<int, string> $targets
297+
* @param array{name: string, type: string} $values
298+
* @param array<int, string> $classes
299+
*/
300+
private function generateUsageExample(string $name, array $targets, array $values, array $classes): string
301+
{
302+
$slugify = fn (string $name) => str_replace('_', '-', Str::asSnakeCase($name));
303+
$controller = $slugify($name);
304+
305+
$htmlTargets = [];
306+
foreach ($targets as $target) {
307+
$htmlTargets[] = \sprintf('<div data-%s-target="%s"></div>', $controller, $target);
308+
}
309+
310+
$htmlValues = [];
311+
foreach ($values as ['name' => $name, 'type' => $type]) {
312+
$value = match ($type) {
313+
'Array' => '[]',
314+
'Boolean' => 'false',
315+
'Number' => '123',
316+
'Object' => '{}',
317+
'String' => 'abc',
318+
default => '',
319+
};
320+
$htmlValues[] = \sprintf('data-%s-%s-value="%s"', $controller, $slugify($name), $value);
321+
}
322+
323+
$htmlClasses = [];
324+
foreach ($classes as $class) {
325+
$value = Str::asLowerCamelCase($class);
326+
$htmlClasses[] = \sprintf('data-%s-%s-class="%s"', $controller, $slugify($class), $value);
327+
}
328+
329+
return \sprintf(
330+
'<div data-controller="%s"%s%s%s>%s%s</div>',
331+
$controller,
332+
$htmlValues ? ("\n ".implode("\n ", $htmlValues)) : '',
333+
$htmlClasses ? ("\n ".implode("\n ", $htmlClasses)) : '',
334+
$htmlValues || $htmlClasses ? "\n" : '',
335+
$htmlTargets ? ("\n ".implode("\n ", $htmlTargets)) : '',
336+
"\n <!-- ... -->\n",
337+
);
338+
}
339+
290340
public function configureDependencies(DependencyBuilder $dependencies): void
291341
{
292342
// lower than 8.1, allow WebpackEncoreBundle

tests/Maker/MakeStimulusControllerTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,70 @@ public function getTestDetails(): \Generator
210210
$this->assertFileDoesNotExist($runner->getPath('assets/controllers/typescript_controller.js'));
211211
}),
212212
];
213+
214+
yield 'it_displays_controller_basic_usage_example' => [$this->createMakerTest()
215+
->run(function (MakerTestRunner $runner) {
216+
$output = $runner->runMaker(
217+
[
218+
'fooBar',
219+
'js',
220+
],
221+
);
222+
223+
$usageExample = <<<HTML
224+
<div data-controller="foo-bar">
225+
<!-- ... -->
226+
</div>
227+
HTML;
228+
229+
$this->assertStringContainsString('- Use the controller in your templates:', $output);
230+
foreach (explode("\n", $usageExample) as $line) {
231+
$this->assertStringContainsString($line, $output);
232+
}
233+
}),
234+
];
235+
236+
yield 'it_displays_controller_complete_usage_example' => [$this->createMakerTest()
237+
->run(function (MakerTestRunner $runner) {
238+
$output = $runner->runMaker(
239+
[
240+
'fooBar',
241+
'js',
242+
'yes', // add targets
243+
'firstOne',
244+
'secondOne',
245+
'',
246+
'yes', // add values
247+
'minItems',
248+
'Number',
249+
'email',
250+
'String',
251+
'',
252+
'yes', // add classes
253+
'isVisible',
254+
'hidden',
255+
'',
256+
],
257+
);
258+
259+
$usageExample = <<<HTML
260+
<div data-controller="foo-bar"
261+
data-foo-bar-min-items-value="123"
262+
data-foo-bar-email-value="abc"
263+
data-foo-bar-is-visible-class="isVisible"
264+
data-foo-bar-hidden-class="hidden"
265+
>
266+
<div data-foo-bar-target="firstOne"></div>
267+
<div data-foo-bar-target="secondOne"></div>
268+
<!-- ... -->
269+
</div>
270+
HTML;
271+
272+
$this->assertStringContainsString('- Use the controller in your templates:', $output);
273+
foreach (explode("\n", $usageExample) as $line) {
274+
$this->assertStringContainsString($line, $output);
275+
}
276+
}),
277+
];
213278
}
214279
}

0 commit comments

Comments
 (0)