10000 feature #1571 [make:twig-component] Improve `make:twig-component` by … · symfony/maker-bundle@08b21c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 08b21c3

Browse files
shadowcjrushlow
andauthored
feature #1571 [make:twig-component] Improve make:twig-component by reading the configuration file
* Improve the make:twig_component command to read the first entry in the twig_component.yaml configuration file to get the proper class path * Add missing argument * Remove \App from the namespace * Code fixes * remove unused import * php-cs-fixer fixes * Add checks for yaml parsing validation * Fix code style * Add test * Update tests/Maker/MakeTwigComponentTest.php Co-authored-by: Jesse Rushlow <40327885+jrushlow@users.noreply.github.com> * call copy before running maker * refactor interact method try..catch * Remove unused import * Add throw statements on errors --------- Co-authored-by: Jesse Rushlow <40327885+jrushlow@users.noreply.github.com> Co-authored-by: Jesse Rushlow <jr@rushlow.dev>
1 parent 2d1007f commit 08b21c3

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/Maker/MakeTwigComponent.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313

1414
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1515
use Symfony\Bundle\MakerBundle\DependencyBuilder;
16+
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
17+
use Symfony\Bundle\MakerBundle\FileManager;
1618
use Symfony\Bundle\MakerBundle\Generator;
1719
use Symfony\Bundle\MakerBundle\InputConfiguration;
1820
use Symfony\Component\Console\Command\Command;
1921
use Symfony\Component\Console\Input\InputArgument;
2022
use Symfony\Component\Console\Input\InputInterface;
2123
use Symfony\Component\Console\Input\InputOption;
24+
use Symfony\Component\Yaml\Yaml;
2225
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
2326
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
2427

@@ -27,6 +30,12 @@
2730
*/
2831
final class MakeTwigComponent extends AbstractMaker
2932
{
33+
private string $namespace = 'Twig\\Components';
34+
35+
public function __construct(private FileManager $fileManager)
36+
{
37+
}
38+
3039
public static function getCommandName(): string
3140
{
3241
return 'make:twig-component';
@@ -62,7 +71,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
6271

6372
$factory = $generator->createClassNameDetails(
6473
$name,
65-
'Twig\\Components',
74+
$this->namespace,
6675
);
6776

6877
$templatePath = str_replace('\\', '/', $factory->getRelativeNameWithoutSuffix());
@@ -93,5 +102,18 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
93102
if (!$input->getOption('live')) {
94103
$input->setOption('live', $io->confirm('Make this a live component?', false));
95104
}
105+
106+
$path = 'config/packages/twig_component.yaml';
107+
108+
if (!$this->fileManager->fileExists($path)) {
109+
throw new RuntimeCommandException(message: 'Unable to find twig_components.yaml');
110+
}
111+
112+
try {
113+
$value = Yaml::parse($this->fileManager->getFileContents($path));
114+
$this->namespace = substr(array_key_first($value['twig_component']['defaults']), 4);
115+
} catch (\Throwable $throwable) {
116+
throw new RuntimeCommandException(message: 'Unable to parse twig_components.yaml', previous: $throwable);
117+
}
96118
}
97119
}

src/Resources/config/makers.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
<service id="maker.maker.make_twig_component" class="Symfony\Bundle\MakerBundle\Maker\MakeTwigComponent">
2424
<tag name="maker.command" />
25+
<argument type="service" id="maker.file_manager" />
2526
</service>
2627

2728
<service id="maker.maker.make_controller" class="Symfony\Bundle\MakerBundle\Maker\MakeController">

tests/Maker/MakeTwigComponentTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ public function getTestDetails(): \Generator
3737
}),
3838
];
3939

40+
yield 'it_generates_twig_component_in_non_default_namespace' => [$this->createMakerTest()
41+
->addExtraDependencies('symfony/ux-twig-component', 'symfony/twig-bundle')
42+
->run(function (MakerTestRunner $runner) {
43+
$runner->copy(
44+
'make-twig-component/custom_twig_component.yaml',
45+
'config/packages/twig_component.yaml'
46+
);
47+
48+
$output = $runner->runMaker(['Alert']);
49+
50+
$this->assertStringContainsString('src/Site/Twig/Components/Alert.php', $output);
51+
$this->assertStringContainsString('templates/components/Alert.html.twig', $output);
52+
$this->assertStringContainsString('To render the component, use <twig:Alert />.', $output);
53+
54+
$runner->copy(
55+
'make-twig-component/tests/it_generates_twig_component.php',
56+
'tests/GeneratedTwigComponentTest.php'
57+
);
58+
$runner->replaceInFile('tests/GeneratedTwigComponentTest.php', '{name}', 'Alert');
59+
$runner->runTests();
60+
}),
61+
];
62+
4063
yield 'it_generates_pascal_case_twig_component' => [$this->createMakerTest()
4164
->addExtraDependencies('symfony/ux-twig-component', 'symfony/twig-bundle')
4265
->run(function (MakerTestRunner $runner) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
twig_component:
2+
anonymous_template_directory: 'components/'
3+
defaults:
4+
# Namespace & directory for components
5+
App\Site\Twig\Components\: 'components/'

0 commit comments

Comments
 (0)
0