Skip to content

Commit

Permalink
Extend symfony DataCollector, do not serialize created_at as \DateTime (
Browse files Browse the repository at this point in the history
  • Loading branch information
toooni authored and greg0ire committed Jul 27, 2019
1 parent d853b77 commit f3d84a5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 62 deletions.
75 changes: 17 additions & 58 deletions src/Profiler/DataCollector/BlockDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Sonata\BlockBundle\Templating\Helper\BlockHelper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

/**
* Block data collector for the symfony web profiling.
Expand All @@ -25,38 +25,18 @@
*
* @author Olivier Paradis <paradis.olivier@gmail.com>
*/
class BlockDataCollector implements DataCollectorInterface, \Serializable
class BlockDataCollector extends DataCollector
{
/**
* @var BlockHelper
*/
protected $blocksHelper;

/**
* @var array
*/
protected $blocks = [];

/**
* @var array
*/
protected $containers = [];

/**
* @var array
*/
protected $realBlocks = [];

/**
* @var array
*/
protected $containerTypes = [];

/**
* @var array
*/
protected $events = [];

/**
* @param BlockHelper $blockHelper Block renderer
* @param array $containerTypes array of container types
Expand All @@ -65,30 +45,31 @@ public function __construct(BlockHelper $blockHelper, array $containerTypes)
{
$this->blocksHelper = $blockHelper;
$this->containerTypes = $containerTypes;
$this->reset();
}

public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->blocks = $this->blocksHelper->getTraces();
$this->data['blocks'] = $this->blocksHelper->getTraces();

// split into containers & real blocks
foreach ($this->blocks as $id => $block) {
foreach ($this->data['blocks'] as $id => $block) {
if (!\is_array($block)) {
return; // something went wrong while collecting information
}

if ('_events' === $id) {
foreach ($block as $uniqid => $event) {
$this->events[$uniqid] = $event;
$this->data['events'][$uniqid] = $event;
}

continue;
}

if (\in_array($block['type'], $this->containerTypes, true)) {
$this->containers[$id] = $block;
$this->data['containers'][$id] = $block;
} else {
$this->realBlocks[$id] = $block;
$this->data['realBlocks'][$id] = $block;
}
}
}
Expand All @@ -100,7 +81,7 @@ public function collect(Request $request, Response $response, \Exception $except
*/
public function getTotalBlock()
{
return \count($this->realBlocks) + \count($this->containers);
return \count($this->data['realBlocks']) + \count($this->data['containers']);
}

/**
Expand All @@ -110,7 +91,7 @@ public function getTotalBlock()
*/
public function getEvents()
{
return $this->events;
return $this->data['events'];
}

/**
Expand All @@ -120,7 +101,7 @@ public function getEvents()
*/
public function getBlocks()
{
return $this->blocks;
return $this->data['blocks'];
}

/**
Expand All @@ -130,7 +111,7 @@ public function getBlocks()
*/
public function getContainers()
{
return $this->containers;
return $this->data['containers'];
}

/**
Expand All @@ -140,29 +121,7 @@ public function getContainers()
*/
public function getRealBlocks()
{
return $this->realBlocks;
}

public function serialize()
{
$data = [
'blocks' => $this->blocks,
'containers' => $this->containers,
'realBlocks' => $this->realBlocks,
'events' => $this->events,
];

return serialize($data);
}

public function unserialize($data)
{
$merged = unserialize($data);

$this->blocks = $merged['blocks'];
$this->containers = $merged['containers'];
$this->realBlocks = $merged['realBlocks'];
$this->events = $merged['events'];
return $this->data['realBlocks'];
}

public function getName()
Expand All @@ -172,9 +131,9 @@ public function getName()

public function reset()
{
$this->blocks = [];
$this->containers = [];
$this->realBlocks = [];
$this->events = [];
$this->data['blocks'] = [];
$this->data['containers'] = [];
$this->data['realBlocks'] = [];
$this->data['events'] = [];
}
}
3 changes: 2 additions & 1 deletion src/Templating/Helper/BlockHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ public function render($block, array $options = [])
}

if ($this->stopwatch) {
$stats['cache']['created_at'] = $response->getDate();
// avoid \DateTime because of serialize/unserialize issue in PHP7.3 (https://bugs.php.net/bug.php?id=77302)
$stats['cache']['created_at'] = null === $response->getDate() ? null : $response->getDate()->getTimestamp();
$stats['cache']['ttl'] = $response->getTtl() ?: 0;
$stats['cache']['age'] = $response->getAge();
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Profiler/DataCollector/BlockDataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ public function testBlockDataCollector()
$blockHelper = $this->prophesize(BlockHelper::class);
$request = $this->prophesize(Request::class);
$response = $this->prophesize(Response::class);
$objectForBlock = new \DateTime();

$blockDataCollector = new BlockDataCollector($blockHelper->reveal(), ['container']);

$expectedEvents = ['1' => '2', '3' => '4'];
$expectedBlocks = [
'_events' => ['1' => '2', '3' => '4'],
'test1' => ['type' => 'container'],
'test2' => ['type' => 'another_type'],
'test2' => ['type' => 'another_type', 'datetime' => $objectForBlock],
];
$expectedContainers = ['test1' => ['type' => 'container']];
$expectedRealBlocks = ['test2' => ['type' => 'another_type']];
$expectedRealBlocks = ['test2' => ['type' => 'another_type', 'datetime' => $objectForBlock]];

$blockHelper->getTraces()->willReturn([
'_events' => ['1' => '2', '3' => '4'],
'test1' => ['type' => 'container'],
'test2' => ['type' => 'another_type'],
'test2' => ['type' => 'another_type', 'datetime' => $objectForBlock],
]);

$blockDataCollector->collect($request->reveal(), $response->reveal());
Expand Down

0 comments on commit f3d84a5

Please sign in to comment.