@@ -57,17 +57,42 @@ public function load(mixed $resource, ?string $type = null): mixed
5757 $ this ->setCurrentDir (\dirname ($ path ));
5858 $ this ->container ->fileExists ($ path );
5959
60+ // Force load ContainerConfigurator to make env(), param() etc available.
61+ class_exists (ContainerConfigurator::class);
62+
6063 // the closure forbids access to the private scope in the included file
6164 $ load = \Closure::bind (function ($ path , $ env ) use ($ container , $ loader , $ resource , $ type ) {
6265 return include $ path ;
6366 }, $ this , ProtectedPhpFileLoader::class);
6467
6568 try {
66- $ callback = $ load ($ path , $ this ->env );
69+ if (1 === $ result = $ load ($ path , $ this ->env )) {
70+ $ result = null ;
71+ }
6772
68- if (\is_object ($ callback ) && \is_callable ($ callback )) {
69- $ this ->executeCallback ($ callback , new ContainerConfigurator ($ this ->container , $ this , $ this ->instanceof , $ path , $ resource , $ this ->env ), $ path );
73+ if (\is_object ($ result ) && \is_callable ($ result )) {
74+ $ result = $ this ->executeCallback ($ result , new ContainerConfigurator ($ this ->container , $ this , $ this ->instanceof , $ path , $ resource , $ this ->env ), $ path );
7075 }
76+ if ($ result instanceof ConfigBuilderInterface) {
77+ $ this ->loadExtensionConfig ($ result ->getExtensionAlias (), ContainerConfigurator::processValue ($ result ->toArray ()));
78+ } elseif (is_iterable ($ result )) {
79+ foreach ($ result as $ key => $ config ) {
80+ if ($ config instanceof ConfigBuilderInterface) {
81+ if (\is_string ($ key ) && $ config ->getExtensionAlias () !== $ key ) {
82+ throw new InvalidArgumentException (\sprintf ('The extension alias "%s" of the "%s" config builder does not match the key "%s" in file "%s". ' , $ config ->getExtensionAlias (), get_debug_type ($ config ), $ key , $ path ));
83+ }
84+ $ this ->loadExtensionConfig ($ config ->getExtensionAlias (), ContainerConfigurator::processValue ($ config ->toArray ()));
85+ } elseif (!\is_string ($ key ) || !\is_array ($ config )) {
86+ throw new InvalidArgumentException (\sprintf ('The configuration returned in file "%s" must yield only string-keyed arrays or ConfigBuilderInterface values. ' , $ path ));
87+ } else {
88+ $ this ->loadExtensionConfig ($ key , ContainerConfigurator::processValue ($ config ));
89+ }
90+ }
91+ } elseif (null !== $ result ) {
92+ throw new InvalidArgumentException (\sprintf ('The return value in config file "%s" is invalid. ' , $ path ));
93+ }
94+
95+ $ this ->loadExtensionConfigs ();
7196 } finally {
7297 $ this ->instanceof = [];
7398 $ this ->registerAliasesForSinglyImplementedInterfaces ();
@@ -92,7 +117,7 @@ public function supports(mixed $resource, ?string $type = null): bool
92117 /**
93118 * Resolve the parameters to the $callback and execute it.
94119 */
95- private function executeCallback (callable $ callback , ContainerConfigurator $ containerConfigurator , string $ path ): void
120+ private function executeCallback (callable $ callback , ContainerConfigurator $ containerConfigurator , string $ path ): mixed
96121 {
97122 $ callback = $ callback (...);
98123 $ arguments = [];
@@ -125,7 +150,7 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont
125150 }
126151
127152 if ($ excluded ) {
128- return ;
153+ return null ;
129154 }
130155
131156 foreach ($ r ->getParameters () as $ parameter ) {
@@ -163,21 +188,19 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont
163188 }
164189 }
165190
166- // Force load ContainerConfigurator to make env(), param() etc available.
167- class_exists (ContainerConfigurator::class);
168-
169191 ++$ this ->importing ;
170192 try {
171- $ callback (...$ arguments );
193+ return $ callback (...$ arguments );
194+ } catch (\Throwable $ e ) {
195+ $ configBuilders = [];
196+ throw $ e ;
172197 } finally {
173198 --$ this ->importing ;
174- }
175199
176- foreach ($ configBuilders as $ configBuilder ) {
177- $ this ->loadExtensionConfig ($ configBuilder ->getExtensionAlias (), ContainerConfigurator::processValue ($ configBuilder ->toArray ()));
200+ foreach ($ configBuilders as $ configBuilder ) {
201+ $ this ->loadExtensionConfig ($ configBuilder ->getExtensionAlias (), ContainerConfigurator::processValue ($ configBuilder ->toArray ()));
202+ }
178203 }
179-
180- $ this ->loadExtensionConfigs ();
181204 }
182205
183206 /**
0 commit comments