Skip to content

Commit aca0772

Browse files
committed
add make:twig-component maker
1 parent 0f40c82 commit aca0772

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

src/Maker/MakeTwigComponent.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Maker;
13+
14+
use Symfony\Bundle\MakerBundle\ConsoleStyle;
15+
use Symfony\Bundle\MakerBundle\DependencyBuilder;
16+
use Symfony\Bundle\MakerBundle\Generator;
17+
use Symfony\Bundle\MakerBundle\InputConfiguration;
18+
use Symfony\Bundle\MakerBundle\Str;
19+
use Symfony\Component\Console\Command\Command;
20+
use Symfony\Component\Console\Input\InputArgument;
21+
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\Console\Input\InputOption;
23+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
24+
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
25+
26+
/**
27+
* @author Kevin Bond <kevinbond@gmail.com>
28+
*/
29+
final class MakeTwigComponent extends AbstractMaker
30+
{
31+
public static function getCommandName(): string
32+
{
33+
return 'make:twig-component';
34+
}
35+
36+
public static function getCommandDescription(): string
37+
{
38+
return 'Creates a twig (or live) component';
39+
}
40+
41+
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
42+
{
43+
$command
44+
->setDescription(self::getCommandDescription())
45+
->addArgument('name', InputArgument::OPTIONAL, 'The name of your twig component (ie <fg=yellow>NotificationComponent</>)')
46+
->addOption('live', null, InputOption::VALUE_NONE, 'Whether to create a live twig component (requires <fg=yellow>symfony/ux-live-component</>)')
47+
;
48+
}
49+
50+
public function configureDependencies(DependencyBuilder $dependencies): void
51+
{
52+
$dependencies->addClassDependency(AsTwigComponent::class, 'symfony/ux-twig-component');
53+
}
54+
55+
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
56+
{
57+
$name = $input->getArgument('name');
58+
$live = $input->getOption('live');
59+
60+
if ($live && !class_exists(AsLiveComponent::class)) {
61+
throw new \RuntimeException('You must install symfony/ux-live-component to create a live component (composer require symfony/ux-live-component)');
62+
}
63+
64+
$factory = $generator->createClassNameDetails(
65+
$name,
66+
'Twig\\Components',
67+
'Component'
68+
);
69+
70+
$shortName = Str::asSnakeCase(Str::removeSuffix($factory->getShortName(), 'Component'));
71+
72+
$generator->generateClass(
73+
$factory->getFullName(),
74+
sprintf('%s/../Resources/skeleton/twig/%s', __DIR__, $live ? 'LiveComponent.tpl.php' : 'Component.tpl.php'),
75+
[
76+
'live' => $live,
77+
'short_name' => $shortName,
78+
]
79+
);
80+
$generator->generateTemplate(
81+
"components/{$shortName}.html.twig",
82+
sprintf('%s/../Resources/skeleton/twig/%s', __DIR__, $live ? 'live_component_template.tpl.php' : 'component_template.tpl.php')
83+
);
84+
85+
$generator->writeChanges();
86+
87+
$this->writeSuccessMessage($io);
88+
}
89+
90+
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
91+
{
92+
if (!$input->getOption('live')) {
93+
$input->setOption('live', $io->confirm('Make this a live component?', class_exists(AsLiveComponent::class)));
94+
}
95+
}
96+
}

src/Resources/config/makers.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
<tag name="maker.command" />
2020
</service>
2121

22+
<service id="maker.maker.make_twig_component" class="Symfony\Bundle\MakerBundle\Maker\MakeTwigComponent">
23+
<tag name="maker.command" />
24+
</service>
25+
2226
<service id="maker.maker.make_controller" class="Symfony\Bundle\MakerBundle\Maker\MakeController">
2327
<argument type="service" id="maker.file_manager" />
2428
<tag name="maker.command" />
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?= "<?php\n" ?>
2+
3+
namespace <?= $namespace; ?>;
4+
5+
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
6+
7+
#[AsTwigComponent('<?= $short_name; ?>', 'components/<?= $short_name; ?>.html.twig')]
8+
final class <?= $class_name."\n" ?>
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?= "<?php\n" ?>
2+
3+
namespace <?= $namespace; ?>;
4+
5+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
6+
7+
#[AsLiveComponent('<?= $short_name; ?>', 'components/<?= $short_name; ?>.html.twig')]
8+
final class <?= $class_name."\n" ?>
9+
{
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- component html -->
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div {{ init_live_component(this) }}>
2+
<!-- component html -->
3+
</div>

0 commit comments

Comments
 (0)