Skip to content

Commit eb3ccf1

Browse files
committed
setCfg() not returning previous value
new utility method: arrayPathGet
1 parent 6ee0b7b commit eb3ccf1

6 files changed

Lines changed: 106 additions & 50 deletions

File tree

src/Debug/Config.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,14 @@ public function getCfg($path = '')
5656
$level1 = isset($path[0]) ? $path[0] : null;
5757
if ($level1 == 'debug') {
5858
\array_shift($path);
59+
return $this->debug->utilities->arrayPathGet($path, $this->cfg);
5960
} elseif (\is_object($this->debug->{$level1})) {
6061
// child class config value
6162
$pathRel = \count($path) > 1
6263
? \implode('/', \array_slice($path, 1))
6364
: null;
6465
return $this->debug->{$level1}->getCfg($pathRel);
6566
}
66-
$ret = $this->cfg;
67-
foreach ($path as $k) {
68-
if (isset($ret[$k])) {
69-
$ret = $ret[$k];
70-
} else {
71-
$ret = null;
72-
break;
73-
}
74-
}
75-
return $ret;
7667
}
7768

7869
/**
@@ -139,7 +130,7 @@ public function setCfg($pathOrVals, $val = null)
139130
}
140131
}
141132
if (\is_string($pathOrVals)) {
142-
$return = $this->getCfg($path);
133+
$return = $this->debug->utilities->arrayPathGet($path, $return);
143134
}
144135
if ($cfg) {
145136
$this->debug->eventManager->publish(

src/Debug/Output.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Brad Kent <bkfake-github@yahoo.com>
77
* @license http://opensource.org/licenses/MIT MIT
88
* @copyright 2014-2018 Brad Kent
9-
* @version v2.1.0
9+
* @version v2.1.1
1010
*/
1111

1212
namespace bdk\Debug;
@@ -93,16 +93,7 @@ public function getCfg($path = null)
9393
} elseif ($path == 'css') {
9494
$ret = $this->getCss();
9595
} else {
96-
$path = \array_filter(\preg_split('#[\./]#', $path), 'strlen');
97-
$ret = $this->cfg;
98-
foreach ($path as $k) {
99-
if (isset($ret[$k])) {
100-
$ret = $ret[$k];
101-
} else {
102-
$ret = null;
103-
break;
104-
}
105-
}
96+
$ret = $this->debug->utilities->arrayPathGet($path, $this->cfg);
10697
}
10798
return $ret;
10899
}

src/Debug/Utilities.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ public static function arrayMergeDeep($arrayDef, $array2)
5050
return $arrayDef;
5151
}
5252

53+
/**
54+
* Get value from array structure
55+
*
56+
* @param array|string $path key path
57+
* @param array $array array to navigate
58+
*
59+
* @return mixed
60+
*/
61+
public static function arrayPathGet($path, $array = array())
62+
{
63+
if (\is_string($path) || \is_null($path)) {
64+
$path = \preg_split('#[\./]#', $path);
65+
$path = \array_filter($path, 'strlen');
66+
}
67+
foreach ($path as $k) {
68+
if (isset($array[$k])) {
69+
$array = $array[$k];
70+
} else {
71+
return null;
72+
}
73+
}
74+
return $array;
75+
}
76+
5377
/**
5478
* Build attribute string
5579
*

tests/ConfigTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Run with --process-isolation option
4+
*/
5+
6+
/**
7+
* PHPUnit tests for Debug class
8+
*/
9+
class ConfigTest extends DebugTestFramework
10+
{
11+
12+
/**
13+
* Test
14+
*
15+
* @return void
16+
*/
17+
public function testGetCfg()
18+
{
19+
$abstracterKeys = array('collectConstants', 'collectMethods', 'objectsExclude', 'objectSort', 'useDebugInfo');
20+
$debugKeys = array('collect', 'file', 'key', 'output', 'errorMask', 'emailFunc', 'emailLog', 'emailTo', 'logEnvInfo', 'logServerKeys', 'onLog',);
21+
22+
$this->assertSame(true, $this->debug->getCfg('collect'));
23+
$this->assertSame(true, $this->debug->getCfg('debug.collect'));
24+
25+
$this->assertSame('visibility', $this->debug->getCfg('objectSort'));
26+
$this->assertSame('visibility', $this->debug->getCfg('abstracter.objectSort'));
27+
$this->assertSame('visibility', $this->debug->getCfg('abstracter/objectSort'));
28+
29+
$this->assertSame($abstracterKeys, array_keys($this->debug->getCfg('abstracter')));
30+
$this->assertSame($abstracterKeys, array_keys($this->debug->getCfg('abstracter/*')));
31+
$this->assertSame($debugKeys, array_keys($this->debug->getCfg()));
32+
$this->assertSame($debugKeys, array_keys($this->debug->getCfg('debug')));
33+
$this->assertSame($debugKeys, array_keys($this->debug->getCfg('debug/*')));
34+
}
35+
36+
/**
37+
* Test
38+
*
39+
* @return void
40+
*/
41+
public function testSetCfg()
42+
{
43+
$this->assertSame(null, $this->debug->setCfg('foo', 'bar'));
44+
$this->assertSame('bar', $this->debug->setCfg('foo', 'baz'));
45+
$this->assertSame('baz', $this->debug->getCfg('foo'));
46+
}
47+
}

tests/DebugTest.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,9 @@ public function testShutDownSubscribers()
118118
$this->assertSame('onShutdown', $subscribers[1][1]);
119119
}
120120

121-
/**
122-
* Test
123-
*
124-
* @return void
125-
*/
126-
public function testGetCfg()
127-
{
128-
$this->assertSame('visibility', $this->debug->getCfg('objectSort'));
129-
$this->assertSame('visibility', $this->debug->getCfg('abstracter.objectSort'));
130-
$this->assertSame('visibility', $this->debug->getCfg('abstracter/objectSort'));
131-
132-
$abstracterKeys = array('collectConstants', 'collectMethods', 'objectsExclude', 'objectSort', 'useDebugInfo');
133-
$debugKeys = array('collect', 'file', 'key', 'output', 'errorMask', 'emailFunc', 'emailLog', 'emailTo', 'logEnvInfo', 'logServerKeys', 'onLog',);
134-
135-
$this->assertSame($abstracterKeys, array_keys($this->debug->getCfg('abstracter')));
136-
$this->assertSame($abstracterKeys, array_keys($this->debug->getCfg('abstracter/*')));
137-
$this->assertSame($debugKeys, array_keys($this->debug->getCfg()));
138-
$this->assertSame($debugKeys, array_keys($this->debug->getCfg('debug')));
139-
$this->assertSame($debugKeys, array_keys($this->debug->getCfg('debug/*')));
140-
}
121+
/*
122+
getCfg tested in ConfigTest
123+
*/
141124

142125
public function testMeta()
143126
{
@@ -186,14 +169,9 @@ public function testOutput()
186169
{
187170
}
188171

189-
/**
190-
* Test
191-
*
192-
* @return void
193-
*/
194-
public function testSetCfg()
195-
{
196-
}
172+
/*
173+
setCfg tested in ConfigTest
174+
*/
197175

198176
/**
199177
* Test

tests/UtilitiesTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ public function testArrayMergeDeep()
5050
$this->assertSame($arrayExpect, $array3);
5151
}
5252

53+
/**
54+
* Test
55+
*
56+
* @return void
57+
*/
58+
public function testArrayPathGet()
59+
{
60+
$array = array(
61+
'surfaces' => array(
62+
'bed' => array(
63+
'comfy' => true,
64+
),
65+
'rock' => array(
66+
'comfy' => false,
67+
)
68+
),
69+
);
70+
$this->assertSame(\bdk\Debug\Utilities::arrayPathGet('surfaces.bed.comfy', $array), true);
71+
$this->assertSame(\bdk\Debug\Utilities::arrayPathGet('surfaces.rock.comfy', $array), false);
72+
$this->assertSame(\bdk\Debug\Utilities::arrayPathGet('surfaces.bed.comfy.foo', $array), null);
73+
$this->assertSame(\bdk\Debug\Utilities::arrayPathGet('surfaces.bed.comfy.0', $array), null);
74+
$this->assertSame(\bdk\Debug\Utilities::arrayPathGet('surfaces.bed', $array), array('comfy'=>true));
75+
$this->assertSame(\bdk\Debug\Utilities::arrayPathGet('surfaces.bed.foo', $array), null);
76+
}
77+
5378
/**
5479
* Test
5580
*

0 commit comments

Comments
 (0)