Skip to content

Commit 51e65ae

Browse files
authored
Merge pull request #73 from glensc/config-loading
2 parents 78909b7 + 09c1b29 commit 51e65ae

6 files changed

Lines changed: 126 additions & 34 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ shutdown handler:
6868
$profiler->start(false);
6969
```
7070

71+
## Using config file
72+
73+
You can create `config/config.php` and load config from there:
74+
75+
1. copy `config/config.default.php` to `config/config.php`
76+
2. use `Config::create()` to `new Profiler`
77+
78+
```php
79+
// Config::create() will load config/config.default.php
80+
// and then merge with config/config.php (if it exists).
81+
$config = \Xhgui\Profiler\Config::create();
82+
$profiler = new \Xhgui\Profiler\Profiler($config);
83+
```
84+
7185
## Advanced Usage
7286

7387
You might want to control capture and sending yourself,

config/config.default.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Default configuration for PHP Profiler.
4+
*
5+
* To change these, create a file called `config.php` file in the same directory
6+
* and return an array from there with your overriding settings.
7+
*/
8+
9+
use Xhgui\Profiler\Profiler;
10+
use Xhgui\Profiler\ProfilingFlags;
11+
12+
return array(
13+
'save.handler' => Profiler::SAVER_STACK,
14+
'save.handler.stack' => array(
15+
'savers' => array(
16+
Profiler::SAVER_UPLOAD,
17+
Profiler::SAVER_FILE,
18+
),
19+
'saveAll' => false,
20+
),
21+
'save.handler.file' => array(
22+
'filename' => sys_get_temp_dir() . '/xhgui.data.jsonl',
23+
),
24+
'profiler.enable' => function () {
25+
return true;
26+
},
27+
'profiler.flags' => array(
28+
ProfilingFlags::CPU,
29+
ProfilingFlags::MEMORY,
30+
ProfilingFlags::NO_BUILTINS,
31+
ProfilingFlags::NO_SPANS,
32+
),
33+
'profiler.options' => array(),
34+
'profiler.exclude-env' => array(),
35+
'profiler.simple_url' => function ($url) {
36+
return preg_replace('/=\d+/', '', $url);
37+
},
38+
'profiler.replace_url' => null,
39+
);

src/Config.php

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
namespace Xhgui\Profiler;
44

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

78
class Config implements ArrayAccess
89
{
910
/** @var array */
10-
private $config;
11+
private $config = array();
1112

1213
public function __construct(array $config = array())
1314
{
14-
$this->config = $this->getDefaultConfig();
15+
$this->loadDefaultConfig();
1516
if ($config) {
1617
$this->merge($config);
1718
}
@@ -25,6 +26,37 @@ public function toArray()
2526
return $this->config;
2627
}
2728

29+
/**
30+
* Create config from defaults, and merge config/config.php if the file exists
31+
*
32+
* @return Config
33+
*/
34+
public static function create()
35+
{
36+
$configDir = dirname(__DIR__) . '/config';
37+
$config = new self();
38+
if (file_exists($file = $configDir . '/config.php')) {
39+
$config->load($file);
40+
}
41+
42+
return $config;
43+
}
44+
45+
/**
46+
* Load a config file, merge with the currently loaded configuration.
47+
*/
48+
public function load($filename)
49+
{
50+
if (!file_exists($filename)) {
51+
throw new ProfilerException("File does not exist: $filename");
52+
}
53+
$config = require $filename;
54+
if ($config === 1) {
55+
throw new ProfilerException("Config did not return an array: $filename");
56+
}
57+
$this->merge($config);
58+
}
59+
2860
private function merge(array $config)
2961
{
3062
$this->config = array_replace($this->config, $config);
@@ -50,38 +82,8 @@ public function offsetUnset($offset)
5082
unset($this->config[$offset]);
5183
}
5284

53-
/**
54-
* @return array
55-
*/
56-
private function getDefaultConfig()
85+
private function loadDefaultConfig()
5786
{
58-
return array(
59-
'save.handler' => Profiler::SAVER_STACK,
60-
'save.handler.stack' => array(
61-
'savers' => array(
62-
Profiler::SAVER_UPLOAD,
63-
Profiler::SAVER_FILE,
64-
),
65-
'saveAll' => false,
66-
),
67-
'save.handler.file' => array(
68-
'filename' => sys_get_temp_dir() . '/xhgui.data.jsonl',
69-
),
70-
'profiler.enable' => function () {
71-
return true;
72-
},
73-
'profiler.flags' => array(
74-
ProfilingFlags::CPU,
75-
ProfilingFlags::MEMORY,
76-
ProfilingFlags::NO_BUILTINS,
77-
ProfilingFlags::NO_SPANS,
78-
),
79-
'profiler.options' => array(),
80-
'profiler.exclude-env' => array(),
81-
'profiler.simple_url' => function ($url) {
82-
return preg_replace('/=\d+/', '', $url);
83-
},
84-
'profiler.replace_url' => null,
85-
);
87+
$this->load(__DIR__ . '/../config/config.default.php');
8688
}
8789
}

tests/ConfigTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,27 @@ 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+
}
22+
23+
public function testFromFile()
24+
{
25+
$config = Config::create();
26+
$this->assertEquals(Profiler::SAVER_STACK, $config['save.handler']);
27+
}
28+
29+
/**
30+
* @expectedException \Xhgui\Profiler\Exception\ProfilerException
31+
* @expectedExceptionMessage Config did not return an array
32+
*/
33+
public function testBadConfig()
34+
{
35+
$config = new Config();
36+
$config->load(__DIR__ . '/Resources/config_bad_return.php');
37+
}
1538
}
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+
$config = array(
6+
'save.handler' => Profiler::SAVER_UPLOAD,
7+
);

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)