Skip to content

Commit

Permalink
[HttpKernel] added LateDataCollectorInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 30, 2013
1 parent 9c4bc9a commit 5cedea2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 30 deletions.
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand All @@ -21,7 +22,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class EventDataCollector extends DataCollector
class EventDataCollector extends DataCollector implements LateDataCollectorInterface
{
protected $dispatcher;

Expand All @@ -41,6 +42,14 @@ public function collect(Request $request, Response $response, \Exception $except
);
}

public function lateCollect()
{
if ($this->dispatcher instanceof TraceableEventDispatcherInterface) {
$this->setCalledListeners($this->dispatcher->getCalledListeners());
$this->setNotCalledListeners($this->dispatcher->getNotCalledListeners());
}
}

/**
* Sets the called listeners.
*
Expand Down Expand Up @@ -89,16 +98,6 @@ public function getNotCalledListeners()
return $this->data['not_called_listeners'];
}

public function serialize()
{
if ($this->dispatcher instanceof TraceableEventDispatcherInterface) {
$this->setCalledListeners($this->dispatcher->getCalledListeners());
$this->setNotCalledListeners($this->dispatcher->getNotCalledListeners());
}

return parent::serialize();
}

/**
* {@inheritdoc}
*/
Expand Down
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* LateDataCollectorInterface.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface LateDataCollectorInterface
{
/**
* Collects data as late as possible.
*/
public function lateCollect();
}
Expand Up @@ -32,6 +32,14 @@ public function __construct($logger = null)
}
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
// everything is done as late as possible
}

This comment has been minimized.

Copy link
@staabm

staabm Oct 1, 2013

Contributor

Method now contained twice in this class?

This comment has been minimized.

Copy link
@staabm

staabm Oct 1, 2013

Contributor

Never mind, already fixed in c741c58


/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -19,7 +20,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class MemoryDataCollector extends DataCollector
class MemoryDataCollector extends DataCollector implements LateDataCollectorInterface
{
public function __construct()
{
Expand All @@ -37,6 +38,14 @@ public function collect(Request $request, Response $response, \Exception $except
$this->updateMemoryUsage();
}

/**
* {@inheritdoc}
*/
public function lateCollect()
{
$this->updateMemoryUsage();
}

/**
* Gets the memory.
*
Expand Down Expand Up @@ -65,13 +74,6 @@ public function updateMemoryUsage()
$this->data['memory'] = memory_get_peak_usage(true);
}

public function serialize()
{
$this->updateMemoryUsage();

return parent::serialize();
}

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -22,7 +23,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TimeDataCollector extends DataCollector
class TimeDataCollector extends DataCollector implements LateDataCollectorInterface
{
protected $kernel;
protected $stopwatch;
Expand Down Expand Up @@ -51,6 +52,17 @@ public function collect(Request $request, Response $response, \Exception $except
);
}

/**
* {@inheritdoc}
*/
public function lateCollect()
{
if (null !== $this->stopwatch && isset($this->data['token'])) {
$this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
}
unset($this->data['token']);
}

/**
* Sets the request events.
*
Expand Down Expand Up @@ -117,16 +129,6 @@ public function getStartTime()
return $this->data['start_time'];
}

public function serialize()
{
if (null !== $this->stopwatch && isset($this->data['token'])) {
$this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
}
unset($this->data['token']);

return parent::serialize();
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpKernel/Profiler/Profiler.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -109,6 +110,13 @@ public function loadProfile($token)
*/
public function saveProfile(Profile $profile)
{
// late collect
foreach ($profile->getCollectors() as $collector) {
if ($collector instanceof LateDataCollectorInterface) {
$collector->lateCollect();
}
}

if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
$this->logger->warning('Unable to store the profiler information.');
}
Expand Down

0 comments on commit 5cedea2

Please sign in to comment.