@@ -33,9 +33,6 @@ abstract class AbstractDebug
3333 /** @var bool */
3434 protected $ bootstrapped = false ;
3535
36- /** @var \bdk\Debug\Config */
37- protected $ config ;
38-
3936 /** @var Container */
4037 protected $ container ;
4138
@@ -109,7 +106,7 @@ public static function __callStatic($methodName, array $args)
109106 /*
110107 Treat as a special case
111108 Want to initialize with the passed config vs initialize, then setCfg
112- ie _setCfg (array('route'=>'html')) via command line
109+ ie Debug::setCfg (array('route'=>'html')) via command line
113110 we don't want to first initialize with default STDERR output
114111 */
115112 $ cfg = \is_array ($ args [0 ])
@@ -195,7 +192,7 @@ public function onConfig(Event $event)
195192 }
196193
197194 /**
198- * Update dependencies
195+ * Update container values
199196 *
200197 * @param ServiceProviderInterface|callable|array $val dependency definitions
201198 *
@@ -206,9 +203,9 @@ public function onCfgServiceProvider($val)
206203 $ rawValues = ContainerUtility::toRawValues ($ val );
207204 $ services = $ this ->container ['services ' ];
208205 foreach ($ rawValues as $ k => $ v ) {
206+ unset($ rawValues [$ k ]);
209207 if (\in_array ($ k , $ services , true )) {
210208 $ this ->serviceContainer [$ k ] = $ v ;
211- unset($ rawValues [$ k ]);
212209 continue ;
213210 }
214211 $ this ->container [$ k ] = $ v ;
@@ -253,22 +250,28 @@ public function publishBubbleEvent($eventName, Event $event, $debug = null)
253250 */
254251 private function bootstrap ($ cfg )
255252 {
256- $ cfgBootstrap = $ this ->bootstrapConfig ($ cfg );
257- $ this ->bootstrapSetInstances ($ cfgBootstrap );
258- $ this ->bootstrapContainer ($ cfgBootstrap );
253+ $ cfgBootstrap = $ this ->bootstrapConfigValues ($ cfg );
254+ $ this ->bootstrapInstances ($ cfgBootstrap ['parent ' ]);
255+ $ this ->bootstrapContainers ($ cfgBootstrap ['container ' ]);
256+ $ this ->onCfgServiceProvider ($ cfgBootstrap ['serviceProvider ' ]);
259257
260- $ this ->config = $ this ->container ['config ' ];
261- $ this ->container ->setCfg ('onInvoke ' , [$ this ->config , 'onContainerInvoke ' ]);
262- $ this ->serviceContainer ->setCfg ('onInvoke ' , [$ this ->config , 'onContainerInvoke ' ]);
263258 $ this ->eventManager ->addSubscriberInterface ($ this ->container ['pluginManager ' ]);
264259
260+ $ cfg = $ this ->configNormalizer ->normalizeArray ($ cfg );
261+ unset($ cfg ['debug ' ]['serviceProvider ' ], $ cfg ['debug ' ]['parent ' ]);
262+
265263 if (!$ this ->parentInstance ) {
266264 // we're the root instance
267- $ this ->cfg ['i18n ' ] = \array_merge ($ this ->cfg ['i18n ' ], $ cfgBootstrap ['i18n ' ]);
268- $ this ->serviceContainer ['errorHandler ' ];
269- $ this ->addPlugins ($ cfgBootstrap ['plugins ' ]);
265+ $ cfgBackup = $ this ->cfg ;
266+ $ cfg = \array_merge (array ('debug ' => array ()), $ cfg );
267+ $ this ->cfg = $ this ->arrayUtil ->mergeDeep ($ this ->cfg , $ cfg ['debug ' ]);
268+
269+ $ this ->serviceContainer ['errorHandler ' ]; // instantiate errorHandler
270+ $ this ->addPlugins ($ this ->cfg ['plugins ' ]);
270271 $ this ->data ->set ('requestId ' , $ this ->requestId ());
271272 $ this ->data ->set ('entryCountInitial ' , $ this ->data ->get ('log/__count__ ' ));
273+
274+ $ this ->cfg = $ cfgBackup ;
272275 }
273276
274277 $ this ->eventManager ->subscribe (Debug::EVENT_CONFIG , [$ this , 'onConfig ' ]);
@@ -284,13 +287,11 @@ private function bootstrap($cfg)
284287 *
285288 * @return array
286289 */
287- private function bootstrapConfig (& $ cfg )
290+ private function bootstrapConfigValues ( $ cfg )
288291 {
289292 $ cfgDefault = array (
290293 'container ' => array (),
291- 'i18n ' => array (),
292294 'parent ' => null ,
293- 'plugins ' => $ this ->cfg ['plugins ' ],
294295 'serviceProvider ' => $ this ->cfg ['serviceProvider ' ],
295296 );
296297
@@ -303,46 +304,43 @@ private function bootstrapConfig(&$cfg)
303304 }
304305 }
305306
306- unset(
307- $ cfg ['debug ' ]['parent ' ],
308- $ cfg ['debug ' ]['serviceProvider ' ],
309- $ cfg ['serviceProvider ' ]
310- );
311-
312307 return \array_replace_recursive ($ cfgDefault , $ cfgValues );
313308 }
314309
315310 /**
316311 * Initialize dependency containers
317312 *
318- * @param array $cfg Initial cfg values
313+ * @param array $cfg Container configuration values
319314 *
320315 * @return void
321316 */
322- private function bootstrapContainer ( $ cfg )
317+ private function bootstrapContainers ( array $ cfg )
323318 {
324319 $ this ->container = new Container (
325320 array (
326321 'debug ' => $ this ,
327322 ),
328- $ cfg[ ' container ' ]
323+ $ cfg
329324 );
330325 $ this ->container ->registerProvider (new ServiceProvider ());
331326 if (empty ($ this ->parentInstance )) {
332327 // root instance
328+ // create a new serviceContainer for our shared "services"
329+ // move the services from the container to the serviceContainer (without instantiating them)
333330 $ this ->serviceContainer = new Container (
334331 array (
335332 'debug ' => $ this ,
336333 ),
337- $ cfg[ ' container ' ]
334+ $ cfg
338335 );
339336 foreach ($ this ->container ['services ' ] as $ service ) {
340337 $ this ->serviceContainer [$ service ] = $ this ->container ->raw ($ service );
341338 unset($ this ->container [$ service ]);
342339 }
340+ $ this ->serviceContainer ->setCfg ('onInvoke ' , [$ this ->config , 'onContainerInvoke ' ]);
343341 }
344342 $ this ->serviceContainer = $ this ->rootInstance ->serviceContainer ;
345- $ this ->cfg [ ' serviceProvider ' ] = $ this ->onCfgServiceProvider ( $ cfg [ ' serviceProvider ' ]);
343+ $ this ->container -> setCfg ( ' onInvoke ' , [ $ this ->config , ' onContainerInvoke ' ]);
346344 }
347345
348346 /**
@@ -352,11 +350,11 @@ private function bootstrapContainer($cfg)
352350 *
353351 * @return void
354352 */
355- private function bootstrapSetInstances ( $ cfg )
353+ private function bootstrapInstances ( $ parentInstance = null )
356354 {
357355 $ this ->rootInstance = $ this ;
358- if (isset ( $ cfg [ ' parent ' ]) ) {
359- $ this ->parentInstance = $ cfg [ ' parent ' ] ;
356+ if ($ parentInstance ) {
357+ $ this ->parentInstance = $ parentInstance ;
360358 $ this ->rootInstance = $ this ->parentInstance ->rootInstance ;
361359 }
362360 }
0 commit comments