1111
1212namespace Symfony \Bundle \MakerBundle \Maker ;
1313
14+ use Symfony \Bundle \FrameworkBundle \Console \Application ;
1415use Symfony \Bundle \MakerBundle \ConsoleStyle ;
1516use Symfony \Bundle \MakerBundle \DependencyBuilder ;
16- use Symfony \Bundle \MakerBundle \Exception \RuntimeCommandException ;
17- use Symfony \Bundle \MakerBundle \FileManager ;
1817use Symfony \Bundle \MakerBundle \Generator ;
1918use Symfony \Bundle \MakerBundle \InputConfiguration ;
19+ use Symfony \Component \Config \Definition \Processor ;
2020use Symfony \Component \Console \Command \Command ;
2121use Symfony \Component \Console \Input \InputArgument ;
2222use Symfony \Component \Console \Input \InputInterface ;
2323use Symfony \Component \Console \Input \InputOption ;
24- use Symfony \Component \Yaml \Yaml ;
24+ use Symfony \Component \DependencyInjection \Compiler \ValidateEnvPlaceholdersPass ;
25+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
2526use Symfony \UX \LiveComponent \Attribute \AsLiveComponent ;
2627use 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