Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ final class Profiler
*/
private $requestContext;

/**
* @var ProfilingData|null
*/
private $profilingData;

/**
* Simple state variable to hold the value of 'Is the profiler running or not?'
*
Expand Down Expand Up @@ -161,7 +166,7 @@ public function disable()
throw new ProfilerException('Unable to disable profiler: Request context is missing');
}

$profile = new ProfilingData($this->config);
$profile = $this->getProfilingData();

return $profile->getProfilingData($data, $context);
}
Expand Down Expand Up @@ -328,4 +333,16 @@ private function captureRequestContext()

return $context;
}

/**
* @return ProfilingData
*/
private function getProfilingData()
{
if ($this->profilingData === null) {
$this->profilingData = new ProfilingData($this->config);
}

return $this->profilingData;
Comment thread
glensc marked this conversation as resolved.
}
}
17 changes: 17 additions & 0 deletions tests/ProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@

namespace Xhgui\Profiler\Test;

use ReflectionMethod;
use Xhgui\Profiler\Config;
use Xhgui\Profiler\Exception\ProfilerException;
use Xhgui\Profiler\ProfilingData;
use Xhgui\Profiler\Profiler;
use Xhgui\Profiler\Test\Resources\TestProfilerStub;

class ProfilerTest extends TestCase
{
public function testGetProfilingDataCachesInstance()
{
$profiler = new Profiler(new Config(array()));
$method = new ReflectionMethod($profiler, 'getProfilingData');
$method->setAccessible(true);

$first = $method->invoke($profiler);
$second = $method->invoke($profiler);

$this->assertInstanceOf(ProfilingData::class, $first);
$this->assertSame($first, $second);
$this->assertSame($first, $this->getPrivateProperty($profiler, 'profilingData'));
}

public function testDisableClearsStateWhenRequestContextIsMissing()
{
$profiler = new Profiler(array());
Expand Down
Loading