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 \ConfigurationInterface ;
20+ use Symfony \Component \Config \Definition \Processor ;
2021use Symfony \Component \Console \Command \Command ;
2122use Symfony \Component \Console \Input \InputArgument ;
2223use Symfony \Component \Console \Input \InputInterface ;
2324use Symfony \Component \Console \Input \InputOption ;
24- use Symfony \Component \Yaml \Yaml ;
25+ use Symfony \Component \DependencyInjection \Compiler \ValidateEnvPlaceholdersPass ;
26+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
2527use Symfony \UX \LiveComponent \Attribute \AsLiveComponent ;
2628use 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