Skip to content

[make:twig-component] Improve make:twig-component by reading the configuration file #1571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 18, 2024
24 changes: 23 additions & 1 deletion src/Maker/MakeTwigComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Yaml\Yaml;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

Expand All @@ -27,6 +30,12 @@
*/
final class MakeTwigComponent extends AbstractMaker
{
private string $namespace = 'Twig\\Components';

public function __construct(private FileManager $fileManager)
{
}

public static function getCommandName(): string
{
return 'make:twig-component';
Expand Down Expand Up @@ -62,7 +71,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

$factory = $generator->createClassNameDetails(
$name,
'Twig\\Components',
$this->namespace,
);

$templatePath = str_replace('\\', '/', $factory->getRelativeNameWithoutSuffix());
Expand Down Expand Up @@ -93,5 +102,18 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
if (!$input->getOption('live')) {
$input->setOption('live', $io->confirm('Make this a live component?', false));
}

$path = 'config/packages/twig_component.yaml';

if (!$this->fileManager->fileExists($path)) {
throw new RuntimeCommandException(message: 'Unable to find twig_components.yaml');
}

try {
$value = Yaml::parse($this->fileManager->getFileContents($path));
$this->namespace = substr(array_key_first($value['twig_component']['defaults']), 4);
} catch (\Throwable $throwable) {
throw new RuntimeCommandException(message: 'Unable to parse twig_components.yaml', previous: $throwable);
}
}
}
1 change: 1 addition & 0 deletions src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<service id="maker.maker.make_twig_component" class="Symfony\Bundle\MakerBundle\Maker\MakeTwigComponent">
<tag name="maker.command" />
<argument type="service" id="maker.file_manager" />
</service>

<service id="maker.maker.make_controller" class="Symfony\Bundle\MakerBundle\Maker\MakeController">
Expand Down
23 changes: 23 additions & 0 deletions tests/Maker/MakeTwigComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ public function getTestDetails(): \Generator
}),
];

yield 'it_generates_twig_component_in_non_default_namespace' => [$this->createMakerTest()
->addExtraDependencies('symfony/ux-twig-component', 'symfony/twig-bundle')
->run(function (MakerTestRunner $runner) {
$runner->copy(
'make-twig-component/custom_twig_component.yaml',
'config/packages/twig_component.yaml'
);

$output = $runner->runMaker(['Alert']);

$this->assertStringContainsString('src/Site/Twig/Components/Alert.php', $output);
$this->assertStringContainsString('templates/components/Alert.html.twig', $output);
$this->assertStringContainsString('To render the component, use <twig:Alert />.', $output);

$runner->copy(
'make-twig-component/tests/it_generates_twig_component.php',
'tests/GeneratedTwigComponentTest.php'
);
$runner->replaceInFile('tests/GeneratedTwigComponentTest.php', '{name}', 'Alert');
$runner->runTests();
}),
];

yield 'it_generates_pascal_case_twig_component' => [$this->createMakerTest()
->addExtraDependencies('symfony/ux-twig-component', 'symfony/twig-bundle')
->run(function (MakerTestRunner $runner) {
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/make-twig-component/custom_twig_component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
twig_component:
anonymous_template_directory: 'components/'
defaults:
# Namespace & directory for components
App\Site\Twig\Components\: 'components/'
Loading