From b07a50d6cde27400d77f4f062c4b30d1efe23d59 Mon Sep 17 00:00:00 2001 From: Indra Gunawan Date: Thu, 5 Dec 2024 19:36:17 +0800 Subject: [PATCH] read processed twig_component config instead parse the yaml file --- config/makers.xml | 1 - src/Maker/MakeTwigComponent.php | 70 ++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/config/makers.xml b/config/makers.xml index ad7d45483..3d913d1ac 100644 --- a/config/makers.xml +++ b/config/makers.xml @@ -22,7 +22,6 @@ - diff --git a/src/Maker/MakeTwigComponent.php b/src/Maker/MakeTwigComponent.php index 4bf55ee8d..98437c9f2 100644 --- a/src/Maker/MakeTwigComponent.php +++ b/src/Maker/MakeTwigComponent.php @@ -11,19 +11,22 @@ namespace Symfony\Bundle\MakerBundle\Maker; +use Symfony\Bundle\FrameworkBundle\Console\Application; 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\Config\Definition\ConfigurationInterface; +use Symfony\Component\Config\Definition\Processor; 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\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; +use Symfony\UX\TwigComponent\DependencyInjection\TwigComponentExtension; /** * @author Kevin Bond @@ -32,10 +35,6 @@ 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'; @@ -103,17 +102,58 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma $input->setOption('live', $io->confirm('Make this a live component?', false)); } - $path = 'config/packages/twig_component.yaml'; + $container = $this->compileContainer($command->getApplication()); - if (!$this->fileManager->fileExists($path)) { - throw new RuntimeCommandException(message: 'Unable to find twig_component.yaml'); + $config = $this->getConfig($container); + + if (isset($config['defaults'])) { + $namespace = array_key_first($config['defaults']); + $this->namespace = substr($namespace, \strpos($namespace, '\\') + 1); } + } + + private function compileContainer(Application $application): ContainerBuilder + { + // logic from \Symfony\Bundle\FrameworkBundle\Command\ConfigDebugCommand + $kernel = clone $application->getKernel(); + $kernel->boot(); + + $method = new \ReflectionMethod($kernel, 'buildContainer'); + $container = $method->invoke($kernel); + $container->getCompiler()->compile($container); + + return $container; + } - 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_component.yaml', previous: $throwable); + private function getConfig(ContainerBuilder $container): mixed + { + return $container->resolveEnvPlaceholders( + $container->getParameterBag()->resolveValue( + $this->getConfigForExtension($container) + ), true + ); + } + + private function getConfigForExtension(ContainerBuilder $container): array + { + $extensionAlias = 'twig_component'; + + $extensionConfig = []; + foreach ($container->getCompilerPassConfig()->getPasses() as $pass) { + if ($pass instanceof ValidateEnvPlaceholdersPass) { + $extensionConfig = $pass->getExtensionConfig(); + break; + } } + + if (isset($extensionConfig[$extensionAlias])) { + return $extensionConfig[$extensionAlias]; + } + + // Fall back to default config if the extension has one + $extension = new TwigComponentExtension(); + $configs = $container->getExtensionConfig($extensionAlias); + + return (new Processor())->processConfiguration($extension, $configs); } }