Skip to content

Commit

Permalink
[HttpKernel] added import/export to Profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Aug 30, 2010
1 parent 5470f02 commit 8c6478d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
30 changes: 26 additions & 4 deletions src/Symfony/Component/HttpKernel/Profiler/Profiler.php
Expand Up @@ -89,6 +89,28 @@ public function loadFromToken($token)
return $profiler;
}

public function export()
{
$unpack = unpack('H*', serialize(array($this->token, $this->collectors, $this->ip, $this->url, $this->time)));

return $unpack[1];
}

public function import($data)
{
list($token, $collectors, $ip, $url, $time) = unserialize(pack('H*', $data));

if (false !== $this->storage->read($token)) {
return false;
}

$unpack = unpack('H*', serialize($this->collectors));

$this->storage->write($token, $unpack[1], $ip, $url, $time);

return $token;
}

/**
* Sets the token.
*
Expand All @@ -99,8 +121,8 @@ public function setToken($token)
$this->token = $token;

if (false !== $items = $this->storage->read($token)) {
list($collectors, $this->ip, $this->url, $this->time) = $items;
$this->setCollectors($collectors);
list($data, $this->ip, $this->url, $this->time) = $items;
$this->setCollectors(unserialize(pack('H*', $data)));

$this->empty = false;
} else {
Expand Down Expand Up @@ -200,9 +222,9 @@ public function collect(Request $request, Response $response, \Exception $except
$this->url = $request->getUri();
$this->time = time();

$unpack = unpack('H*', serialize($this->collectors));
try {
$this->storage->write($this->token, $this->collectors, $this->ip, $this->url, $this->time);

$this->storage->write($this->token, $unpack[1], $this->ip, $this->url, $this->time);
$this->empty = false;
} catch (\Exception $e) {
if (null !== $this->logger) {
Expand Down
Expand Up @@ -2,8 +2,6 @@

namespace Symfony\Component\HttpKernel\Profiler;

use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;

/*
* This file is part of the Symfony framework.
*
Expand Down Expand Up @@ -38,18 +36,18 @@ function find($ip, $url, $limit);
*
* @param string $token A token
*
* @return DataCollectorInterface[] An array of DataCollectorInterface instance
* @return string The data associated with token
*/
function read($token);

/**
* Reads data associated with the given token.
*
* @param string $token A token
* @param DataCollectorInterface[] $collectors An array of DataCollectorInterface instances
* @param string $ip An IP
* @param string $url An URL
* @param integer $time The time of the data
* @param string $token A token
* @param string $data The data associated with token
* @param string $ip An IP
* @param string $url An URL
* @param integer $time The time of the data
*/
function write($token, $collectors, $ip, $url, $time);
function write($token, $data, $ip, $url, $time);
}
Expand Up @@ -69,7 +69,7 @@ public function read($token)
$data = $this->fetch($db, 'SELECT data, ip, url, time FROM data WHERE token = :token ORDER BY time DESC LIMIT 1', $args);
$this->close($db);
if (isset($data[0]['data'])) {
return array(unserialize(pack('H*', $data[0]['data'])), $data[0]['ip'], $data[0]['url'], $data[0]['time']);
return array($data[0]['data'], $data[0]['ip'], $data[0]['url'], $data[0]['time']);
} else {
return false;
}
Expand All @@ -78,11 +78,8 @@ public function read($token)
/**
* {@inheritdoc}
*/
public function write($token, $collectors, $ip, $url, $time)
public function write($token, $data, $ip, $url, $time)
{
$unpack = unpack('H*', serialize($collectors));
$data = $unpack[1];

$db = $this->initDb();
$args = array(
':token' => $token,
Expand Down Expand Up @@ -125,6 +122,7 @@ protected function initDb()

$db->exec('CREATE TABLE IF NOT EXISTS data (token STRING, data STRING, ip STRING, url STRING, time INTEGER)');
$db->exec('CREATE INDEX IF NOT EXISTS data_data ON data (time)');
$db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON data (token)');

return $db;
}
Expand Down

0 comments on commit 8c6478d

Please sign in to comment.