Skip to content

Commit ada8c8c

Browse files
dmitryukglensc
andauthored
Add support for custom savers (#63)
Co-authored-by: Elan Ruusamäe <glen@pld-linux.org>
1 parent 794c435 commit ada8c8c

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ To deliver captured data to XHGui, you will need one of the savers to submit to
124124
- [File saver](#file-saver)
125125
- [MongoDB Saver](#mongodb-saver) (deprecated)
126126
- [PDO Saver](#pdo-saver) (deprecated)
127+
- [Custom Saver](#custom-saver) Custom saver
127128

128129
### Stack saver
129130

@@ -252,6 +253,31 @@ Example config:
252253
),
253254
```
254255

256+
## Custom Saver
257+
You can create your own profile saver by implementing `SaverInterface` and calling `setSaver()`.
258+
259+
```php
260+
use Xhgui\Profiler\Profiler;
261+
use Xhgui\Profiler\Saver\SaverInterface;
262+
263+
class StdOutSaver implements SaverInterface
264+
{
265+
public function isSupported()
266+
{
267+
return true;
268+
}
269+
270+
public function save(array $data)
271+
{
272+
fwrite(STDOUT, json_encode($data));
273+
}
274+
}
275+
276+
//...
277+
/** @var Profiler $profiler */
278+
$profiler->setSaver(new StdOutSaver());
279+
```
280+
255281
## Configure Profiling Rate
256282

257283
You may want to change how frequently you profile the application. The

src/Profiler.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ public function isRunning()
177177
return $this->running;
178178
}
179179

180+
/**
181+
* Set profiler saver
182+
*
183+
* @param SaverInterface $saver
184+
*/
185+
public function setSaver(SaverInterface $saver)
186+
{
187+
$this->saveHandler = $saver;
188+
}
189+
180190
/**
181191
* Returns value of `profiler.enable` function evaluation
182192
*

tests/Resources/NullSaver.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Xhgui\Profiler\Test\Resources;
4+
5+
use Exception;
6+
use Xhgui\Profiler\Saver\SaverInterface;
7+
8+
class NullSaver implements SaverInterface
9+
{
10+
public function isSupported()
11+
{
12+
return true;
13+
}
14+
15+
public function save(array $data)
16+
{
17+
throw new Exception('NullSaver executed');
18+
}
19+
}

tests/Saver/NullSaverTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Xhgui\Profiler\Test\Saver;
4+
5+
use Exception;
6+
use Xhgui\Profiler\Profiler;
7+
use Xhgui\Profiler\Test\Resources\NullSaver;
8+
use Xhgui\Profiler\Test\TestCase;
9+
10+
class NullSaverTest extends TestCase
11+
{
12+
public function setCustomSaver()
13+
{
14+
$saver = new NullSaver();
15+
$profiler = new Profiler(array());
16+
$profiler->setSaver($saver);
17+
$profiler->start();
18+
try {
19+
$profiler->stop();
20+
$this->markTestIncomplete('Custom saver not executed');
21+
} catch (Exception $e) {
22+
$this->assertEquals('CustomSaver executed', $e->getMessage());
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)