Skip to content

Commit 4625649

Browse files
committed
Add load method to config
1 parent 78909b7 commit 4625649

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/Config.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Xhgui\Profiler;
44

55
use ArrayAccess;
6+
use Xhgui\Profiler\Exception\ProfilerException;
67

78
class Config implements ArrayAccess
89
{
@@ -25,6 +26,18 @@ public function toArray()
2526
return $this->config;
2627
}
2728

29+
/**
30+
* Load a config file, merge with the currently loaded configuration.
31+
*/
32+
public function load($filename)
33+
{
34+
if (!file_exists($filename)) {
35+
throw new ProfilerException("File does not exist: $filename");
36+
}
37+
$config = require $filename;
38+
$this->merge($config);
39+
}
40+
2841
private function merge(array $config)
2942
{
3043
$this->config = array_replace($this->config, $config);

tests/ConfigTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ public function testDefaults()
1212
$config = new Config();
1313
$this->assertEquals(Profiler::SAVER_STACK, $config['save.handler']);
1414
}
15+
16+
public function testLoadConfig()
17+
{
18+
$config = new Config();
19+
$config->load(__DIR__ . '/Resources/config_saver.php');
20+
$this->assertEquals(Profiler::SAVER_UPLOAD, $config['save.handler']);
21+
}
1522
}

tests/Resources/config_saver.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Xhgui\Profiler\Profiler;
4+
5+
return array(
6+
'save.handler' => Profiler::SAVER_UPLOAD,
7+
);

0 commit comments

Comments
 (0)