Skip to content

Commit b07a50d

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

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-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: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@
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\ConfigurationInterface;
20+
use Symfony\Component\Config\Definition\Processor;
2021
use Symfony\Component\Console\Command\Command;
2122
use Symfony\Component\Console\Input\InputArgument;
2223
use Symfony\Component\Console\Input\InputInterface;
2324
use Symfony\Component\Console\Input\InputOption;
24-
use Symfony\Component\Yaml\Yaml;
25+
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
26+
use Symfony\Component\DependencyInjection\ContainerBuilder;
2527
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
2628
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
29+
use Symfony\UX\TwigComponent\DependencyInjection\TwigComponentExtension;
2730

2831
/**
2932
* @author Kevin Bond <[email protected]>
@@ -32,10 +35,6 @@ final class MakeTwigComponent extends AbstractMaker
3235
{
3336
private string $namespace = 'Twig\\Components';
3437

35-
public function __construct(private FileManager $fileManager)
36-
{
37-
}
38-
3938
public static function getCommandName(): string
4039
{
4140
return 'make:twig-component';
@@ -103,17 +102,58 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
103102
$input->setOption('live', $io->confirm('Make this a live component?', false));
104103
}
105104

106-
$path = 'config/packages/twig_component.yaml';
105+
$container = $this->compileContainer($command->getApplication());
107106

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

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

0 commit comments

Comments
 (0)