Skip to content

Commit 510bfb0

Browse files
committed
maker command
1 parent 572a79f commit 510bfb0

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"symfony/ux-live-component": "^2.16",
3737
"symfony/ux-twig-component": "2.16",
3838
"symfonycasts/sass-bundle": "^0.5.1",
39-
"symfonycasts/tailwind-bundle": "^0.5.0"
39+
"symfonycasts/tailwind-bundle": "^0.5.0",
40+
"symfony/maker-bundle": "^1.60"
4041
},
4142
"conflict": {
4243
"symfony/ux-twig-component": "2.17"

src/DependencyInjection/StorybookExtension.php

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Storybook\EventListener\ComponentMockSubscriber;
1313
use Storybook\EventListener\ProxyRequestListener;
1414
use Storybook\Exception\UnauthorizedStoryException;
15+
use Storybook\Maker\MakeStory;
1516
use Storybook\Mock\ComponentProxyFactory;
1617
use Storybook\StoryRenderer;
1718
use Storybook\Twig\StorybookEnvironmentConfigurator;
@@ -139,6 +140,17 @@ static function (ChildDefinition $definition, AsComponentMock $attributeInstance
139140
$container->register('storybook.component_mock_subscriber', ComponentMockSubscriber::class)
140141
->setArgument(0, new Reference('storybook.component_proxy_factory'))
141142
->addTag('kernel.event_subscriber');
143+
144+
// Maker
145+
$container->register('storybook.maker.story_renderer', \Storybook\Maker\StoryRenderer::class)
146+
->setArgument(0, new Reference('maker.generator'))
147+
->setArgument(1, new Reference('ux.twig_component.component_factory'))
148+
->setArgument(2, '%kernel.project_dir%/stories');
149+
150+
$container->register('storybook.maker.story_maker', MakeStory::class)
151+
->setArgument(0, new Reference('storybook.maker.story_renderer'))
152+
->addTag('maker.command')
153+
;
142154
}
143155

144156
public function getConfigTreeBuilder(): TreeBuilder

src/Maker/MakeStory.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Storybook\Maker;
4+
5+
use Symfony\Bundle\MakerBundle\ConsoleStyle;
6+
use Symfony\Bundle\MakerBundle\DependencyBuilder;
7+
use Symfony\Bundle\MakerBundle\Generator;
8+
use Symfony\Bundle\MakerBundle\InputConfiguration;
9+
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Input\InputArgument;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Question\Question;
14+
use Symfony\Component\Finder\Finder;
15+
16+
/**
17+
* @method string getCommandDescription()
18+
*/
19+
class MakeStory extends AbstractMaker
20+
{
21+
public function __construct(
22+
private StoryRenderer $storyRenderer
23+
) {}
24+
25+
public static function getCommandName(): string
26+
{
27+
return 'make:storybook:story';
28+
}
29+
30+
public static function getCommandDescription(): string
31+
{
32+
return 'Creates a new Storybook story';
33+
}
34+
35+
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
36+
{
37+
$command
38+
->addArgument('component', InputArgument::OPTIONAL, 'The name of the component you want a story for')
39+
;
40+
}
41+
42+
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
43+
{
44+
if (null === $input->getArgument('component')) {
45+
$question = new Question('What is the name of the component?');
46+
$question->setValidator(fn ($value) => null !== $value);
47+
$question->setMaxAttempts(3);
48+
$input->setArgument('component', $io->askQuestion($question));
49+
}
50+
}
51+
52+
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
53+
{
54+
$componentName = $input->getArgument('component');
55+
56+
$this->storyRenderer->render($componentName);
57+
}
58+
59+
public function configureDependencies(DependencyBuilder $dependencies)
60+
{
61+
}
62+
}

src/Maker/StoryRenderer.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Storybook\Maker;
4+
5+
use Symfony\Bundle\MakerBundle\Generator;
6+
use Symfony\UX\TwigComponent\ComponentFactory;
7+
8+
class StoryRenderer
9+
{
10+
public function __construct(
11+
private Generator $generator,
12+
private ComponentFactory $componentFactory,
13+
private string $storiesPath
14+
) {}
15+
16+
public function render(string $componentName): void
17+
{
18+
$componentMetadata = $this->componentFactory->metadataFor($componentName);
19+
20+
$this->generator->generateFile($this->storiesPath . '/' . $componentName . '.stories.js', __DIR__ . '/../Resources/skeleton/Story.tpl.php', [
21+
'componentName' => $componentName,
22+
'template' => $componentMetadata->getTemplate()
23+
]);
24+
25+
$this->generator->writeChanges();
26+
}
27+
}

src/Resources/skeleton/Story.tpl.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import <?= $componentName ?> from "../templates/<?= $template ?>";
2+
import { twig } from '@sensiolabs/storybook-symfony-webpack5';
3+
4+
export default {
5+
component: (args) => ({
6+
component: {<?= $componentName ?>},
7+
template: twig`
8+
<twig:<?= $componentName ?>/>
9+
`,
10+
})
11+
}
12+

0 commit comments

Comments
 (0)