Skip to content

Commit 414f39b

Browse files
committed
read processed twig_component config instead parse the yaml file
1 parent ce60831 commit 414f39b

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

config/makers.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
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" />
2625
</service>
2726

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

src/Maker/MakeTwigComponent.php

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@
1111

1212
namespace Symfony\Bundle\MakerBundle\Maker;
1313

14+
use Symfony\Bundle\FrameworkBundle\Console\Application;
1415
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1516
use Symfony\Bundle\MakerBundle\DependencyBuilder;
16-
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
17-
use Symfony\Bundle\MakerBundle\FileManager;
1817
use Symfony\Bundle\MakerBundle\Generator;
1918
use Symfony\Bundle\MakerBundle\InputConfiguration;
19+
use Symfony\Component\Config\Definition\Processor;
2020
use Symfony\Component\Console\Command\Command;
2121
use Symfony\Component\Console\Input\InputArgument;
2222
use Symfony\Component\Console\Input\InputInterface;
2323
use Symfony\Component\Console\Input\InputOption;
24-
use Symfony\Component\Yaml\Yaml;
24+
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
25+
use Symfony\Component\DependencyInjection\ContainerBuilder;
2526
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
2627
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
28+
use Symfony\UX\TwigComponent\DependencyInjection\TwigComponentExtension;
2729

2830
/**
2931
* @author Kevin Bond <[email protected]>
@@ -32,10 +34,6 @@ final class MakeTwigComponent extends AbstractMaker
3234
{
3335
private string $namespace = 'Twig\\Components';
3436

35-
public function __construct(private FileManager $fileManager)
36-
{
37-
}
38-
3937
public static function getCommandName(): string
4038
{
4139
return 'make:twig-component';
@@ -103,17 +101,63 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
103101
$input->setOption('live', $io->confirm('Make this a live component?', false));
104102
}
105103

106-
$path = 'config/packages/twig_component.yaml';
104+
$application = $command->getApplication();
105+
assert($application instanceof Application);
107106

108-
if (!$this->fileManager->fileExists($path)) {
109-
throw new RuntimeCommandException(message: 'Unable to find twig_component.yaml');
107+
$container = $this->compileContainer($application);
108+
$config = $this->getConfig($container);
109+
110+
if (isset($config['defaults'])) {
111+
$namespace = array_key_first($config['defaults']);
112+
$this->namespace = substr($namespace, \strpos($namespace, '\\') + 1);
110113
}
114+
}
115+
116+
private function compileContainer(Application $application): ContainerBuilder
117+
{
118+
// logic from \Symfony\Bundle\FrameworkBundle\Command\ConfigDebugCommand
119+
$kernel = clone $application->getKernel();
120+
$kernel->boot();
121+
122+
$method = new \ReflectionMethod($kernel, 'buildContainer');
123+
$container = $method->invoke($kernel);
124+
$container->getCompiler()->compile($container);
125+
126+
return $container;
127+
}
111128

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_component.yaml', previous: $throwable);
129+
private function getConfig(ContainerBuilder $container): mixed
130+
{
131+
return $container->resolveEnvPlaceholders(
132+
$container->getParameterBag()->resolveValue(
133+
$this->getConfigForExtension($container)
134+
), true
135+
);
136+
}
137+
138+
/**
139+
* @return array<string, mixed>
140+
*/
141+
private function getConfigForExtension(ContainerBuilder $container): array
142+
{
143+
$extensionAlias = 'twig_component';
144+
145+
$extensionConfig = [];
146+
foreach ($container->getCompilerPassConfig()->getPasses() as $pass) {
147+
if ($pass instanceof ValidateEnvPlaceholdersPass) {
148+
$extensionConfig = $pass->getExtensionConfig();
149+
break;
150+
}
117151
}
152+
153+
if (isset($extensionConfig[$extensionAlias])) {
154+
return $extensionConfig[$extensionAlias];
155+
}
156+
157+
// Fall back to default config if the extension has one
158+
$extension = new TwigComponentExtension();
159+
$configs = $container->getExtensionConfig($extensionAlias);
160+
161+
return (new Processor())->processConfiguration($extension, $configs);
118162
}
119163
}

0 commit comments

Comments
 (0)