Skip to content

Commit

Permalink
[HttpKernel] fixed profiler file storage for children
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 14, 2011
1 parent cc76da1 commit 5b69dae
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
68 changes: 38 additions & 30 deletions src/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php
Expand Up @@ -120,17 +120,7 @@ public function read($token)
return null;
}

$profile = unserialize(file_get_contents($file));

$childrenFile = $this->getChildrenFilename($token);
if (file_exists($childrenFile)) {
$childrenTokens = explode(',', file_get_contents($childrenFile));
foreach ($childrenTokens as $childToken) {
$profile->addChild($this->read($childToken));
}
}

return $profile;
return $this->createProfileFromData($token, unserialize(file_get_contents($file)));
}

/**
Expand All @@ -148,7 +138,16 @@ public function write(Profile $profile)
}

// Store profile
file_put_contents($file, serialize($profile));
$data = array(
'token' => $profile->getToken(),
'parent' => $profile->getParent() ? $profile->getParent()->getToken() : null,
'children' => array_map(function ($p) { return $p->getToken(); }, $profile->getChildren()),
'data' => $profile->getCollectors(),
'ip' => $profile->getIp(),
'url' => $profile->getUrl(),
'time' => $profile->getTime(),
);
file_put_contents($file, serialize($data));

// Add to index
$file = fopen($this->getIndexFilename(), 'a');
Expand All @@ -161,14 +160,6 @@ public function write(Profile $profile)
));
fclose($file);

if ($profile->getParent()) {
$childrenFile = $this->getChildrenFilename($profile->getParent()->getToken());
$toWrite = file_exists($childrenFile) ? ','.$profile->getToken() : $profile->getToken();
$fp = fopen($childrenFile, 'a');
fputs($fp, $toWrite);
fclose($fp);
}

return !$exists;
}

Expand All @@ -186,16 +177,6 @@ protected function getFilename($token)
return $this->folder.'/'.$folderA.'/'.$folderB.'/'.$token;
}

/**
* Gets filename to store children tokens.
*
* @return string The tokens filename
*/
protected function getChildrenFilename($token)
{
return $this->getFilename($token).'.children';
}

/**
* Gets the index filename.
*
Expand Down Expand Up @@ -246,4 +227,31 @@ protected function readLineFromFile($file)

return $str === '' ? $this->readLineFromFile($file) : $str;
}

protected function createProfileFromData($token, $data, $parent = null)
{
$profile = new Profile($token);
$profile->setIp($data['ip']);
$profile->setUrl($data['url']);
$profile->setTime($data['time']);
$profile->setCollectors($data['data']);

if (!$parent && $data['parent']) {
$parent = $this->read($data['parent']);
}

if ($parent) {
$profile->setParent($parent);
}

foreach ($data['children'] as $token) {
if (!$token || !file_exists($file = $this->getFilename($token))) {
continue;
}

$profile->addChild($this->createProfileFromData($token, unserialize(file_get_contents($file)), $profile));
}

return $profile;
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/Profiler/Profile.php
Expand Up @@ -141,6 +141,7 @@ public function setChildren(array $children)
public function addChild(Profile $child)
{
$this->children[] = $child;
$child->setParent($this);
}

public function getCollector($name)
Expand Down
Expand Up @@ -70,13 +70,13 @@ public function testChildren()
$parentProfile->setIp('127.0.0.1');
$parentProfile->setUrl('http://foo.bar/parent');

self::$storage->write($parentProfile);

$childProfile = new Profile('token_child');
$childProfile->setIp('127.0.0.1');
$childProfile->setUrl('http://foo.bar/child');
$childProfile->setParent($parentProfile);

$parentProfile->addChild($childProfile);

self::$storage->write($parentProfile);
self::$storage->write($childProfile);

// Load them from storage
Expand Down

0 comments on commit 5b69dae

Please sign in to comment.