Skip to content

Commit 637a514

Browse files
committed
Tweak the bootstrapping code
update translations
1 parent 8b4b444 commit 637a514

12 files changed

Lines changed: 205 additions & 199 deletions

File tree

src/Debug/AbstractDebug.php

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Debug/Config.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ class Config
3535
/**
3636
* Constructor
3737
*
38-
* @param Debug $debug debug instance
38+
* @param Debug $debug debug instance
39+
* @param ConfigNormalizer|null $normalizer (optional) specify config normalizer
3940
*/
40-
public function __construct(Debug $debug)
41+
public function __construct(Debug $debug, $normalizer = null)
4142
{
4243
$this->debug = $debug;
43-
$this->normalizer = new ConfigNormalizer($debug);
44+
$this->normalizer = $normalizer ?: new ConfigNormalizer($debug);
4445
}
4546

4647
/**
@@ -146,7 +147,7 @@ private function doSet(array $configs, $options = 0)
146147
Now set the values
147148
*/
148149
// debug uses a Debug::EVENT_CONFIG subscriber to get updated config values
149-
unset($configs['debug']);
150+
unset($configs['debug']); // 'debug' was set via event above
150151
foreach ($configs as $debugProp => $cfg) {
151152
$this->setPropCfg($debugProp, $cfg);
152153
}
@@ -279,14 +280,11 @@ private function publishConfigEvent(array $configs)
279280
private function setPropCfg($debugProp, array $cfg)
280281
{
281282
$obj = null;
282-
$matches = array();
283283
if (\in_array($debugProp, $this->invokedServices, true)) {
284284
$obj = $this->debug->{$debugProp};
285-
} elseif (\preg_match('/^(dump|route)(.+)$/', $debugProp, $matches)) {
286-
$cat = $matches[1];
287-
$what = $matches[2];
288-
$func = 'get' . \ucfirst($cat);
289-
$obj = $this->debug->{$func}($what);
285+
} elseif (\preg_match('/^(dump|route)(.+)$/', $debugProp) && isset($this->debug->{$debugProp})) {
286+
// getDump and getRoute store the instance in debug's container
287+
$obj = $this->debug->{$debugProp};
290288
}
291289
if (\is_object($obj)) {
292290
$this->debug->{$debugProp}->setCfg($cfg);

src/Debug/ConfigNormalizer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ class ConfigNormalizer
9090
'errorStatsFile',
9191
],
9292
),
93-
'i18n' => array(),
9493
'routeHtml' => [
9594
'css',
9695
'drawer',

src/Debug/Plugin/ConfigEvents.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private function onCfgKey($val, $name, Event $event) // @phpcs:ignore Generic.Co
196196
}
197197

198198
/**
199-
* Convert logEnvInfo & logRequestInfo values to key=>bool arrays
199+
* Convert logEnvInfo & logRequestInfo values to key => bool arrays
200200
*
201201
* @param mixed $val value
202202
* @param string $name 'logEnvInfo'|'logRequestInfo'

src/Debug/ServiceProvider.php

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@
2424
*/
2525
class ServiceProvider implements ServiceProviderInterface
2626
{
27+
protected $utilities = [
28+
'arrayUtil',
29+
'configNormalizer',
30+
'errorLevel',
31+
'findExit',
32+
'html',
33+
'php',
34+
'phpDoc',
35+
'reflection',
36+
'sql',
37+
'stopWatch',
38+
'stringUtil',
39+
'utf8',
40+
'utility',
41+
];
42+
2743
/**
2844
* Register services and factories
2945
*
@@ -33,31 +49,24 @@ class ServiceProvider implements ServiceProviderInterface
3349
*/
3450
public function register(Container $container)
3551
{
36-
$this->registerCoreServices($container);
52+
$this->registerCore($container);
3753
$this->registerRoutes($container);
3854
$this->registerUtilities($container);
3955
$this->registerMisc($container);
4056

4157
/*
4258
These "services" are reused between channels
43-
each debug "rootInstance" gets at most one instance of the following
59+
these service definitions will be moved to a dedicated ServiceProvider that is shared between channels
4460
*/
45-
$container['services'] = [
46-
'arrayUtil',
61+
$container['services'] = \array_merge($this->utilities, [
4762
'backtrace',
4863
'data',
4964
'errorHandler',
50-
'errorLevel',
51-
'html',
5265
'i18n',
53-
'phpDoc',
5466
'pluginHighlight',
55-
'response',
67+
'response', // app may provide \Psr\Http\Message\ServerRequestInterface
5668
'serverRequest',
57-
'stringUtil',
58-
'utf8',
59-
'utility',
60-
];
69+
]);
6170

6271
// ensure that PHPDebugConsole receives ServerRequestExtended
6372
$container->extend('serverRequest', static function (ServerRequestInterface $serverRequest) {
@@ -66,13 +75,13 @@ public function register(Container $container)
6675
}
6776

6877
/**
69-
* Register "core" services
78+
* Register "core"
7079
*
7180
* @param Container $container Container instance
7281
*
7382
* @return void
7483
*/
75-
protected function registerCoreServices(Container $container) // phpcs:ignore SlevomatCodingStandard.Functions.FunctionLength
84+
protected function registerCore(Container $container) // phpcs:ignore SlevomatCodingStandard.Functions.FunctionLength
7685
{
7786
$container['abstracter'] = static function (Container $container) {
7887
$debug = $container['debug'];
@@ -87,12 +96,13 @@ protected function registerCoreServices(Container $container) // phpcs:ignore Sl
8796
return $backtrace;
8897
};
8998
$container['config'] = static function (Container $container) {
90-
$debug = $container['debug'];
91-
return new \bdk\Debug\Config($debug);
99+
return new \bdk\Debug\Config($container['debug'], $container['debug']->configNormalizer);
100+
};
101+
$container['configNormalizer'] = static function (Container $container) {
102+
return new \bdk\Debug\ConfigNormalizer($container['debug']);
92103
};
93104
$container['data'] = static function (Container $container) {
94-
$debug = $container['debug'];
95-
return new \bdk\Debug\Data($debug);
105+
return new \bdk\Debug\Data($container['debug']);
96106
};
97107
$container['errorHandler'] = static function (Container $container) {
98108
$debug = $container['debug'];
@@ -165,6 +175,7 @@ protected function registerMisc(Container $container)
165175
// \Psr\Http\Message\ServerRequestInterface
166176
// or
167177
// \bdk\HttpMessage\ServerRequestExtendedInterface
178+
// we'll ensure it becomes a ServerRequestExtended instance
168179
return \bdk\HttpMessage\Utility\ServerRequest::fromGlobals();
169180
};
170181
}
@@ -238,15 +249,15 @@ protected function registerUtilities(Container $container) // phpcs:ignore Slevo
238249
$container['sqlQueryAnalysis'] = static function (Container $container) {
239250
return new \bdk\Debug\Utility\SqlQueryAnalysis($container['debug']);
240251
};
241-
$container['stringUtil'] = static function () {
242-
return new \bdk\Debug\Utility\StringUtil();
243-
};
244252
$container['stopWatch'] = static function (Container $container) {
245253
$debug = $container['debug'];
246254
return new \bdk\Debug\Utility\StopWatch(array(
247255
'requestTime' => $debug->getServerParam('REQUEST_TIME_FLOAT'),
248256
));
249257
};
258+
$container['stringUtil'] = static function () {
259+
return new \bdk\Debug\Utility\StringUtil();
260+
};
250261
$container['utf8'] = static function () {
251262
return new \bdk\Debug\Utility\Utf8();
252263
};

tests/Debug/ConfigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testGetCfg()
7272
'abstracter',
7373
'debug',
7474
'errorHandler',
75-
'i18n',
75+
// 'i18n',
7676
'routeHtml',
7777
'routeStream',
7878
);
@@ -126,6 +126,7 @@ public function testGetCfg()
126126
'channelName',
127127
'channelShow',
128128
'channelSort',
129+
'container',
129130
'enableProfiling',
130131
'errorLogNormal',
131132
'errorMask',
@@ -159,7 +160,6 @@ public function testGetCfg()
159160
'sessionName',
160161
'wampPublisher',
161162
'slowQueryDurationMs',
162-
'container',
163163
);
164164
\sort($debugKeys);
165165

0 commit comments

Comments
 (0)