2525use Symfony \Component \DependencyInjection \Extension \ConfigurationExtensionInterface ;
2626use Symfony \Component \DependencyInjection \Extension \ExtensionInterface ;
2727use Symfony \Component \DependencyInjection \Loader \Configurator \ContainerConfigurator ;
28+ use Symfony \Config \ImportsConfig ;
29+ use Symfony \Config \ParametersConfig ;
2830use Symfony \Config \ServicesConfig ;
2931
3032/**
@@ -97,7 +99,7 @@ public function load(mixed $resource, ?string $type = null): mixed
9799 if (\is_object ($ result ) && \is_callable ($ result )) {
98100 $ result = $ this ->callConfigurator ($ result , new ContainerConfigurator ($ this ->container , $ this , $ this ->instanceof , $ path , $ resource , $ this ->env ), $ path );
99101 }
100- if ($ result instanceof ConfigBuilderInterface || $ result instanceof ServicesConfig ) {
102+ if ($ result instanceof ServicesConfig || $ result instanceof ParametersConfig || $ result instanceof ImportsConfig || $ result instanceof ConfigBuilderInterface ) {
101103 $ result = [$ result ];
102104 } elseif (!is_iterable ($ result ?? [])) {
103105 throw new InvalidArgumentException (\sprintf ('The return value in config file "%s" is invalid: "%s" given. ' , $ path , get_debug_type ($ result )));
@@ -108,25 +110,33 @@ public function load(mixed $resource, ?string $type = null): mixed
108110 $ config = [$ key => $ config ];
109111 } elseif (!$ this ->env || 'when@ ' .$ this ->env !== $ key ) {
110112 continue ;
111- } elseif ($ config instanceof ServicesConfig || $ config instanceof ConfigBuilderInterface) {
113+ } elseif ($ config instanceof ServicesConfig || $ config instanceof ParametersConfig || $ config instanceof ImportsConfig || $ config instanceof ConfigBuilderInterface) {
112114 $ config = [$ config ];
113115 } elseif (!is_iterable ($ config )) {
114116 throw new InvalidArgumentException (\sprintf ('The "%s" key should contain an array in "%s". ' , $ key , $ path ));
115117 }
116118
117119 foreach ($ config as $ key => $ config ) {
118- if ($ config instanceof ServicesConfig || \in_array ($ key , ['imports ' , 'parameters ' , 'services ' ], true )) {
119- if (!$ config instanceof ServicesConfig) {
120+ $ expectedKey = match (true ) {
121+ $ config instanceof ServicesConfig => 'services ' ,
122+ $ config instanceof ParametersConfig => 'parameters ' ,
123+ $ config instanceof ImportsConfig => 'imports ' ,
124+ default => null ,
125+ };
126+ if (null !== $ expectedKey || \in_array ($ key , ['services ' , 'parameters ' , 'imports ' ], true )) {
127+ if (null === $ expectedKey ) {
120128 $ config = [$ key => $ config ];
121- } elseif (\is_string ($ key ) && 'services ' !== $ key ) {
122- throw new InvalidArgumentException (\sprintf ('Invalid key "%s" returned for the "%s" config builder; none or "services" expected in file "%s". ' , $ key , get_debug_type ($ config ), $ path ));
129+ } elseif (\is_string ($ key ) && $ key !== $ expectedKey ) {
130+ throw new InvalidArgumentException (\sprintf ('Invalid key "%s" returned for the "%s" config builder; none or "%s" expected in file "%s". ' , $ key , get_debug_type ($ config ), $ expectedKey , $ path ));
131+ } else {
132+ $ config = [$ expectedKey => $ config ->config ];
123133 }
124134 $ yamlLoader = new YamlFileLoader ($ this ->container , $ this ->locator , $ this ->env , $ this ->prepend );
125135 $ yamlLoader ->setResolver (new LoaderResolver ([$ this ]));
126136 $ loadContent = new \ReflectionMethod (YamlFileLoader::class, 'loadContent ' );
127137 ++$ this ->importing ;
128138 try {
129- $ loadContent ->invoke ($ yamlLoader , ContainerConfigurator::processValue (( array ) $ config ), $ path );
139+ $ loadContent ->invoke ($ yamlLoader , ContainerConfigurator::processValue ($ config ), $ path );
130140 } finally {
131141 --$ this ->importing ;
132142 }
@@ -288,7 +298,7 @@ private function configBuilder(string $namespace): ConfigBuilderInterface
288298 throw new InvalidArgumentException (\sprintf ('Could not find or generate class "%s". ' , $ namespace ));
289299 }
290300
291- if (is_a ($ namespace , ServicesConfig::class, true )) {
301+ if (is_a ($ namespace , ServicesConfig::class, true ) || is_a ( $ namespace , ParametersConfig::class, true ) || is_a ( $ namespace , ImportsConfig::class, true ) ) {
292302 throw new \LogicException (\sprintf ('You cannot use "%s" as a config builder; create an instance and return it instead. ' , $ namespace ));
293303 }
294304
0 commit comments