Skip to content
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

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

Merged
merged 14 commits into from
Jun 18, 2024
29 changes: 28 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\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\Exception\ParseException;
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,23 @@ 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)) {
return;
}

try {
$value = Yaml::parse($this->fileManager->getFileContents($path));
} catch (ParseException $e) {
return;
}

if (!\array_key_exists('twig_component', $value) || !\array_key_exists('defaults', $value['twig_component'])) {
return;
}

$this->namespace = substr(array_key_first($value['twig_component']['defaults']), 4);
}
}
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
22 changes: 22 additions & 0 deletions tests/Maker/MakeTwigComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@
}),
];

yield 'it_generates_twig_component_in_non_default_namespace' => [$this->createMakerTest()
->addExtraDependencies('symfony/ux-twig-component', 'symfony/twig-bundle')
->run(function (MakerTestRunner $runner) {
$output = $runner->runMaker(['Alert']);

$this->assertStringContainsString('src/Site/Twig/Components/Alert.php', $output);

Check failure on line 45 in tests/Maker/MakeTwigComponentTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @6.4.x-dev highest deps

Failed asserting that '\n

Check failure on line 45 in tests/Maker/MakeTwigComponentTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @7.0.x-dev highest deps

Failed asserting that '\n

Check failure on line 45 in tests/Maker/MakeTwigComponentTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 + @7.1.x-dev highest deps

Failed asserting that '\n

Check failure on line 45 in tests/Maker/MakeTwigComponentTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 + @6.4.* lowest deps

Failed asserting that '\n

Check failure on line 45 in tests/Maker/MakeTwigComponentTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 + @6.4.* highest deps

Failed asserting that '\n
$this->assertStringContainsString('templates/components/Alert.html.twig', $output);
$this->assertStringContainsString('To render the component, use <twig:Alert />.', $output);

$runner->copy(
'make-twig-component/tests/custom_twig_component.yaml',
shadowc marked this conversation as resolved.
Show resolved Hide resolved
'config/packages/twig_component.yaml'
);
$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