Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Fix the ProfilerListener (fix #3620) #3935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2012
Merged
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
53 changes: 38 additions & 15 deletions src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;

Expand All @@ -32,6 +33,7 @@ class ProfilerListener
protected $exception;
protected $children;
protected $requests;
protected $profiles;

/**
* Constructor.
Expand All @@ -48,6 +50,7 @@ public function __construct(Profiler $profiler, RequestMatcherInterface $matcher
$this->onlyException = (Boolean) $onlyException;
$this->onlyMasterRequests = (Boolean) $onlyMasterRequests;
$this->children = new \SplObjectStorage();
$this->profiles = array();
}

/**
Expand Down Expand Up @@ -85,42 +88,62 @@ public function onKernelResponse(FilterResponseEvent $event)
return;
}

$request = $event->getRequest();
$exception = $this->exception;
$this->exception = null;

if (null !== $this->matcher && !$this->matcher->matches($event->getRequest())) {
if (null !== $this->matcher && !$this->matcher->matches($request)) {
return;
}

if (!$profile = $this->profiler->collect($event->getRequest(), $event->getResponse(), $exception)) {
if (!$profile = $this->profiler->collect($request, $event->getResponse(), $exception)) {
return;
}

$this->profiles[] = $profile;

if (null !== $exception) {
foreach ($this->profiles as $profile) {
$this->profiler->saveProfile($profile);
}

return;
}

// keep the profile as the child of its parent
if (!$master) {
array_pop($this->requests);

$parent = $this->requests[count($this->requests) - 1];
if (!isset($this->children[$parent])) {
$profiles = array($profile);
} else {
$profiles = $this->children[$parent];
$profiles[] = $profile;
}

$parent = end($this->requests);
$profiles = isset($this->children[$parent]) ? $this->children[$parent] : array();
$profiles[] = $profile;
$this->children[$parent] = $profiles;
}

// store the profile and its children
if (isset($this->children[$event->getRequest()])) {
foreach ($this->children[$event->getRequest()] as $child) {
if (isset($this->children[$request])) {
foreach ($this->children[$request] as $child) {
$child->setParent($profile);
$profile->addChild($child);
$this->profiler->saveProfile($child);
}
$this->children[$event->getRequest()] = array();
$this->children[$request] = array();
}

if ($master) {
$this->saveProfiles($profile);
}
}

/**
* Saves the profile hierarchy.
*
* @param Profile $profile The root profile
*/
private function saveProfiles(Profile $profile)
{
$this->profiler->saveProfile($profile);
foreach ($profile->getChildren() as $profile) {
$this->saveProfiles($profile);
}
}
}